Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 588

Show
Ignore:
Timestamp:
09/02/05 03:34:08
Author:
fumanchu
Message:

Updated lib\defaultformmask.py to smart attribute quoting and XHTML.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/lib/defaultformmask.py

    r229 r588  
    3131""" 
    3232 
     33from xml.sax.saxutils import quoteattr as q 
     34 
     35 
     36def selected(value): 
     37    """If value is True, return a valid XHTML 'selected' attribute, else ''.""" 
     38    if value: 
     39        return ' selected="selected" ' 
     40    return '' 
     41 
     42def checked(value): 
     43    """If value is True, return a valid XHTML 'checked' attribute, else ''.""" 
     44    if value: 
     45        return ' checked="checked" ' 
     46    return '' 
     47 
     48 
    3349def defaultMask(field): 
    34     res="<tr><td valign=top>%s</td>"%field.label 
    35     if field.typ=='text': 
    36         res+='<td><input name="%s" type=text value="%s" size=%s></td>'%(field.name, field.currentValue, field.size) 
    37     elif field.typ=='forced': 
    38         res+='<td><input name="%s" type=hidden value="%s">%s</td>'%(field.name, field.currentValue, field.currentValue) 
    39     elif field.typ=='password': 
    40         res+='<td><input name="%s" type=password value="%s"></td>'%(field.name, field.currentValue) 
    41     elif field.typ=='select': 
    42         res+='<td><select name="%s">'%field.name 
     50     
     51    res = ["<tr>", 
     52           "<td valign='top'>%s</td>" % field.label] 
     53     
     54    if field.typ == 'text': 
     55        res.append('<td><input name=%s type="text" value=%s size=%s /></td>' 
     56                   % (q(field.name), q(field.currentValue), q(field.size))) 
     57    elif field.typ == 'forced': 
     58        res.append('<td><input name=%s type="hidden" value=%s />%s</td>' 
     59                   % (q(field.name), q(field.currentValue), field.currentValue)) 
     60    elif field.typ == 'password': 
     61        res.append('<td><input name=%s type="password" value=%s /></td>' 
     62                   % (q(field.name), q(field.currentValue))) 
     63    elif field.typ == 'select': 
     64        res.append('<td><select name=%s>' % q(field.name)) 
    4365        for option in field.optionList: 
    44             if type(option)==type(()): 
    45                 optionId, optionLabel=option 
    46                 if optionId==field.currentValue or str(optionId)==field.currentValue: res+="<option selected value=%s>%s</option>"%(optionId, optionLabel
    47                 else: res+="<option value=%s>%s</option>"%(optionId, optionLabel
     66            if isinstance(option, tuple): 
     67                id, option = option 
     68                sel = selected(field.currentValue in (id, str(id))
     69                value = " value=%s" % q(id
    4870            else: 
    49                 if option==field.currentValue: res+="<option selected>%s</option>"%option 
    50                 else: res+="<option>%s</option>"%option 
    51         res+='</select></td>' 
    52     elif field.typ=='textarea': 
    53         # Size is colsxrows 
    54         if field.size==15: size="15x15" 
    55         else: size=field.size 
    56         cols, rows=size.split('x') 
    57         res+='<td><textarea name="%s" rows="%s" cols="%s">%s</textarea></td>'%(field.name, rows, cols, field.currentValue) 
    58     elif field.typ=='submit': 
    59         res+='<td><input type=submit value="%s"></td>'%field.name 
    60     elif field.typ=='hidden': 
    61         if type(field.currentValue)==type([]): currentValue=field.currentValue 
    62         else: currentValue=[field.currentValue] 
    63         res="" 
    64         for value in currentValue: 
    65             res+='<input name="%s" type=hidden value="%s">'%(field.name, value) 
    66         return res 
    67     elif field.typ=='checkbox' or field.typ=='radio': 
    68         res+='<td>' 
    69         # print "##### currentValue:", field.currentValue # TBC 
     71                sel = selected(option == field.currentValue) 
     72                value = "" 
     73            res.append("    <option%s%s>%s</option>" % (sel, value, option)) 
     74        res.append('</select></td>') 
     75    elif field.typ == 'textarea': 
     76        # Size is cols x rows 
     77        if field.size == 15: 
     78            size = "15x15" 
     79        else: 
     80            size = field.size 
     81        cols, rows = size.split('x') 
     82        res.append('<td><textarea name=%s rows="%s" cols="%s">%s</textarea></td>' 
     83                   % (q(field.name), rows, cols, field.currentValue)) 
     84    elif field.typ == 'submit': 
     85        res.append('<td><input type="submit" value=%s /></td>' % q(field.name)) 
     86    elif field.typ == 'hidden': 
     87        if isinstance(field.currentValue, list): 
     88            vals = field.currentValue 
     89        else: 
     90            vals = [field.currentValue] 
     91        i = '<input name=%s type="hidden" value=%%s />' % q(field.name) 
     92        return [i % q(v) for v in vals] 
     93    elif field.typ in ('checkbox', 'radio'): 
     94        res.append('<td>') 
    7095        for option in field.optionList: 
    71             if type(option)==type(()): optionValue, optionLabel=option 
    72             else: optionValue, optionLabel=option, option 
    73             res+='<input type="%s" name="%s" value="%s"'%(field.typ, field.name, optionValue) 
    74             if type(field.currentValue)==type([]): 
    75                 if optionValue in field.currentValue: res+=' checked' 
     96            if isinstance(option, tuple): 
     97                val, label = option 
    7698            else: 
    77                 if optionValue==field.currentValue: res+=' checked' 
    78             res+='>&nbsp;&nbsp;%s<br>'%optionLabel 
    79         res+='</td>' 
     99                val, label = option, option 
     100             
     101            if isinstance(field.currentValue, list): 
     102                c = checked(val in field.currentValue) 
     103            else: 
     104                c = checked(val == field.currentValue) 
     105             
     106            res.append('<input type="%s" name=%s value=%s %s />&nbsp;&nbsp;%s<br />' 
     107                       % (field.typ, q(field.name), q(val), c, label)) 
     108        res.append('</td>') 
     109     
    80110    if field.errorMessage: 
    81         res+="<td><font color=red>%s</font></td>"%field.errorMessage 
     111        res.append("<td><font color='red'>%s</font></td>" % field.errorMessage) 
    82112    else: 
    83         res+="<td>&nbsp;</td>" 
    84     return res+"</tr>" 
     113        res.append("<td>&nbsp;</td>") 
     114     
     115    res.append("</tr>") 
     116    return "\n".join(res) 
     117 
    85118def hiddenMask(field): 
    86         if type(field.currentValue)==type([]): currentValue=field.currentValue 
    87         else: currentValue=[field.currentValue] 
    88         res="" 
    89         for value in currentValue: 
    90             res+='<input name="%s" type=hidden value="%s">'%(field.name, value) 
    91         return res 
     119    if isinstance(field.currentValue, list): 
     120        currentValue = field.currentValue 
     121    else: 
     122        currentValue = [field.currentValue] 
     123    return "\n".join(['<input name=%s type="hidden" value=%s />' % 
     124                      (q(field.name), q(value)) for value in currentValue]) 
     125 
    92126def defaultHeader(label): 
    93127    return "<table>" 
     128 
    94129def defaultFooter(label): 
    95130    return "</table>" 
     131 
    96132def echoMask(label): 
    97133    return label 

Hosted by WebFaction

Log in as guest/cpguest to create tickets