diff --git a/common/lib/capa/capa/inputtypes.py b/common/lib/capa/capa/inputtypes.py index c487efed69..3e8036ce30 100644 --- a/common/lib/capa/capa/inputtypes.py +++ b/common/lib/capa/capa/inputtypes.py @@ -1214,9 +1214,11 @@ class FormulaEquationInput(InputTypeBase): Example: - + options: size -- width of the textbox. + trailing_text -- text to show after the input textbox when + rendered, same as textline (useful for units) """ template = "formulaequationinput.html" @@ -1231,6 +1233,7 @@ class FormulaEquationInput(InputTypeBase): Attribute('size', '20'), Attribute('inline', False), Attribute('label', ''), + Attribute('trailing_text', ''), ] def _extra_context(self): diff --git a/common/lib/capa/capa/templates/formulaequationinput.html b/common/lib/capa/capa/templates/formulaequationinput.html index 206a8bc21c..ad733b1acd 100644 --- a/common/lib/capa/capa/templates/formulaequationinput.html +++ b/common/lib/capa/capa/templates/formulaequationinput.html @@ -9,6 +9,7 @@ size="${size}" % endif /> + ${trailing_text | h} diff --git a/common/lib/capa/capa/tests/test_input_templates.py b/common/lib/capa/capa/tests/test_input_templates.py index 5af21fd5e2..6d33f60dc1 100644 --- a/common/lib/capa/capa/tests/test_input_templates.py +++ b/common/lib/capa/capa/tests/test_input_templates.py @@ -486,6 +486,7 @@ class FormulaEquationInputTemplateTest(TemplateTestCase): 'label': 'test', 'previewer': 'file.js', 'reported_status': 'REPORTED_STATUS', + 'trailing_text': None, } super(FormulaEquationInputTemplateTest, self).setUp() diff --git a/common/lib/capa/capa/tests/test_inputtypes.py b/common/lib/capa/capa/tests/test_inputtypes.py index 6946bbed97..649f6692b9 100644 --- a/common/lib/capa/capa/tests/test_inputtypes.py +++ b/common/lib/capa/capa/tests/test_inputtypes.py @@ -1126,9 +1126,53 @@ class FormulaEquationTest(unittest.TestCase): 'size': self.size, 'previewer': '/dummy-static/js/capa/src/formula_equation_preview.js', 'inline': False, + 'trailing_text': '', } self.assertEqual(context, expected) + def test_trailing_text_rendering(self): + """ + Verify that the render context matches the expected render context with trailing_text + """ + 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': 'x^2+1/2', } + the_input = lookup_tag('formulaequationinput')(test_capa_system(), element, state) + + context = the_input._get_render_context() # pylint: disable=protected-access + + expected = { + 'STATIC_URL': '/dummy-static/', + 'id': 'prob_1_2', + 'value': 'x^2+1/2', + 'status': inputtypes.Status('unanswered'), + 'label': '', + 'msg': '', + 'size': size, + 'previewer': '/dummy-static/js/capa/src/formula_equation_preview.js', + 'inline': False, + 'trailing_text': expected_text, + } + + self.assertEqual(context, expected) + def test_formcalc_ajax_sucess(self): """ Verify that using the correct dispatch and valid data produces a valid response