-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
parse5.js
32 lines (27 loc) · 997 Bytes
/
parse5.js
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
var parse5 = require('parse5');
var htmlparser2Adapter = require('parse5-htmlparser2-tree-adapter');
exports.parse = function (content, options, isDocument) {
var opts = {
treeAdapter: htmlparser2Adapter,
sourceCodeLocationInfo: options.sourceCodeLocationInfo,
};
var context = options.context;
return isDocument
? parse5.parse(content, opts)
: parse5.parseFragment(context, content, opts);
};
exports.render = function (dom) {
// `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.
var nodes = 'length' in dom ? dom : [dom];
for (var index = 0; index < nodes.length; index += 1) {
if (nodes[index].type === 'root') {
nodes.splice.apply(nodes, [index, 1].concat(nodes[index].children));
}
}
return parse5.serialize(
{ children: nodes },
{ treeAdapter: htmlparser2Adapter }
);
};