-
-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathparser-location-info.test.ts
More file actions
355 lines (291 loc) · 12.6 KB
/
parser-location-info.test.ts
File metadata and controls
355 lines (291 loc) · 12.6 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import { it, assert, describe } from 'vitest';
import { outdent } from 'outdent';
import {
type ParserOptions,
type TreeAdapterTypeMap,
parse,
parseFragment,
type DefaultTreeAdapterMap,
type Token,
} from 'parse5';
import {
generateLocationInfoParserTests,
assertStartTagLocation,
assertNodeLocation,
} from 'parse5-test-utils/utils/generate-location-info-parser-tests.js';
import { generateTestsForEachTreeAdapter, treeAdapters } from 'parse5-test-utils/utils/common.js';
generateLocationInfoParserTests('location-info-parser', (input: string, opts: ParserOptions<TreeAdapterTypeMap>) => ({
node: parse(input, opts),
}));
generateTestsForEachTreeAdapter('location-info-parser', (treeAdapter) => {
it('Regression - Incorrect LocationInfo.endOffset for implicitly closed <p> element (GH-109)', () => {
const html = '<p>1<p class="2">3';
const opts = {
treeAdapter,
sourceCodeLocationInfo: true,
};
const fragment = parseFragment(html, opts);
const firstP = treeAdapter.getChildNodes(fragment)[0];
const firstPLocation = treeAdapter.getNodeSourceCodeLocation(firstP);
assert.ok(firstPLocation);
assert.strictEqual(html.substring(firstPLocation.startOffset, firstPLocation.endOffset), '<p>1');
});
it('Regression - Incorrect LocationInfo.endOffset for element with closing tag (GH-159)', () => {
const html = '<i>1</i>2';
const opts = {
treeAdapter,
sourceCodeLocationInfo: true,
};
const fragment = parseFragment(html, opts);
const firstChild = treeAdapter.getChildNodes(fragment)[0];
const location = treeAdapter.getNodeSourceCodeLocation(firstChild);
assert.ok(location);
assert.strictEqual(html.substring(location.startOffset, location.endOffset), '<i>1</i>');
});
it('Regression - Location info not exposed with parseFragment (GH-82)', () => {
const html = '<html><head></head><body>foo</body></html>';
const opts = {
treeAdapter,
sourceCodeLocationInfo: true,
};
const fragment = parseFragment(html, opts);
const firstChild = treeAdapter.getChildNodes(fragment)[0];
assert.ok(treeAdapter.getNodeSourceCodeLocation(firstChild));
});
it('Regression - location info mixin error when parsing <template> elements (GH-90)', () => {
const html = '<template>hello</template>';
const opts = {
treeAdapter,
sourceCodeLocationInfo: true,
};
assert.doesNotThrow(() => {
parseFragment(html, opts);
});
});
it('Regression - location info not attached for empty attributes (GH-96)', () => {
const html = '<div test-attr></div>';
const opts = {
treeAdapter,
sourceCodeLocationInfo: true,
};
const fragment = parseFragment(html, opts);
const firstChild = treeAdapter.getChildNodes(fragment)[0];
assert.ok(treeAdapter.getNodeSourceCodeLocation(firstChild)?.attrs?.['test-attr']);
});
it('Regression - location line incorrect when a character is unconsumed (GH-151)', () => {
const html = outdent`
<html><body><script>
var x = window.scrollY <
100;
</script>
</body></html>
`;
const opts = {
treeAdapter,
sourceCodeLocationInfo: true,
};
const document = parse(html, opts);
const htmlEl = treeAdapter.getChildNodes(document)[0];
const bodyEl = treeAdapter.getChildNodes(htmlEl)[1];
const scriptEl = treeAdapter.getChildNodes(bodyEl)[0];
const scriptLocation = treeAdapter.getNodeSourceCodeLocation(scriptEl);
assert.strictEqual(treeAdapter.getTagName(scriptEl), 'script');
assert.equal(scriptLocation?.endTag?.startLine, 4);
});
it('Regression - location.startTag should be available if end tag is missing (GH-181)', () => {
const html = '<p>test';
const opts = {
treeAdapter,
sourceCodeLocationInfo: true,
};
const fragment = parseFragment(html, opts);
const p = treeAdapter.getChildNodes(fragment)[0];
const location = treeAdapter.getNodeSourceCodeLocation(p);
assert.ok(location);
assertNodeLocation(location, html, html, [html]);
assertStartTagLocation(location, html, html, [html]);
assert.ok(!location.endTag);
});
it('Regression - location.endTag should be available adjusted SVG elements (GH-352)', () => {
const html = '<svg><foreignObject></foreignObject></svg>';
const opts = {
treeAdapter,
sourceCodeLocationInfo: true,
};
const fragment = parseFragment(html, opts);
const svg = treeAdapter.getChildNodes(fragment)[0];
const foreignObject = treeAdapter.getChildNodes(svg)[0];
const location = treeAdapter.getNodeSourceCodeLocation(foreignObject);
assert.ok(location && location.startTag && location.endTag);
assert.strictEqual(
html.slice(location.startTag.startOffset, location.endTag.endOffset),
'<foreignObject></foreignObject>',
);
});
it('Regression - SVG attribute location keys should match adjusted camelCase names (GH-318)', () => {
const html = '<svg viewBox="0 0 100 100"></svg>';
const opts = {
treeAdapter,
sourceCodeLocationInfo: true,
};
const fragment = parseFragment(html, opts);
const svg = treeAdapter.getChildNodes(fragment)[0];
const location = treeAdapter.getNodeSourceCodeLocation(svg);
const attrs = treeAdapter.getAttrList(svg);
assert.ok(location?.attrs);
// The attrs array should have the camelCase name
const viewBoxAttr = attrs.find((a) => a.name === 'viewBox');
assert.ok(viewBoxAttr, 'attrs should contain viewBox with camelCase name');
// The location attrs should use the same camelCase key
assert.ok(location.attrs['viewBox'], 'location.attrs should have camelCase key "viewBox"');
assert.ok(!location.attrs['viewbox'], 'location.attrs should not have lowercase key "viewbox"');
});
it('Regression - MathML definitionURL attribute location key should match adjusted name (GH-318)', () => {
const html = '<math><mprescripts definitionURL="http://example.com"></mprescripts></math>';
const opts = {
treeAdapter,
sourceCodeLocationInfo: true,
};
const fragment = parseFragment(html, opts);
const math = treeAdapter.getChildNodes(fragment)[0];
const mprescripts = treeAdapter.getChildNodes(math)[0];
const location = treeAdapter.getNodeSourceCodeLocation(mprescripts);
const attrs = treeAdapter.getAttrList(mprescripts);
assert.ok(location?.attrs);
const attr = attrs.find((a) => a.name === 'definitionURL');
assert.ok(attr, 'attrs should contain definitionURL');
assert.ok(location.attrs['definitionURL'], 'location.attrs should have "definitionURL"');
assert.ok(!location.attrs['definitionurl'], 'location.attrs should not have "definitionurl"');
});
it('Regression - Escaped script content has incorrect location info (GH-265)', () => {
const html = '<script>"<!--";</script>';
const opts = {
treeAdapter,
sourceCodeLocationInfo: true,
};
const fragment = parseFragment(html, opts);
const script = treeAdapter.getChildNodes(fragment)[0];
const location = treeAdapter.getNodeSourceCodeLocation(script);
const textLocation = treeAdapter.getNodeSourceCodeLocation(treeAdapter.getChildNodes(script)[0]);
assert.ok(location && textLocation);
assertNodeLocation(location, html, html, [html]);
assertStartTagLocation(location, html, html, [html]);
assertNodeLocation(textLocation, html.slice(8, 15), html, [html]);
});
it("Should use the HTML element's position for BODY, if BODY isn't closed", () => {
const html = outdent`
<html>
<body>
<p>test</p>
</html>
<!-- comment -->
`;
const opts = {
treeAdapter,
sourceCodeLocationInfo: true,
};
const document = parse(html, opts);
const htmlEl = treeAdapter.getChildNodes(document)[0];
const bodyEl = treeAdapter.getChildNodes(htmlEl)[1];
const htmlLocation = treeAdapter.getNodeSourceCodeLocation(htmlEl);
const bodyLocation = treeAdapter.getNodeSourceCodeLocation(bodyEl);
assert.ok(htmlLocation?.endTag && bodyLocation);
// HTML element's end tag's start location should be BODY's end location
assert.strictEqual(htmlLocation.endTag.startOffset, bodyLocation.endOffset);
assert.strictEqual(htmlLocation.endTag.startLine, bodyLocation.endLine);
assert.strictEqual(htmlLocation.endTag.startCol, bodyLocation.endCol);
// The HTML element's location should not be the location of EOF
assert.notStrictEqual(htmlLocation.endOffset, html.length);
});
it('Should set HTML location to EOF if no end tag is supplied', () => {
const html = outdent`
<html>
<body>
<p>test</p>
<!-- comment -->
`;
const opts = {
treeAdapter,
sourceCodeLocationInfo: true,
};
const document = parse(html, opts);
const htmlEl = treeAdapter.getChildNodes(document)[0];
const bodyEl = treeAdapter.getChildNodes(htmlEl)[1];
const htmlLocation = treeAdapter.getNodeSourceCodeLocation(htmlEl);
const bodyLocation = treeAdapter.getNodeSourceCodeLocation(bodyEl);
assert.ok(htmlLocation && bodyLocation);
assert.strictEqual(htmlLocation.endOffset, html.length);
assert.strictEqual(bodyLocation.endOffset, html.length);
});
});
describe('location-info-parser', () => {
it('Updating node source code location (GH-314)', () => {
const sourceCodeLocationSetter = {
setNodeSourceCodeLocation(
node: DefaultTreeAdapterMap['node'],
location: Token.ElementLocation | null,
): void {
node.sourceCodeLocation =
location === null
? null
: {
startLine: location.startLine * 2,
startCol: location.startCol * 2,
startOffset: location.startOffset * 2,
endLine: location.endLine * 2,
endCol: location.endCol * 2,
endOffset: location.endOffset * 2,
};
},
updateNodeSourceCodeLocation(
node: DefaultTreeAdapterMap['node'],
endLocation: Token.ElementLocation,
): void {
if (node.sourceCodeLocation) {
node.sourceCodeLocation = {
...node.sourceCodeLocation,
endLine: endLocation.endLine * 2,
endCol: endLocation.endCol * 2,
endOffset: endLocation.endOffset * 2,
};
}
},
};
const treeAdapter = { ...treeAdapters.default, ...sourceCodeLocationSetter };
const document = parse('<!doctype><body>Testing location</body>', {
treeAdapter,
sourceCodeLocationInfo: true,
});
const [doctype, html] = document.childNodes;
assert.ok(treeAdapters.default.isElementNode(html));
const [head, body] = html.childNodes;
assert.ok(treeAdapters.default.isElementNode(body));
const [text] = body.childNodes;
assert.deepEqual(doctype.sourceCodeLocation, {
startLine: 2,
startCol: 2,
startOffset: 0,
endLine: 2,
endCol: 22,
endOffset: 20,
});
assert.strictEqual(html.sourceCodeLocation, null);
assert.strictEqual(head.sourceCodeLocation, null);
assert.deepEqual(body.sourceCodeLocation, {
startLine: 2,
startCol: 22,
startOffset: 20,
endLine: 2,
endCol: 80,
endOffset: 78,
});
assert.deepEqual(text.sourceCodeLocation, {
startLine: 2,
startCol: 34,
startOffset: 32,
endLine: 2,
endCol: 66,
endOffset: 64,
});
});
});