2022-01-13 23:08:38 +00:00
|
|
|
from django.forms import CharField, Form, TextInput
|
2015-08-31 02:13:42 +00:00
|
|
|
from django.utils.safestring import mark_safe
|
|
|
|
|
|
|
|
from .base import WidgetTest
|
|
|
|
|
|
|
|
|
|
|
|
class TextInputTest(WidgetTest):
|
|
|
|
widget = TextInput()
|
|
|
|
|
|
|
|
def test_render(self):
|
2018-01-21 07:09:10 +00:00
|
|
|
self.check_html(
|
|
|
|
self.widget, "email", "", html='<input type="text" name="email">'
|
|
|
|
)
|
2015-08-31 02:13:42 +00:00
|
|
|
|
|
|
|
def test_render_none(self):
|
2018-01-21 07:09:10 +00:00
|
|
|
self.check_html(
|
|
|
|
self.widget, "email", None, html='<input type="text" name="email">'
|
|
|
|
)
|
2015-08-31 02:13:42 +00:00
|
|
|
|
|
|
|
def test_render_value(self):
|
|
|
|
self.check_html(
|
|
|
|
self.widget,
|
|
|
|
"email",
|
|
|
|
"test@example.com",
|
2018-01-21 07:09:10 +00:00
|
|
|
html=('<input type="text" name="email" value="test@example.com">'),
|
2015-08-31 02:13:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_render_boolean(self):
|
|
|
|
"""
|
|
|
|
Boolean values are rendered to their string forms ("True" and
|
|
|
|
"False").
|
|
|
|
"""
|
|
|
|
self.check_html(
|
|
|
|
self.widget,
|
|
|
|
"get_spam",
|
|
|
|
False,
|
2018-01-21 07:09:10 +00:00
|
|
|
html=('<input type="text" name="get_spam" value="False">'),
|
2015-08-31 02:13:42 +00:00
|
|
|
)
|
|
|
|
self.check_html(
|
|
|
|
self.widget,
|
|
|
|
"get_spam",
|
|
|
|
True,
|
2018-01-21 07:09:10 +00:00
|
|
|
html=('<input type="text" name="get_spam" value="True">'),
|
2015-08-31 02:13:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_render_quoted(self):
|
|
|
|
self.check_html(
|
|
|
|
self.widget,
|
|
|
|
"email",
|
|
|
|
'some "quoted" & ampersanded value',
|
2018-01-21 07:09:10 +00:00
|
|
|
html=(
|
|
|
|
'<input type="text" name="email" '
|
|
|
|
'value="some "quoted" & ampersanded value">'
|
|
|
|
),
|
2015-08-31 02:13:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_render_custom_attrs(self):
|
|
|
|
self.check_html(
|
|
|
|
self.widget,
|
|
|
|
"email",
|
|
|
|
"test@example.com",
|
|
|
|
attrs={"class": "fun"},
|
2018-01-21 07:09:10 +00:00
|
|
|
html=(
|
|
|
|
'<input type="text" name="email" value="test@example.com" class="fun">'
|
|
|
|
),
|
2015-08-31 02:13:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_render_unicode(self):
|
|
|
|
self.check_html(
|
|
|
|
self.widget,
|
|
|
|
"email",
|
|
|
|
"ŠĐĆŽćžšđ",
|
|
|
|
attrs={"class": "fun"},
|
|
|
|
html=(
|
|
|
|
'<input type="text" name="email" '
|
2018-01-21 07:09:10 +00:00
|
|
|
'value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" class="fun">'
|
2015-08-31 02:13:42 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_constructor_attrs(self):
|
|
|
|
widget = TextInput(attrs={"class": "fun", "type": "email"})
|
|
|
|
self.check_html(
|
|
|
|
widget, "email", "", html='<input type="email" class="fun" name="email">'
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2015-08-31 02:13:42 +00:00
|
|
|
self.check_html(
|
2022-02-03 19:24:19 +00:00
|
|
|
widget,
|
|
|
|
"email",
|
2015-08-31 02:13:42 +00:00
|
|
|
"foo@example.com",
|
2018-01-21 07:09:10 +00:00
|
|
|
html=(
|
|
|
|
'<input type="email" class="fun" value="foo@example.com" name="email">'
|
|
|
|
),
|
2015-08-31 02:13:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_attrs_precedence(self):
|
|
|
|
"""
|
|
|
|
`attrs` passed to render() get precedence over those passed to the
|
|
|
|
constructor
|
|
|
|
"""
|
|
|
|
widget = TextInput(attrs={"class": "pretty"})
|
|
|
|
self.check_html(
|
|
|
|
widget,
|
|
|
|
"email",
|
|
|
|
"",
|
|
|
|
attrs={"class": "special"},
|
2018-01-21 07:09:10 +00:00
|
|
|
html='<input type="text" class="special" name="email">',
|
2015-08-31 02:13:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_attrs_safestring(self):
|
|
|
|
widget = TextInput(attrs={"onBlur": mark_safe("function('foo')")})
|
2018-01-21 07:09:10 +00:00
|
|
|
self.check_html(
|
|
|
|
widget,
|
|
|
|
"email",
|
2022-02-03 19:24:19 +00:00
|
|
|
"",
|
2018-01-21 07:09:10 +00:00
|
|
|
html='<input onBlur="function(\'foo\')" type="text" name="email">',
|
|
|
|
)
|
2016-08-12 17:59:01 +00:00
|
|
|
|
|
|
|
def test_use_required_attribute(self):
|
|
|
|
# Text inputs can safely trigger the browser validation.
|
|
|
|
self.assertIs(self.widget.use_required_attribute(None), True)
|
|
|
|
self.assertIs(self.widget.use_required_attribute(""), True)
|
|
|
|
self.assertIs(self.widget.use_required_attribute("resume.txt"), True)
|
2022-01-13 23:08:38 +00:00
|
|
|
|
|
|
|
def test_fieldset(self):
|
|
|
|
class TestForm(Form):
|
|
|
|
template_name = "forms_tests/use_fieldset.html"
|
|
|
|
field = CharField(widget=self.widget)
|
|
|
|
|
|
|
|
form = TestForm()
|
|
|
|
self.assertIs(self.widget.use_fieldset, False)
|
|
|
|
self.assertHTMLEqual(
|
|
|
|
'<div><label for="id_field">Field:</label>'
|
|
|
|
'<input type="text" name="field" required id="id_field"></div>',
|
|
|
|
form.render(),
|
|
|
|
)
|