mirror of
https://github.com/mwilliamson/mammoth.js.git
synced 2024-12-08 15:14:29 +00:00
Reuse matchesElement from HTML paths
This commit is contained in:
parent
aac776b171
commit
e04d054a1b
@ -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 || {});
|
||||
};
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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")])]));
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user