Commits

Anonymous committed 75dd133

Widgets: We now output HTML5 shortened syntax by default. This is a major breaking change. All tests updated to match.

Comments (0)

Files changed (3)

         self.assertEqual(form.a.data, u'a')
         self.assertEqual(form.b.data, None)
         self.assertEqual(form.validate(), False)
-        self.assertEqual(form.a(), u"""<select id="a" name="a"><option selected="selected" value="a">hello</option><option value="btest">bye</option></select>""")
+        self.assertEqual(form.a(), u"""<select id="a" name="a"><option selected value="a">hello</option><option value="btest">bye</option></select>""")
         self.assertEqual(form.b(), u"""<select id="b" name="b"><option value="1">Item 1</option><option value="2">Item 2</option></select>""")
 
     def test_with_data(self):
         form = self.F(DummyPostData(a=[u'btest']))
         self.assertEqual(form.a.data, u'btest')
-        self.assertEqual(form.a(), u"""<select id="a" name="a"><option value="a">hello</option><option selected="selected" value="btest">bye</option></select>""")
+        self.assertEqual(form.a(), u"""<select id="a" name="a"><option value="a">hello</option><option selected value="btest">bye</option></select>""")
 
     def test_value_coercion(self):
         form = self.F(DummyPostData(b=[u'2']))
     def test_iterable_options(self):
         form = self.F()
         self.assert_(isinstance(list(form.a)[0], form.a._Option))
-        self.assertEqual(list(unicode(x) for x in form.a), [u'<option selected="selected" value="a">hello</option>', '<option value="btest">bye</option>'])
+        self.assertEqual(list(unicode(x) for x in form.a), [u'<option selected value="a">hello</option>', '<option value="btest">bye</option>'])
         self.assert_(isinstance(list(form.a)[0].widget, widgets.Option))
         self.assert_(isinstance(list(form.b)[0].widget, widgets.TextInput))
 
         self.assertEqual(form.a.data, u'a')
         self.assertEqual(form.b.data, None)
         self.assertEqual(form.validate(), False)
-        self.assertEqual(form.a(), u"""<ul id="a"><li><input checked="checked" id="a-0" name="a" type="radio" value="a" /> <label for="a-0">hello</label></li><li><input id="a-1" name="a" type="radio" value="b" /> <label for="a-1">bye</label></li></ul>""")
-        self.assertEqual(form.b(), u"""<ul id="b"><li><input id="b-0" name="b" type="radio" value="1" /> <label for="b-0">Item 1</label></li><li><input id="b-1" name="b" type="radio" value="2" /> <label for="b-1">Item 2</label></li></ul>""")
-        self.assertEqual([unicode(x) for x in form.a], [u'<input checked="checked" id="a-0" name="a" type="radio" value="a" />', u'<input id="a-1" name="a" type="radio" value="b" />'])
+        self.assertEqual(form.a(), u"""<ul id="a"><li><input checked id="a-0" name="a" type="radio" value="a"> <label for="a-0">hello</label></li><li><input id="a-1" name="a" type="radio" value="b"> <label for="a-1">bye</label></li></ul>""")
+        self.assertEqual(form.b(), u"""<ul id="b"><li><input id="b-0" name="b" type="radio" value="1"> <label for="b-0">Item 1</label></li><li><input id="b-1" name="b" type="radio" value="2"> <label for="b-1">Item 2</label></li></ul>""")
+        self.assertEqual([unicode(x) for x in form.a], [u'<input checked id="a-0" name="a" type="radio" value="a">', u'<input id="a-1" name="a" type="radio" value="b">'])
 
 
 class TextFieldTest(TestCase):
     def test(self):
         form = self.F()
         self.assertEqual(form.a.data, None)
-        self.assertEqual(form.a(), u"""<input id="a" name="a" type="text" value="" />""")
+        self.assertEqual(form.a(), u"""<input id="a" name="a" type="text" value="">""")
         form = self.F(DummyPostData(a=['hello']))
         self.assertEqual(form.a.data, u'hello')
-        self.assertEqual(form.a(), u"""<input id="a" name="a" type="text" value="hello" />""")
+        self.assertEqual(form.a(), u"""<input id="a" name="a" type="text" value="hello">""")
         form = self.F(DummyPostData(b=['hello']))
         self.assertEqual(form.a.data, u'')
 
 
     def test(self):
         form = self.F()
-        self.assertEqual(form.a(), u"""<input id="a" name="a" type="hidden" value="LE DEFAULT" />""")
+        self.assertEqual(form.a(), u"""<input id="a" name="a" type="hidden" value="LE DEFAULT">""")
 
 
 class TextAreaFieldTest(TestCase):
 
     def test(self):
         form = self.F()
-        self.assertEqual(form.a(), u"""<input id="a" name="a" type="password" value="LE DEFAULT" />""")
-        self.assertEqual(form.b(), u"""<input id="b" name="b" type="password" value="" />""")
+        self.assertEqual(form.a(), u"""<input id="a" name="a" type="password" value="LE DEFAULT">""")
+        self.assertEqual(form.b(), u"""<input id="b" name="b" type="password" value="">""")
 
 
 class FileFieldTest(TestCase):
 
     def test(self):
         form = self.F()
-        self.assertEqual(form.a(), u"""<input id="a" name="a" type="file" value="LE DEFAULT" />""")
+        self.assertEqual(form.a(), u"""<input id="a" name="a" type="file" value="LE DEFAULT">""")
 
 
 class IntegerFieldTest(TestCase):
         form = self.F(DummyPostData(a=['v'], b=['-15']))
         self.assertEqual(form.a.data, None)
         self.assertEqual(form.a.raw_data, [u'v'])
-        self.assertEqual(form.a(), u"""<input id="a" name="a" type="text" value="v" />""")
+        self.assertEqual(form.a(), u"""<input id="a" name="a" type="text" value="v">""")
         self.assertEqual(form.b.data, -15)
-        self.assertEqual(form.b(), u"""<input id="b" name="b" type="text" value="-15" />""")
+        self.assertEqual(form.b(), u"""<input id="b" name="b" type="text" value="-15">""")
         self.assert_(not form.a.validate(form))
         self.assert_(form.b.validate(form))
         form = self.F(DummyPostData(a=[], b=['']))
         form = self.F(DummyPostData(a=['v'], b=['-15.0']))
         self.assertEqual(form.a.data, None)
         self.assertEqual(form.a.raw_data, [u'v'])
-        self.assertEqual(form.a(), u"""<input id="a" name="a" type="text" value="v" />""")
+        self.assertEqual(form.a(), u"""<input id="a" name="a" type="text" value="v">""")
         self.assertEqual(form.b.data, -15.0)
-        self.assertEqual(form.b(), u"""<input id="b" name="b" type="text" value="-15.0" />""")
+        self.assertEqual(form.b(), u"""<input id="b" name="b" type="text" value="-15.0">""")
         self.assert_(not form.a.validate(form))
         self.assert_(form.b.validate(form))
         form = self.F(DummyPostData(a=[], b=['']))
 
     def test_rendering(self):
         form = self.BoringForm(DummyPostData(bool2=u"x"))
-        self.assertEqual(form.bool1(), u'<input id="bool1" name="bool1" type="checkbox" value="y" />')
-        self.assertEqual(form.bool2(), u'<input checked="checked" id="bool2" name="bool2" type="checkbox" value="x" />')
+        self.assertEqual(form.bool1(), u'<input id="bool1" name="bool1" type="checkbox" value="y">')
+        self.assertEqual(form.bool2(), u'<input checked id="bool2" name="bool2" type="checkbox" value="x">')
         self.assertEqual(form.bool2.raw_data, [u'x'])
 
     def test_with_postdata(self):
         d = datetime(2008, 5, 5, 4, 30, 0, 0)
         form = self.F(DummyPostData(a=['2008-05-05', '04:30:00'], b=['2008-05-05 04:30']))
         self.assertEqual(form.a.data, d)
-        self.assertEqual(form.a(), u"""<input id="a" name="a" type="text" value="2008-05-05 04:30:00" />""")
+        self.assertEqual(form.a(), u"""<input id="a" name="a" type="text" value="2008-05-05 04:30:00">""")
         self.assertEqual(form.b.data, d)
-        self.assertEqual(form.b(), u"""<input id="b" name="b" type="text" value="2008-05-05 04:30" />""")
+        self.assertEqual(form.b(), u"""<input id="b" name="b" type="text" value="2008-05-05 04:30">""")
         self.assert_(form.validate())
         form = self.F(DummyPostData(a=['2008-05-05']))
         self.assert_(not form.validate())
         a = SubmitField(u'Label')
 
     def test(self):
-        self.assertEqual(self.F().a(), """<input id="a" name="a" type="submit" value="Label" />""")
+        self.assertEqual(self.F().a(), """<input id="a" name="a" type="submit" value="Label">""")
 
 
 class FormFieldTest(TestCase):
         self.assertEqual(obj_inner.b, None)
 
     def test_widget(self):
-        self.assertEqual(self.F1().a(), u'''<table id="a"><tr><th><label for="a-a">A</label></th><td><input id="a-a" name="a-a" type="text" value="" /></td></tr><tr><th><label for="a-b">B</label></th><td><input id="a-b" name="a-b" type="text" value="" /></td></tr></table>''')
+        self.assertEqual(self.F1().a(), u'''<table id="a"><tr><th><label for="a-a">A</label></th><td><input id="a-a" name="a-a" type="text" value=""></td></tr><tr><th><label for="a-b">B</label></th><td><input id="a-b" name="a-b" type="text" value=""></td></tr></table>''')
 
     def test_separator(self):
         form = self.F2(DummyPostData({'a-a': 'fake', 'a::a': 'real'}))
         self.assert_(html.__html__() is html)
 
     def test_text_input(self):
-        self.assertEqual(TextInput()(self.field), u'<input id="id" name="bar" type="text" value="foo" />')
+        self.assertEqual(TextInput()(self.field), u'<input id="id" name="bar" type="text" value="foo">')
 
     def test_password_input(self):
         self.assert_(u'type="password"' in PasswordInput()(self.field))
         self.assert_(u'type="hidden"' in HiddenInput()(self.field))
 
     def test_checkbox_input(self):
-        self.assertEqual(CheckboxInput()(self.field, value='v'), '<input checked="checked" id="id" name="bar" type="checkbox" value="v" />')
+        self.assertEqual(CheckboxInput()(self.field, value='v'), '<input checked id="id" name="bar" type="checkbox" value="v">')
         field2 = DummyField(False)
         self.assert_(u'checked' not in CheckboxInput()(field2))
 
 
     def test(self):
         self.assertEqual(Select()(self.field), 
-            u'<select id="" name="f"><option selected="selected" value="foo">lfoo</option><option value="bar">lbar</option></select>')
+            u'<select id="" name="f"><option selected value="foo">lfoo</option><option value="bar">lbar</option></select>')
         self.assertEqual(Select(multiple=True)(self.field), 
-            '<select id="" multiple="multiple" name="f"><option selected="selected" value="foo">lfoo</option><option value="bar">lbar</option></select>')
+            '<select id="" multiple name="f"><option selected value="foo">lfoo</option><option value="bar">lbar</option></select>')
 
 if __name__ == '__main__':
     from unittest import main

wtforms/widgets/core.py

     for k,v in sorted(kwargs.iteritems()):
         if k in ('class_', 'class__', 'for_'):
             k = k[:-1]
-        params.append(u'%s="%s"' % (unicode(k), escape(unicode(v), quote=True)))
+        if v is True:
+            params.append(k)
+        else:
+            params.append(u'%s="%s"' % (unicode(k), escape(unicode(v), quote=True)))
     return u' '.join(params)
 
+
 class HTMLString(unicode):
     def __html__(self):
         return self
     By default, the `_value()` method will be called upon the associated field
     to provide the ``value=`` HTML attribute.
     """
+    html_params = staticmethod(html_params)
+
     def __init__(self, input_type=None):
         if input_type is not None:
             self.input_type = input_type
         kwargs.setdefault('type', self.input_type)
         if 'value' not in kwargs:
             kwargs['value'] = field._value()
-        return HTMLString(u'<input %s />' % html_params(name=field.name, **kwargs))
+        return HTMLString(u'<input %s>' % self.html_params(name=field.name, **kwargs))
 
 
 class TextInput(Input):
 
     def __call__(self, field, **kwargs):
         if getattr(field, 'checked', field.data):
-            kwargs['checked'] = u'checked'
+            kwargs['checked'] = True
         return super(CheckboxInput, self).__call__(field, **kwargs)
 
 
 
     def __call__(self, field, **kwargs):
         if field.checked:
-            kwargs['checked'] = u'checked'
+            kwargs['checked'] = True 
         return super(RadioInput, self).__call__(field, **kwargs)
 
 
         value = field._value()
         if value:
             kwargs.setdefault('value', value)
-        return HTMLString(u'<input %s />' % html_params(name=field.name, type=u'file', **kwargs))
+        return HTMLString(u'<input %s>' % html_params(name=field.name, type=u'file', **kwargs))
 
 
 class SubmitInput(Input):
     def __call__(self, field, **kwargs):
         kwargs.setdefault('id', field.id)
         if self.multiple:
-            kwargs['multiple'] = 'multiple'
+            kwargs['multiple'] = True 
         html = [u'<select %s>' % html_params(name=field.name, **kwargs)]
         for val, label, selected in field.iter_choices():
             html.append(self.render_option(val, label, selected))
     def render_option(cls, value, label, selected):
         options = {'value': value}
         if selected:
-            options['selected'] = u'selected'
+            options['selected'] = True 
         return HTMLString(u'<option %s>%s</option>' % (html_params(**options), escape(unicode(label))))