Skip to content

Commit a786074

Browse files
author
Tobias Herp
committed
default attributes for input fields:
- type=text (useful for CSS) - id (useful for scriping and html labels)
1 parent d7a1fe6 commit a786074

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

roundup/cgi/templating.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -393,12 +393,28 @@ def lookupKeys(linkcl, key, ids, num_re=re.compile('^-?\d+$')):
393393
l.append(entry)
394394
return l
395395

396+
def _set_input_default_args(dic):
397+
# 'text' is the default value anyway --
398+
# but for CSS usage it should be present
399+
dic.setdefault('type', 'text')
400+
# useful e.g for HTML LABELs:
401+
if not dic.has_key('id'):
402+
try:
403+
if dic['text'] in ('radio', 'checkbox'):
404+
dic['id'] = '%(name)s-%(value)s' % dic
405+
else:
406+
dic['id'] = dic['name']
407+
except KeyError:
408+
pass
409+
396410
def input_html4(**attrs):
397411
"""Generate an 'input' (html4) element with given attributes"""
412+
_set_input_default_args(attrs)
398413
return '<input %s>'%' '.join(['%s="%s"'%item for item in attrs.items()])
399414

400415
def input_xhtml(**attrs):
401416
"""Generate an 'input' (xhtml) element with given attributes"""
417+
_set_input_default_args(attrs)
402418
return '<input %s/>'%' '.join(['%s="%s"'%item for item in attrs.items()])
403419

404420
class HTMLInputMixin:
@@ -1336,9 +1352,11 @@ def multiline(self, escape=0, rows=5, cols=40):
13361352
else:
13371353
value = cgi.escape(str(self._value))
13381354

1339-
value = '&quot;'.join(value.split('"'))
1340-
return '<textarea name="%s" rows="%s" cols="%s">%s</textarea>'%(
1341-
self._formname, rows, cols, value)
1355+
value = '&quot;'.join(value.split('"'))
1356+
name = self._formname
1357+
return ('<textarea name="%(name)s" id="%(name)s"'
1358+
' rows="%(rows)s" cols="%(cols)s">'
1359+
'%(value)s</textarea>') % locals()
13421360

13431361
def email(self, escape=1):
13441362
''' Render the value of the property as an obscured email address
@@ -1373,7 +1391,7 @@ def plain(self):
13731391
return ''
13741392
return self._('*encrypted*')
13751393

1376-
def field(self, size = 30):
1394+
def field(self, size=30):
13771395
''' Render a form edit field for the property.
13781396
13791397
If not editable, just display the value via plain().
@@ -1383,7 +1401,7 @@ def field(self, size = 30):
13831401

13841402
return self.input(type="password", name=self._formname, size=size)
13851403

1386-
def confirm(self, size = 30):
1404+
def confirm(self, size=30):
13871405
''' Render a second form edit field for the property, used for
13881406
confirmation that the user typed the password correctly. Generates
13891407
a field with name "@confirm@name".
@@ -1394,7 +1412,9 @@ def confirm(self, size = 30):
13941412
return ''
13951413

13961414
return self.input(type="password",
1397-
name="@confirm@%s"%self._formname, size=size)
1415+
name="@confirm@%s"%self._formname,
1416+
id="%s-confirm"%self._formname,
1417+
size=size)
13981418

13991419
class NumberHTMLProperty(HTMLProperty):
14001420
def plain(self):
@@ -1408,7 +1428,7 @@ def plain(self):
14081428

14091429
return str(self._value)
14101430

1411-
def field(self, size = 30):
1431+
def field(self, size=30):
14121432
''' Render a form edit field for the property.
14131433
14141434
If not editable, just display the value via plain().
@@ -1638,7 +1658,7 @@ def local(self, offset):
16381658
return DateHTMLProperty(self._client, self._classname, self._nodeid,
16391659
self._prop, self._formname, self._value, offset=offset)
16401660

1641-
def popcal(self, width=300, height=200, label= "(cal)",
1661+
def popcal(self, width=300, height=200, label="(cal)",
16421662
form="itemSynopsis"):
16431663
"""Generate a link to a calendar pop-up window.
16441664
@@ -1679,7 +1699,7 @@ def pretty(self):
16791699

16801700
return self._value.pretty()
16811701

1682-
def field(self, size = 30):
1702+
def field(self, size=30):
16831703
''' Render a form edit field for the property
16841704
16851705
If not editable, just display the value via plain().
@@ -1760,8 +1780,9 @@ def field(self, showid=0, size=None):
17601780
value = self._value
17611781
value = cgi.escape(str(value))
17621782
value = '&quot;'.join(value.split('"'))
1763-
return '<input name="%s" value="%s" size="%s">'%(self._formname,
1764-
value, size)
1783+
return self.input(name=self._formname,
1784+
value=value,
1785+
size=size)
17651786

17661787
def menu(self, size=None, height=None, showid=0, additional=[], value=None,
17671788
sort_on=None, **conditions):

0 commit comments

Comments
 (0)