From: Aleksander Ɓukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Date: Mon, 8 Jul 2013 21:26:40 +0000 (+0200)
Subject: DocumentTextElement.before
X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/58d1e96373dee3849af719bdedb3d57c51adc5c3?ds=inline;hp=0054f55492949c1b2b473a80d7fc170931b0a857

DocumentTextElement.before
---

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('<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() {
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('<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({