From 2ec50c1931fa2337309067dd1b4ee0a3aeaaaa8f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Thu, 3 Oct 2013 10:27:37 +0200 Subject: [PATCH 1/1] Wrapping text/element node with element node --- src/smartxml/smartxml.js | 24 +++++++++++++++--------- src/smartxml/smartxml.test.js | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index 04449cf..43e3041 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -29,7 +29,21 @@ $.extend(DocumentNode.prototype, { sameNode: function(otherNode) { return this.nativeNode === otherNode.nativeNode; - } + }, + + parent: function() { + return this.nativeNode.parentNode ? new ElementNode(this.nativeNode.parentNode) : null; + }, + + before: function(node) { + this._$.before(node.nativeNode); + }, + + wrapWith: function(node) { + if(this.parent()) + this.before(node); + node.append(this); + }, }); var ElementNode = function(nativeNode) { @@ -58,10 +72,6 @@ $.extend(ElementNode.prototype, DocumentNode.prototype, { return this._$.contents().index(node._$); }, - parent: function() { - return new ElementNode(this._$.parent()); - }, - getAttr: function(name) { return this._$.attr(name); }, @@ -74,10 +84,6 @@ $.extend(ElementNode.prototype, DocumentNode.prototype, { this._$.append(documentNode.nativeNode); }, - before: function(node) { - this._$.before(node.nativeNode); - }, - unwrapContent: function() { var parent = this.parent(); if(!parent) diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index f7375df..2abdc80 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -52,6 +52,24 @@ describe.only('smartxml', function() { expect(node1.contents()[0].sameNode(node2)).to.be.true; }); + it('wraps element node with another element node', function() { + var node = elementNodeFromXML('
'), + wrapper = elementNodeFromXML(''); + + node.wrapWith(wrapper); + expect(node.parent().sameNode(wrapper)).to.be.true; + }); + + it('wraps text node with element node', function() { + var node = elementNodeFromXML('
Alice
'), + textNode = node.contents()[0], + wrapper = elementNodeFromXML(''); + + textNode.wrapWith(wrapper); + expect(textNode.parent().sameNode(wrapper)).to.be.true; + expect(node.contents()).to.have.length(1); + }); + it('unwraps element node contents', function() { var node = elementNodeFromXML('
Alice
has propably a cat
!
'), outerDiv = node.contents()[1]; -- 2.20.1