From 2880ff3baf382416e865baf167ee4f69ac624fcb Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 30 Dec 2014 00:25:57 +0100 Subject: [PATCH] Added some new tests for html handling. --- .../app/controllers/ticket_zoom.js.coffee | 9 ++- .../app/lib/app_post/utils.js.coffee | 27 +++++--- .../app/lib/base/jquery.contenteditable.js | 9 ++- app/models/channel/email_build.rb | 4 +- app/models/channel/sendmail.rb | 3 +- app/models/channel/smtp.rb | 3 +- lib/core_ext/string.rb | 7 ++- public/assets/tests/html-utils.js | 42 ++++++++++++- test/unit/email_build_test.rb | 63 +++++++++++++++++++ 9 files changed, 141 insertions(+), 26 deletions(-) diff --git a/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee b/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee index 10e0b2fb2..e62f7ef52 100644 --- a/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee +++ b/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee @@ -1390,8 +1390,7 @@ class ArticleView extends App.Controller # convert to html signature = @ui.signature.body - #signature = signature.replace(//g, " ") - signature = '

' + signature.replace(/\n/g, '

') + '

' + signature = App.Utils.text2html( signature ) regexp = new RegExp( escapeRegExp( signature ) , 'im') #console.log('aaa', body, regexp) if !body || !body.match(regexp) @@ -1490,11 +1489,11 @@ class ArticleView extends App.Controller body = @ui.el.find('[data-name="body"]').html() || '' # quote text - selectedText = selectedText.replace /^(.*)$/mg, (match) => - '> ' + match + selectedText = App.Utils.textCleanup( selectedText ) + selectedText = App.Utils.quote( selectedText ) # convert to html - selectedText = '

' + selectedText.replace(/\n/g, "

") + '

' + selectedText = App.Utils.text2html( selectedText ) articleNew.body = selectedText + body 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 084b7cf76..8878a2d33 100644 --- a/app/assets/javascripts/app/lib/app_post/utils.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/utils.js.coffee @@ -1,24 +1,26 @@ class App.Utils # textCleand = App.Utils.textCleanup( rawText ) - @textCleanup: ( ascii ) -> $.trim( ascii ) .replace(/(\r\n|\n\r)/g, "\n") # cleanup .replace(/\r/g, "\n") # cleanup - .replace(/\s+$/gm, "\n") # remove tailing spaces - .replace(/\n{2,9}/gm, "\n\n") # remove multible empty lines + .replace(/[ ]\n/g, "\n") # remove tailing spaces + .replace(/\n{3,9}/g, "\n\n") # remove multible empty lines # htmlEscapedAndLinkified = App.Utils.text2html( rawText ) - @text2html: ( ascii ) -> + console.log('AA0', ascii) ascii = @textCleanup(ascii) #ascii = @htmlEscape(ascii) + console.log('AA1', ascii) ascii = @linkify(ascii) - ascii.replace( /\n/g, '
' ) + #ascii.replace( /\n/g, '
' ) + console.log('AA', ascii) + ascii = '
' + ascii.replace(/\n/g, '
') + '
' + ascii.replace(/
<\/div>/g, '

') # htmlEscaped = App.Utils.htmlEscape( rawText ) - @htmlEscape: ( ascii ) -> ascii.replace(/&/g, '&') .replace(/ - window.linkify( ascii ) \ No newline at end of file + window.linkify( ascii ) + + # quotedText = App.Utils.quote( rawText ) + @quote: (ascii) -> + ascii = @textCleanup(ascii) + $.trim( ascii ) + .replace /^(.*)$/mg, (match) => + if match + '> ' + match + else + '>' \ No newline at end of file diff --git a/app/assets/javascripts/app/lib/base/jquery.contenteditable.js b/app/assets/javascripts/app/lib/base/jquery.contenteditable.js index 4f2953ffa..f9994ba7b 100644 --- a/app/assets/javascripts/app/lib/base/jquery.contenteditable.js +++ b/app/assets/javascripts/app/lib/base/jquery.contenteditable.js @@ -45,7 +45,7 @@ // max length validation var validation = function(element) { - console.log('pp', element, $(element)) + // try to set error on framework form var parent = $(element).parent().parent() if ( parent.hasClass('controls') ) { @@ -76,6 +76,13 @@ mode: editorMode, maxLength: this.options.maxlength || -1, maxLengthReached: validation, + tags: { + 'break': 'br', + 'horizontalRule': 'hr', + 'paragraph': 'div', + 'outerLevel': ['pre', 'blockquote', 'figure'], + 'innerLevel': ['a', 'b', 'u', 'i', 'img', 'strong'] + }, }); } diff --git a/app/models/channel/email_build.rb b/app/models/channel/email_build.rb index 2cddfa960..bec840e3b 100644 --- a/app/models/channel/email_build.rb +++ b/app/models/channel/email_build.rb @@ -15,7 +15,7 @@ module Channel::EmailBuild =end - def build(attr, notification = false) + def self.build(attr, notification = false) mail = Mail.new # set organization @@ -48,7 +48,7 @@ module Channel::EmailBuild content_type 'text/html; charset=UTF-8' # complete check - attr[:body] = html_complete_check( attr[:body] ) + attr[:body] = Channel::EmailBuild.html_complete_check( attr[:body] ) body attr[:body] end diff --git a/app/models/channel/sendmail.rb b/app/models/channel/sendmail.rb index 12326324b..21b81426a 100644 --- a/app/models/channel/sendmail.rb +++ b/app/models/channel/sendmail.rb @@ -1,13 +1,12 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ class Channel::Sendmail - include Channel::EmailBuild def send(attr, channel, notification = false) # return if we run import mode return if Setting.get('import_mode') - mail = build(attr, notification) + mail = Channel::EmailBuild.build(attr, notification) mail.delivery_method :sendmail mail.deliver end diff --git a/app/models/channel/smtp.rb b/app/models/channel/smtp.rb index 75a53a8eb..42e8f6d3b 100644 --- a/app/models/channel/smtp.rb +++ b/app/models/channel/smtp.rb @@ -1,13 +1,12 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ class Channel::SMTP - include Channel::EmailBuild def send(attr, channel, notification = false) # return if we run import mode return if Setting.get('import_mode') - mail = self.build(attr, notification) + mail = Channel::EmailBuild.build(attr, notification) mail.delivery_method :smtp, { :openssl_verify_mode => 'none', :address => channel[:options][:host], diff --git a/lib/core_ext/string.rb b/lib/core_ext/string.rb index 0d7f77e23..41eb02e59 100644 --- a/lib/core_ext/string.rb +++ b/lib/core_ext/string.rb @@ -45,7 +45,7 @@ class String =end - # from https://gist.github.com/petrblaho/657856 + # base from https://gist.github.com/petrblaho/657856 def html2text text = self. gsub(/( |\n|\s)+/im, ' ').squeeze(' ').strip. @@ -65,7 +65,8 @@ class String gsub(/]*)>/i, "___\n"). gsub(/]*)>/i, "\n* "). gsub(/]*)>/i, '> '). - gsub(/<(br)(| [^>]*)>/i, "\n"). + gsub(/<(br)(|\/| [^>]*)>/i, "\n"). + gsub(/<\/div(| [^>]*)>/i, "\n"). gsub(/<(\/h[\d]+|p)(| [^>]*)>/i, "\n\n"). gsub(/<[^>]*>/, '') ).lstrip.gsub(/\n[ ]+/, "\n") + "\n" @@ -74,6 +75,6 @@ class String text = text + "\n [#{i+1}] <#{CGI.unescapeHTML(links[i])}>" unless links[i].nil? end links = nil - text + text.chomp end end \ No newline at end of file diff --git a/public/assets/tests/html-utils.js b/public/assets/tests/html-utils.js index f23c39b5b..cb8607f6b 100644 --- a/public/assets/tests/html-utils.js +++ b/public/assets/tests/html-utils.js @@ -33,6 +33,12 @@ test( "textCleanup", function() { result = App.Utils.textCleanup( source ) equal( result, should, source ) + source = "> Welcome!\n> \n> Thank you for installing Zammad.\n> \n> You will find ..." + should = "> Welcome!\n>\n> Thank you for installing Zammad.\n>\n> You will find ..." + result = App.Utils.textCleanup( source ) + equal( result, should, source ) + + }); // htmlEscape @@ -95,17 +101,22 @@ test( "htmlEscape", function() { test( "text2html", function() { var source = "Some\nValue\n\n\nTest" - var should = "Some
Value

Test" + var should = "
Some
Value

Test
" var result = App.Utils.text2html( source ) equal( result, should, source ) source = "Some\nValue\n" - should = "Some
Value" + should = "
Some
Value
" result = App.Utils.text2html( source ) equal( result, should, source ) source = "Some\nValue\n" - should = "Some
<b>Value</b>" + should = "
Some
<b>Value</b>
" + result = App.Utils.text2html( source ) + equal( result, should, source ) + + source = "> Welcome!\n> \n> Thank you for installing Zammad.\n> \n> You will find ..." + should = "
> Welcome!
>
> Thank you for installing Zammad.
>
> You will find ...
" result = App.Utils.text2html( source ) equal( result, should, source ) @@ -149,4 +160,29 @@ test( "linkify", function() { }); +// quote +test( "quote", function() { + + var source = "some text" + var should = '> some text' + var result = App.Utils.quote( source ) + equal( result, should, source ) + + source = "some text\nsome other text\n" + should = "> some text\n> some other text" + result = App.Utils.quote( source ) + equal( result, should, source ) + + source = "\n\nsome text\nsome other text\n \n" + should = "> some text\n> some other text" + result = App.Utils.quote( source ) + equal( result, should, source ) + + source = "Welcome!\n\nThank you for installing Zammad.\n\nYou will find ..." + should = "> Welcome!\n>\n> Thank you for installing Zammad.\n>\n> You will find ..." + result = App.Utils.quote( source ) + equal( result, should, source ) + +}); + } \ No newline at end of file diff --git a/test/unit/email_build_test.rb b/test/unit/email_build_test.rb index f21e9eff6..a3cd255de 100644 --- a/test/unit/email_build_test.rb +++ b/test/unit/email_build_test.rb @@ -24,4 +24,67 @@ class EmailBuildTest < ActiveSupport::TestCase assert( result =~ /test<\/b>/, 'test 2') end + + test 'html email check' do + html = ' + + + + + +
> Welcome!
>
> Thank you for installing Zammad.
>
+ +' + mail = Channel::EmailBuild.build( + :from => 'sender@example.com', + :to => 'recipient@example.com', + :body => html, + :content_type => 'text/html', + ) + + should = '> Welcome! +> +> Thank you for installing Zammad. +> +' + assert_equal( should, mail.text_part.body.to_s ) + assert_equal( html, mail.html_part.body.to_s ) + + end + + + test 'html2text' do + html = ' + + + + + +
> Welcome!
>
> Thank you for installing Zammad.
>
+ +' + should = '> Welcome! +> +> Thank you for installing Zammad. +> +' + assert_equal( should, html.html2text ) + + + html = ' line 1
+you
+-----&' + should = 'line 1 +you +-----&' + assert_equal( should, html.html2text ) + + + html = '
  • #1
  • #2
' + should = '* #1 +* #2' + assert_equal( should, html.html2text ) + + + end end \ No newline at end of file