Reuse matchesElement from HTML paths

This commit is contained in:
Michael Williamson 2015-10-13 22:14:41 +01:00
parent aac776b171
commit e04d054a1b
3 changed files with 15 additions and 2 deletions

View File

@ -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 || {});
};

View File

@ -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);

View File

@ -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")])]));
});
});