-
-
Notifications
You must be signed in to change notification settings - Fork 378
/
core.ts
165 lines (137 loc) · 4.06 KB
/
core.ts
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import HTMLParser from './htmlparser'
import Reporter from './reporter'
import * as HTMLRules from './rules'
import { Hint, Rule, Ruleset } from './types'
export interface FormatOptions {
colors?: boolean
indent?: number
}
class HTMLHintCore {
public rules: { [id: string]: Rule } = {}
public readonly defaultRuleset: Ruleset = {
'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,
}
public addRule(rule: Rule) {
this.rules[rule.id] = rule
}
public verify(html: string, ruleset: Ruleset = this.defaultRuleset) {
if (Object.keys(ruleset).length === 0) {
ruleset = this.defaultRuleset
}
// parse inline ruleset
html = html.replace(
/^\s*<!--\s*htmlhint\s+([^\r\n]+?)\s*-->/i,
(all, strRuleset: string) => {
// For example:
// all is '<!-- htmlhint alt-require:true-->'
// strRuleset is 'alt-require:true'
strRuleset.replace(
/(?:^|,)\s*([^:,]+)\s*(?:\:\s*([^,\s]+))?/g,
(all, ruleId: string, value: string | undefined) => {
// For example:
// all is 'alt-require:true'
// ruleId is 'alt-require'
// value is 'true'
ruleset[ruleId] =
value !== undefined && value.length > 0 ? JSON.parse(value) : true
return ''
}
)
return ''
}
)
const parser = new HTMLParser()
const reporter = new Reporter(html, ruleset)
const rules = this.rules
let rule: Rule
for (const id in ruleset) {
rule = rules[id]
if (rule !== undefined && ruleset[id] !== false) {
rule.init(parser, reporter, ruleset[id])
}
}
parser.parse(html)
return reporter.messages
}
public format(arrMessages: Hint[], options: FormatOptions = {}) {
const arrLogs: string[] = []
const colors = {
white: '',
grey: '',
red: '',
reset: '',
}
if (options.colors) {
colors.white = '\x1b[37m'
colors.grey = '\x1b[90m'
colors.red = '\x1b[31m'
colors.reset = '\x1b[39m'
}
const indent = options.indent || 0
arrMessages.forEach((hint) => {
const leftWindow = 40
const rightWindow = leftWindow + 20
let evidence = hint.evidence
const line = hint.line
const col = hint.col
const evidenceCount = evidence.length
let leftCol = col > leftWindow + 1 ? col - leftWindow : 1
let 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
let pointCol = col - leftCol
// add double byte character
// eslint-disable-next-line no-control-regex
const 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: number, str?: string) {
return new Array(n + 1).join(str || ' ')
}
export const HTMLHint = new HTMLHintCore()
Object.keys(HTMLRules).forEach((key) => {
// TODO: need a fix
// @ts-expect-error
HTMLHint.addRule(HTMLRules[key])
})
export { HTMLRules, Reporter, HTMLParser }