diff --git a/app/assets/javascripts/app/lib/app_post/utils.js.coffee b/app/assets/javascripts/app/lib/app_post/utils.js.coffee index 9a4609e10..58f8444a6 100644 --- a/app/assets/javascripts/app/lib/app_post/utils.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/utils.js.coffee @@ -103,7 +103,7 @@ class App.Utils ) # remove tags & content - html.find('li, ul, ol, a, b, u, i, strong, blockquote, h1, h2, h3, h4, h5, h6, br, hr, img').remove() + html.find('li, ul, ol, a, b, u, i, strong, blockquote, h1, h2, h3, h4, h5, h6, hr, img').remove() html diff --git a/app/assets/javascripts/app/lib/base/jquery.textmodule.js b/app/assets/javascripts/app/lib/base/jquery.textmodule.js index b73a6f89a..c57c7634d 100644 --- a/app/assets/javascripts/app/lib/base/jquery.textmodule.js +++ b/app/assets/javascripts/app/lib/base/jquery.textmodule.js @@ -179,7 +179,33 @@ // get cursor position var marker = '' - Medium.Injector.prototype.inject( marker ) + + // IE9 and non-IE + sel = window.getSelection(); + if (sel.getRangeAt && sel.rangeCount) { + range = sel.getRangeAt(0); + range.deleteContents(); + + // Range.createContextualFragment() would be useful here but is + // only relatively recently standardized and is not supported in + // some browsers (IE9, for one) + var el = document.createElement("div"); + el.innerHTML = marker; + var frag = document.createDocumentFragment(), node, lastNode; + while ( (node = el.firstChild) ) { + lastNode = frag.appendChild(node); + } + range.insertNode(frag); + + // Preserve the selection + if (lastNode) { + range = range.cloneRange(); + range.setStartAfter(lastNode); + range.collapse(true); + sel.removeAllRanges(); + sel.addRange(range); + } + } position = $('#js-cursor-position').position() $('#js-cursor-position').remove() if (!position) return diff --git a/public/assets/tests/html-utils.js b/public/assets/tests/html-utils.js index 76621fb35..dee6c54ec 100644 --- a/public/assets/tests/html-utils.js +++ b/public/assets/tests/html-utils.js @@ -223,6 +223,16 @@ test( "htmlRemoveRichtext", function() { result = App.Utils.htmlRemoveRichtext( $(source) ) equal( result.html(), should, source ) + source = "

" + should = "
" + result = App.Utils.htmlClanup( $(source) ) + equal( result.html(), should, source ) + + source = "

" + should = "

" + result = App.Utils.htmlRemoveRichtext( $(source) ) + equal( result.html(), should, source ) + }); // htmlClanup @@ -253,6 +263,11 @@ test( "htmlClanup", function() { result = App.Utils.htmlClanup( $(source) ) equal( result.html(), should, source ) + source = "

" + should = "

" + result = App.Utils.htmlRemoveRichtext( $(source) ) + equal( result.html(), should, source ) + }); // wrap