From b97b74396876577d3edeb8486a9aebeb234f85ac Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 14 Oct 2015 19:59:21 +0100 Subject: Fix recursive collapsing elements --- lib/html/simplify.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'lib/html') 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); } -- cgit v1.2.3