From c93b8076bf8fcdf36087f49d0335ede0b2c3d908 Mon Sep 17 00:00:00 2001 From: Diana Huang Date: Wed, 10 Apr 2013 14:47:45 -0400 Subject: [PATCH 1/5] Add trailing text to textlines, most useful for units. --- common/lib/capa/capa/inputtypes.py | 1 + common/lib/capa/capa/templates/textline.html | 3 +++ 2 files changed, 4 insertions(+) diff --git a/common/lib/capa/capa/inputtypes.py b/common/lib/capa/capa/inputtypes.py index 18bc92f0a3..b8af5a7286 100644 --- a/common/lib/capa/capa/inputtypes.py +++ b/common/lib/capa/capa/inputtypes.py @@ -483,6 +483,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..9fd1bd8786 100644 --- a/common/lib/capa/capa/templates/textline.html +++ b/common/lib/capa/capa/templates/textline.html @@ -31,6 +31,9 @@ style="display:none;" % endif /> + % if trailing_text: + ${trailing_text} + % endif

% if status == 'unsubmitted': From fd35b2c7e317515209a2f54701d571f927bc118d Mon Sep 17 00:00:00 2001 From: Diana Huang Date: Wed, 10 Apr 2013 14:52:38 -0400 Subject: [PATCH 2/5] Fix and create new test for trailing text --- .../lib/capa/capa/tests/test_html_render.py | 1 + common/lib/capa/capa/tests/test_inputtypes.py | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+) 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..818240c188 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,36 @@ 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" + trailing_text = 'm/s' + xml_str = """""".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) + class FileSubmissionTest(unittest.TestCase): ''' From d6e722a698211b2b87fd885a222198fa24a66afd Mon Sep 17 00:00:00 2001 From: Diana Huang Date: Thu, 11 Apr 2013 09:33:23 -0400 Subject: [PATCH 3/5] Remove unnecessary check and add better textline documentation --- common/lib/capa/capa/inputtypes.py | 8 ++++++++ common/lib/capa/capa/templates/textline.html | 2 -- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/common/lib/capa/capa/inputtypes.py b/common/lib/capa/capa/inputtypes.py index b8af5a7286..cca195baac 100644 --- a/common/lib/capa/capa/inputtypes.py +++ b/common/lib/capa/capa/inputtypes.py @@ -457,8 +457,16 @@ class TextLine(InputTypeBase): """ 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. + This 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" diff --git a/common/lib/capa/capa/templates/textline.html b/common/lib/capa/capa/templates/textline.html index 9fd1bd8786..01cf4dc535 100644 --- a/common/lib/capa/capa/templates/textline.html +++ b/common/lib/capa/capa/templates/textline.html @@ -31,9 +31,7 @@ style="display:none;" % endif /> - % if trailing_text: ${trailing_text} - % endif

% if status == 'unsubmitted': From ab7a616552fa74f16444bcbef73f35cb6bb4714a Mon Sep 17 00:00:00 2001 From: Diana Huang Date: Thu, 11 Apr 2013 14:13:09 -0400 Subject: [PATCH 4/5] Add the ability to handle escaped HTML characters in trailing text Add tests for unicode and escaped HTML characters --- common/lib/capa/capa/inputtypes.py | 5 +- common/lib/capa/capa/templates/textline.html | 2 +- common/lib/capa/capa/tests/test_inputtypes.py | 66 ++++++++++++++++++- 3 files changed, 68 insertions(+), 5 deletions(-) diff --git a/common/lib/capa/capa/inputtypes.py b/common/lib/capa/capa/inputtypes.py index cca195baac..df09cff2f6 100644 --- a/common/lib/capa/capa/inputtypes.py +++ b/common/lib/capa/capa/inputtypes.py @@ -457,8 +457,9 @@ class TextLine(InputTypeBase): """ 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. - This is useful if you would like to specify a specific type of units for the text input. + 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. diff --git a/common/lib/capa/capa/templates/textline.html b/common/lib/capa/capa/templates/textline.html index 01cf4dc535..ffc23cc90b 100644 --- a/common/lib/capa/capa/templates/textline.html +++ b/common/lib/capa/capa/templates/textline.html @@ -31,7 +31,7 @@ style="display:none;" % endif /> - ${trailing_text} + ${trailing_text | h}

% if status == 'unsubmitted': diff --git a/common/lib/capa/capa/tests/test_inputtypes.py b/common/lib/capa/capa/tests/test_inputtypes.py index 818240c188..6c820503b1 100644 --- a/common/lib/capa/capa/tests/test_inputtypes.py +++ b/common/lib/capa/capa/tests/test_inputtypes.py @@ -1,5 +1,6 @@ +# coding='utf-8' """ -Tests of input types. +feature Tests of input types. TODO: - refactor: so much repetive code (have factory methods that build xml elements directly, etc) @@ -20,6 +21,7 @@ import json from lxml import etree import unittest import xml.sax.saxutils as saxutils +import unicodedata as ud from . import test_system from capa import inputtypes @@ -219,7 +221,10 @@ class TextLineTest(unittest.TestCase): def test_trailing_text_rendering(self): size = "42" trailing_text = 'm/s' - xml_str = """""".format(size=size, tt=trailing_text) + xml_str = """""".format(size=size, tt=trailing_text) element = etree.fromstring(xml_str) @@ -241,6 +246,63 @@ class TextLineTest(unittest.TestCase): self.assertEqual(context, expected) + def test_trailing_unicode(self): + size = "42" + trailing_text = u'\xc3' + print trailing_text + xml_str = u"""""".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 < b' + xml_str = """""".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): ''' Check that file submission inputs work From 06a54a8c38813f2254e09a58e6e15eaa08e342b2 Mon Sep 17 00:00:00 2001 From: Diana Huang Date: Thu, 11 Apr 2013 16:27:10 -0400 Subject: [PATCH 5/5] Consolidate tests into one larger test --- common/lib/capa/capa/inputtypes.py | 16 ++- common/lib/capa/capa/tests/test_inputtypes.py | 109 +++++------------- 2 files changed, 40 insertions(+), 85 deletions(-) diff --git a/common/lib/capa/capa/inputtypes.py b/common/lib/capa/capa/inputtypes.py index df09cff2f6..aa507a5281 100644 --- a/common/lib/capa/capa/inputtypes.py +++ b/common/lib/capa/capa/inputtypes.py @@ -457,17 +457,21 @@ class TextLine(InputTypeBase): """ 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, - 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 "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. + 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. + 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" diff --git a/common/lib/capa/capa/tests/test_inputtypes.py b/common/lib/capa/capa/tests/test_inputtypes.py index 6c820503b1..7e2f956f56 100644 --- a/common/lib/capa/capa/tests/test_inputtypes.py +++ b/common/lib/capa/capa/tests/test_inputtypes.py @@ -1,6 +1,5 @@ -# coding='utf-8' """ -feature Tests of input types. +Tests of input types. TODO: - refactor: so much repetive code (have factory methods that build xml elements directly, etc) @@ -21,7 +20,6 @@ import json from lxml import etree import unittest import xml.sax.saxutils as saxutils -import unicodedata as ud from . import test_system from capa import inputtypes @@ -220,87 +218,40 @@ class TextLineTest(unittest.TestCase): def test_trailing_text_rendering(self): size = "42" - trailing_text = 'm/s' - xml_str = """""".format(size=size, tt=trailing_text) + # 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')) - element = etree.fromstring(xml_str) + for xml_text, expected_text in trailing_text: + xml_str = u"""""".format(size=size, tt=xml_text) - state = {'value': 'BumbleBee', } - the_input = lookup_tag('textline')(test_system, element, state) + element = etree.fromstring(xml_str) - context = the_input._get_render_context() + state = {'value': 'BumbleBee', } + the_input = lookup_tag('textline')(test_system, element, state) - 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) + context = the_input._get_render_context() - - def test_trailing_unicode(self): - size = "42" - trailing_text = u'\xc3' - print trailing_text - xml_str = u"""""".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 < b' - xml_str = """""".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) + 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):