From: Aleksander Ɓukasz Date: Tue, 6 Aug 2013 10:06:52 +0000 (+0200) Subject: canvas api: prepending element to node element X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/c601e8aff30efc0b4fd69765d5671e3ee88c4bf4?ds=inline;hp=ecf3b34e969acfeeb5d09b7287b233ce4bffed83 canvas api: prepending element to node element --- diff --git a/modules/documentCanvas/canvas/canvas.test3.js b/modules/documentCanvas/canvas/canvas.test3.js index 3c53f09..ded28c2 100644 --- a/modules/documentCanvas/canvas/canvas.test3.js +++ b/modules/documentCanvas/canvas/canvas.test3.js @@ -315,6 +315,25 @@ describe('Canvas', function() { expect(children[0].getText()).to.equal('Alice'); }); + it('can put new NodeElement at the beginning', function() { + var c = canvas.fromXML('
'), + prepended = c.doc().prepend({tag: 'header', klass: 'some.class'}), + children = c.doc().children(); + + expect(children).to.have.length(2); + expect(children[0].sameNode(prepended)).to.be.true; + }); + + it('can put new TextElement at the beginning', function() { + var c = canvas.fromXML('
'), + prepended = c.doc().prepend({text: 'Alice'}), + children = c.doc().children(); + + expect(children).to.have.length(2) + expect(children[0].sameNode(prepended)).to.be.true; + expect(children[0].getText()).to.equal('Alice'); + }); + it('can put new NodeElement after another NodeElement', function() { var c = canvas.fromXML('
'), div = c.doc().children()[0], diff --git a/modules/documentCanvas/canvas/documentElement.js b/modules/documentCanvas/canvas/documentElement.js index 749a2ef..9768236 100644 --- a/modules/documentCanvas/canvas/documentElement.js +++ b/modules/documentCanvas/canvas/documentElement.js @@ -165,7 +165,7 @@ var manipulate = function(e, params, action) { } else { element = DocumentElement.create(params); } - var target = action === 'append' ? e._container() : e.dom(); + var target = (action === 'append' || action === 'prepend') ? e._container() : e.dom(); target[action](element.dom()); return element; }; @@ -257,6 +257,9 @@ $.extend(DocumentNodeElement.prototype, { this.data('orig-end', undefined); return manipulate(this, params, 'append'); }, + prepend: function(params) { + return manipulate(this, params, 'prepend'); + }, before: function(params) { return manipulate(this, params, 'before');