aboutsummaryrefslogtreecommitdiff
path: root/lib/html/ast.js
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-10-13 23:29:41 +0100
committerMichael Williamson <mike@zwobble.org>2015-10-13 23:49:31 +0100
commit7b7ce0d563ddc07172b0f241a5c3baf344f256e8 (patch)
tree43f5fddf920d47e281a30139bde99da5a689d667 /lib/html/ast.js
parente04d054a1b414e843fa6d6f5ee1ae75c0df533b1 (diff)
Collapse similar HTML elements
Diffstat (limited to 'lib/html/ast.js')
-rw-r--r--lib/html/ast.js31
1 files changed, 28 insertions, 3 deletions
diff --git a/lib/html/ast.js b/lib/html/ast.js
index afecfce..96aa6c6 100644
--- a/lib/html/ast.js
+++ b/lib/html/ast.js
@@ -20,24 +20,49 @@ function elementWithTag(tag, children) {
return {
type: "element",
tag: tag,
- children: collapseFragments(children)
+ children: collapseFragments(children),
+ isEmpty: _.all(children, function(child) {
+ return child.isEmpty;
+ }),
+ appendChild: appendChild
+ };
+}
+
+function appendChild(child) {
+ this.children.push(child);
+ this.isEmpty = this.isEmpty && child.isEmpty;
+}
+
+function selfClosingElement(tagName, attributes) {
+ return {
+ type: "selfClosingElement",
+ tagName: tagName,
+ attributes: attributes
};
}
function text(value) {
return {
type: "text",
- value: value
+ value: value,
+ isEmpty: !value
};
}
+var forceWrite = {
+ type: "forceWrite",
+ isEmpty: false
+};
+
function collapseFragments(elements) {
return _.flatten((elements || []).map(function(element) {
return element.type === "fragment" ? element.children : [element];
- }), true)
+ }), true);
}
exports.fragment = fragment;
exports.element = element;
exports.elementWithTag = elementWithTag;
+exports.selfClosingElement = selfClosingElement;
exports.text = text;
+exports.forceWrite = forceWrite;