Fixes #3393 - HTML structure that is not parseable by jQuery causes JavaScript error that breaks Ticket Zoom view.
This commit is contained in:
parent
b706c86fde
commit
215801ef95
2 changed files with 37 additions and 2 deletions
|
@ -771,7 +771,7 @@ class App.Utils
|
|||
if @isMicrosoftOffice message
|
||||
return @signatureIdentifyByPlaintext message
|
||||
|
||||
message_element = $($.parseHTML(message))
|
||||
message_element = $(App.Utils.safeParseHtml(message))
|
||||
if message_element.length == 1 && $(message_element[0])?.children()?.length
|
||||
message_element[0].innerHTML = @signatureIdentifyByHtmlHelper(message_element[0].innerHTML)
|
||||
return message_element[0].outerHTML
|
||||
|
@ -792,7 +792,10 @@ class App.Utils
|
|||
return true if tag is 'DIV' && (el.data('signature') || el.prop('class') is 'yahoo_quoted')
|
||||
_.some el.children(), (el) -> isQuoteOrSignature el
|
||||
|
||||
$('<div/>').html(message).contents().each (index, node) ->
|
||||
try content = $('<div/>').html(message)
|
||||
catch e then content = $('<div/>').html('<div>' + message + '</div>')
|
||||
|
||||
content.contents().each (index, node) ->
|
||||
text = $(node).text()
|
||||
if node.nodeType == Node.TEXT_NODE
|
||||
# convert text back to HTML as it was before
|
||||
|
@ -1457,3 +1460,10 @@ class App.Utils
|
|||
htmlString = App.Utils.text2html(text)
|
||||
|
||||
htmlString
|
||||
|
||||
# Parses HTML text to DOM tree
|
||||
# jQuery's parseHTML sometimes fail when element does not have a single root element
|
||||
# in that case, fall back to fake root element and try again
|
||||
@safeParseHtml: (input) ->
|
||||
try $.parseHTML(input)
|
||||
catch e then $.parseHTML('<div>' + input + '</div>')[0].childNodes
|
||||
|
|
|
@ -1051,6 +1051,14 @@ test("identify signature by HTML", function() {
|
|||
var result = App.Utils.signatureIdentifyByHtml(message)
|
||||
equal(result, should)
|
||||
|
||||
|
||||
// test if, according to jQuery, invalid HTML does not cause a a crash
|
||||
// https://github.com/zammad/zammad/issues/3393
|
||||
message = "<td></td><table></table><div>test 123 </div>"
|
||||
should = message
|
||||
result = App.Utils.signatureIdentifyByHtml(message)
|
||||
equal(result, should)
|
||||
|
||||
// simple case 1
|
||||
message = '<div>actual content</div><blockquote>quoted content</blockquote>'
|
||||
should = '<div>actual content</div><span class="js-signatureMarker"></span><blockquote>quoted content</blockquote>'
|
||||
|
@ -3386,3 +3394,20 @@ test('App.Utils.signatureIdentifyByHtmlHelper()', function() {
|
|||
|
||||
equal(result, "<script>alert('fish2');</script><span class=\"js-signatureMarker\"></span><blockquote></blockquote>", 'signatureIdentifyByHtmlHelper does not reactivate alert')
|
||||
});
|
||||
|
||||
test("#safeParseHtml", function() {
|
||||
var unwrap = input => $('<div>').html(input)[0].innerHTML
|
||||
|
||||
var html = "<div>test 123 </div>"
|
||||
var result = App.Utils.safeParseHtml(html)
|
||||
var should = html
|
||||
equal(unwrap(result), html)
|
||||
|
||||
|
||||
// test if, according to jQuery, invalid HTML does not cause a a crash
|
||||
// https://github.com/zammad/zammad/issues/3393
|
||||
html = "<td></td><table></table><div>test 123 </div>"
|
||||
should = "<table></table><div>test 123 </div>"
|
||||
result = App.Utils.safeParseHtml(html)
|
||||
equal(unwrap(result), should)
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue