Commit 9ac8ef54 by Will Daly

Overall message HTMl now correctly renders when multiple HTML tags are

passed as the message (even when there's no root tag)
parent 952716af
......@@ -199,19 +199,7 @@ class LoncapaResponse(object):
# Add a <div> 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 <div>
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 <div>
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 <div> 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 <div> """
# First try wrapping the text in a <div> and parsing
# it as an XHTML tree
try:
response_msg_div = etree.XML('<div>%s</div>' % str(response_msg))
# If we can't do that, create the <div> and set the message
# as the text of the <div>
except:
response_msg_div = etree.Element('div')
response_msg_div.text = str(response_msg)
# Set the css class of the message <div>
response_msg_div.set("class", "response_message")
return response_msg_div
#-----------------------------------------------------------------------------
......
......@@ -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': '<p>Test message</p>',
msg = '<p>Test message 1<br /></p><p>Test message 2</p>'
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 <div> 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):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment