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

root/branches/cherrypy-2.1/cherrypy/lib/defaultformmask.py

Revision 588 (checked in by fumanchu, 3 years ago)

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

Line 
1 """
2 Copyright (c) 2004, CherryPy Team (team@cherrypy.org)
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8     * Redistributions of source code must retain the above copyright notice,
9       this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above copyright notice,
11       this list of conditions and the following disclaimer in the documentation
12       and/or other materials provided with the distribution.
13     * Neither the name of the CherryPy Team nor the names of its contributors
14       may be used to endorse or promote products derived from this software
15       without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 """
28
29 """
30 Default mask for the form.py module
31 """
32
33 from xml.sax.saxutils import quoteattr as q
34
35
36 def selected(value):
37     """If value is True, return a valid XHTML 'selected' attribute, else ''."""
38     if value:
39         return ' selected="selected" '
40     return ''
41
42 def 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
49 def defaultMask(field):
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))
65         for option in field.optionList:
66             if isinstance(option, tuple):
67                 id, option = option
68                 sel = selected(field.currentValue in (id, str(id)))
69                 value = " value=%s" % q(id)
70             else:
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>')
95         for option in field.optionList:
96             if isinstance(option, tuple):
97                 val, label = option
98             else:
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    
110     if field.errorMessage:
111         res.append("<td><font color='red'>%s</font></td>" % field.errorMessage)
112     else:
113         res.append("<td>&nbsp;</td>")
114    
115     res.append("</tr>")
116     return "\n".join(res)
117
118 def hiddenMask(field):
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
126 def defaultHeader(label):
127     return "<table>"
128
129 def defaultFooter(label):
130     return "</table>"
131
132 def echoMask(label):
133     return label
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets