X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/84f2fdcdf9bd8e62fbecaa9a47e7418cc8a4f858..9f6d1535529f8d33732ecaac54bbd56afb536b0f:/src/smartxml/smartxml.js diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index f9f78e8..b449d82 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -17,12 +17,21 @@ var DocumentNode = function(nativeNode, document) { throw new Error('undefined document for a node'); } this.document = document; + this.object = {}; this._setNativeNode(nativeNode); }; $.extend(DocumentNode.prototype, { + getProperty: function(propName) { + var toret = this.object[propName]; + if(toret && _.isFunction(toret)) { + toret = toret.call(this); + } + return toret; + }, + transform: function(Transformation, args) { var transformation = new Transformation(this.document, this, args); return this.document.transform(transformation); @@ -140,12 +149,9 @@ $.extend(DocumentNode.prototype, { var node = (metaData && metaData.node) ? metaData.node : this, event = new events.ChangeEvent(type, $.extend({node: node}, metaData || {})); if(type === 'nodeDetached' || this.document.containsNode(event.meta.node)) { - if(type === 'nodeMoved') { - event.meta.parent = origParent; - } this.document.trigger('change', event); } - if((type === 'nodeAdded' || type === 'nodeMoved') && !this.document.containsNode(this) && nodeWasContained) { + if(type === 'nodeAdded' && !this.document.containsNode(this) && nodeWasContained) { event = new events.ChangeEvent('nodeDetached', {node: node, parent: origParent}); this.document.trigger('change', event); } @@ -156,10 +162,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() { @@ -289,7 +299,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];