canvas api: prepending element to node element
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 6 Aug 2013 10:06:52 +0000 (12:06 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 6 Aug 2013 10:06:52 +0000 (12:06 +0200)
modules/documentCanvas/canvas/canvas.test3.js
modules/documentCanvas/canvas/documentElement.js

index 3c53f09..ded28c2 100644 (file)
@@ -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('<section><div></div></section>'),
+                        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('<section><div></div></section>'),
+                        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('<section><div></div></section>'),
                         div = c.doc().children()[0],
index 749a2ef..9768236 100644 (file)
@@ -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');