X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/fadd66e0cc7a22d06a1d8f4ab08871fec8dafcdf..06ff71dcb6c913a66280ebf812481224ee461f8c:/src/smartxml/smartxml.js?ds=sidebyside diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index 40f3aad..05583c5 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -91,13 +91,28 @@ $.extend(DocumentNode.prototype, { }), wrapWith: function(node) { - node = node instanceof ElementNode ? node : this.document.createElementNode(node); - + var insertion = this.getNodeInsertion(node); if(this.parent()) { - this.before(node); + this.before(insertion.ofNode); + } + insertion.ofNode.append(this); + return insertion.ofNode; + }, + + /** + * Removes parent of a node if node has no siblings. + */ + unwrap: function() { + if(this.isRoot()) { + return; + } + var parent = this.parent(), + grandParent; + if(parent.contents().length === 1) { + grandParent = parent.parent(); + parent.unwrapContent(); + return grandParent; } - node.append(this); - return node; }, triggerChangeEvent: function(type, metaData) { @@ -120,6 +135,9 @@ $.extend(DocumentNode.prototype, { }, getIndex: function() { + if(this.isRoot()) { + return 0; + } return this.parent().indexOf(this); } }); @@ -239,6 +257,7 @@ $.extend(ElementNode.prototype, { myContents = this.contents(), myIdx = parent.indexOf(this); + if(myContents.length === 0) { return this.detach(); } @@ -275,22 +294,6 @@ $.extend(ElementNode.prototype, { }; }, - /** - * Removes parent of a node if node has no siblings. - */ - unwrap: function() { - if(this.isRoot()) { - return; - } - var parent = this.parent(), - grandParent; - if(parent.contents().length === 1) { - grandParent = parent.parent(); - parent.unwrapContent(); - return grandParent; - } - }, - wrapText: function(params) { return this.document._wrapText(_.extend({inside: this}, params)); },