From 73f6cc6389f6c11cce73892d4ae35dcbbd0574d8 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 17 May 2016 20:40:00 +0200 Subject: [PATCH] Improved use of App.Utils.htmlRemoveRichtext. Fixed twitter browser tests. --- .../ticket_zoom/article_new.coffee | 24 +++++++++++++------ .../javascripts/app/lib/app_init/track.coffee | 4 ++-- .../javascripts/app/lib/app_post/utils.coffee | 4 +++- .../app/lib/base/jquery.contenteditable.js | 3 ++- public/assets/tests/html_utils.js | 8 +++---- 5 files changed, 28 insertions(+), 15 deletions(-) diff --git a/app/assets/javascripts/app/controllers/ticket_zoom/article_new.coffee b/app/assets/javascripts/app/controllers/ticket_zoom/article_new.coffee index 2bc5cb8e8..58d98d44e 100644 --- a/app/assets/javascripts/app/controllers/ticket_zoom/article_new.coffee +++ b/app/assets/javascripts/app/controllers/ticket_zoom/article_new.coffee @@ -283,15 +283,24 @@ class App.TicketZoomArticleNew extends App.Controller params.type_id = type.id if params.type is 'twitter status' - App.Utils.htmlRemoveRichtext(@$('[data-name=body]')) + rawHTML = @$('[data-name=body]').html() + cleanHTML = App.Utils.htmlRemoveRichtext(rawHTML) + if cleanHTML && cleanHTML.html() != rawHTML + @$('[data-name=body]').html(cleanHTML) params.content_type = 'text/plain' params.body = "#{App.Utils.html2text(params.body, true)}\n#{@signature.text()}" if params.type is 'twitter direct-message' - App.Utils.htmlRemoveRichtext(@$('[data-name=body]')) + rawHTML = @$('[data-name=body]').html() + cleanHTML = App.Utils.htmlRemoveRichtext(rawHTML) + if cleanHTML && cleanHTML.html() != rawHTML + @$('[data-name=body]').html(cleanHTML) params.content_type = 'text/plain' params.body = "#{App.Utils.html2text(params.body, true)}\n#{@signature.text()}" if params.type is 'facebook feed comment' - App.Utils.htmlRemoveRichtext(@$('[data-name=body]')) + rawHTML = @$('[data-name=body]').html() + cleanHTML = App.Utils.htmlRemoveRichtext(rawHTML) + if cleanHTML && cleanHTML.html() != rawHTML + @$('[data-name=body]').html(cleanHTML) params.content_type = 'text/plain' params.body = App.Utils.html2text(params.body, true) @@ -438,10 +447,11 @@ class App.TicketZoomArticleNew extends App.Controller @$('[data-name=body] [data-signature=true]').remove() # remove richtext - if @type is 'twitter status' - App.Utils.htmlRemoveRichtext(@$('[data-name=body]')) - if @type is 'twitter direct-message' - App.Utils.htmlRemoveRichtext(@$('[data-name=body]')) + if @type is 'twitter status' || @type is 'twitter direct-message' + rawHTML = @$('[data-name=body]').html() + cleanHTML = App.Utils.htmlRemoveRichtext(rawHTML) + if cleanHTML && cleanHTML.html() != rawHTML + @$('[data-name=body]').html(cleanHTML) # show/hide attributes/features @maxTextLength = undefined diff --git a/app/assets/javascripts/app/lib/app_init/track.coffee b/app/assets/javascripts/app/lib/app_init/track.coffee index 94cc57b41..43798a7b3 100644 --- a/app/assets/javascripts/app/lib/app_init/track.coffee +++ b/app/assets/javascripts/app/lib/app_init/track.coffee @@ -183,11 +183,11 @@ class _trackSingleton } window.onerrorOld = window.onerror window.onerror = function(errorMsg, url, lineNumber) { - console.error(errorMsg + " - in " + url + ", line " + lineNumber + "\n" + window.getStackTrace().join('\n')); + console.error(errorMsg + " - in " + url + ", line " + lineNumber + "\n" + window.getStackTrace().join("\n")) if (window.onerrorOld) { window.onerrorOld(errorMsg, url, lineNumber) } - return true + return false } var console = window.console diff --git a/app/assets/javascripts/app/lib/app_post/utils.coffee b/app/assets/javascripts/app/lib/app_post/utils.coffee index cbd435b54..a79fadd64 100644 --- a/app/assets/javascripts/app/lib/app_post/utils.coffee +++ b/app/assets/javascripts/app/lib/app_post/utils.coffee @@ -19,6 +19,7 @@ class App.Utils # rawText = App.Utils.html2text(html, no_trim) @html2text: (html, no_trim) -> + return html if !html if no_trim html = html @@ -130,6 +131,8 @@ class App.Utils # htmlOnlyWithRichtext = App.Utils.htmlRemoveRichtext(html) @htmlRemoveRichtext: (html) -> + return html if !html + html = @_checkTypeOf(html) # remove comments @@ -195,7 +198,6 @@ class App.Utils @_checkTypeOf: (item) -> return item if typeof item isnt 'string' - return $(item) if item.substr(0,9) isnt '#{item}") @_removeAttributes: (html) -> diff --git a/app/assets/javascripts/app/lib/base/jquery.contenteditable.js b/app/assets/javascripts/app/lib/base/jquery.contenteditable.js index 51a10207e..1d9b70204 100644 --- a/app/assets/javascripts/app/lib/base/jquery.contenteditable.js +++ b/app/assets/javascripts/app/lib/base/jquery.contenteditable.js @@ -148,7 +148,8 @@ document.execCommand('RemoveFormat') } if (e.keyCode == 89) { - App.Utils.htmlRemoveRichtext(_this.$element) + var cleanHtml = App.Utils.htmlRemoveRichtext(_this.$element.html()) + _this.$element.html(cleanHtml) } if (e.keyCode == 90) { document.execCommand('insertHorizontalRule') diff --git a/public/assets/tests/html_utils.js b/public/assets/tests/html_utils.js index 16473cd89..279ec546f 100644 --- a/public/assets/tests/html_utils.js +++ b/public/assets/tests/html_utils.js @@ -305,10 +305,10 @@ test("htmlRemoveTags", function() { equal(result.html(), should, source) source = "
This is some text!
" - //should = "
This is some text!
" should = "This is some text!" result = App.Utils.htmlRemoveRichtext($(source)) equal(result.html(), should, source) + should = "
This is some text!
" result = App.Utils.htmlRemoveRichtext(source) equal(result.html(), should, source) @@ -398,16 +398,16 @@ test("htmlRemoveRichtext", function() { source = "
\n" //should = "
test 123
" - should = '
Gruppe *
Besitzer
Status *
' + should = '
Gruppe *
Besitzer
Status *
' + "\n" result = App.Utils.htmlRemoveRichtext(source) equal(result.html(), should, source) source = "
This is some text!
" - //should = "
This is some text!
" should = "This is some text!" result = App.Utils.htmlRemoveRichtext($(source)) equal(result.html(), should, source) + should = "
This is some text!
" result = App.Utils.htmlRemoveRichtext(source) equal(result.html(), should, source) @@ -498,7 +498,7 @@ test("htmlCleanup", function() { source = "
\n" //should = "
test 123
" - should = '
Gruppe *
Besitzer
Status *
' + should = '
Gruppe *
Besitzer
Status *
' + "\n" result = App.Utils.htmlCleanup(source) equal(result.html(), should, source)