smartxml: fixing inheritance to support for instanceof operator to work properly
[fnpeditor.git] / src / smartxml / smartxml.js
index 2e507db..a396d3e 100644 (file)
@@ -36,8 +36,16 @@ $.extend(DocumentNode.prototype, {
         return this.nativeNode.parentNode ? this.document.createElementNode(this.nativeNode.parentNode) : null;
     },
 
+    after: function(node) {
+        node = node instanceof ElementNode ? node : this.document.createElementNode(node);
+        this._$.after(node.nativeNode);
+        return node;
+    },
+
     before: function(node) {
+        node = node instanceof ElementNode ? node : this.document.createElementNode(node);
         this._$.before(node.nativeNode);
+        return node;
     },
 
     wrapWith: function(node) {
@@ -59,8 +67,9 @@ $.extend(DocumentNode.prototype, {
 var ElementNode = function(nativeNode, document) {
     DocumentNode.call(this, nativeNode, document);
 };
+ElementNode.prototype = Object.create(DocumentNode.prototype);
 
-$.extend(ElementNode.prototype, DocumentNode.prototype, {
+$.extend(ElementNode.prototype, {
     nodeType: Node.ELEMENT_NODE,
 
     setData: function(key, value) {
@@ -204,8 +213,9 @@ $.extend(ElementNode.prototype, DocumentNode.prototype, {
 var TextNode = function(nativeNode, document) {
     DocumentNode.call(this, nativeNode, document);
 };
+TextNode.prototype = Object.create(DocumentNode.prototype);
 
-$.extend(TextNode.prototype, DocumentNode.prototype, {
+$.extend(TextNode.prototype, {
     nodeType: Node.TEXT_NODE,
 
     getText: function() {
@@ -248,7 +258,11 @@ $.extend(Document.prototype, Backbone.Events, {
 
     createElementNode: function(from) {
         if(!(from instanceof HTMLElement)) {
-            from = $('<' + from.tagName + '>')[0];
+            if(from.text) {
+                from = document.createTextNode(from.text);
+            } else {
+                from = $('<' + from.tagName + '>')[0];
+            }
         }
         return new this.ElementNodeFactory(from, this);
     },