X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/9f6d1535529f8d33732ecaac54bbd56afb536b0f..e104b4b87dc39066cbe397984c6f0e6bcc0459f8:/src/smartxml/smartxml.js diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index b449d82..c0b9aa4 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -12,6 +12,8 @@ define([ /* globals Node */ +var privateKey = '_smartxml'; + var DocumentNode = function(nativeNode, document) { if(!document) { throw new Error('undefined document for a node'); @@ -48,7 +50,7 @@ $.extend(DocumentNode.prototype, { clone.find('*').addBack().each(function() { var el = this, clonedData = $(this).data(); - + $(el).removeData(privateKey); _.pairs(clonedData).forEach(function(pair) { var key = pair[0], value = pair[1]; @@ -180,6 +182,7 @@ $.extend(DocumentNode.prototype, { var ElementNode = function(nativeNode, document) { DocumentNode.call(this, nativeNode, document); + $(nativeNode).data(privateKey, {node: this}); }; ElementNode.prototype = Object.create(DocumentNode.prototype); @@ -203,7 +206,9 @@ $.extend(ElementNode.prototype, { if(key) { return this._$.data(key); } - return this._$.data(); + var toret = _.clone(this._$.data()); + delete toret[privateKey]; + return toret; }, getTagName: function() { @@ -275,12 +280,33 @@ $.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); $.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; }, @@ -352,7 +378,15 @@ $.extend(Document.prototype, Backbone.Events, fragments, { TextNodeFactory: TextNode, createDocumentNode: function(from) { - if(!(from instanceof Node)) { + var cached; + + if(from instanceof Node) { + /* globals Text */ + cached = from instanceof Text ? from.__smartxmlTextNodeInstance : ($(from).data(privateKey) || {}).node; + if(cached instanceof DocumentNode) { + return cached; + } + } else { if(typeof from === 'string') { from = parseXML(from); this.normalizeXML(from); @@ -578,7 +612,6 @@ $.extend(Document.prototype, Backbone.Events, fragments, { if(transformations.length > 1) { // In case of real transactions we don't want to run undo on all of transformations if we don't have to. - stopAt = undefined; transformations.some(function(t, idx) { if(!t.undo && t.getChangeRoot().sameNode(doc.root)) { stopAt = idx; @@ -666,8 +699,11 @@ $.extend(Document.prototype, Backbone.Events, fragments, { getNodeByPath: function(path) { var toret = this.root; - path.forEach(function(idx) { + path.some(function(idx) { toret = toret.contents()[idx]; + if(!toret) { + return true; + } }); return toret; },