Skip to content

Commit

Permalink
tag-pair(rule): Show the line of the start tag
Browse files Browse the repository at this point in the history
  • Loading branch information
yaniswang committed Oct 6, 2015
1 parent 6a121d7 commit 895970d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ add:
2. Allow comments in json
3. Support hint any file without `.html` or `.htm` extension, just like: `htmlhint test.xhtml`
4. Support json raw format in cli
5. tag-pair(rule): Show the line of the start tag

fix:

Expand Down
2 changes: 1 addition & 1 deletion coverage.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/htmlhint.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "htmlhint",
"version": "0.9.7",
"version": "0.9.8",
"description": "A Static Code Analysis Tool for HTML",
"main": "./index",
"dependencies": {
Expand Down
20 changes: 13 additions & 7 deletions src/rules/tag-pair.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,29 @@ HTMLHint.addRule({
parser.addListener('tagstart', function(event){
var tagName = event.tagName.toLowerCase();
if (mapEmptyTags[tagName] === undefined && !event.close){
stack.push(tagName);
stack.push({
tagName: tagName,
line: event.line,
raw: event.raw
});
}
});
parser.addListener('tagend', function(event){
var tagName = event.tagName.toLowerCase();
//向上寻找匹配的开始标签
for(var pos = stack.length-1;pos >= 0; pos--){
if(stack[pos] === tagName){
if(stack[pos].tagName === tagName){
break;
}
}
if(pos >= 0){
var arrTags = [];
for(var i=stack.length-1;i>pos;i--){
arrTags.push('</'+stack[i]+'>');
arrTags.push('</'+stack[i].tagName+'>');
}
if(arrTags.length > 0){
reporter.error('Tag must be paired, missing: [ '+ arrTags.join('') + ' ]', event.line, event.col, self, event.raw);
var lastEvent = stack[stack.length-1];
reporter.error('Tag must be paired, missing: [ '+ arrTags.join('') + ' ], start tag match failed [ ' + lastEvent.raw + ' ] on line ' + lastEvent.line + '.', event.line, event.col, self, event.raw);
}
stack.length=pos;
}
Expand All @@ -40,11 +45,12 @@ HTMLHint.addRule({
parser.addListener('end', function(event){
var arrTags = [];
for(var i=stack.length-1;i>=0;i--){
arrTags.push('</'+stack[i]+'>');
arrTags.push('</'+stack[i].tagName+'>');
}
if(arrTags.length > 0){
reporter.error('Tag must be paired, missing: [ '+ arrTags.join('') + ' ]', event.line, event.col, self, '');
var lastEvent = stack[stack.length-1];
reporter.error('Tag must be paired, missing: [ '+ arrTags.join('') + ' ], open tag match failed [ ' + lastEvent.raw + ' ] on line ' + lastEvent.line + '.', event.line, event.col, self, '');
}
});
}
});
});

0 comments on commit 895970d

Please sign in to comment.