-
-
Notifications
You must be signed in to change notification settings - Fork 383
/
core.js
150 lines (143 loc) · 3.73 KB
/
core.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import HTMLParser from './htmlparser';
import Reporter from './reporter';
import * as HTMLRules from './rules';
class HTMLHint {
constructor() {
this.rules = {};
this.defaultRuleset = {
'tagname-lowercase': true,
'attr-lowercase': true,
'attr-value-double-quotes': true,
'doctype-first': true,
'tag-pair': true,
'spec-char-escape': true,
'id-unique': true,
'src-not-empty': true,
'attr-no-duplication': true,
'title-require': true
};
}
addRule(rule) {
this.rules[rule.id] = rule;
}
verify(html, ruleset) {
if (ruleset === undefined || Object.keys(ruleset).length === 0) {
ruleset = this.defaultRuleset;
}
// parse inline ruleset
html = html.replace(/^\s*<!--\s*htmlhint\s+([^\r\n]+?)\s*-->/i, function(
all,
strRuleset
) {
if (ruleset === undefined) {
ruleset = {};
}
strRuleset.replace(/(?:^|,)\s*([^:,]+)\s*(?:\:\s*([^,\s]+))?/g, function(
all,
key,
value
) {
if (value === 'false') {
value = false;
} else if (value === 'true') {
value = true;
}
ruleset[key] = value === undefined ? true : value;
});
return '';
});
var parser = new HTMLParser();
var reporter = new Reporter(html, ruleset);
var rules = this.rules,
rule;
for (var id in ruleset) {
rule = rules[id];
if (rule !== undefined && ruleset[id] !== false) {
rule.init(parser, reporter, ruleset[id]);
}
}
parser.parse(html);
return reporter.messages;
}
format(arrMessages, options) {
options = options || {};
var arrLogs = [];
var colors = {
white: '',
grey: '',
red: '',
reset: ''
};
if (options.colors) {
colors.white = '\x1b[37m';
colors.grey = '\x1b[90m';
colors.red = '\x1b[31m';
colors.reset = '\x1b[39m';
}
var indent = options.indent || 0;
arrMessages.forEach(hint => {
var leftWindow = 40;
var rightWindow = leftWindow + 20;
var evidence = hint.evidence;
var line = hint.line;
var col = hint.col;
var evidenceCount = evidence.length;
var leftCol = col > leftWindow + 1 ? col - leftWindow : 1;
var rightCol =
evidence.length > col + rightWindow ? col + rightWindow : evidenceCount;
if (col < leftWindow + 1) {
rightCol += leftWindow - col + 1;
}
evidence = evidence.replace(/\t/g, ' ').substring(leftCol - 1, rightCol);
// add ...
if (leftCol > 1) {
evidence = '...' + evidence;
leftCol -= 3;
}
if (rightCol < evidenceCount) {
evidence += '...';
}
// show evidence
arrLogs.push(
colors.white +
repeatStr(indent) +
'L' +
line +
' |' +
colors.grey +
evidence +
colors.reset
);
// show pointer & message
var pointCol = col - leftCol;
// add double byte character
var match = evidence.substring(0, pointCol).match(/[^\u0000-\u00ff]/g);
if (match !== null) {
pointCol += match.length;
}
arrLogs.push(
colors.white +
repeatStr(indent) +
repeatStr(String(line).length + 3 + pointCol) +
'^ ' +
colors.red +
hint.message +
' (' +
hint.rule.id +
')' +
colors.reset
);
});
return arrLogs;
}
}
// repeat string
function repeatStr(n, str) {
return new Array(n + 1).join(str || ' ');
}
const hint = new HTMLHint();
Object.keys(HTMLRules).forEach(key => {
hint.addRule(HTMLRules[key]);
});
export default hint;
export { HTMLRules, Reporter, HTMLParser, HTMLHint };