From 091b7b516be2b5c2053e2a9103f8a910efbad68a Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Thu, 9 Mar 2017 15:16:00 +0100 Subject: [PATCH] empty text nodes after spans --- src/editor/plugins/core/core.js | 6 ++++++ src/wlxml/wlxml.js | 9 +++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/editor/plugins/core/core.js b/src/editor/plugins/core/core.js index 661c738..5f978d8 100644 --- a/src/editor/plugins/core/core.js +++ b/src/editor/plugins/core/core.js @@ -122,6 +122,8 @@ plugin.documentExtension.textNode.transformations = { parent.contents().some(function(n) { return move(n, newNode); }); + // span can't be the last node + newNode.append({'text': ''}); if(newNodes.second.contents()[0].getText().length === 0) { var textNode = newNodes.second.contents()[0]; newNodes.second.detach(); @@ -523,6 +525,10 @@ var createWrapTextAction = function(createParams) { offsetEnd: params.fragment.endOffset, textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()] }); + if(wrapper.next() === null) { + // span can't be the last node + parent.append({text: ''}); + } lastTextNode = wrapper.getLastTextNode(); if(lastTextNode) { diff --git a/src/wlxml/wlxml.js b/src/wlxml/wlxml.js index 1837e14..989fe5d 100644 --- a/src/wlxml/wlxml.js +++ b/src/wlxml/wlxml.js @@ -239,7 +239,9 @@ $.extend(WLXMLDocument.prototype, { }); nativeNode.normalize(); $(nativeNode).find('*').each(function() { - if (this.childNodes.length === 0) { + 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); } @@ -253,8 +255,7 @@ $.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', - onlyChild = el.is(':only-child'); + hasSpanAfter = el.next().length && $(el.next()).prop('tagName') === 'SPAN'; var addInfo = function(toAdd, where, transformed, original) { @@ -295,7 +296,7 @@ $.extend(WLXMLDocument.prototype, { } } - if(!text.transformed && !onlyChild) { + if(!text.transformed && !(el.is(':only-child') || (el.is(':last-child') && hasSpanBefore))) { addInfo(text.original, 'below'); el.remove(); return true; // continue -- 2.20.1