},
after: function(node) {
- node = node instanceof ElementNode ? node : this.document.createElementNode(node);
- this._$.after(node.nativeNode);
- return node;
+ var insertion = this.getNodeInsertion(node);
+ this._$.after(insertion.ofNode.nativeNode);
+ this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode});
+ return insertion.ofNode;
},
before: function(node) {
- node = node instanceof ElementNode ? node : this.document.createElementNode(node);
- this._$.before(node.nativeNode);
- return node;
+ var insertion = this.getNodeInsertion(node);
+ this._$.before(insertion.ofNode.nativeNode);
+ this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode});
+ return insertion.ofNode;
},
wrapWith: function(node) {
var event = new events.ChangeEvent(type, $.extend({node: this}, metaData || {}));
this.document.trigger('change', event);
},
+
+ getNodeInsertion: function(node) {
+ var insertion = {};
+ if(node instanceof DocumentNode) {
+ insertion.ofNode = node;
+ insertion.insertsNew = !this.document.containsNode(node);
+ } else {
+ insertion.ofNode = this.document.createElementNode(node);
+ insertion.insertsNew = true;
+ }
+ return insertion;
+ },
+
+ getIndex: function() {
+ return this.parent().indexOf(this);
+ }
});
var ElementNode = function(nativeNode, document) {
},
append: function(node) {
- node = node instanceof DocumentNode ? node : this.document.createElementNode(node);
- this._$.append(node.nativeNode);
+ var insertion = this.getNodeInsertion(node);
+ this._$.append(insertion.ofNode.nativeNode);
+ this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode});
+ return insertion.ofNode;
},
prepend: function(node) {
- node = node instanceof DocumentNode ? node : this.document.createElementNode(node);
- this._$.prepend(node.nativeNode);
+ var insertion = this.getNodeInsertion(node);
+ this._$.prepend(insertion.ofNode.nativeNode);
+ this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode});
+ return insertion.ofNode;
},
unwrapContent: function() {
textNodeIdx: this.parent().indexOf(this),
offsetStart: Math.min(desc.start, desc.end),
offsetEnd: Math.max(desc.start, desc.end),
- _with: {tag: desc.tagName, attrs: desc.attrs}
+ _with: {tagName: desc.tagName, attrs: desc.attrs}
});
} else {
return DocumentNode.prototype.wrapWith.call(this, desc);
return this.root.toXML();
},
+ containsNode: function(node) {
+ return node._$.parents().index(this.root._$) !== -1;
+ },
+
wrapNodes: function(params) {
if(!(params.element1.parent().sameNode(params.element2.parent()))) {
throw new Error('Wrapping non-sibling nodes not supported.');
throw new Error('Wrapping text in non-sibling text nodes not supported.');
}
- var wrapperElement = this.createElementNode({tagName: params._with.tag, attrs: params._with.attrs});
+ var wrapperElement = this.createElementNode({tagName: params._with.tagName, attrs: params._with.attrs});
textNode1.after(wrapperElement);
textNode1.detach();