diff --git a/common/lib/capa/capa/inputtypes.py b/common/lib/capa/capa/inputtypes.py index 18bc92f0a3..aa507a5281 100644 --- a/common/lib/capa/capa/inputtypes.py +++ b/common/lib/capa/capa/inputtypes.py @@ -457,8 +457,21 @@ class TextLine(InputTypeBase): """ A text line input. Can do math preview if "math"="1" is specified. - 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. + If "trailing_text" is set to a value, then the textline will be shown with + the value after 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 to the value of the hidden attribute. This + is used e.g. for embedding simulations turned into questions. + + Example: + + + This example will render out a text line with a math preview and the text 'm/s' + after the end of the text line. """ template = "textline.html" @@ -483,6 +496,7 @@ class TextLine(InputTypeBase): Attribute('dojs', None, render=False), Attribute('preprocessorClassName', None, render=False), Attribute('preprocessorSrc', None, render=False), + Attribute('trailing_text', ''), ] def setup(self): diff --git a/common/lib/capa/capa/templates/textline.html b/common/lib/capa/capa/templates/textline.html index fbb5467b67..ffc23cc90b 100644 --- a/common/lib/capa/capa/templates/textline.html +++ b/common/lib/capa/capa/templates/textline.html @@ -31,6 +31,7 @@ style="display:none;" % endif /> + ${trailing_text | h}

% if status == 'unsubmitted': diff --git a/common/lib/capa/capa/tests/test_html_render.py b/common/lib/capa/capa/tests/test_html_render.py index e99308587e..492fcb2743 100644 --- a/common/lib/capa/capa/tests/test_html_render.py +++ b/common/lib/capa/capa/tests/test_html_render.py @@ -156,6 +156,7 @@ class CapaHtmlRenderTest(unittest.TestCase): 'hidden': False, 'do_math': False, 'id': '1_2_1', + 'trailing_text': '', 'size': None} expected_solution_context = {'id': '1_solution_1'} diff --git a/common/lib/capa/capa/tests/test_inputtypes.py b/common/lib/capa/capa/tests/test_inputtypes.py index e7f0b784bc..7e2f956f56 100644 --- a/common/lib/capa/capa/tests/test_inputtypes.py +++ b/common/lib/capa/capa/tests/test_inputtypes.py @@ -182,6 +182,7 @@ class TextLineTest(unittest.TestCase): 'hidden': False, 'inline': False, 'do_math': False, + 'trailing_text': '', 'preprocessor': None} self.assertEqual(context, expected) @@ -209,11 +210,49 @@ class TextLineTest(unittest.TestCase): 'msg': '', 'hidden': False, 'inline': False, + 'trailing_text': '', 'do_math': True, 'preprocessor': {'class_name': preprocessorClass, 'script_src': script}} self.assertEqual(context, expected) + def test_trailing_text_rendering(self): + size = "42" + # store (xml_text, expected) + trailing_text = [] + # standard trailing text + trailing_text.append(('m/s', 'm/s')) + # unicode trailing text + trailing_text.append((u'\xc3', u'\xc3')) + # html escaped trailing text + # this is the only one we expect to change + trailing_text.append(('a < b','a < b')) + + for xml_text, expected_text in trailing_text: + xml_str = u"""""".format(size=size, tt=xml_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': expected_text, + 'preprocessor': None} + self.assertEqual(context, expected) + class FileSubmissionTest(unittest.TestCase): '''