DocumentTextElement.before
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 8 Jul 2013 21:26:40 +0000 (23:26 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 8 Jul 2013 21:26:40 +0000 (23:26 +0200)
modules/documentCanvas/canvas/canvas.test3.js
modules/documentCanvas/canvas/documentElement.js

index 95f9f60..dc29f5d 100644 (file)
@@ -208,6 +208,18 @@ describe('Canvas', function() {
                     expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
                     expect(children[1].sameNode(added)).to.be.true;
                 });
                     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('<section>Alice</section>'),
+                        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() {
             });
 
             describe('Splitting text', function() {
index 62c6731..d82f8e4 100644 (file)
@@ -146,6 +146,20 @@ $.extend(DocumentTextElement.prototype, {
         this.$element.unwrap();
         return documentElementFromHTMLElement(dom[0]);
     },
         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('<div>');
+        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({
     wrapWithNodeElement: function(wlxmlNode) {
         if(wlxmlNode.start && wlxmlNode.end) {
             return this.canvas.wrapText({