X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/8c59ee4d6be4babb8e6a908c07576bff7e2e59f2..5a57bc3cb22f2cb424186d8a7992d7dc7af68326:/src/smartxml/smartxml.js diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index 8039916..9cff5ec 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]; @@ -162,10 +164,14 @@ $.extend(DocumentNode.prototype, { }, getIndex: function() { + var parent; + if(this.isRoot()) { return 0; } - return this.parent().indexOf(this); + + parent = this.parent(); + return parent ? parent.indexOf(this) : undefined; }, getNearestElementNode: function() { @@ -176,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); @@ -199,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() { @@ -295,7 +304,7 @@ $.extend(TextNode.prototype, { var parseXML = function(xml) { var toret = $($.trim(xml)); - if(!toret.length) { + if(toret.length !== 1) { throw new Error('Unable to parse XML: ' + xml); } return toret[0]; @@ -348,7 +357,14 @@ $.extend(Document.prototype, Backbone.Events, fragments, { TextNodeFactory: TextNode, createDocumentNode: function(from) { - if(!(from instanceof Node)) { + var cached; + + if(from instanceof Node) { + cached = ($(from).data(privateKey) || {}).node; + if(cached instanceof DocumentNode) { + return cached; + } + } else { if(typeof from === 'string') { from = parseXML(from); this.normalizeXML(from); @@ -574,7 +590,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; @@ -662,8 +677,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; },