-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
parse5-adapter.ts
49 lines (43 loc) · 1.56 KB
/
parse5-adapter.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
import { Node, Document, isDocument } from 'domhandler';
import { parse as parseDocument, parseFragment, serialize } from 'parse5';
import htmlparser2Adapter from 'parse5-htmlparser2-tree-adapter';
import type { InternalOptions } from '../options';
interface Parse5Options extends InternalOptions {
context?: Node;
}
export function parse(
content: string,
options: Parse5Options,
isDocument?: boolean
): Document {
const opts = {
scriptingEnabled:
typeof options.scriptingEnabled === 'boolean'
? options.scriptingEnabled
: true,
treeAdapter: htmlparser2Adapter,
sourceCodeLocationInfo: options.sourceCodeLocationInfo,
};
const { context } = options;
// @ts-expect-error The tree adapter unfortunately doesn't return the exact types.
return isDocument
? parseDocument(content, opts)
: // @ts-expect-error Same issue again.
parseFragment(context, content, opts);
}
export function render(dom: Node | ArrayLike<Node>): string {
/*
* `dom-serializer` passes over the special "root" node and renders the
* node's children in its place. To mimic this behavior with `parse5`, an
* equivalent operation must be applied to the input array.
*/
const nodes = 'length' in dom ? dom : [dom];
for (let index = 0; index < nodes.length; index += 1) {
const node = nodes[index];
if (isDocument(node)) {
Array.prototype.splice.call(nodes, index, 1, ...node.children);
}
}
// @ts-expect-error Types don't align here either.
return serialize({ children: nodes }, { treeAdapter: htmlparser2Adapter });
}