From: Aleksander Ɓukasz Date: Mon, 8 Jul 2013 11:18:11 +0000 (+0200) Subject: Appending DocumentTextElement X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/88a280279a6df31138dd0ffa397ef7fe953b051d Appending DocumentTextElement --- diff --git a/modules/documentCanvas/canvas/canvas.test3.js b/modules/documentCanvas/canvas/canvas.test3.js index 0fb40aa..77c6871 100644 --- a/modules/documentCanvas/canvas/canvas.test3.js +++ b/modules/documentCanvas/canvas/canvas.test3.js @@ -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('
'), + 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('
'), div = c.doc().children()[0], diff --git a/modules/documentCanvas/canvas/documentElement.js b/modules/documentCanvas/canvas/documentElement.js index 91e9e62..9fa290f 100644 --- a/modules/documentCanvas/canvas/documentElement.js +++ b/modules/documentCanvas/canvas/documentElement.js @@ -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; };