+ 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);
+ }