-
-
Notifications
You must be signed in to change notification settings - Fork 232
/
index.test.ts
105 lines (86 loc) · 4.04 KB
/
index.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
import * as assert from 'node:assert';
import * as parse5 from 'parse5';
import { Parser, ParserOptions } from './index.js';
import type { TreeAdapterTypeMap } from './../tree-adapters/interface.js';
import { generateParsingTests } from 'parse5-test-utils/utils/generate-parsing-tests.js';
import { treeAdapters } from 'parse5-test-utils/utils/common.js';
import { NAMESPACES as NS } from '../common/html.js';
const origParseFragment = Parser.parseFragment;
generateParsingTests('parser', 'Parser', {}, (test, opts) => ({
node: test.fragmentContext
? parse5.parseFragment(test.fragmentContext, test.input, opts)
: parse5.parse(test.input, opts),
}));
describe('parser', () => {
it('Regression - HTML5 Legacy Doctype Misparsed with htmlparser2 tree adapter (GH-45)', () => {
const html = '<!DOCTYPE html SYSTEM "about:legacy-compat"><html><head></head><body>Hi there!</body></html>';
const document = parse5.parse(html, { treeAdapter: treeAdapters.htmlparser2 });
assert.ok(treeAdapters.htmlparser2.isDocumentTypeNode(document.childNodes[0]));
assert.strictEqual(document.childNodes[0].data, '!DOCTYPE html SYSTEM "about:legacy-compat"');
});
describe('Regression - Incorrect arguments fallback for the parser.parseFragment (GH-82, GH-83)', () => {
beforeEach(() => {
Parser.parseFragment = function <T extends TreeAdapterTypeMap>(
html: string,
fragmentContext?: T['element'],
options?: ParserOptions<T>
): {
html: string;
fragmentContext: T['element'] | null | undefined;
options: ParserOptions<T> | undefined;
} {
return {
html,
fragmentContext,
options,
};
};
});
afterEach(() => {
Parser.parseFragment = origParseFragment;
});
it('parses correctly', () => {
const fragmentContext = treeAdapters.default.createElement('div', NS.HTML, []);
const html = '<script></script>';
const opts = { sourceCodeLocationInfo: true };
let args: any = parse5.parseFragment(fragmentContext, html, opts);
expect(args).toHaveProperty('fragmentContext', fragmentContext);
expect(args).toHaveProperty('html', html);
assert.ok(args.options.sourceCodeLocationInfo);
args = parse5.parseFragment(html, opts);
assert.ok(!args.fragmentContext);
expect(args).toHaveProperty('html', html);
assert.ok(args.options.sourceCodeLocationInfo);
args = parse5.parseFragment(html);
assert.ok(!args.fragmentContext);
expect(args).toHaveProperty('html', html);
assert.ok(!args.options);
});
});
describe("Regression - Don't inherit from Object when creating collections (GH-119)", () => {
beforeEach(() => {
/*eslint-disable no-extend-native*/
// @ts-expect-error Adding unknown prototype method
Object.prototype.heyYo = 123;
/*eslint-enable no-extend-native*/
});
afterEach(() => {
// @ts-expect-error Deleting unknown prototype property
delete Object.prototype.heyYo;
});
it('parses correctly', () => {
const fragment = parse5.parseFragment('<div id="123">', {
treeAdapter: treeAdapters.htmlparser2,
});
assert.ok(treeAdapters.htmlparser2.isElementNode(fragment.childNodes[0]));
assert.strictEqual(treeAdapters.htmlparser2.getAttrList(fragment.childNodes[0]).length, 1);
});
});
it('Regression - DOCTYPE empty fields (GH-236)', () => {
const document = parse5.parse('<!DOCTYPE>');
const doctype = document.childNodes[0];
expect(doctype).toHaveProperty('name', '');
expect(doctype).toHaveProperty('publicId', '');
expect(doctype).toHaveProperty('systemId', '');
});
});