From 58d1e96373dee3849af719bdedb3d57c51adc5c3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Mon, 8 Jul 2013 23:26:40 +0200 Subject: [PATCH] DocumentTextElement.before --- modules/documentCanvas/canvas/canvas.test3.js | 12 ++++++++++++ modules/documentCanvas/canvas/documentElement.js | 14 ++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/modules/documentCanvas/canvas/canvas.test3.js b/modules/documentCanvas/canvas/canvas.test3.js index 95f9f60..dc29f5d 100644 --- a/modules/documentCanvas/canvas/canvas.test3.js +++ b/modules/documentCanvas/canvas/canvas.test3.js @@ -208,6 +208,18 @@ describe('Canvas', function() { expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement); expect(children[1].sameNode(added)).to.be.true; }); + it('can put new DocumentNodeElement before DocumentTextElement', function() { + var c = canvas.fromXML('
Alice
'), + text = c.doc().children()[0], + added = text.before({tag: 'p'}), + children = c.doc().children(); + + expect(children.length).to.equal(2); + expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement); + expect(children[0].sameNode(added)).to.be.true; + expect(children[1]).to.be.instanceOf(documentElement.DocumentTextElement); + expect(children[1].getText()).to.equal('Alice'); + }); }); describe('Splitting text', function() { diff --git a/modules/documentCanvas/canvas/documentElement.js b/modules/documentCanvas/canvas/documentElement.js index 62c6731..d82f8e4 100644 --- a/modules/documentCanvas/canvas/documentElement.js +++ b/modules/documentCanvas/canvas/documentElement.js @@ -146,6 +146,20 @@ $.extend(DocumentTextElement.prototype, { this.$element.unwrap(); return documentElementFromHTMLElement(dom[0]); }, + before: function(params) { + if(params instanceof DocumentTextElement || params.text) + return false; + var dom; + if(params instanceof DocumentNodeElement) { + dom = params.dom(); + } else { + dom = DocumentNodeElement.createDOM(params); + } + this.$element.wrap('
'); + this.$element.parent().before(dom[0]); + this.$element.unwrap(); + return documentElementFromHTMLElement(dom[0]); + }, wrapWithNodeElement: function(wlxmlNode) { if(wlxmlNode.start && wlxmlNode.end) { return this.canvas.wrapText({ -- 2.20.1