-
-
Notifications
You must be signed in to change notification settings - Fork 232
/
parser-location-info.test.ts
291 lines (239 loc) · 10.5 KB
/
parser-location-info.test.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
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
import * as assert from 'node:assert';
import { outdent } from 'outdent';
import * as parse5 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';
import { TreeAdapterTypeMap } from 'parse5/dist/tree-adapters/interface.js';
generateLocationInfoParserTests(
'location-info-parser',
'Parser',
(input: string, opts: parse5.ParserOptions<TreeAdapterTypeMap>) => ({
node: parse5.parse(input, opts),
})
);
generateTestsForEachTreeAdapter('location-info-parser', (treeAdapter) => {
test('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 = parse5.parseFragment(html, opts);
const firstP = treeAdapter.getChildNodes(fragment)[0];
const firstPLocation = treeAdapter.getNodeSourceCodeLocation(firstP)!;
assert.strictEqual(html.substring(firstPLocation.startOffset, firstPLocation.endOffset), '<p>1');
});
test('Regression - Incorrect LocationInfo.endOffset for element with closing tag (GH-159)', () => {
const html = '<i>1</i>2';
const opts = {
treeAdapter,
sourceCodeLocationInfo: true,
};
const fragment = parse5.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>');
});
test('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 = parse5.parseFragment(html, opts);
const firstChild = treeAdapter.getChildNodes(fragment)[0];
assert.ok(treeAdapter.getNodeSourceCodeLocation(firstChild));
});
test('Regression - location info mixin error when parsing <template> elements (GH-90)', () => {
const html = '<template>hello</template>';
const opts = {
treeAdapter,
sourceCodeLocationInfo: true,
};
assert.doesNotThrow(() => {
parse5.parseFragment(html, opts);
});
});
test('Regression - location info not attached for empty attributes (GH-96)', () => {
const html = '<div test-attr></div>';
const opts = {
treeAdapter,
sourceCodeLocationInfo: true,
};
const fragment = parse5.parseFragment(html, opts);
const firstChild = treeAdapter.getChildNodes(fragment)[0];
assert.ok(treeAdapter.getNodeSourceCodeLocation(firstChild)?.attrs?.['test-attr']);
});
test('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 = parse5.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);
});
test('Regression - location.startTag should be available if end tag is missing (GH-181)', () => {
const html = '<p>test';
const opts = {
treeAdapter,
sourceCodeLocationInfo: true,
};
const fragment = parse5.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);
});
test('Regression - location.endTag should be available adjusted SVG elements (GH-352)', () => {
const html = '<svg><foreignObject></foreignObject></svg>';
const opts = {
treeAdapter,
sourceCodeLocationInfo: true,
};
const fragment = parse5.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>'
);
});
test('Regression - Escaped script content has incorrect location info (GH-265)', () => {
const html = '<script>"<!--";</script>';
const opts = {
treeAdapter,
sourceCodeLocationInfo: true,
};
const fragment = parse5.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]);
});
test("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 = parse5.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);
});
test('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 = parse5.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: any, location: any): void {
node.sourceCodeLocation =
location === null
? null
: {
start: {
line: location.startLine,
column: location.startCol,
offset: location.startOffset,
},
end: {
line: location.endLine,
column: location.endCol,
offset: location.endOffset,
},
};
},
updateNodeSourceCodeLocation(node: any, endLocation: any): void {
node.sourceCodeLocation = {
start: node.sourceCodeLocation.start,
end: {
line: endLocation.endLine,
column: endLocation.endCol,
offset: endLocation.endOffset,
},
};
},
};
const treeAdapter = { ...treeAdapters.default, ...sourceCodeLocationSetter };
const document = parse5.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, {
start: { line: 1, column: 1, offset: 0 },
end: { line: 1, column: 11, offset: 10 },
});
assert.strictEqual(html.sourceCodeLocation, null);
assert.strictEqual(head.sourceCodeLocation, null);
assert.deepEqual(body.sourceCodeLocation, {
start: { line: 1, column: 11, offset: 10 },
end: { line: 1, column: 40, offset: 39 },
});
assert.deepEqual(text.sourceCodeLocation, {
start: { line: 1, column: 17, offset: 16 },
end: { line: 1, column: 33, offset: 32 },
});
});
});