Fix for BLD-993

In certain cases jQuery errors when it is passed an invalid
selector. A fix for this is to silently catch these errors.

Jasmine tests were added to prevent this bug in the future.

BLD-993
This commit is contained in:
Valera Rozuvan
2014-04-15 16:17:18 +00:00
committed by Valera Rozuvan
parent 352ca49cb0
commit 87ab4c0958
2 changed files with 26 additions and 1 deletions

View File

@@ -255,6 +255,20 @@ describe 'Problem', ->
expect($('.show .show-label')).toHaveText 'Hide Answer'
expect(window.SR.readElts).toHaveBeenCalled()
it 'toggle the show answer button, answers are strings', ->
spyOn($, 'postWithPrefix').andCallFake (url, callback) -> callback(answers: '1_1': 'One', '1_2': 'Two')
@problem.show()
expect($('.show .show-label')).toHaveText 'Hide Answer'
expect(window.SR.readElts).toHaveBeenCalledWith ['<p>Answer: One</p>', '<p>Answer: Two</p>']
it 'toggle the show answer button, answers are elements', ->
answer1 = '<div><span class="detailed-solution">one</span></div>'
answer2 = '<div><span class="detailed-solution">two</span></div>'
spyOn($, 'postWithPrefix').andCallFake (url, callback) -> callback(answers: '1_1': answer1, '1_2': answer2)
@problem.show()
expect($('.show .show-label')).toHaveText 'Hide Answer'
expect(window.SR.readElts).toHaveBeenCalledWith [jasmine.any(jQuery), jasmine.any(jQuery)]
it 'add the showed class to element', ->
spyOn($, 'postWithPrefix').andCallFake (url, callback) -> callback(answers: {})
@problem.show()

View File

@@ -342,7 +342,18 @@ class @Problem
answer = @$("#answer_#{key}, #solution_#{key}")
answer.html(value)
Collapsible.setCollapsibles(answer)
solution = $(value).find('.detailed-solution')
# Sometimes, `value` is just a string containing a MathJax formula.
# If this is the case, jQuery will throw an error in some corner cases
# because of an incorrect selector. We setup a try..catch so that
# the script doesn't break in such cases.
#
# We will fallback to the second `if statement` below, if an
# error is thrown by jQuery.
try
solution = $(value).find('.detailed-solution')
catch e
solution = {}
if solution.length
answer_text.push(solution)
else