smartxml: refactoring
[fnpeditor.git] / src / smartxml / smartxml.js
index 40f3aad..05583c5 100644 (file)
@@ -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));
     },