From a77e53b783db30a8e6e7935ddabe158a67e4221f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Wed, 14 May 2014 15:34:58 +0200 Subject: [PATCH 1/1] smartxml: Support for Node.object.describesParent property --- src/smartxml/core.js | 18 +++++++--- src/smartxml/smartxml.test.js | 63 +++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 4 deletions(-) diff --git a/src/smartxml/core.js b/src/smartxml/core.js index 007468f..fdce751 100644 --- a/src/smartxml/core.js +++ b/src/smartxml/core.js @@ -187,15 +187,21 @@ var elementNodeTransformations = { return; } + this.contents() + .filter(function(child) { + return child.getProperty('describesParent'); + }.bind(this)) + .forEach(function(child) { + child.detach(); + }); + var myContents = this.contents(), myIdx = parent.indexOf(this); - if(myContents.length === 0) { return this.detach(); } - var childrenLength = this.contents().length, first = true, shiftRange = false; @@ -381,7 +387,9 @@ var documentTransformations = { } for(var i = idx1; i <= idx2; i++) { - wrapper.append(parentContents[i].detach()); + if(!parentContents[i].getProperty('describesParent')) { + wrapper.append(parentContents[i].detach()); + } } insertingTarget[insertingMethod](wrapper); @@ -426,7 +434,9 @@ var documentTransformations = { wrapperElement.append({text: prefixInside}); } for(var i = idx1 + 1; i < idx2; i++) { - wrapperElement.append(contentsInside[i]); + if(!contentsInside[i].getProperty('describesParent')) { + wrapperElement.append(contentsInside[i]); + } } if(suffixInside.length > 0) { wrapperElement.append({text: suffixInside}); diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index 5430278..b7676e2 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -578,6 +578,24 @@ describe('smartxml', function() { expect(node.contents()[2].getText()).to.equal(' a cat!'); }); + it('removes parent-describing sibling nodes of unwrapped node', function() { + var doc = getDocumentFromXML('
'), + div = doc.root.contents()[0], + x = div.contents()[1]; + + doc.registerExtension({documentNode: {methods: { + object: { + describesParent: function() { + return this.getTagName() === 'x'; + } + } + }}}); + + div.unwrapContent(); + expect(doc.root.contents().length).to.equal(2); + expect(x.isInDocument()).to.be.false; + }); + it('unwrap single element node from its parent', function() { var doc = getDocumentFromXML('
'), div = doc.root, @@ -630,6 +648,28 @@ describe('smartxml', function() { expect(wrapperContents[1].contents().length).to.equal(1); expect(wrapperContents[1].contents()[0].getText()).to.equal('small'); }); + + it('keeps parent-describing nodes in place', function() { + var doc = getDocumentFromXML('Alice has a cat'), + root = doc.root, + x = root.contents()[1]; + + doc.registerExtension({documentNode: {methods: { + object: { + describesParent: function() { + return this.getTagName() === 'x'; + } + } + }}}); + + root.wrapText({ + _with: {tagName: 'span', attrs: {'attr1': 'value1'}}, + offsetStart: 1, + offsetEnd: 4, + textNodeIdx: [0,2] + }); + expect(x.parent().sameNode(root)).to.be.true; + }); }); describe('Wrapping Nodes', function() { @@ -678,6 +718,29 @@ describe('smartxml', function() { expect(headerChildren[0].sameNode(div2)).to.equal(true, 'first node wrapped'); expect(headerChildren[1].sameNode(div3)).to.equal(true, 'second node wrapped'); }); + + it('keeps parent-describing nodes in place', function() { + var section = elementNodeFromXML('
Alice
a cat
'), + aliceText = section.contents()[0], + x = section.contents()[1], + lastDiv = section.contents()[2]; + + section.document.registerExtension({documentNode: {methods: { + object: { + describesParent: function() { + return this.getTagName() === 'x'; + } + } + }}}); + + section.document.wrapNodes({ + node1: aliceText, + node2: lastDiv, + _with: {tagName: 'header'} + }); + + expect(x.parent().sameNode(section)).to.be.true; + }); }); }); -- 2.20.1