Commit ab7a6165 by Diana Huang

Add the ability to handle escaped HTML characters in trailing text

Add tests for unicode and escaped HTML characters
parent d6e722a6
...@@ -457,8 +457,9 @@ class TextLine(InputTypeBase): ...@@ -457,8 +457,9 @@ class TextLine(InputTypeBase):
""" """
A text line input. Can do math preview if "math"="1" is specified. A text line input. Can do math preview if "math"="1" is specified.
If "trailing_text" is set to a value, then the textline will be shown with the value after the text input. If "trailing_text" is set to a value, then the textline will be shown with the value after the text input,
This is useful if you would like to specify a specific type of units for the text input. and before the checkmark or any input-specific feedback. HTML will not work, but properly escaped HTML characters will.
This feature is useful if you would like to specify a specific type of units for the text input.
If the hidden attribute is specified, the textline is hidden and the input id is stored in a div with name equal If the hidden attribute is specified, the textline is hidden and the input id is stored in a div with name equal
to the value of the hidden attribute. This is used e.g. for embedding simulations turned into questions. to the value of the hidden attribute. This is used e.g. for embedding simulations turned into questions.
......
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
style="display:none;" style="display:none;"
% endif % endif
/> />
${trailing_text} ${trailing_text | h}
<p class="status"> <p class="status">
% if status == 'unsubmitted': % if status == 'unsubmitted':
......
# coding='utf-8'
""" """
Tests of input types. feature Tests of input types.
TODO: TODO:
- refactor: so much repetive code (have factory methods that build xml elements directly, etc) - refactor: so much repetive code (have factory methods that build xml elements directly, etc)
...@@ -20,6 +21,7 @@ import json ...@@ -20,6 +21,7 @@ import json
from lxml import etree from lxml import etree
import unittest import unittest
import xml.sax.saxutils as saxutils import xml.sax.saxutils as saxutils
import unicodedata as ud
from . import test_system from . import test_system
from capa import inputtypes from capa import inputtypes
...@@ -219,7 +221,10 @@ class TextLineTest(unittest.TestCase): ...@@ -219,7 +221,10 @@ class TextLineTest(unittest.TestCase):
def test_trailing_text_rendering(self): def test_trailing_text_rendering(self):
size = "42" size = "42"
trailing_text = 'm/s' trailing_text = 'm/s'
xml_str = """<textline id="prob_1_2" size="{size}" trailing_text="{tt}"/>""".format(size=size, tt=trailing_text) xml_str = """<textline id="prob_1_2"
size="{size}"
trailing_text="{tt}"
/>""".format(size=size, tt=trailing_text)
element = etree.fromstring(xml_str) element = etree.fromstring(xml_str)
...@@ -241,6 +246,63 @@ class TextLineTest(unittest.TestCase): ...@@ -241,6 +246,63 @@ class TextLineTest(unittest.TestCase):
self.assertEqual(context, expected) self.assertEqual(context, expected)
def test_trailing_unicode(self):
size = "42"
trailing_text = u'\xc3'
print trailing_text
xml_str = u"""<textline id="prob_1_2"
size="{size}"
trailing_text="{tt}"
/>""".format(size=size, tt=trailing_text)
element = etree.fromstring(xml_str)
state = {'value': 'BumbleBee', }
the_input = lookup_tag('textline')(test_system, element, state)
context = the_input._get_render_context()
expected = {'id': 'prob_1_2',
'value': 'BumbleBee',
'status': 'unanswered',
'size': size,
'msg': '',
'hidden': False,
'inline': False,
'do_math': False,
'trailing_text': trailing_text,
'preprocessor': None}
self.assertEqual(context, expected)
def test_trailing_text_special_characters(self):
size = "42"
trailing_text = 'a &lt; b'
xml_str = """<textline id="prob_1_2"
size="{size}"
trailing_text="{tt}"
/>""".format(size=size, tt=trailing_text)
element = etree.fromstring(xml_str)
state = {'value': 'BumbleBee', }
the_input = lookup_tag('textline')(test_system, element, state)
context = the_input._get_render_context()
expected = {'id': 'prob_1_2',
'value': 'BumbleBee',
'status': 'unanswered',
'size': size,
'msg': '',
'hidden': False,
'inline': False,
'do_math': False,
'trailing_text': 'a < b',
'preprocessor': None}
self.assertEqual(context, expected)
class FileSubmissionTest(unittest.TestCase): class FileSubmissionTest(unittest.TestCase):
''' '''
Check that file submission inputs work Check that file submission inputs work
......
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