aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-10-14 19:59:21 +0100
committerMichael Williamson <mike@zwobble.org>2015-10-14 19:59:21 +0100
commitb97b74396876577d3edeb8486a9aebeb234f85ac (patch)
tree45f8ffd16e0284e8355872cce4f06969164874ab
parent0b7a06d79c070f1dc25858a577e86b5b6e348d27 (diff)
Fix recursive collapsing elements
-rw-r--r--NEWS4
-rw-r--r--lib/html/simplify.js22
-rw-r--r--tests/html/simplify.tests.js10
3 files changed, 26 insertions, 10 deletions
diff --git a/NEWS b/NEWS
index 9027366..b6b5d0f 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,7 @@
+# 0.3.27
+
+* Fix recursive collapsing of elements.
+
# 0.3.26
* Keep HTML elements open correctly when the HTML path contains no fresh
diff --git a/lib/html/simplify.js b/lib/html/simplify.js
index 8ab4a1f..5b65034 100644
--- a/lib/html/simplify.js
+++ b/lib/html/simplify.js
@@ -28,19 +28,23 @@ 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 && 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.
- lastChild.appendChild(grandChild);
- });
- } else {
- children.push(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);
}
diff --git a/tests/html/simplify.tests.js b/tests/html/simplify.tests.js
index 27b9de0..2a16ba4 100644
--- a/tests/html/simplify.tests.js
+++ b/tests/html/simplify.tests.js
@@ -29,7 +29,7 @@ describe("simplify", function() {
fragment([]));
});
- test("children of element is simplified", function() {
+ test("empty children of element are removed", function() {
assert.deepEqual(
html.simplify(fragment([nonFreshElement("p", {}, [text("Hello"), text("")])])),
fragment([nonFreshElement("p", {}, [text("Hello")])]));
@@ -70,4 +70,12 @@ describe("simplify", function() {
fragment([
pathToNode(freshPath, [text("Hello"), text(" there")])]));
});
+
+ test("children of collapsed element can collapse with children of another collapsed element", function() {
+ assert.deepEqual(
+ html.simplify(fragment([
+ nonFreshElement("blockquote", {}, [nonFreshElement("p", {}, [text("Hello")])]),
+ nonFreshElement("blockquote", {}, [nonFreshElement("p", {}, [text("there")])])])),
+ fragment([nonFreshElement("blockquote", {}, [nonFreshElement("p", {}, [text("Hello"), text("there")])])]));
+ });
});