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);
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);
}
},
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() {
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];
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;
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;
},