From: Aleksander Ɓukasz Date: Thu, 24 Oct 2013 11:32:11 +0000 (+0200) Subject: smartxml: TextNode.before/after X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/676e34c6c2ce383f61cb4b20fec74aaba0dcbdbd?ds=sidebyside;hp=58425daa1aa7e717e92eaec170262de31ed47bc0 smartxml: TextNode.before/after --- 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() {