diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-03-23 00:11:16 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-03-23 00:11:16 +0000 |
| commit | 2e818b7beafe567a19447c81f9a033e32cc2022f (patch) | |
| tree | 18ff106e289c73cf5cb8f5c65214dc8c3c78fda7 | |
| parent | b8b685562a374bc14719fa1746477a6916fe0da8 (diff) | |
Improve collapsing of elements when there are empty elements
Specifically, strip out empty nodes before attempting to collapse.
| -rw-r--r-- | NEWS | 4 | ||||
| -rw-r--r-- | lib/html/simplify.js | 36 | ||||
| -rw-r--r-- | tests/html/simplify.tests.js | 14 |
3 files changed, 39 insertions, 15 deletions
@@ -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")])])); + }); }); |
