this._$.detach();
if(existed) {
this.triggerChangeEvent('nodeDetached', {parent: parent});
+ if(!parent) {
+ // This was the root of the document
+ this.document._defineDocumentProperties(null);
+ }
}
return this;
},
if(this.isRoot()) {
return this.document.replaceRoot(node);
}
- toret = this.after(node);
- this.detach();
- return toret;
+ if(this.parent()) {
+ toret = this.after(node);
+ this.detach();
+ return toret;
+ }
+ throw new Error('Cannot replace node without a parent.');
},
after: INSERTION(function(node) {
wrapWith: function(node) {
var insertion = this.getNodeInsertion(node);
- if(this.parent()) {
- this.before(insertion.ofNode);
+
+ if(this.parent() || this.isRoot()) {
+ this.replaceWith(insertion.ofNode);
}
insertion.ofNode.append(this);
return insertion.ofNode;