Introduced App.Utils class.

This commit is contained in:
Martin Edenhofer 2014-12-29 13:59:16 +01:00
parent 52de425e0c
commit 4f98f39211
12 changed files with 265 additions and 43 deletions

View file

@ -229,7 +229,7 @@ class App.Controller extends Spine.Controller
title: ->
ticket_id = $(@).data('id')
ticket = App.Ticket.fullLocal( ticket_id )
HTMLEscape( ticket.title )
App.Utils.htmlEscape( ticket.title )
content: ->
ticket_id = $(@).data('id')
ticket = App.Ticket.fullLocal( ticket_id )
@ -264,7 +264,7 @@ class App.Controller extends Spine.Controller
title: ->
user_id = $(@).data('id')
user = App.User.fullLocal( user_id )
HTMLEscape( user.displayName() )
App.Utils.htmlEscape( user.displayName() )
content: ->
user_id = $(@).data('id')
user = App.User.fullLocal( user_id )
@ -311,7 +311,7 @@ class App.Controller extends Spine.Controller
title: ->
organization_id = $(@).data('id')
organization = App.Organization.fullLocal( organization_id )
HTMLEscape( organization.name )
App.Utils.htmlEscape( organization.name )
content: ->
organization_id = $(@).data('id')
organization = App.Organization.fullLocal( organization_id )

View file

@ -442,12 +442,12 @@ class App.GenericHistory extends App.ControllerModal
if item.value_from
if item.value_to
content += " #{ @T( 'from' ) }"
content += " '#{ HTMLEscape(item.value_from) }'"
content += " '#{ App.Utils.htmlEscape(item.value_from) }'"
if item.value_to
if item.value_from
content += " #{ @T( 'to' ) }"
content += " '#{ HTMLEscape(item.value_to) }'"
content += " '#{ App.Utils.htmlEscape(item.value_to) }'"
newItem.records.push content

View file

@ -145,7 +145,6 @@ class App.TicketZoom extends App.Controller
# get data
@ticket = App.Ticket.fullLocal( @ticket_id )
@ticket.article = undefined
console.log('KLKL',@ticket)
# render page
@render(force)
@ -849,7 +848,7 @@ class Edit extends App.Controller
@open_textarea(null, true)
for key, value of data.article
if key is 'body'
@$('[data-name="' + key + '"]').text(value)
@$('[data-name="' + key + '"]').html(value)
else
@$('[name="' + key + '"]').val(value)
)
@ -1384,17 +1383,20 @@ class ArticleView extends App.Controller
false
checkIfSignatureIsNeeded: (type) =>
console.log('checkIfSignatureIsNeeded', type, @ui.signature)
# add signature
if @ui.signature && @ui.signature.body && type.name is 'email'
body = @ui.el.find('[name="body"]').val() || ''
regexp = new RegExp( escapeRegExp( @ui.signature.body ) , 'i')
if !body.match(regexp)
body = body + "\n" + @ui.signature.body
@ui.el.find('[name="body"]').val( body )
body = @ui.el.find('[data-name="body"]').html() || ''
# update textarea size
@ui.el.find('[name="body"]').trigger('change')
# convert to html
signature = @ui.signature.body
#signature = signature.replace(//g, " ")
signature = '<p>' + signature.replace(/\n/g, '</p><p>') + '</p>'
regexp = new RegExp( escapeRegExp( signature ) , 'im')
#console.log('aaa', body, regexp)
if !body || !body.match(regexp)
body = body + signature
@ui.el.find('[data-name="body"]').html( body )
replyAll: (e) =>
@reply(e, true)
@ -1483,13 +1485,18 @@ class ArticleView extends App.Controller
# add quoted text if needed
selectedText = App.ClipBoard.getSelected()
console.log('selectedText', selectedText)
if selectedText
body = @ui.el.find('[data-name="body"]').text() || ''
body = @ui.el.find('[data-name="body"]').html() || ''
# quote text
selectedText = selectedText.replace /^(.*)$/mg, (match) =>
'> ' + match
body = selectedText + "\n" + body
articleNew.body = body
# convert to html
selectedText = '<p>' + selectedText.replace(/\n/g, "</p><p>") + '</p>'
articleNew.body = selectedText + body
App.Event.trigger('ui::ticket::setArticleType', { ticket: @ticket, type: type, article: articleNew } )
@ -1514,12 +1521,7 @@ class Article extends App.Controller
return
# build html body
# cleanup body
# @article['html'] = @article.body.trim()
@article['html'] = $.trim( @article.body )
@article['html'].replace( /\n\r/g, "\n" )
@article['html'].replace( /\r/g, "\n" )
@article['html'].replace( /\n\n/g, "\n" )
@article['html'] = App.Utils.textCleanup( @article.body )
# if body has more then x lines / else search for signature
preview = 10
@ -1532,7 +1534,7 @@ class Article extends App.Controller
else
article_lines.splice( preview - 1, 0, '-----SEEMORE-----' )
@article['html'] = article_lines.join("\n")
@article['html'] = window.linkify( @article['html'] )
@article['html'] = App.Utils.linkify( @article['html'] )
notify = '<a href="#" class="show_toogle">' + App.i18n.translateContent('See more') + '</a>'
# preview mode
@ -1553,7 +1555,7 @@ class Article extends App.Controller
if @article_changed
@article['html'] = @article['html'] + '</div>'
@article['html'] = @article['html'].replace( /\n/g, '<br>' )
@article['html'] = App.Utils.text2html( @article.body )
actionRow: ->
if @isRole('Customer')

View file

@ -92,7 +92,7 @@ class App extends Spine.Controller
# define linkify helper
params.L = ( item ) ->
if item && typeof item is 'string'
return window.linkify( item )
return App.Utils.linkify( item )
item
# define config helper
@ -118,9 +118,9 @@ class App extends Spine.Controller
if result
result = result + ', '
if item.name
result = result + HTMLEscape(item.name) + ' '
result = result + App.Utils.htmlEscape(item.name) + ' '
if item.address
result = result + " <span class=\"text-muted\">&lt;#{HTMLEscape(item.address)}&gt</span>"
result = result + " <span class=\"text-muted\">&lt;#{App.Utils.htmlEscape(item.address)}&gt</span>"
result

View file

@ -130,13 +130,13 @@ class _i18nSingleton extends Spine.Module
)
translateInline: ( string, args... ) =>
HTMLEscape( @translate( string, args... ) )
App.Utils.htmlEscape( @translate( string, args... ) )
translateContent: ( string, args... ) =>
translated = HTMLEscape( @translate( string, args... ) )
# replace = '<span class="translation" contenteditable="true" data-text="' + HTMLEscape(string) + '">' + translated + '<span class="icon-edit"></span>'
translated = App.Utils.htmlEscape( @translate( string, args... ) )
# replace = '<span class="translation" contenteditable="true" data-text="' + App.Utils.htmlEscape(string) + '">' + translated + '<span class="icon-edit"></span>'
if App.Config.get( 'Translation' )
replace = '<span class="translation" contenteditable="true" data-text="' + HTMLEscape(string) + '">' + translated + ''
replace = '<span class="translation" contenteditable="true" data-text="' + App.Utils.htmlEscape(string) + '">' + translated + ''
# if !@_translated
# replace += '<span class="missing">XX</span>'
replace += '</span>'

View file

@ -0,0 +1,32 @@
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
# htmlEscapedAndLinkified = App.Utils.text2html( rawText )
@text2html: ( ascii ) ->
ascii = @textCleanup(ascii)
#ascii = @htmlEscape(ascii)
ascii = @linkify(ascii)
ascii.replace( /\n/g, '<br>' )
# htmlEscaped = App.Utils.htmlEscape( rawText )
@htmlEscape: ( ascii ) ->
ascii.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;')
# htmlEscapedAndLinkified = App.Utils.linkify( rawText )
@linkify: (ascii) ->
window.linkify( ascii )

View file

@ -76,15 +76,6 @@ function clone(object) {
return JSON.parse(JSON.stringify(object));
}
function HTMLEscape(string) {
return ( '' + string )
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;')
}
jQuery.event.special.remove = {
remove: function(e) {
if (e.handler) e.handler();

View file

@ -37,6 +37,13 @@ class TestsController < ApplicationController
end
end
# GET /tests/html_utils
def html_utils
respond_to do |format|
format.html # index.html.erb
end
end
# GET /test/wait
def wait
sleep params[:sec].to_i

View file

@ -0,0 +1,15 @@
<link rel="stylesheet" href="/assets/tests/qunit-1.10.0.css">
<script src="/assets/tests/qunit-1.10.0.js"></script>
<script src="/assets/tests/html-utils.js"></script>
<style type="text/css">
body {
padding-top: 0px;
}
</style>
<script type="text/javascript">
</script>
<div id="qunit"></div>

View file

@ -6,6 +6,7 @@ Zammad::Application.routes.draw do
match '/tests-form', :to => 'tests#form', :via => :get
match '/tests-form-extended', :to => 'tests#form_extended', :via => :get
match '/tests-table', :to => 'tests#table', :via => :get
match '/tests-html-utils', :to => 'tests#html_utils', :via => :get
match '/tests/wait/:sec', :to => 'tests#wait', :via => :get
end

View file

@ -0,0 +1,152 @@
window.onload = function() {
// textCleanup
test( "textCleanup", function() {
var source = "Some\nValue\n\n\nTest"
var should = "Some\nValue\n\nTest"
var result = App.Utils.textCleanup( source )
equal( result, should, source )
source = "Some\nValue\n\n \n\n\nTest"
should = "Some\nValue\n\nTest"
result = App.Utils.textCleanup( source )
equal( result, should, source )
source = "Some\n\rValue\n\r\n\r\n\rTest"
should = "Some\nValue\n\nTest"
result = App.Utils.textCleanup( source )
equal( result, should, source )
source = "Some\n\rValue\n\r\n\r\n\rTest\r"
should = "Some\nValue\n\nTest"
result = App.Utils.textCleanup( source )
equal( result, should, source )
source = "Some\r\nValue\r\n\r\n\r\nTest\r\n"
should = "Some\nValue\n\nTest"
result = App.Utils.textCleanup( source )
equal( result, should, source )
source = "Some\r\nValue\r\n\r\n\r\n\r\n\r\n\r\nTest\r\n"
should = "Some\nValue\n\nTest"
result = App.Utils.textCleanup( source )
equal( result, should, source )
});
// htmlEscape
test( "htmlEscape", function() {
var source = "<"
var should = "&lt;"
var result = App.Utils.htmlEscape( source )
equal( result, should, source )
source = ">"
should = "&gt;"
result = App.Utils.htmlEscape( source )
equal( result, should, source )
source = "&"
should = "&amp;"
result = App.Utils.htmlEscape( source )
equal( result, should, source )
source = "&amp;"
should = "&amp;amp;"
result = App.Utils.htmlEscape( source )
equal( result, should, source )
source = "&amp ;"
should = "&amp;amp ;"
result = App.Utils.htmlEscape( source )
equal( result, should, source )
source = "& amp;"
should = "&amp; amp;"
result = App.Utils.htmlEscape( source )
equal( result, should, source )
source = "'test'"
should = "&#39;test&#39;"
result = App.Utils.htmlEscape( source )
equal( result, should, source )
source = '"test"'
should = "&quot;test&quot;"
result = App.Utils.htmlEscape( source )
equal( result, should, source )
source = "<>"
should = "&lt;&gt;"
result = App.Utils.htmlEscape( source )
equal( result, should, source )
source = "<&lt;>"
should = "&lt;&amp;lt;&gt;"
result = App.Utils.htmlEscape( source )
equal( result, should, source )
});
// text2html
test( "text2html", function() {
var source = "Some\nValue\n\n\nTest"
var should = "Some<br>Value<br><br>Test"
var result = App.Utils.text2html( source )
equal( result, should, source )
source = "Some\nValue\n"
should = "Some<br>Value"
result = App.Utils.text2html( source )
equal( result, should, source )
source = "Some\n<b>Value</b>\n"
should = "Some<br>&lt;b&gt;Value&lt;/b&gt;"
result = App.Utils.text2html( source )
equal( result, should, source )
});
// linkify
test( "linkify", function() {
var source = "http://example.com"
var should = '<a href="http://example.com" title="http://example.com" target="_blank">http://example.com</a>'
var result = App.Utils.linkify( source )
equal( result, should, source )
source = "http://example.com?some_param=lalala"
should = '<a href="http://example.com?some_param=lalala" title="http://example.com?some_param=lalala" target="_blank">http://example.com?some_param=lalala</a>'
result = App.Utils.linkify( source )
equal( result, should, source )
source = "example.com"
should = '<a href="http://example.com" title="http://example.com" target="_blank">example.com</a>'
result = App.Utils.linkify( source )
equal( result, should, source )
source = "some text example.com"
should = 'some text <a href="http://example.com" title="http://example.com" target="_blank">example.com</a>'
result = App.Utils.linkify( source )
equal( result, should, source )
source = "example.com some text"
should = '<a href="http://example.com" title="http://example.com" target="_blank">example.com</a> some text'
result = App.Utils.linkify( source )
equal( result, should, source )
/*
source = "<b>example.com</b>"
should = '<b><a href="http://example.com" title="http://example.com" target="_blank">http://example.com</a></b>'
result = App.Utils.linkify( source )
equal( result, should, source )
*/
});
}

View file

@ -134,4 +134,26 @@ class AAbUnitTest < TestCase
]
browser_single_test(tests)
end
def test_html_utils
tests = [
{
:name => 'start',
:instance => browser_instance,
:url => browser_url + '/tests-html-utils',
:action => [
{
:execute => 'wait',
:value => 8,
},
{
:execute => 'match',
:css => '.result .failed',
:value => '0',
:match_result => true,
},
],
},
]
browser_single_test(tests)
end
end