aboutsummaryrefslogtreecommitdiff
path: root/lib/html/simplify.js
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-06-09 22:32:30 +0100
committerMichael Williamson <mike@zwobble.org>2016-06-20 12:06:19 +0100
commitac3981df81148fad21c32b22496ecc145ad36e98 (patch)
treecfba69d2fedb0cacc7d544749f199bd339a1c6ad /lib/html/simplify.js
parent8617c85c8626354fff63eeaeb775402715fe63a4 (diff)
Use simpler structure for HTML without fragments
Diffstat (limited to 'lib/html/simplify.js')
-rw-r--r--lib/html/simplify.js78
1 files changed, 43 insertions, 35 deletions
diff --git a/lib/html/simplify.js b/lib/html/simplify.js
index 24a3447..081c3f8 100644
--- a/lib/html/simplify.js
+++ b/lib/html/simplify.js
@@ -1,79 +1,87 @@
+var _ = require("underscore");
+
var ast = require("./ast");
-function simplify(node) {
- return collapse(removeEmpty(node));
+function simplify(nodes) {
+ return collapse(removeEmpty(nodes));
+}
+
+function collapse(nodes) {
+ var children = [];
+
+ nodes.map(collapseNode).forEach(function(child) {
+ appendChild(children, child);
+ });
+ return children;
}
-function collapse(node) {
+function collapseNode(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));
+ return ast.elementWithTag(node.tag, collapse(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) {
+function appendChild(children, child) {
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));
+ appendChild(lastChild.children, grandChild);
});
} else {
- append(child);
+ children.push(child);
}
}
-function removeEmpty(node) {
- return emptiers[node.type](node);
+function removeEmpty(nodes) {
+ return flatMap(nodes, function(node) {
+ return emptiers[node.type](node);
+ });
+}
+
+function flatMap(values, func) {
+ return _.flatten(_.map(values, func), true);
}
var emptiers = {
- fragment: fragmentEmptier,
element: elementEmptier,
- selfClosingElement: identity,
- text: identity,
- forceWrite: identity
+ selfClosingElement: neverEmpty,
+ text: textEmptier,
+ forceWrite: neverEmpty
};
-function fragmentEmptier(fragment) {
- return ast.fragment(listEmptier(fragment.children));
+function neverEmpty(node) {
+ return [node];
}
function elementEmptier(element) {
- return ast.elementWithTag(element.tag, listEmptier(element.children));
+ var children = removeEmpty(element.children);
+ if (children.length === 0) {
+ return [];
+ } else {
+ return ast.elementWithTag(element.tag, children);
+ }
}
-function listEmptier(elements) {
- return elements.map(removeEmpty).filter(function(child) {
- return !child.isEmpty;
- });
+function textEmptier(node) {
+ if (node.value.length === 0) {
+ return [];
+ } else {
+ return [node];
+ }
}
-
module.exports = simplify;