Appending DocumentTextElement
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 8 Jul 2013 11:18:11 +0000 (13:18 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 8 Jul 2013 11:44:19 +0000 (13:44 +0200)
modules/documentCanvas/canvas/canvas.test3.js
modules/documentCanvas/canvas/documentElement.js

index 0fb40aa..77c6871 100644 (file)
@@ -147,6 +147,16 @@ describe('Canvas', function() {
                     expect(children[0].sameNode(appended));
                 });
 
+                it('can put new TextElement at the end', function() {
+                    var c = canvas.fromXML('<section></section>'),
+                        appended = c.doc().append({text: 'Alice'}),
+                        children = c.doc().children();
+
+                    expect(children.length).to.equal(1);
+                    expect(children[0].sameNode(appended));
+                    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 91e9e62..9fa290f 100644 (file)
@@ -71,26 +71,31 @@ DocumentTextElement.prototype = new DocumentElement();
 
 $.extend(DocumentNodeElement.prototype, {
     append: function(params) {
-        var to_append = DocumentNodeElement.createDOM(params.tag, params.klass);
+        var to_append = DocumentNodeElement.createDOM(params);
         this.$element.append(to_append);
         return documentElementFromHTMLElement(to_append);
     },
     before: function(params) {
-        var to_append = DocumentNodeElement.createDOM(params.tag, params.klass);
+        var to_append = DocumentNodeElement.createDOM(params);
         this.$element.before(to_append);
         return documentElementFromHTMLElement(to_append);
     },
     after: function(params) {
-        var to_append = DocumentNodeElement.createDOM(params.tag, params.klass);
+        var to_append = DocumentNodeElement.createDOM(params);
         this.$element.after(to_append);
         return documentElementFromHTMLElement(to_append);   
     }
 });
 
-DocumentNodeElement.createDOM = function(tag, klass) {
-    var dom = $('<' + tag + '>');
-    if(klass)
-        dom.attr('class', klass);
+DocumentNodeElement.createDOM = function(params) {
+    var dom;
+    if(params.text) {
+        dom = $(document.createTextNode(params.text));
+    } else {
+        dom = $('<' + params.tag + '>');
+        if(params.klass)
+            dom.attr('class', params.klass);
+    }
     return dom;
 };