+ },
+
+ containsNode: function(node) {
+ return this.root && (node.nativeNode === this.root.nativeNode || node._$.parents().index(this.root._$) !== -1);
+ },
+
+ wrapNodes: function(params) {
+ if(!(params.node1.parent().sameNode(params.node2.parent()))) {
+ throw new Error('Wrapping non-sibling nodes not supported.');
+ }
+
+ var parent = params.node1.parent(),
+ parentContents = parent.contents(),
+ wrapper = this.createDocumentNode({
+ tagName: params._with.tagName,
+ attrs: params._with.attrs}),
+ idx1 = parent.indexOf(params.node1),
+ idx2 = parent.indexOf(params.node2);
+
+ if(idx1 > idx2) {
+ var tmp = idx1;
+ idx1 = idx2;
+ idx2 = tmp;
+ }
+
+ var insertingMethod, insertingTarget;
+ if(idx1 === 0) {
+ insertingMethod = 'prepend';
+ insertingTarget = parent;
+ } else {
+ insertingMethod = 'after';
+ insertingTarget = parentContents[idx1-1];
+ }
+
+ for(var i = idx1; i <= idx2; i++) {
+ wrapper.append(parentContents[i].detach());
+ }
+
+ insertingTarget[insertingMethod](wrapper);
+ return wrapper;
+ },
+
+ 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]};
+ },
+
+ _wrapText: function(params) {
+ params = _.extend({textNodeIdx: 0}, params);
+ if(typeof params.textNodeIdx === 'number') {
+ params.textNodeIdx = [params.textNodeIdx];
+ }
+
+ var contentsInside = params.inside.contents(),
+ idx1 = Math.min.apply(Math, params.textNodeIdx),
+ idx2 = Math.max.apply(Math, params.textNodeIdx),
+ textNode1 = contentsInside[idx1],
+ textNode2 = contentsInside[idx2],
+ sameNode = textNode1.sameNode(textNode2),
+ prefixOutside = textNode1.getText().substr(0, params.offsetStart),
+ prefixInside = textNode1.getText().substr(params.offsetStart),
+ suffixInside = textNode2.getText().substr(0, params.offsetEnd),
+ suffixOutside = textNode2.getText().substr(params.offsetEnd)
+ ;
+
+ if(!(textNode1.parent().sameNode(textNode2.parent()))) {
+ throw new Error('Wrapping text in non-sibling text nodes not supported.');
+ }
+
+ var wrapperElement = this.createDocumentNode({tagName: params._with.tagName, attrs: params._with.attrs});
+ textNode1.after(wrapperElement);
+ textNode1.detach();
+
+ if(prefixOutside.length > 0) {
+ wrapperElement.before({text:prefixOutside});
+ }
+ if(sameNode) {
+ var core = textNode1.getText().substr(params.offsetStart, params.offsetEnd - params.offsetStart);
+ wrapperElement.append({text: core});
+ } else {
+ textNode2.detach();
+ if(prefixInside.length > 0) {
+ wrapperElement.append({text: prefixInside});
+ }
+ for(var i = idx1 + 1; i < idx2; i++) {
+ wrapperElement.append(contentsInside[i]);
+ }
+ if(suffixInside.length > 0) {
+ wrapperElement.append({text: suffixInside});
+ }
+ }
+ if(suffixOutside.length > 0) {
+ wrapperElement.after({text: suffixOutside});
+ }
+ return wrapperElement;
+ },
+
+ trigger: function() {
+ //console.log('trigger: ' + arguments[0] + (arguments[1] ? ', ' + arguments[1].type : ''));
+ 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;
+ },
+
+ replaceRoot: function(node) {
+ var insertion = this.getNodeInsertion(node);
+ this.root.detach();
+ defineDocumentProperties(this, insertion.ofNode._$);
+ insertion.ofNode.triggerChangeEvent('nodeAdded');
+ return insertion.ofNode;
+ },
+
+ transform: function(transformation, args) {
+ console.log('transform');
+ var Transformation, toret;
+ if(typeof transformation === 'string') {
+ Transformation = this.transformations.get(transformation);
+ if(Transformation) {
+ transformation = new Transformation(this, this, args);
+ }
+ }
+ if(transformation) {
+ this._transformationLevel++;
+ toret = transformation.run();
+ if(this._transformationLevel === 1) {
+ this.undoStack.push(transformation);
+ }
+ this._transformationLevel--;
+ console.log('clearing redo stack');
+ this.redoStack = [];
+ return toret;
+ } else {
+ throw new Error('Transformation ' + transformation + ' doesn\'t exist!');
+ }
+ },
+ undo: function() {
+ var transformation = this.undoStack.pop();
+ if(transformation) {
+ transformation.undo();
+ this.redoStack.push(transformation);
+ }
+ },
+ redo: function() {
+ var transformation = this.redoStack.pop();
+ if(transformation) {
+ transformation.run();
+ this.undoStack.push(transformation);
+ }
+ },
+
+ getNodeByPath: function(path) {
+ var toret = this.root;
+ path.forEach(function(idx) {
+ toret = toret.contents()[idx];
+ });
+ return toret;