empty text nodes after spans
[fnpeditor.git] / src / wlxml / wlxml.js
index ef70d3d..ce0ac0a 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;})
@@ -247,7 +255,8 @@ $.extend(WLXMLDocument.prototype, {
                     elParent = el.parent(),
                     hasSpanParent = elParent.prop('tagName') === 'SPAN',
                     hasSpanBefore = el.prev().length && $(el.prev()).prop('tagName') === 'SPAN',
-                    hasSpanAfter = el.next().length && $(el.next()).prop('tagName') === 'SPAN';
+                    hasSpanAfter = el.next().length && $(el.next()).prop('tagName') === 'SPAN',
+                    onlyChild = el.is(':only-child');
 
 
                 var addInfo = function(toAdd, where, transformed, original) {
@@ -288,7 +297,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