Commits

Andriy Kornatskyy committed 37c8447

Refactored widgets by removing unnecessary kwargs argument.

Comments (0)

Files changed (1)

src/wheezy/html/widgets.py

 id = lambda name: name.replace('_', '-')
 
 
-def hidden(name, value, attr=None, **kwargs):
+def hidden(name, value, attrs=None):
     """
         >>> hidden('pref', 'abc')
         <input type="hidden" name="pref" value="abc" />
     """
-    tag_attr = {
+    return Tag('input', attrs={
             'name': name,
             'type': 'hidden',
             'value': value
-    }
-    if attr:
-        tag_attr.update(attr)
-    return Tag('input', attr=tag_attr, **kwargs)
+    })
 
 
-def textbox(name, value, attr=None, **kwargs):
+def textbox(name, value, attrs=None):
     """
-        >>> textbox('zip_code', '79053',
-        ...         attr={'size': '10'})  #doctest: +NORMALIZE_WHITESPACE
-        <input size="10" type="text" id="zip-code"
-            value="79053" name="zip_code" />
+        >>> textbox('zip_code', '79053')
+        <input type="text" id="zip-code" value="79053" name="zip_code" />
     """
-    tag_attr = {
+    return Tag('input', attrs={
             'id': id(name),
             'name': name,
             'type': 'text',
             'value': value
-    }
-    if attr:
-        tag_attr.update(attr)
-    return Tag('input', attr=tag_attr, **kwargs)
+    })
 
 
-def checkbox(name, checked, attr=None, **kwargs):
+def textarea(name, value, attrs=None):
+    """
+        >>> textarea('message_text', 'x')  #doctest: +NORMALIZE_WHITESPACE
+        <textarea rows="9" cols="40" id="message-text"
+            name="message_text">x</textarea>
+
+        ``value`` is empty.
+
+        >>> textarea('message_text', '')  #doctest: +NORMALIZE_WHITESPACE
+        <textarea rows="9" cols="40" id="message-text"
+            name="message_text"></textarea>
+    """
+    return Tag('textarea', value, attrs={
+            'id': id(name),
+            'name': name,
+            'rows': '9',
+            'cols': '40'
+    })
+
+
+def checkbox(name, checked, attrs=None):
     """
         >>> checkbox('accept', True)  #doctest: +NORMALIZE_WHITESPACE
         <input checked="checked" type="checkbox" id="accept"
         >>> checkbox('accept', False)  #doctest: +NORMALIZE_WHITESPACE
         <input type="checkbox" id="accept" value="1" name="accept"
             /><input type="hidden" name="accept" />
-    """
-    tag_attr = {
-        'id': id(name),
-        'name': name,
-        'type': 'checkbox',
-        'value': '1'
+
+        >>> checkbox('accept', True,
+        ...         attrs={'class_': 'b'})  #doctest: +NORMALIZE_WHITESPACE
+        <input class="b" checked="checked" name="accept"
+            type="checkbox" id="accept" value="1" /><input
+            type="hidden" name="accept" />
+     """
+    tag_attrs = {
+            'id': id(name),
+            'name': name,
+            'type': 'checkbox',
+            'value': '1'
     }
     if checked:
-        tag_attr['checked'] = 'checked'
-    if attr:
-        tag_attr.update(attr)
+        tag_attrs['checked'] = 'checked'
+    if attrs:
+        tag_attrs.update(attrs)
     return Fragment(
-            Tag('input', attr=tag_attr, **kwargs),
-            Tag('input', attr={'name': name, 'type': 'hidden'})
+            Tag('input', attrs=tag_attrs),
+            Tag('input', attrs={'name': name, 'type': 'hidden'})
     )
 
 
-def label(name, text, attr=None, **kwargs):
+def label(name, value, attrs=None):
     """
         >>> label('zip_code', 'Zip Code')
         <label for="zip-code">Zip Code</label>
     """
-    tag_attr = {
+    tag_attrs = {
             'for': id(name)
     }
-    if attr:
-        tag_attr.update(attr)
-    return Tag('label', inner=text, attr=tag_attr, **kwargs)
+    return Tag('label', inner=value, attrs=tag_attrs)
 
 
-def dropdown(name, selected, attr=None, **kwargs):
+def dropdown(name, value, attrs=None):
     """
-        >>> colors = {'yellow': 'Yellow', 'red': 'Red'}
-        >>> dropdown('favorite_color', 'yellow', attr={
+        >>> colors = (('1', 'Yellow'), ('2', 'Red'))
+        >>> dropdown('favorite_color', '1', attrs={
         ...     'choices': colors})  #doctest: +NORMALIZE_WHITESPACE
         <select id="favorite-color" name="favorite_color"><option
-        value="red">Red</option><option selected="selected"
-        value="yellow">Yellow</option></select>
+            selected="selected" value="1">Yellow</option><option
+            value="2">Red</option></select>
     """
-    choices = attr.pop('choices')
+    choices = attrs.pop('choices')
     options = []
-    for value, text in iteritems(choices):
-        if value == selected:
-            tag_attr = {
-                    'value': value,
+    for key, text in choices:
+        if key == value:
+            tag_attrs = {
+                    'value': key,
                     'selected': 'selected'
             }
         else:
-            tag_attr = {
-                    'value': value
+            tag_attrs = {
+                    'value': key
             }
-        options.append(Tag('option', inner=text, attr=tag_attr))
+        options.append(Tag('option', inner=text, attrs=tag_attrs))
     options = Fragment(*options)
-    tag_attr = {
+    tag_attrs = {
             'id': id(name),
             'name': name
     }
-    if attr:
-        tag_attr.update(attr)
-    return Tag('select', inner=options, attr=tag_attr, **kwargs)
+    return Tag('select', inner=options, attrs=tag_attrs)
+
 
 default = {
         'hidden': hidden,
         'textbox': textbox,
+        'textarea': textarea,
         'checkbox': checkbox,
-        'label': label
+        'label': label,
+        'dropdown': dropdown
 }