From 2e818b7beafe567a19447c81f9a033e32cc2022f Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 23 Mar 2016 00:11:16 +0000 Subject: Improve collapsing of elements when there are empty elements Specifically, strip out empty nodes before attempting to collapse. --- NEWS | 4 ++++ lib/html/simplify.js | 36 +++++++++++++++++++++--------------- tests/html/simplify.tests.js | 14 ++++++++++++++ 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/NEWS b/NEWS index c10d83e..7375048 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,7 @@ +# 0.3.33 + +* Improve collapsing of elements when there are empty elements. + # 0.3.32 * Allow bold and italic style mappings to be configured. diff --git a/lib/html/simplify.js b/lib/html/simplify.js index 5b65034..24a3447 100644 --- a/lib/html/simplify.js +++ b/lib/html/simplify.js @@ -1,33 +1,37 @@ var ast = require("./ast"); function simplify(node) { - return removeEmpty(simplifiers[node.type](node)); + return collapse(removeEmpty(node)); } -var simplifiers = { - fragment: simplifyFragment, - element: simplifyElement, +function collapse(node) { + return collapsers[node.type](node); +} + +var collapsers = { + fragment: collapseFragment, + element: collapseElement, selfClosingElement: identity, text: identity, forceWrite: identity }; -function simplifyFragment(node) { - return ast.fragment(simplifyNodes(node.children)); +function collapseFragment(node) { + return ast.fragment(collapseNodes(node.children)); } -function simplifyElement(node) { - return ast.elementWithTag(node.tag, simplifyNodes(node.children)); +function collapseElement(node) { + return ast.elementWithTag(node.tag, collapseNodes(node.children)); } function identity(value) { return value; } -function simplifyNodes(nodes) { +function collapseNodes(nodes) { var children = []; - nodes.map(simplify).forEach(function(child) { + nodes.map(collapse).forEach(function(child) { appendChild(children, child, children.push.bind(children)); }); return children; @@ -58,15 +62,17 @@ var emptiers = { }; function fragmentEmptier(fragment) { - return ast.fragment(fragment.children.filter(function(child) { - return !child.isEmpty; - })); + return ast.fragment(listEmptier(fragment.children)); } function elementEmptier(element) { - return ast.elementWithTag(element.tag, element.children.filter(function(child) { + return ast.elementWithTag(element.tag, listEmptier(element.children)); +} + +function listEmptier(elements) { + return elements.map(removeEmpty).filter(function(child) { return !child.isEmpty; - })); + }); } diff --git a/tests/html/simplify.tests.js b/tests/html/simplify.tests.js index 2a16ba4..db7beb4 100644 --- a/tests/html/simplify.tests.js +++ b/tests/html/simplify.tests.js @@ -78,4 +78,18 @@ describe("simplify", function() { nonFreshElement("blockquote", {}, [nonFreshElement("p", {}, [text("there")])])])), fragment([nonFreshElement("blockquote", {}, [nonFreshElement("p", {}, [text("Hello"), text("there")])])])); }); + + test("empty elements are removed before collapsing", function() { + var freshPath = htmlPaths.elements([ + htmlPaths.element("p", {}, {fresh: true})]); + var nonFreshPath = htmlPaths.elements([ + htmlPaths.element("p", {}, {fresh: false})]); + assert.deepEqual( + html.simplify(fragment([ + pathToNode(nonFreshPath, [text("Hello")]), + pathToNode(freshPath, []), + pathToNode(nonFreshPath, [text(" there")])])), + fragment([ + pathToNode(nonFreshPath, [text("Hello"), text(" there")])])); + }); }); -- cgit v1.2.3