diff --git a/lib/html-paths.js b/lib/html-paths.js index b881131..04bc655 100644 --- a/lib/html-paths.js +++ b/lib/html-paths.js @@ -39,7 +39,7 @@ function Element(tagName, attributes, options) { this.attributes = attributes || {}; this.fresh = options.fresh; } - + Element.prototype.matchesElement = function(element) { return this.tagNames[element.tagName] && _.isEqual(this.attributes || {}, element.attributes || {}); }; diff --git a/lib/html/simplify.js b/lib/html/simplify.js index 52a1b0a..291e5d4 100644 --- a/lib/html/simplify.js +++ b/lib/html/simplify.js @@ -29,7 +29,7 @@ function simplifyNodes(nodes) { nodes.map(simplify).forEach(function(child) { var lastChild = children[children.length - 1]; - if (child.type === "element" && !child.tag.fresh && lastChild && _.isEqual(lastChild.tag, child.tag)) { + if (child.type === "element" && !child.tag.fresh && lastChild && child.tag.matchesElement(lastChild.tag)) { child.children.forEach(function(grandChild) { // Mutation is fine since simplifying elements create a copy of the children. lastChild.children.push(grandChild); diff --git a/tests/html/simplify.tests.js b/tests/html/simplify.tests.js index 7893663..7e17903 100644 --- a/tests/html/simplify.tests.js +++ b/tests/html/simplify.tests.js @@ -33,4 +33,17 @@ describe("simplify", function() { fragment([ pathToNode(path, [text("Hello"), text(" there")])])); }); + + test("non-fresh can collapse into preceding non-fresh element", 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(freshPath, [text("Hello")]), + pathToNode(nonFreshPath, [text(" there")])])), + fragment([ + pathToNode(freshPath, [text("Hello"), text(" there")])])); + }); });