From 676e34c6c2ce383f61cb4b20fec74aaba0dcbdbd Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Thu, 24 Oct 2013 13:32:11 +0200 Subject: [PATCH] smartxml: TextNode.before/after --- src/smartxml/smartxml.js | 14 +++++++++++++- src/smartxml/smartxml.test.js | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index 2e507db..47e2b2e 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -36,8 +36,16 @@ $.extend(DocumentNode.prototype, { return this.nativeNode.parentNode ? this.document.createElementNode(this.nativeNode.parentNode) : null; }, + after: function(node) { + node = node instanceof ElementNode ? node : this.document.createElementNode(node); + this._$.after(node.nativeNode); + return node; + }, + before: function(node) { + node = node instanceof ElementNode ? node : this.document.createElementNode(node); this._$.before(node.nativeNode); + return node; }, wrapWith: function(node) { @@ -248,7 +256,11 @@ $.extend(Document.prototype, Backbone.Events, { createElementNode: function(from) { if(!(from instanceof HTMLElement)) { - from = $('<' + from.tagName + '>')[0]; + if(from.text) { + from = document.createTextNode(from.text); + } else { + from = $('<' + from.tagName + '>')[0]; + } } return new this.ElementNodeFactory(from, this); }, diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index 58156c2..31d854a 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -179,6 +179,20 @@ describe('smartxml', function() { expect(event.type).to.equal('nodeTextChange'); }); + it('puts NodeElement after itself', function() { + var node = elementNodeFromXML('
Alice
'), + textNode = node.contents()[0], + returned = textNode.after({tagName:'div'}); + expect(returned.sameNode(node.contents()[1])).to.be.true; + }); + + it('puts NodeElement before itself', function() { + var node = elementNodeFromXML('
Alice
'), + textNode = node.contents()[0], + returned = textNode.before({tagName:'div'}); + expect(returned.sameNode(node.contents()[0])).to.be.true; + }); + describe('Wrapping TextNode contents', function() { it('wraps DocumentTextElement', function() { -- 2.20.1