From: Aleksander Ɓukasz Date: Mon, 23 Jun 2014 14:02:43 +0000 (+0200) Subject: smartxml: support for setting data on text nodes X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/f046659fbe4fd9fb752854f8ccb547918eddc5df?ds=sidebyside;hp=--cc smartxml: support for setting data on text nodes This is possible thanks to caching text node instances. Cloning text node data is not supported yet. --- f046659fbe4fd9fb752854f8ccb547918eddc5df diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index c0b3c42..c0b9aa4 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -280,6 +280,7 @@ $.extend(ElementNode.prototype, { var TextNode = function(nativeNode, document) { DocumentNode.call(this, nativeNode, document); + this._data = Object.create({}); nativeNode.__smartxmlTextNodeInstance = this; }; TextNode.prototype = Object.create(DocumentNode.prototype); @@ -287,6 +288,25 @@ TextNode.prototype = Object.create(DocumentNode.prototype); $.extend(TextNode.prototype, { nodeType: Node.TEXT_NODE, + setData: function(arg1, arg2) { + if(arguments.length === 2) { + if(_.isUndefined(arg2)) { + delete this._data[arg1]; + } else { + this._data[arg1] = arg2; + } + } else { + this._data = _.extend({}, arg1); + } + }, + + getData: function(key) { + if(key) { + return this._data[key]; + } + return this._data; + }, + getText: function() { return this.nativeNode.data; }, diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index 1be4cc1..f506f09 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -105,9 +105,10 @@ describe('smartxml', function() { }); it('can be cloned with its contents and its contents data', function() { - var doc = getDocumentFromXML('
'), + var doc = getDocumentFromXML('
text
'), root = doc.root, - div = root.contents()[0]; + div = root.contents()[0], + text = div.contents()[0]; var ClonableObject = function(arg) { this.arg = arg; @@ -118,15 +119,19 @@ describe('smartxml', function() { div.setData('key', 'value'); div.setData('clonableObject', new ClonableObject('test')); + text.setData('key', 'value'); var rootClone = root.clone(), divClone = rootClone.contents()[0], + textClone = divClone.contents()[0], stringClone = divClone.getData('key'), objClone = divClone.getData('clonableObject'); expect(stringClone).to.equal('value'); expect(objClone.arg).to.equal('test', 'clonable object got copied'); expect(objClone !== div.getData('clonableObject')).to.be.equal(true, 'copy of the clonable object is a new object'); + + expect(textClone.getData('key')).to.be.equal(undefined, 'cloning text node data is not supported yet'); }); it('knows its path in the document tree', function() {