From 87ab4c09584d14af666e7c6ca469296d7f5b7828 Mon Sep 17 00:00:00 2001 From: Valera Rozuvan Date: Tue, 15 Apr 2014 16:17:18 +0000 Subject: [PATCH] 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 --- .../xmodule/js/spec/capa/display_spec.coffee | 14 ++++++++++++++ .../lib/xmodule/xmodule/js/src/capa/display.coffee | 13 ++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/common/lib/xmodule/xmodule/js/spec/capa/display_spec.coffee b/common/lib/xmodule/xmodule/js/spec/capa/display_spec.coffee index 08490cc579..203a70d9d1 100644 --- a/common/lib/xmodule/xmodule/js/spec/capa/display_spec.coffee +++ b/common/lib/xmodule/xmodule/js/spec/capa/display_spec.coffee @@ -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 ['

Answer: One

', '

Answer: Two

'] + + it 'toggle the show answer button, answers are elements', -> + answer1 = '
one
' + answer2 = '
two
' + 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() diff --git a/common/lib/xmodule/xmodule/js/src/capa/display.coffee b/common/lib/xmodule/xmodule/js/src/capa/display.coffee index 74f1d40a03..e8326f55e9 100644 --- a/common/lib/xmodule/xmodule/js/src/capa/display.coffee +++ b/common/lib/xmodule/xmodule/js/src/capa/display.coffee @@ -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