diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-10-14 19:59:21 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-10-14 19:59:21 +0100 |
| commit | b97b74396876577d3edeb8486a9aebeb234f85ac (patch) | |
| tree | 45f8ffd16e0284e8355872cce4f06969164874ab | |
| parent | 0b7a06d79c070f1dc25858a577e86b5b6e348d27 (diff) | |
Fix recursive collapsing elements
| -rw-r--r-- | NEWS | 4 | ||||
| -rw-r--r-- | lib/html/simplify.js | 22 | ||||
| -rw-r--r-- | tests/html/simplify.tests.js | 10 |
3 files changed, 26 insertions, 10 deletions
@@ -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")])])])); + }); }); |
