diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-10-14 19:14:11 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-10-14 19:14:11 +0100 |
| commit | 46ad965f936563829a7c0549a5eb1af89155ee48 (patch) | |
| tree | 3cb9ab57466bc375926c1d04fce9a6f4f4e8858b | |
| parent | 27895603885e677e0ae6677553c8636b4af51cb3 (diff) | |
Add extra tests for simplification of empty elements
| -rw-r--r-- | lib/html/ast.js | 7 | ||||
| -rw-r--r-- | lib/html/index.js | 1 | ||||
| -rw-r--r-- | tests/html/simplify.tests.js | 20 |
3 files changed, 23 insertions, 5 deletions
diff --git a/lib/html/ast.js b/lib/html/ast.js index 96aa6c6..6056f5c 100644 --- a/lib/html/ast.js +++ b/lib/html/ast.js @@ -10,6 +10,12 @@ function fragment(children) { }; } +function nonFreshElement(tagName, attributes, children) { + return elementWithTag( + htmlPaths.element(tagName, attributes, {fresh: false}), + children); +} + function element(tagName, attributes, children) { return elementWithTag( htmlPaths.element(tagName, attributes, {fresh: true}), @@ -62,6 +68,7 @@ function collapseFragments(elements) { exports.fragment = fragment; exports.element = element; +exports.nonFreshElement = nonFreshElement; exports.elementWithTag = elementWithTag; exports.selfClosingElement = selfClosingElement; exports.text = text; diff --git a/lib/html/index.js b/lib/html/index.js index f74a98f..6de2a6e 100644 --- a/lib/html/index.js +++ b/lib/html/index.js @@ -2,6 +2,7 @@ var ast = require("./ast"); exports.fragment = ast.fragment; exports.element = ast.element; +exports.nonFreshElement = ast.nonFreshElement; exports.elementWithTag = ast.elementWithTag; exports.selfClosingElement = ast.selfClosingElement; exports.text = ast.text; diff --git a/tests/html/simplify.tests.js b/tests/html/simplify.tests.js index e23c379..3c8a254 100644 --- a/tests/html/simplify.tests.js +++ b/tests/html/simplify.tests.js @@ -5,6 +5,7 @@ var html = require("../../lib/html"); var htmlPaths = require("../../lib/html-paths"); +var nonFreshElement = html.nonFreshElement; var fragment = html.fragment; var text = html.text; var pathToNode = html.pathToNode; @@ -16,15 +17,24 @@ describe("simplify", function() { fragment([])); }); - test("empty elements are removed", function() { - var path = htmlPaths.elements([ - htmlPaths.element("p", {}, {fresh: false})]); + test("elements with no children are removed", function() { assert.deepEqual( - html.simplify(fragment([ - pathToNode(path, [text("")])])), + html.simplify(fragment([nonFreshElement("p", {}, [])])), + fragment([])); + }); + + test("elements only containing empty nodes are removed", function() { + assert.deepEqual( + html.simplify(fragment([nonFreshElement("p", {}, [text("")])])), fragment([])); }); + test("children of element is simplified", function() { + assert.deepEqual( + html.simplify(fragment([nonFreshElement("p", {}, [text("Hello"), text("")])])), + fragment([nonFreshElement("p", {}, [text("Hello")])])); + }); + test("successive fresh elements are not collapsed", function() { var path = htmlPaths.elements([ htmlPaths.element("p", {}, {fresh: true})]); |
