-
Notifications
You must be signed in to change notification settings - Fork 668
/
hint.ts
175 lines (139 loc) · 5.99 KB
/
hint.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
166
167
168
169
170
171
172
173
174
175
/**
* @fileoverview Check if a `<meta charset="utf-8">` is specified
* as the first thing in `<head>`.
*/
/*
* ------------------------------------------------------------------------------
* Requirements
* ------------------------------------------------------------------------------
*/
import { IHint } from 'hint/dist/src/lib/types';
import { HintContext } from 'hint/dist/src/lib/hint-context';
import { HTMLEvents } from '@hint/parser-html';
import { normalizeString } from '@hint/utils-string';
import { Severity } from '@hint/utils-types';
import meta from './meta';
import { getMessage } from './i18n.import';
/*
* ------------------------------------------------------------------------------
* Public
* ------------------------------------------------------------------------------
*/
export default class MetaCharsetUTF8Hint implements IHint {
public static readonly meta = meta;
public constructor(context: HintContext<HTMLEvents>) {
let validated = false;
context.on('scan::end', () => {
validated = false;
});
context.on('parse::end::html', ({ document, html, resource }) => {
// The first time we receive this event is the main content, we don't care about iframes, requests by ads, etc.
if (validated) {
return;
}
validated = true;
if (document.isFragment) {
return;
}
/*
* There are 2 versions of the charset meta element:
*
* * <meta charset="charset">
* * <meta http-equiv="content-type" content="text/html; charset=<charset>">
*
* Also, there is a XML declaration:
*
* * <?xml version="1.0" encoding="<charset>"?>
*
* but for regular HTML, it should not be used.
*/
const charsetMetaElements = document.querySelectorAll('meta[charset], meta[http-equiv="content-type" i]');
if (charsetMetaElements.length === 0) {
context.report(
resource,
getMessage('metaElementNotSpecified', context.language),
{ severity: Severity.warning }
);
return;
}
/*
* Treat the first charset meta element as the one
* the user intended to use, and check if it's:
*/
const charsetMetaElement = charsetMetaElements[0];
// * `<meta charset="utf-8">`
if (charsetMetaElement.getAttribute('http-equiv') !== null) {
context.report(
resource,
getMessage('metaElementShorter', context.language),
{
element: charsetMetaElement,
severity: Severity.warning
});
} else {
const metaValue = normalizeString(charsetMetaElement.getAttribute('charset'));
if (metaValue !== 'utf-8') {
const severity = metaValue === 'utf8' ?
Severity.warning :
Severity.error;
context.report(
resource,
getMessage('metaElementWrongValue', context.language, charsetMetaElement.getAttribute('charset')!),
{
element: charsetMetaElement,
severity
});
}
}
/*
* * specified as the first thing in `<head>`
*
* Note: The Charset meta element should be included completely
* within the first 1024 bytes of the document, but
* that check will be done by the html/markup validator.
*/
const firstHeadElement = document.querySelectorAll('head :first-child')[0];
const isCharsetMetaFirstHeadElement = charsetMetaElement && firstHeadElement && charsetMetaElement.isSame(firstHeadElement);
const headElementContent = document.querySelectorAll('head')[0].outerHTML;
const isMetaElementFirstHeadContent = (/^<head[^>]*>\s*<meta/).test(headElementContent);
if (!isCharsetMetaFirstHeadElement || !isMetaElementFirstHeadContent) {
const severity = (firstHeadElement.getLocation().endOffset || 0) <= 1024 ?
Severity.hint :
Severity.error;
context.report(
resource,
getMessage('metaElementFirstThing', context.language),
{
element: charsetMetaElement,
severity
});
}
// * specified in the `<body>`.
const bodyMetaElements = document.querySelectorAll('body meta[charset], body meta[http-equiv="content-type" i]');
if (bodyMetaElements[0] && bodyMetaElements[0].isSame(charsetMetaElement)) {
context.report(
resource,
getMessage('metaElementInBody', context.language),
{
element: charsetMetaElement,
severity: Severity.error
});
return;
}
// All other charset meta elements should not be included.
if (charsetMetaElements.length > 1) {
const metaElements = charsetMetaElements.slice(1);
for (const metaElement of metaElements) {
context.report(
resource,
getMessage('metaElementDuplicated', context.language),
{
element: metaElement,
severity: Severity.warning
}
);
}
}
});
}
}