aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-10-13 22:10:42 +0100
committerMichael Williamson <mike@zwobble.org>2015-10-13 22:10:42 +0100
commitaac776b1711d1e286812035a28108c622e73994a (patch)
tree366673f18143771fabbadb0fcef86d638ce33f43
parent2fb7a36230b167aac99fe8d43959833bd3b244dd (diff)
Add simplification of nodes
-rw-r--r--lib/html/ast.js13
-rw-r--r--lib/html/index.js14
-rw-r--r--lib/html/simplify.js44
-rw-r--r--tests/html/ast.tests.js8
-rw-r--r--tests/html/simplify.tests.js36
5 files changed, 108 insertions, 7 deletions
diff --git a/lib/html/ast.js b/lib/html/ast.js
index 25f4874..afecfce 100644
--- a/lib/html/ast.js
+++ b/lib/html/ast.js
@@ -1,5 +1,8 @@
var _ = require("underscore");
+var htmlPaths = require("../html-paths");
+
+
function fragment(children) {
return {
type: "fragment",
@@ -8,10 +11,15 @@ function fragment(children) {
}
function element(tagName, attributes, children) {
+ return elementWithTag(
+ htmlPaths.element(tagName, attributes, {fresh: true}),
+ children);
+}
+
+function elementWithTag(tag, children) {
return {
type: "element",
- tagName: tagName,
- attributes: attributes,
+ tag: tag,
children: collapseFragments(children)
};
}
@@ -31,4 +39,5 @@ function collapseFragments(elements) {
exports.fragment = fragment;
exports.element = element;
+exports.elementWithTag = elementWithTag;
exports.text = text;
diff --git a/lib/html/index.js b/lib/html/index.js
index f9e957a..41d348f 100644
--- a/lib/html/index.js
+++ b/lib/html/index.js
@@ -1,5 +1,17 @@
-var ast = require("./ast.js");
+var ast = require("./ast");
exports.fragment = ast.fragment;
exports.element = ast.element;
+exports.elementWithTag = ast.elementWithTag;
exports.text = ast.text;
+
+function pathToNode(path, children) {
+ var result = ast.fragment(children);
+ for (var index = path.length - 1; index >= 0; index--) {
+ result = ast.elementWithTag(path[index], [result]);
+ }
+ return result;
+}
+
+exports.pathToNode = pathToNode;
+exports.simplify = require("./simplify");
diff --git a/lib/html/simplify.js b/lib/html/simplify.js
new file mode 100644
index 0000000..52a1b0a
--- /dev/null
+++ b/lib/html/simplify.js
@@ -0,0 +1,44 @@
+var _ = require("underscore");
+
+var ast = require("./ast");
+
+function simplify(node) {
+ return simplifiers[node.type](node);
+};
+
+var simplifiers = {
+ fragment: simplifyFragment,
+ element: simplifyElement,
+ text: simplifyText
+};
+
+function simplifyFragment(node) {
+ return ast.fragment(simplifyNodes(node.children));
+}
+
+function simplifyElement(node) {
+ return ast.elementWithTag(node.tag, simplifyNodes(node.children));
+}
+
+function simplifyText(node) {
+ return node;
+}
+
+function simplifyNodes(nodes) {
+ var children = [];
+
+ nodes.map(simplify).forEach(function(child) {
+ var lastChild = children[children.length - 1];
+ if (child.type === "element" && !child.tag.fresh && lastChild && _.isEqual(lastChild.tag, child.tag)) {
+ child.children.forEach(function(grandChild) {
+ // Mutation is fine since simplifying elements create a copy of the children.
+ lastChild.children.push(grandChild);
+ });
+ } else {
+ children.push(child);
+ }
+ });
+ return children;
+}
+
+module.exports = simplify;
diff --git a/tests/html/ast.tests.js b/tests/html/ast.tests.js
index c851377..c2a1479 100644
--- a/tests/html/ast.tests.js
+++ b/tests/html/ast.tests.js
@@ -7,14 +7,14 @@ var html = require("../../lib/html");
describe("HTML ast", function() {
test("nested fragments are collapsed", function() {
assert.deepEqual(
- html.fragment([html.fragment([html.text("Hello")])]),
- html.fragment([html.text("Hello")]));
+ html.fragment([html.text("Hello")]),
+ html.fragment([html.fragment([html.text("Hello")])]));
});
test("children of elements are collapsed", function() {
assert.deepEqual(
- html.element("p", {}, [html.fragment([html.text("Hello")])]),
- html.element("p", {}, [html.text("Hello")]));
+ html.element("p", {}, [html.text("Hello")]),
+ html.element("p", {}, [html.fragment([html.text("Hello")])]));
});
});
diff --git a/tests/html/simplify.tests.js b/tests/html/simplify.tests.js
new file mode 100644
index 0000000..7893663
--- /dev/null
+++ b/tests/html/simplify.tests.js
@@ -0,0 +1,36 @@
+var assert = require("assert");
+
+var test = require("../testing").test;
+var html = require("../../lib/html");
+var htmlPaths = require("../../lib/html-paths");
+
+
+var fragment = html.fragment;
+var element = html.element;
+var text = html.text;
+var pathToNode = html.pathToNode;
+
+describe("simplify", function() {
+ test("successive fresh elements are not collapsed", function() {
+ var path = htmlPaths.elements([
+ htmlPaths.element("p", {}, {fresh: true})]);
+ var original = fragment([
+ pathToNode(path, [text("Hello")]),
+ pathToNode(path, [text(" there")])]);
+
+ assert.deepEqual(
+ html.simplify(original),
+ original);
+ });
+
+ test("successive plain non-fresh elements are collapsed if they have the same tag name", function() {
+ var path = htmlPaths.elements([
+ htmlPaths.element("p", {}, {fresh: false})]);
+ assert.deepEqual(
+ html.simplify(fragment([
+ pathToNode(path, [text("Hello")]),
+ pathToNode(path, [text(" there")])])),
+ fragment([
+ pathToNode(path, [text("Hello"), text(" there")])]));
+ });
+});