-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtag-pair.js
95 lines (79 loc) · 2.14 KB
/
tag-pair.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* @file rule: tag-pair
* @author nighca<[email protected]>
*/
module.exports = {
name: 'tag-pair',
desc: 'Tag must be paired.',
target: 'parser',
lint: function (getCfg, parser, reporter) {
// http://www.w3.org/TR/html5/syntax.html#elements-0
var voidElements = [
'area',
'base',
'br',
'col',
'embed',
'hr',
'img',
'input',
'keygen',
'link',
'meta',
'param',
'source',
'track',
'wbr'
];
// stack of unclosed tags
var stack = [];
// check & report
var check = function (tag) {
if (voidElements.indexOf(tag.name) < 0) {
reporter.warn(
tag.pos,
'035',
'Tag ' + tag.name + ' is not paired.'
);
}
};
// record unclosed tags
parser.on('opentag', function (name) {
stack.push({
name: name.toLowerCase(),
pos: this.startIndex
});
});
// do close & check unclosed tags
parser.tokenizer.on('closetag', function (name) {
if (!getCfg()) {
return;
}
name = name.toLowerCase();
// find the matching tag
var l = stack.length;
var i = l - 1;
for (; i >= 0; i--) {
if (stack[i].name === name) {
break;
}
}
// if the matching tag found,
// all tags after the macthing tag are unpaired
if (i >= 0) {
for (var j = i + 1; j < l; j++) {
check(stack[j]);
}
stack = stack.slice(0, i);
}
});
// check left tags
parser.on('end', function () {
if (!getCfg()) {
return;
}
// all unclosed tags in the end are unpaired
stack.forEach(check);
});
}
};