Skip to content

Commit

Permalink
title-require(rule): report error when `<html><head><title></title></…
Browse files Browse the repository at this point in the history
…head><body></body></html>`
  • Loading branch information
yaniswang committed Oct 9, 2015
1 parent 6aa6011 commit 4db9b6e
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ add:
fix:

1. title-require(rule): report error when `<html><title>test</title><head></head><body></body></html>`
1. title-require(rule): report error when `<html><head><title></title></head><body></body></html>`

## ver 0.9.9 (2015-10-9)

Expand Down
2 changes: 1 addition & 1 deletion lib/htmlhint.js

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/htmlparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var HTMLParser = (function(undefined){
self._listeners = {};
self._mapCdataTags = self.makeMap("script,style");
self._arrBlocks = [];
self.lastEvent = null;
},

makeMap: function(str){
Expand Down Expand Up @@ -186,6 +187,12 @@ var HTMLParser = (function(undefined){
if (listenersAll !== undefined){
listeners = listeners.concat(listenersAll);
}
var lastEvent = self.lastEvent;
if(lastEvent !== null){
delete lastEvent['lastEvent'];
data.lastEvent = lastEvent;
}
self.lastEvent = data;
for (var i = 0, l = listeners.length; i < l; i++){
listeners[i].call(self, data);
}
Expand Down
15 changes: 11 additions & 4 deletions src/rules/title-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,26 @@ HTMLHint.addRule({
description: '<title> must be present in <head> tag.',
init: function(parser, reporter){
var self = this;
var headBegin = false,
hasTitle = false;
var headBegin = false;
var hasTitle = false;
function onTagStart(event){
var tagName = event.tagName.toLowerCase();
if(tagName === 'head'){
headBegin = true;
}
if(tagName === 'title' && headBegin){
else if(tagName === 'title' && headBegin){
hasTitle = true;
}
}
function onTagEnd(event){
if(event.tagName.toLowerCase() === 'head'){
var tagName = event.tagName.toLowerCase();
if(hasTitle && tagName === 'title'){
var lastEvent = event.lastEvent;
if(lastEvent.type !== 'text' || (lastEvent.type === 'text' && /^\s*$/.test(lastEvent.raw) === true)){
reporter.error('<title></title> must not be empty.', event.line, event.col, self, event.raw);
}
}
else if(tagName === 'head'){
if(hasTitle === false){
reporter.error('<title> must be present in <head> tag.', event.line, event.col, self, event.raw);
}
Expand Down
13 changes: 10 additions & 3 deletions test/htmlparser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ expect.Assertion.prototype.event = function(type, attr){
attr.type = type;
}
else{
attr = {'type': type};
if(typeof attr === 'string'){
attr = {'type': type};
}
else{
attr = type;
}
}
self.assert(
eqlEvent(obj, attr),
Expand All @@ -24,7 +29,7 @@ expect.Assertion.prototype.event = function(type, attr){

function eqlEvent(event, attr){
for(var name in attr){
if(event[name] !== attr[name]){
if(name !== 'attrs' && event[name] !== attr[name]){
return false;
}
}
Expand All @@ -47,7 +52,9 @@ describe('HTMLParser: Base parse', function(){
var arrEvents = [];
var targetEvents = [{"pos":0,"line":1,"col":1,"type":"start"},{"content":"DOCTYPE HTML","long":false,"raw":"<!DOCTYPE HTML>","pos":0,"line":1,"col":1,"type":"comment"},{"tagName":"html","attrs":[],"close":"","raw":"<html>","pos":15,"line":1,"col":16,"type":"tagstart"},{"tagName":"head","attrs":[],"close":"","raw":"<head>","pos":21,"line":1,"col":22,"type":"tagstart"},{"tagName":"meta","attrs":[{"name":"charset","value":"UTF-8","quote":"\"","index":0,"raw":" charset=\"UTF-8\""}],"close":"","raw":"<meta charset=\"UTF-8\">","pos":27,"line":1,"col":28,"type":"tagstart"},{"tagName":"title","attrs":[],"close":"","raw":"<title>","pos":49,"line":1,"col":50,"type":"tagstart"},{"raw":"testtitle","pos":56,"line":1,"col":57,"type":"text"},{"tagName":"title","raw":"</title>","pos":65,"line":1,"col":66,"type":"tagend"},{"tagName":"head","raw":"</head>","pos":73,"line":1,"col":74,"type":"tagend"},{"tagName":"body","attrs":[],"close":"","raw":"<body>","pos":80,"line":1,"col":81,"type":"tagstart"},{"tagName":"p","attrs":[],"close":"","raw":"<p>","pos":86,"line":1,"col":87,"type":"tagstart"},{"tagName":"a","attrs":[{"name":"href","value":"testhref","quote":"\"","index":0,"raw":" href=\"testhref\""},{"name":"title","value":"atitle","quote":"\"","index":16,"raw":" title=\"atitle\""}],"close":"","raw":"<a href=\"testhref\" title=\"atitle\">","pos":89,"line":1,"col":90,"type":"tagstart"},{"raw":"aaa","pos":123,"line":1,"col":124,"type":"text"},{"tagName":"span","attrs":[],"close":"","raw":"<span>","pos":126,"line":1,"col":127,"type":"tagstart"},{"raw":"bbb","pos":132,"line":1,"col":133,"type":"text"},{"tagName":"span","raw":"</span>","pos":135,"line":1,"col":136,"type":"tagend"},{"raw":"ccc","pos":142,"line":1,"col":143,"type":"text"},{"tagName":"a","raw":"</a>","pos":145,"line":1,"col":146,"type":"tagend"},{"tagName":"p","raw":"</p>","pos":149,"line":1,"col":150,"type":"tagend"},{"tagName":"body","raw":"</body>","pos":153,"line":1,"col":154,"type":"tagend"},{"tagName":"html","raw":"</html>","pos":160,"line":1,"col":161,"type":"tagend"},{"pos":167,"line":1,"col":168,"type":"end"}];
getAllEvents(parser, arrEvents, function(){
expect(arrEvents).to.eql(targetEvents);
arrEvents.forEach(function(event, i){
expect(event).to.event(targetEvents[i]);
});
done();
});
parser.parse('<!DOCTYPE HTML><html><head><meta charset="UTF-8"><title>testtitle</title></head><body><p><a href="testhref" title="atitle">aaa<span>bbb</span>ccc</a></p></body></html>');
Expand Down
12 changes: 11 additions & 1 deletion test/rules/title-require.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ruleOptions[ruldId] = true;
describe('Rules: '+ruldId, function(){

it('<title> be present in <head> tag should not result in an error', function(){
var code = '<html><head><title></title></head><body></body></html>';
var code = '<html><head><title>test</title></head><body></body></html>';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});
Expand All @@ -39,4 +39,14 @@ describe('Rules: '+ruldId, function(){
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

it('<title></title> is empty should result in an error', function(){
var code = '<html><head><title></title></head><body></body></html>';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(1);

code = '<html><head><title> \t </title></head><body></body></html>';
messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(1);
});
});

0 comments on commit 4db9b6e

Please sign in to comment.