smartxml: node.unwrap
[fnpeditor.git] / src / smartxml / smartxml.js
index 0dd7082..c176f08 100644 (file)
@@ -36,6 +36,10 @@ $.extend(DocumentNode.prototype, {
         this._$ = $(nativeNode);
     },
 
+    isRoot: function() {
+        return this.document.root.sameNode(this);
+    },
+
     detach: function() {
         var parent = this.parent();
         this._$.detach();
@@ -245,6 +249,22 @@ $.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));
     },