+ },
+
+ containsNode: function(node) {
+ return this.root && (node.nativeNode === this.root.nativeNode || node._$.parents().index(this.root._$) !== -1);
+ },
+
+ getSiblingParents: function(params) {
+ var parents1 = [params.node1].concat(params.node1.parents()).reverse(),
+ parents2 = [params.node2].concat(params.node2.parents()).reverse(),
+ noSiblingParents = null;
+
+ if(parents1.length === 0 || parents2.length === 0 || !(parents1[0].sameNode(parents2[0]))) {
+ return noSiblingParents;
+ }
+
+ var i;
+ for(i = 0; i < Math.min(parents1.length, parents2.length); i++) {
+ if(parents1[i].sameNode(parents2[i])) {
+ continue;
+ }
+ break;
+ }
+ return {node1: parents1[i], node2: parents2[i]};
+ },
+
+ trigger: function() {
+ Backbone.Events.trigger.apply(this, arguments);
+ },
+
+ getNodeInsertion: function(node) {
+ var insertion = {};
+ if(node instanceof DocumentNode) {
+ insertion.ofNode = node;
+ insertion.insertsNew = !this.containsNode(node);
+ } else {
+ insertion.ofNode = this.createDocumentNode(node);
+ insertion.insertsNew = true;
+ }
+ return insertion;
+ },
+
+ registerMethod: function(methodName, method, dstName) {
+ var doc = this;
+ var destination = {
+ document: doc,
+ documentNode: doc._nodeMethods,
+ textNode: doc._textNodeMethods,
+ elementNode: doc._elementNodeMethods
+ }[dstName];
+ registerMethod(methodName, method, destination);
+ },
+
+ registerTransformation: function(desc, name, dstName) {
+ var doc = this;
+ var destination = {
+ document: doc,
+ documentNode: doc._nodeTransformations,
+ textNode: doc._textNodeTransformations,
+ elementNode: doc._elementNodeTransformations
+ }[dstName];
+ registerTransformation(desc, name, destination);
+ },
+
+ registerExtension: function(extension) {
+ var doc = this;
+
+ ['document', 'documentNode', 'elementNode', 'textNode'].forEach(function(dstName) {
+ var dstExtension = extension[dstName];
+ if(dstExtension) {
+ if(dstExtension.methods) {
+ _.pairs(dstExtension.methods).forEach(function(pair) {
+ var methodName = pair[0],
+ method = pair[1];
+
+ doc.registerMethod(methodName, method, dstName);
+
+ });
+ }
+
+ if(dstExtension.transformations) {
+ _.pairs(dstExtension.transformations).forEach(function(pair) {
+ var name = pair[0],
+ desc = pair[1];
+ doc.registerTransformation(desc, name, dstName);
+ });
+ }
+ }
+ });
+ },
+
+ transform: function(Transformation, args) {
+ var toret, transformation;
+
+ if(typeof Transformation === 'function') {
+ transformation = new Transformation(this, this, args);
+ } else {
+ transformation = Transformation;
+ }
+ if(transformation) {
+ this._transformationLevel++;
+ toret = transformation.run({beUndoable:this._transformationLevel === 1});
+ if(this._transformationLevel === 1 && !this._undoInProgress) {
+ this.undoStack.push(transformation);
+ }
+ if(!this._undoInProgress && this._transformationLevel === 1) {
+ this.redoStack = [];
+ }
+ this._transformationLevel--;
+ return toret;
+ } else {
+ throw new Error('Transformation ' + transformation + ' doesn\'t exist!');
+ }
+ },
+ undo: function() {
+ var transformation = this.undoStack.pop();
+ if(transformation) {
+ this._undoInProgress = true;
+ transformation.undo();
+ this._undoInProgress = false;
+ this.redoStack.push(transformation);
+ }
+ },
+ redo: function() {
+ var transformation = this.redoStack.pop();
+ if(transformation) {
+ this._transformationLevel++;
+ transformation.run({beUndoable: true});
+ this._transformationLevel--;
+ this.undoStack.push(transformation);
+
+ }
+ },
+
+ getNodeByPath: function(path) {
+ var toret = this.root;
+ path.forEach(function(idx) {
+ toret = toret.contents()[idx];
+ });
+ return toret;
+ },
+
+ _defineDocumentProperties: function($document) {
+ var doc = this;
+ Object.defineProperty(doc, 'root', {get: function() {
+ return doc.createDocumentNode($document[0]);
+ }, configurable: true});
+ Object.defineProperty(doc, 'dom', {get: function() {
+ return $document[0];
+ }, configurable: true});