some other minor changes from milpeer
[fnpeditor.git] / src / wlxml / wlxml.js
index ef70d3d..a5a87d2 100644 (file)
@@ -238,6 +238,14 @@ $.extend(WLXMLDocument.prototype, {
             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;})
@@ -249,7 +257,6 @@ $.extend(WLXMLDocument.prototype, {
                     hasSpanBefore = el.prev().length && $(el.prev()).prop('tagName') === 'SPAN',
                     hasSpanAfter = el.next().length && $(el.next()).prop('tagName') === 'SPAN';
 
-
                 var addInfo = function(toAdd, where, transformed, original) {
                     var parentContents = elParent.contents(),
                         idx = parentContents.index(el[0]),
@@ -288,7 +295,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