'libs/backbone',
'smartxml/events',
'smartxml/transformations',
- 'smartxml/core'
-], function($, _, Backbone, events, transformations, coreTransformations) {
+ 'smartxml/core',
+ 'smartxml/fragments'
+], function($, _, Backbone, events, transformations, coreTransformations, fragments) {
'use strict';
/* globals Node */
return this.document.root.sameNode(this);
},
+ isInDocument: function() {
+ return this.document.containsNode(this);
+ },
+
+ isSiblingOf: function(node) {
+ return node && this.parent().sameNode(node.parent());
+ },
+
sameNode: function(otherNode) {
return !!(otherNode) && this.nativeNode === otherNode.nativeNode;
},
return myIdx < parentContents.length - 1 ? parentContents[myIdx+1] : null;
},
- isSurroundedByTextElements: function() {
- var prev = this.prev(),
- next = this.next();
- return prev && (prev.nodeType === Node.TEXT_NODE) && next && (next.nodeType === Node.TEXT_NODE);
+ isSurroundedByTextNodes: function() {
+ return this.isPrecededByTextNode() && this.isFollowedByTextNode();
+ },
+
+ isPrecededByTextNode: function() {
+ var prev = this.prev();
+ return prev && prev.nodeType === Node.TEXT_NODE;
+ },
+
+ isFollowedByTextNode: function() {
+ var next = this.next();
+ return next && next.nodeType === Node.TEXT_NODE;
},
triggerChangeEvent: function(type, metaData, origParent, nodeWasContained) {
return 0;
}
return this.parent().indexOf(this);
+ },
+
+ getNearestElementNode: function() {
+ return this.nodeType === Node.ELEMENT_NODE ? this : this.parent();
}
});
return node && (node.nativeNode === this.nativeNode || node._$.parents().index(this._$) !== -1);
},
+ getLastTextNode: function() {
+ var contents = this.contents(),
+ toret;
+
+ contents.reverse().some(function(node) {
+ if(node.nodeType === Node.TEXT_NODE) {
+ toret = node;
+ return true;
+ }
+ toret = node.getLastTextNode();
+ return !!toret;
+ });
+
+ return toret;
+ },
+
toXML: function() {
var wrapper = $('<div>');
wrapper.append(this._getXMLDOMToDump());
return this.nativeNode.data;
},
+
+ containsNode: function() {
+ return false;
+ },
+
triggerTextChangeEvent: function() {
var event = new events.ChangeEvent('nodeTextChange', {node: this});
this.document.trigger('change', event);
this.loadXML(xml);
};
-$.extend(Document.prototype, Backbone.Events, {
+$.extend(Document.prototype, Backbone.Events, fragments, {
ElementNodeFactory: ElementNode,
TextNodeFactory: TextNode,
if(!this._currentTransaction) {
return this.transaction(function() {
return this.transform(Transformation, args);
- }, this);
+ }, {context: this});
}
if(typeof Transformation === 'function') {
this._undoInProgress = false;
this.redoStack.push(transaction);
+ this.trigger('operationEnd');
}
},
redo: function() {
});
this._transformationLevel--;
this.undoStack.push(transaction);
+ this.trigger('operationEnd');
+
}
},
}
if(this._currentTransaction.hasTransformations()) {
this.undoStack.push(this._currentTransaction);
+ this.trigger('operationEnd');
}
this._currentTransaction = null;
},
}
return $document[0];
}, configurable: true});
+ },
+
+ createFragment: function(Type, params) {
+ if(!Type.prototype instanceof fragments.Fragment) {
+ throw new Error('Can\'t create a fragment: `Type` is not a valid Fragment');
+ }
+ return new Type(this, params);
}
});