+ containsNode: function(node) {
+ return this.root && this.root.containsNode(node);
+ },
+
+ 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 stop = Math.min(parents1.length, parents2.length),
+ i;
+ for(i = 0; i < stop; i++) {
+ if(parents1[i].sameNode(parents2[i])) {
+ continue;
+ }
+ break;
+ }
+ if(i === stop) {
+ i--;
+ }
+ 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);
+ });
+ }
+ }
+ });
+ },
+
+ ifChanged: function(context, action, documentChangedHandler, documentUnchangedHandler) {
+ var hasChanged = false,
+ changeMonitor = function() {
+ hasChanged = true;
+ };
+
+ this.on('change', changeMonitor);
+ action.call(context);
+ this.off('change', changeMonitor);
+
+ if(hasChanged) {
+ if(documentChangedHandler) {
+ documentChangedHandler.call(context);
+ }
+ } else {
+ if(documentUnchangedHandler) {
+ documentUnchangedHandler.call(context);
+ }