diff --git a/src/js/core.js b/src/js/core.js index e71455b95..c40605dbe 100644 --- a/src/js/core.js +++ b/src/js/core.js @@ -939,7 +939,15 @@ // do some DOM clean-up for known browser issues after the action if (action === 'insertunorderedlist' || action === 'insertorderedlist') { - MediumEditor.util.cleanListDOM(this.options.ownerDocument, this.getSelectedParentElement()); + var element = this.getSelectedParentElement(); + + if (element.nodeName.toLowerCase() === 'li') { + MediumEditor.util.cleanListDOM(this.options.ownerDocument, element); + } else { + this.saveSelection(); + MediumEditor.util.cleanAndWrapSelectedTextNodeToParagraph(this.options.ownerDocument); + setTimeout(this.restoreSelection.bind(this), 0); + } } this.checkSelection(); diff --git a/src/js/util.js b/src/js/util.js index f5c1f7bbc..ddaa147a3 100644 --- a/src/js/util.js +++ b/src/js/util.js @@ -667,34 +667,6 @@ }, cleanListDOM: function (ownerDocument, element) { - if (element.nodeName.toLowerCase() !== 'li') { - var selection = ownerDocument.getSelection(), - node = selection.anchorNode; - - if (node && node.nodeType === 3) { - var nextElement = node.nextElementSibling, - p = ownerDocument.createElement('p'), - context = this; - - if ( - nextElement && - nextElement.tagName && - nextElement.tagName.toLowerCase() === 'br' && - nextElement.parentNode - ) { - nextElement.parentNode.removeChild(nextElement); - } - - setTimeout(function () { - context.moveTextRangeIntoElement(node, node, p); - MediumEditor.selection.moveCursor(ownerDocument, node, node.textContent.length); - }, 0); - - } - - return; - } - var list = element.parentElement; if (list.parentElement.nodeName.toLowerCase() === 'p') { // yes we need to clean up @@ -706,6 +678,30 @@ } }, + cleanAndWrapSelectedTextNodeToParagraph: function (ownerDocument) { + var selection = ownerDocument.getSelection(), + node = selection.anchorNode; + + if (node && node.nodeType === 3) { + var nextElement = node.nextElementSibling, + p = ownerDocument.createElement('p'), + context = this; + + if ( + nextElement && + nextElement.tagName && + nextElement.tagName.toLowerCase() === 'br' && + nextElement.parentNode + ) { + nextElement.parentNode.removeChild(nextElement); + } + + setTimeout(function () { + context.moveTextRangeIntoElement(node, node, p); + }, 0); + } + }, + /* splitDOMTree * * Given a root element some descendant element, split the root element