forked from capricorn86/happy-dom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XMLParser.test.ts
467 lines (410 loc) · 17.5 KB
/
XMLParser.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import XMLParser from '../../src/xml-parser/XMLParser';
import Window from '../../src/window/Window';
import IWindow from '../../src/window/IWindow';
import IDocument from '../../src/nodes/document/IDocument';
import Node from '../../src/nodes/node/Node';
import IHTMLElement from '../../src/nodes/html-element/IHTMLElement';
import IHTMLTemplateElement from '../../src/nodes/html-template-element/IHTMLTemplateElement';
import XMLParserHTML from './data/XMLParserHTML';
import NamespaceURI from '../../src/config/NamespaceURI';
import DocumentType from '../../src/nodes/document-type/DocumentType';
import XMLSerializer from '../../src/xml-serializer/XMLSerializer';
const GET_EXPECTED_HTML = (html: string): string =>
html
.replace('<?Question mark comment>', '<!--?Question mark comment-->')
.replace('<!Exclamation mark comment>', '<!--Exclamation mark comment-->')
.replace('<self-closing-custom-tag />', '<self-closing-custom-tag></self-closing-custom-tag>')
.replace(/[\s]/gm, '');
describe('XMLParser', () => {
let window: IWindow;
let document: IDocument;
beforeEach(() => {
window = new Window();
document = window.document;
});
describe('parse()', () => {
it('Parses HTML with a single <div>.', () => {
const root = XMLParser.parse(window.document, '<div></div>');
expect(root.childNodes.length).toBe(1);
expect(root.childNodes[0].childNodes.length).toBe(0);
expect((<IHTMLElement>root.childNodes[0]).tagName).toBe('DIV');
});
it('Parses HTML with a single <div> with attributes.', () => {
const root = XMLParser.parse(
window.document,
'<div class="class1 class2" id="id" data-no-value></div>'
);
expect(root.childNodes.length).toBe(1);
expect(root.childNodes[0].childNodes.length).toBe(0);
expect((<IHTMLElement>root.childNodes[0]).tagName).toBe('DIV');
expect((<IHTMLElement>root.childNodes[0]).id).toBe('id');
expect((<IHTMLElement>root.childNodes[0]).className).toBe('class1 class2');
expect((<IHTMLElement>root.childNodes[0]).attributes.length).toBe(3);
expect((<IHTMLElement>root.childNodes[0]).attributes[0].name).toBe('class');
expect((<IHTMLElement>root.childNodes[0]).attributes[0].value).toBe('class1 class2');
expect((<IHTMLElement>root.childNodes[0]).attributes[0].namespaceURI).toBe(null);
expect((<IHTMLElement>root.childNodes[0]).attributes[0].specified).toBe(true);
expect(
(<IHTMLElement>root.childNodes[0]).attributes[0].ownerElement === root.childNodes[0]
).toBe(true);
expect((<IHTMLElement>root.childNodes[0]).attributes[0].ownerDocument === document).toBe(
true
);
expect((<IHTMLElement>root.childNodes[0]).attributes[1].name).toBe('id');
expect((<IHTMLElement>root.childNodes[0]).attributes[1].value).toBe('id');
expect((<IHTMLElement>root.childNodes[0]).attributes[1].namespaceURI).toBe(null);
expect((<IHTMLElement>root.childNodes[0]).attributes[1].specified).toBe(true);
expect(
(<IHTMLElement>root.childNodes[0]).attributes[1].ownerElement === root.childNodes[0]
).toBe(true);
expect((<IHTMLElement>root.childNodes[0]).attributes[1].ownerDocument === document).toBe(
true
);
expect((<IHTMLElement>root.childNodes[0]).attributes[2].name).toBe('data-no-value');
expect((<IHTMLElement>root.childNodes[0]).attributes[2].value).toBe('');
expect((<IHTMLElement>root.childNodes[0]).attributes[2].namespaceURI).toBe(null);
expect((<IHTMLElement>root.childNodes[0]).attributes[2].specified).toBe(true);
expect(
(<IHTMLElement>root.childNodes[0]).attributes[2].ownerElement === root.childNodes[0]
).toBe(true);
expect((<IHTMLElement>root.childNodes[0]).attributes[2].ownerDocument === document).toBe(
true
);
expect((<IHTMLElement>root.childNodes[0]).attributes['class'].name).toBe('class');
expect((<IHTMLElement>root.childNodes[0]).attributes['class'].value).toBe('class1 class2');
expect((<IHTMLElement>root.childNodes[0]).attributes['class'].namespaceURI).toBe(null);
expect((<IHTMLElement>root.childNodes[0]).attributes['class'].specified).toBe(true);
expect(
(<IHTMLElement>root.childNodes[0]).attributes['class'].ownerElement === root.childNodes[0]
).toBe(true);
expect(
(<IHTMLElement>root.childNodes[0]).attributes['class'].ownerDocument === document
).toBe(true);
expect((<IHTMLElement>root.childNodes[0]).attributes['id'].name).toBe('id');
expect((<IHTMLElement>root.childNodes[0]).attributes['id'].value).toBe('id');
expect((<IHTMLElement>root.childNodes[0]).attributes['id'].namespaceURI).toBe(null);
expect((<IHTMLElement>root.childNodes[0]).attributes['id'].specified).toBe(true);
expect(
(<IHTMLElement>root.childNodes[0]).attributes['id'].ownerElement === root.childNodes[0]
).toBe(true);
expect((<IHTMLElement>root.childNodes[0]).attributes['id'].ownerDocument === document).toBe(
true
);
expect((<IHTMLElement>root.childNodes[0]).attributes['data-no-value'].name).toBe(
'data-no-value'
);
expect((<IHTMLElement>root.childNodes[0]).attributes['data-no-value'].value).toBe('');
expect((<IHTMLElement>root.childNodes[0]).attributes['data-no-value'].namespaceURI).toBe(
null
);
expect((<IHTMLElement>root.childNodes[0]).attributes['data-no-value'].specified).toBe(true);
expect(
(<IHTMLElement>root.childNodes[0]).attributes['data-no-value'].ownerElement ===
root.childNodes[0]
).toBe(true);
expect(
(<IHTMLElement>root.childNodes[0]).attributes['data-no-value'].ownerDocument === document
).toBe(true);
});
it('Parses an entire HTML page.', () => {
const root = XMLParser.parse(window.document, XMLParserHTML);
expect(new XMLSerializer().serializeToString(root).replace(/[\s]/gm, '')).toBe(
GET_EXPECTED_HTML(XMLParserHTML)
);
});
it('Parses a page with document type set to "HTML 4.01".', () => {
const pageHTML = XMLParserHTML.trim().replace(
'<!DOCTYPE html>',
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
);
const root = XMLParser.parse(window.document, pageHTML);
const doctype = <DocumentType>root.childNodes[0];
expect(doctype.name).toBe('HTML');
expect(doctype.publicId).toBe('-//W3C//DTD HTML 4.01//EN');
expect(doctype.systemId).toBe('http://www.w3.org/TR/html4/strict.dtd');
expect(new XMLSerializer().serializeToString(root).replace(/[\s]/gm, '')).toBe(
GET_EXPECTED_HTML(pageHTML)
);
});
it('Parses a page with document type set to "MathML 1.01".', () => {
const pageHTML = XMLParserHTML.trim().replace(
'<!DOCTYPE html>',
'<!DOCTYPE math SYSTEM "http://www.w3.org/Math/DTD/mathml1/mathml.dtd">'
);
const root = XMLParser.parse(window.document, pageHTML);
const doctype = <DocumentType>root.childNodes[0];
expect(doctype.name).toBe('math');
expect(doctype.publicId).toBe('');
expect(doctype.systemId).toBe('http://www.w3.org/Math/DTD/mathml1/mathml.dtd');
expect(new XMLSerializer().serializeToString(root).replace(/[\s]/gm, '')).toBe(
GET_EXPECTED_HTML(pageHTML)
);
});
it('Handles unclosed tags of unnestable elements (e.g. <a>, <li>).', () => {
const root = XMLParser.parse(
window.document,
`
<div class="test" disabled>
<ul>
<li><a href="http://localhost:8080/test/test" target="_blank">Test</a>
<li><span>Test 2</span></li>
<li><b>Test 3</b></li>
</ul>
<a><a>Test</a></a>
</div>
`
);
expect(new XMLSerializer().serializeToString(root).replace(/[\s]/gm, '')).toBe(
`
<div class="test" disabled="">
<ul>
<li><a href="http://localhost:8080/test/test" target="_blank">Test</a></li>
<li><span>Test 2</span></li>
<li><b>Test 3</b></li>
</ul>
<a></a><a>Test</a>
</div>
`.replace(/[\s]/gm, '')
);
});
it('Does not parse the content of script, style and template elements.', () => {
const root = XMLParser.parse(
window.document,
`<div>
<script>if(1<Math['random']()){}else if(Math['random']()>1){console.log("1")}</script>
<script><b></b></script>
<style><b></b></style>
<template><b></b></template>
</div>`
);
expect((<IHTMLElement>root.children[0].children[0]).innerText).toBe(
`if(1<Math['random']()){}else if(Math['random']()>1){console.log("1")}`
);
expect((<IHTMLElement>root.children[0].children[1]).innerText).toBe('<b></b>');
expect((<IHTMLElement>root.children[0].children[2]).innerText).toBe('<b></b>');
expect((<IHTMLTemplateElement>root.children[0].children[3]).content.textContent).toBe(
'<b></b>'
);
expect(new XMLSerializer().serializeToString(root).replace(/[\s]/gm, '')).toBe(
`<div>
<script>if(1<Math['random']()){}else if(Math['random']()>1){console.log("1")}</script>
<script><b></b></script>
<style><b></b></style>
<template><b></b></template>
</div>`.replace(/[\s]/gm, '')
);
const root2 = XMLParser.parse(
window.document,
`<html>
<head>
<title>Title</title>
</head>
<body>
<script type="text/javascript">var vars = []; for (var i=0;i<vars.length;i++) {}</script>
</body>
</html>`
);
expect((<IHTMLElement>root2.children[0].children[1].children[0]).innerText).toBe(
'var vars = []; for (var i=0;i<vars.length;i++) {}'
);
});
it('Handles unclosed regular elements.', () => {
const root = XMLParser.parse(window.document, `<div>test`);
expect(root.childNodes.length).toBe(1);
expect((<IHTMLElement>root.childNodes[0]).tagName).toBe('DIV');
expect(root.childNodes[0].childNodes[0].nodeType).toBe(Node.TEXT_NODE);
});
it('Parses an SVG with "xmlns" set to HTML.', () => {
const root = XMLParser.parse(
window.document,
`
<div>
<svg viewBox="0 0 300 100" stroke="red" fill="grey" xmlns="${NamespaceURI.html}">
<circle cx="50" cy="50" r="40" />
<circle cx="150" cy="50" r="4" />
<svg viewBox="0 0 10 10" x="200" width="100">
<circle cx="5" cy="5" r="4" />
</svg>
</svg>
</div>
`
);
const div = root.children[0];
const svg = div.children[0];
const circle = svg.children[0];
expect(div.namespaceURI).toBe(NamespaceURI.html);
expect(svg.namespaceURI).toBe(NamespaceURI.svg);
expect(circle.namespaceURI).toBe(NamespaceURI.svg);
// Attributes should be in lower-case now as the namespace is HTML
expect(svg.attributes.length).toBe(4);
expect(svg.attributes[0].name).toBe('viewBox');
expect(svg.attributes[0].value).toBe('0 0 300 100');
expect(svg.attributes[0].namespaceURI).toBe(null);
expect(svg.attributes[0].specified).toBe(true);
expect(svg.attributes[0].ownerElement === svg).toBe(true);
expect(svg.attributes[0].ownerDocument === document).toBe(true);
expect(svg.attributes[1].name).toBe('stroke');
expect(svg.attributes[1].value).toBe('red');
expect(svg.attributes[1].namespaceURI).toBe(null);
expect(svg.attributes[1].specified).toBe(true);
expect(svg.attributes[1].ownerElement === svg).toBe(true);
expect(svg.attributes[1].ownerDocument === document).toBe(true);
expect(svg.attributes[2].name).toBe('fill');
expect(svg.attributes[2].value).toBe('grey');
expect(svg.attributes[2].namespaceURI).toBe(null);
expect(svg.attributes[2].specified).toBe(true);
expect(svg.attributes[2].ownerElement === svg).toBe(true);
expect(svg.attributes[2].ownerDocument === document).toBe(true);
expect(svg.attributes[3].name).toBe('xmlns');
expect(svg.attributes[3].value).toBe(NamespaceURI.html);
expect(svg.attributes[3].namespaceURI).toBe(NamespaceURI.html);
expect(svg.attributes[3].specified).toBe(true);
expect(svg.attributes[3].ownerElement === svg).toBe(true);
expect(svg.attributes[3].ownerDocument === document).toBe(true);
expect(svg.attributes['viewBox'].name).toBe('viewBox');
expect(svg.attributes['viewBox'].value).toBe('0 0 300 100');
expect(svg.attributes['viewBox'].namespaceURI).toBe(null);
expect(svg.attributes['viewBox'].specified).toBe(true);
expect(svg.attributes['viewBox'].ownerElement === svg).toBe(true);
expect(svg.attributes['viewBox'].ownerDocument === document).toBe(true);
expect(svg.attributes['stroke'].name).toBe('stroke');
expect(svg.attributes['stroke'].value).toBe('red');
expect(svg.attributes['stroke'].namespaceURI).toBe(null);
expect(svg.attributes['stroke'].specified).toBe(true);
expect(svg.attributes['stroke'].ownerElement === svg).toBe(true);
expect(svg.attributes['stroke'].ownerDocument === document).toBe(true);
expect(svg.attributes['fill'].name).toBe('fill');
expect(svg.attributes['fill'].value).toBe('grey');
expect(svg.attributes['fill'].namespaceURI).toBe(null);
expect(svg.attributes['fill'].specified).toBe(true);
expect(svg.attributes['fill'].ownerElement === svg).toBe(true);
expect(svg.attributes['fill'].ownerDocument === document).toBe(true);
expect(svg.attributes['xmlns'].name).toBe('xmlns');
expect(svg.attributes['xmlns'].value).toBe(NamespaceURI.html);
expect(svg.attributes['xmlns'].namespaceURI).toBe(NamespaceURI.html);
expect(svg.attributes['xmlns'].specified).toBe(true);
expect(svg.attributes['xmlns'].ownerElement === svg).toBe(true);
expect(svg.attributes['xmlns'].ownerDocument === document).toBe(true);
expect(new XMLSerializer().serializeToString(root).replace(/[\s]/gm, '')).toBe(
`
<div>
<svg viewBox="0 0 300 100" stroke="red" fill="grey" xmlns="${NamespaceURI.html}">
<circle cx="50" cy="50" r="40"></circle>
<circle cx="150" cy="50" r="4"></circle>
<svg viewBox="0 0 10 10" x="200" width="100">
<circle cx="5" cy="5" r="4"></circle>
</svg>
</svg>
</div>
`.replace(/[\s]/gm, '')
);
});
it('Parses a malformed SVG with "xmlns" set to HTML.', () => {
const root = XMLParser.parse(
window.document,
`
<div>
<svg viewBox="0 0 300 100" stroke="red" fill="grey" xmlns="${NamespaceURI.html}">
<ellipse cx="50" cy="50" r="40">
<line cx="50" cy="50" r="40">
<path cx="50" cy="50" r="40">
<polygon cx="50" cy="50" r="40">
<polyline cx="50" cy="50" r="40" />
<rect cx="50" cy="50" r="40" />
<stop cx="50" cy="50" r="40" />
<use cx="50" cy="50" r="40" />
<circle cx="150" cy="50" r="4"><test></test></circle>
<svg viewBox="0 0 10 10" x="200" width="100">
<circle cx="5" cy="5" r="4" />
</svg>
</svg>
</div>
`
);
expect(new XMLSerializer().serializeToString(root).replace(/[\s]/gm, '')).toBe(
`
<div>
<svg viewBox="0 0 300 100" stroke="red" fill="grey" xmlns="${NamespaceURI.html}">
<ellipse cx="50" cy="50" r="40">
<line cx="50" cy="50" r="40">
<path cx="50" cy="50" r="40">
<polygon cx="50" cy="50" r="40">
<polyline cx="50" cy="50" r="40"></polyline>
<rect cx="50" cy="50" r="40"></rect>
<stop cx="50" cy="50" r="40"></stop>
<use cx="50" cy="50" r="40"></use>
<circle cx="150" cy="50" r="4"><test></test></circle>
<svg viewBox="0 0 10 10" x="200" width="100">
<circle cx="5" cy="5" r="4"></circle>
</svg>
</polygon></path></line></ellipse></svg>
</div>
`.replace(/[\s]/gm, '')
);
});
it('Parses childless elements with start and end tag names in different case', () => {
const root = XMLParser.parse(
window.document,
`
<script type="text/JavaScript">console.log('hello')</SCRIPT>
`
);
expect((<IHTMLElement>root.children[0]).innerText).toBe(`console.log('hello')`);
});
it('Handles different value types.', () => {
const root1 = XMLParser.parse(window.document, null);
expect(new XMLSerializer().serializeToString(root1)).toBe('');
const root2 = XMLParser.parse(window.document, undefined);
expect(new XMLSerializer().serializeToString(root2)).toBe('');
const root3 = XMLParser.parse(window.document, <string>(<unknown>1000));
expect(new XMLSerializer().serializeToString(root3)).toBe('1000');
const root4 = XMLParser.parse(window.document, <string>(<unknown>false));
expect(new XMLSerializer().serializeToString(root4)).toBe('false');
});
it('Parses conditional comments', () => {
const testHTML = [
'<!--[if IE 8]>\n' + '<p>Welcome to Internet Explorer 8.</p>\n' + '<![endif]-->',
'<!--[if gte IE 7]>\n' +
'<script>\n' +
' alert("Congratulations! You are running Internet Explorer 7 or a later version of Internet Explorer.");\n' +
'</script>\n' +
'<p>Thank you for closing the message box.</p>\n' +
'<![endif]-->',
'<!--[if IE 5]>\n' +
'<p>Welcome to any incremental version of Internet Explorer 5!</p>\n' +
'<![endif]-->',
'<!--[if IE 5.0000]>\n' + '<p>Welcome to Internet Explorer 5.0!</p>\n' + '<![endif]-->',
'<!--[if WindowsEdition 1]>\n' +
'<p>You are using Windows Ultimate Edition.</p>\n' +
'<![endif]-->',
'<!--[if lt Contoso 2]>\n' +
'<p>Your version of the Contoso control is out of date; Please update to the latest.</p>\n' +
'<![endif]-->',
'<!DOCTYPE html><html lang="en">\n' +
'<head>\n' +
' <meta charset="UTF-8">\n' +
' <title>Title</title>\n' +
'</head>\n' +
'<body>\n' +
'<!--[if lt IE 9]>\n' +
"<script>window.location = 'browser.htm';</script>\n" +
'<![endif]-->\n' +
'\n' +
'\n' +
'<script>\n' +
" const node = document.createElement('a');\n" +
" node.href = 'http://www.google.com';\n" +
" node.target = '_blank';\n" +
" node.innerHTML = 'google';\n" +
' window.document.body.appendChild(node);\n' +
'</script>\n' +
'</body>\n' +
'</html>\n'
];
for (const html of testHTML) {
const root = XMLParser.parse(window.document, html);
expect(new XMLSerializer().serializeToString(root)).toBe(html);
}
});
});
});