diff --git a/common/lib/capa/capa/responsetypes.py b/common/lib/capa/capa/responsetypes.py index 698ec41a0a..6b57895013 100644 --- a/common/lib/capa/capa/responsetypes.py +++ b/common/lib/capa/capa/responsetypes.py @@ -199,19 +199,7 @@ class LoncapaResponse(object): # Add a
for the message at the end of the response if response_msg: - response_msg_div = etree.SubElement(tree, 'div') - response_msg_div.set("class", "response_message") - - # If the response message can be represented as an XHTML tree, - # create the tree and append it to the message
- try: - response_tree = etree.XML(response_msg) - response_msg_div.append(response_tree) - - # Otherwise, assume that the message is text (not XHTML) - # and insert it as the text of the message
- except: - response_msg_div.text = response_msg + tree.append(self._render_response_msg_html(response_msg)) return tree @@ -337,6 +325,29 @@ class LoncapaResponse(object): def __unicode__(self): return u'LoncapaProblem Response %s' % self.xml.tag + def _render_response_msg_html(self, response_msg): + """ Render a
for a message that applies to the entire response. + + *response_msg* is a string, which may contain XHTML markup + + Returns an etree element representing the response message
""" + # First try wrapping the text in a
and parsing + # it as an XHTML tree + try: + response_msg_div = etree.XML('
%s
' % str(response_msg)) + + # If we can't do that, create the
and set the message + # as the text of the
+ except: + response_msg_div = etree.Element('div') + response_msg_div.text = str(response_msg) + + + # Set the css class of the message
+ response_msg_div.set("class", "response_message") + + return response_msg_div + #----------------------------------------------------------------------------- diff --git a/common/lib/capa/capa/tests/test_html_render.py b/common/lib/capa/capa/tests/test_html_render.py index 257e63b611..64f031ea59 100644 --- a/common/lib/capa/capa/tests/test_html_render.py +++ b/common/lib/capa/capa/tests/test_html_render.py @@ -135,7 +135,8 @@ class CapaHtmlRenderTest(unittest.TestCase): # CustomResponse script that sets an overall_message script=textwrap.dedent(""" def check_func(*args): - return {'overall_message': '

Test message

', + msg = '

Test message 1

Test message 2

' + return {'overall_message': msg, 'input_list': [ {'ok': True, 'msg': '' } ] } """) @@ -160,9 +161,12 @@ class CapaHtmlRenderTest(unittest.TestCase): self.assertEqual(msg_div_element.get('class'), "response_message") # Expect that the
contains our message (as part of the XML tree) - msg_p_element = msg_div_element.find('p') - self.assertEqual(msg_p_element.tag, "p") - self.assertEqual(msg_p_element.text, "Test message") + msg_p_elements = msg_div_element.findall('p') + self.assertEqual(msg_p_elements[0].tag, "p") + self.assertEqual(msg_p_elements[0].text, "Test message 1") + + self.assertEqual(msg_p_elements[1].tag, "p") + self.assertEqual(msg_p_elements[1].text, "Test message 2") def test_substitute_python_vars(self):