fix selecting nodes with no text
authorJan Szejko <janek37@gmail.com>
Fri, 4 Nov 2016 14:06:24 +0000 (15:06 +0100)
committerJan Szejko <janek37@gmail.com>
Fri, 4 Nov 2016 14:06:24 +0000 (15:06 +0100)
src/wlxml/wlxml.js

index ef70d3d..d05eb64 100644 (file)
@@ -238,6 +238,12 @@ $.extend(WLXMLDocument.prototype, {
             metadataNode.remove();
         });
         nativeNode.normalize();
             metadataNode.remove();
         });
         nativeNode.normalize();
+        $(nativeNode).find('*').each(function() {
+            if (this.childNodes.length === 0) {
+                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;})
@@ -247,7 +253,8 @@ $.extend(WLXMLDocument.prototype, {
                     elParent = el.parent(),
                     hasSpanParent = elParent.prop('tagName') === 'SPAN',
                     hasSpanBefore = el.prev().length && $(el.prev()).prop('tagName') === 'SPAN',
                     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) {
 
 
                 var addInfo = function(toAdd, where, transformed, original) {
@@ -288,7 +295,7 @@ $.extend(WLXMLDocument.prototype, {
                     }
                 }
 
                     }
                 }
 
-                if(!text.transformed) {
+                if(!text.transformed  && !onlyChild) {
                     addInfo(text.original, 'below');
                     el.remove();
                     return true; // continue
                     addInfo(text.original, 'below');
                     el.remove();
                     return true; // continue