X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/e3f4ff223c5acd7cf97b6a9ba1cdd83111dba63b..2c49337a7dba1ad424843c6dc460743dbba0c815:/src/wlxml/wlxml.js diff --git a/src/wlxml/wlxml.js b/src/wlxml/wlxml.js index cb43f07..ce0ac0a 100644 --- a/src/wlxml/wlxml.js +++ b/src/wlxml/wlxml.js @@ -44,6 +44,19 @@ var WLXMLDocumentNodeMethods = { } }.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()); } }; @@ -106,6 +119,16 @@ $.extend(WLXMLElementNode.prototype, WLXMLDocumentNodeMethods, smartxml.ElementN return child.is(query); }.bind(this)); }, + find: function(query) { + var doc = this.document; + return this._$.find('*') + .map(function() { + return doc.createDocumentNode(this); + }) + .filter(function() { + return this.is(query); + }); + }, _getXMLDOMToDump: function() { var DOM = this._$.clone(true, true), @@ -173,7 +196,6 @@ var WLXMLDocumentNode = function() { }; WLXMLDocumentNode.prototype = Object.create(smartxml.DocumentNode.prototype); - var WLXMLTextNode = function() { smartxml.TextNode.apply(this, arguments); }; @@ -216,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;}) @@ -225,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) { @@ -266,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