diff options
Diffstat (limited to 'tests/regressiontests/forms/widgets.py')
| -rw-r--r-- | tests/regressiontests/forms/widgets.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/tests/regressiontests/forms/widgets.py b/tests/regressiontests/forms/widgets.py index cb1d084631..0e69602103 100644 --- a/tests/regressiontests/forms/widgets.py +++ b/tests/regressiontests/forms/widgets.py @@ -2,6 +2,7 @@ tests = r""" >>> from django.newforms import * >>> from django.newforms.widgets import RadioFieldRenderer +>>> from django.utils.safestring import mark_safe >>> import datetime >>> import time >>> import re @@ -128,6 +129,13 @@ u'<input type="hidden" class="fun" value="\u0160\u0110\u0106\u017d\u0107\u017e\u >>> w.render('email', '', attrs={'class': 'special'}) u'<input type="hidden" class="special" name="email" />' +Boolean values are rendered to their string forms ("True" and "False"). +>>> w = HiddenInput() +>>> w.render('get_spam', False) +u'<input type="hidden" name="get_spam" value="False" />' +>>> w.render('get_spam', True) +u'<input type="hidden" name="get_spam" value="True" />' + # MultipleHiddenInput Widget ################################################## >>> w = MultipleHiddenInput() @@ -205,6 +213,8 @@ u'<textarea rows="10" cols="40" name="msg"></textarea>' u'<textarea rows="10" cols="40" name="msg">value</textarea>' >>> w.render('msg', 'some "quoted" & ampersanded value') u'<textarea rows="10" cols="40" name="msg">some "quoted" & ampersanded value</textarea>' +>>> w.render('msg', mark_safe('pre "quoted" value')) +u'<textarea rows="10" cols="40" name="msg">pre "quoted" value</textarea>' >>> w.render('msg', 'value', attrs={'class': 'pretty', 'rows': 20}) u'<textarea class="pretty" rows="20" cols="40" name="msg">value</textarea>' @@ -375,6 +385,17 @@ If 'choices' is passed to both the constructor and render(), then they'll both b <option value="5">5</option> </select> +# Choices are escaped correctly +>>> print w.render('escape', None, choices=(('bad', 'you & me'), ('good', mark_safe('you > me')))) +<select name="escape"> +<option value="1">1</option> +<option value="2">2</option> +<option value="3">3</option> +<option value="bad">you & me</option> +<option value="good">you > me</option> +</select> + +# Unicode choices are correctly rendered as HTML >>> w.render('email', 'ŠĐĆŽćžšđ', choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]) u'<select name="email">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" selected="selected">\u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</option>\n<option value="\u0107\u017e\u0161\u0111">abc\u0107\u017e\u0161\u0111</option>\n</select>' @@ -538,6 +559,17 @@ If 'choices' is passed to both the constructor and render(), then they'll both b <option value="5">5</option> </select> +# Choices are escaped correctly +>>> print w.render('escape', None, choices=(('bad', 'you & me'), ('good', mark_safe('you > me')))) +<select multiple="multiple" name="escape"> +<option value="1">1</option> +<option value="2">2</option> +<option value="3">3</option> +<option value="bad">you & me</option> +<option value="good">you > me</option> +</select> + +# Unicode choices are correctly rendered as HTML >>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]) u'<select multiple="multiple" name="nums">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" selected="selected">\u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</option>\n<option value="\u0107\u017e\u0161\u0111">abc\u0107\u017e\u0161\u0111</option>\n</select>' @@ -663,6 +695,16 @@ You can create your own custom renderers for RadioSelect to use. <label><input checked="checked" type="radio" name="beatle" value="G" /> George</label><br /> <label><input type="radio" name="beatle" value="R" /> Ringo</label> +Or you can use custom RadioSelect fields that use your custom renderer. +>>> class CustomRadioSelect(RadioSelect): +... renderer = MyRenderer +>>> w = CustomRadioSelect() +>>> print w.render('beatle', 'G', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) +<label><input type="radio" name="beatle" value="J" /> John</label><br /> +<label><input type="radio" name="beatle" value="P" /> Paul</label><br /> +<label><input checked="checked" type="radio" name="beatle" value="G" /> George</label><br /> +<label><input type="radio" name="beatle" value="R" /> Ringo</label> + A RadioFieldRenderer object also allows index access to individual RadioInput objects. >>> w = RadioSelect() @@ -682,6 +724,14 @@ Traceback (most recent call last): ... IndexError: list index out of range +# Choices are escaped correctly +>>> w = RadioSelect() +>>> print w.render('escape', None, choices=(('bad', 'you & me'), ('good', mark_safe('you > me')))) +<ul> +<li><label><input type="radio" name="escape" value="bad" /> you & me</label></li> +<li><label><input type="radio" name="escape" value="good" /> you > me</label></li> +</ul> + # Unicode choices are correctly rendered as HTML >>> w = RadioSelect() >>> unicode(w.render('email', 'ŠĐĆŽćžšđ', choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')])) @@ -811,6 +861,17 @@ If 'choices' is passed to both the constructor and render(), then they'll both b <li><label><input type="checkbox" name="nums" value="5" /> 5</label></li> </ul> +# Choices are escaped correctly +>>> print w.render('escape', None, choices=(('bad', 'you & me'), ('good', mark_safe('you > me')))) +<ul> +<li><label><input type="checkbox" name="escape" value="1" /> 1</label></li> +<li><label><input type="checkbox" name="escape" value="2" /> 2</label></li> +<li><label><input type="checkbox" name="escape" value="3" /> 3</label></li> +<li><label><input type="checkbox" name="escape" value="bad" /> you & me</label></li> +<li><label><input type="checkbox" name="escape" value="good" /> you > me</label></li> +</ul> + +# Unicode choices are correctly rendered as HTML >>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]) u'<ul>\n<li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>\n<li><label><input type="checkbox" name="nums" value="2" /> 2</label></li>\n<li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>\n<li><label><input checked="checked" type="checkbox" name="nums" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" /> \u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</label></li>\n<li><label><input type="checkbox" name="nums" value="\u0107\u017e\u0161\u0111" /> abc\u0107\u017e\u0161\u0111</label></li>\n</ul>' |
