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
|
var ast = require("./ast");
function simplify(node) {
return collapse(removeEmpty(node));
}
function collapse(node) {
return collapsers[node.type](node);
}
var collapsers = {
fragment: collapseFragment,
element: collapseElement,
selfClosingElement: identity,
text: identity,
forceWrite: identity
};
function collapseFragment(node) {
return ast.fragment(collapseNodes(node.children));
}
function collapseElement(node) {
return ast.elementWithTag(node.tag, collapseNodes(node.children));
}
function identity(value) {
return value;
}
function collapseNodes(nodes) {
var children = [];
nodes.map(collapse).forEach(function(child) {
appendChild(children, child, children.push.bind(children));
});
return children;
}
function appendChild(children, child, append) {
var lastChild = children[children.length - 1];
if (child.type === "element" && !child.tag.fresh && lastChild && lastChild.type === "element" && child.tag.matchesElement(lastChild.tag)) {
child.children.forEach(function(grandChild) {
// Mutation is fine since simplifying elements create a copy of the children.
appendChild(lastChild.children, grandChild, lastChild.appendChild.bind(lastChild));
});
} else {
append(child);
}
}
function removeEmpty(node) {
return emptiers[node.type](node);
}
var emptiers = {
fragment: fragmentEmptier,
element: elementEmptier,
selfClosingElement: identity,
text: identity,
forceWrite: identity
};
function fragmentEmptier(fragment) {
return ast.fragment(listEmptier(fragment.children));
}
function elementEmptier(element) {
return ast.elementWithTag(element.tag, listEmptier(element.children));
}
function listEmptier(elements) {
return elements.map(removeEmpty).filter(function(child) {
return !child.isEmpty;
});
}
module.exports = simplify;
|