fix problem with delete
[fnpeditor.git] / src / wlxml / wlxml.js
index 40ab196..989fe5d 100644 (file)
@@ -44,6 +44,19 @@ var WLXMLDocumentNodeMethods =  {
             }
         }.bind(this));
         return toret;
             }
         }.bind(this));
         return toret;
+    },
+    getContextRoot: function() {
+        var contextRoot;
+        [this].concat(this.parents()).some(function(n) {
+            if(n.isContextRoot()) {
+                contextRoot = n;
+                return true;
+            }
+        });
+        return contextRoot;
+    },
+    hasSameContextRoot: function(other) {
+        return this.getContextRoot().sameNode(other.getContextRoot());
     }
 };
 
     }
 };
 
@@ -183,7 +196,6 @@ var WLXMLDocumentNode = function() {
 };
 WLXMLDocumentNode.prototype = Object.create(smartxml.DocumentNode.prototype);
 
 };
 WLXMLDocumentNode.prototype = Object.create(smartxml.DocumentNode.prototype);
 
-
 var WLXMLTextNode = function() {
     smartxml.TextNode.apply(this, arguments);
 };
 var WLXMLTextNode = function() {
     smartxml.TextNode.apply(this, arguments);
 };
@@ -226,6 +238,14 @@ $.extend(WLXMLDocument.prototype, {
             metadataNode.remove();
         });
         nativeNode.normalize();
             metadataNode.remove();
         });
         nativeNode.normalize();
+        $(nativeNode).find('*').each(function() {
+            var emptyNode = this.childNodes.length === 0;
+            var endsWithSpan = !emptyNode && this.childNodes[this.childNodes.length - 1].nodeName.toLowerCase() === 'span';
+            if(emptyNode || endsWithSpan) {
+                var fakeTextNode = window.document.createTextNode("");
+                this.appendChild(fakeTextNode);
+            }
+        });
 
         $(nativeNode).find(':not(iframe)').addBack().contents()
             .filter(function() {return this.nodeType === Node.TEXT_NODE;})
 
         $(nativeNode).find(':not(iframe)').addBack().contents()
             .filter(function() {return this.nodeType === Node.TEXT_NODE;})
@@ -276,7 +296,7 @@ $.extend(WLXMLDocument.prototype, {
                     }
                 }
 
                     }
                 }
 
-                if(!text.transformed) {
+                if(!text.transformed && !(el.is(':only-child') || (el.is(':last-child') && hasSpanBefore))) {
                     addInfo(text.original, 'below');
                     el.remove();
                     return true; // continue
                     addInfo(text.original, 'below');
                     el.remove();
                     return true; // continue