[Mpuls-commits] r1838 - in wasko/branches/2.0: . mpulsweb/lib

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Feb 26 11:35:39 CET 2010


Author: bh
Date: 2010-02-26 11:35:37 +0100 (Fri, 26 Feb 2010)
New Revision: 1838

Modified:
   wasko/branches/2.0/ChangeLog
   wasko/branches/2.0/mpulsweb/lib/renderer.py
Log:
* mpulsweb/lib/renderer.py (tag): Better handling of boolean
attributes.  Now attribute values are converted to strings for
most attributes and only treated as booleans for a predefined set
of attributes.  Update the doc-string accordingly.


Modified: wasko/branches/2.0/ChangeLog
===================================================================
--- wasko/branches/2.0/ChangeLog	2010-02-26 09:04:49 UTC (rev 1837)
+++ wasko/branches/2.0/ChangeLog	2010-02-26 10:35:37 UTC (rev 1838)
@@ -1,3 +1,10 @@
+2010-02-26  Bernhard Herzog  <bh at intevation.de>
+
+	* mpulsweb/lib/renderer.py (tag): Better handling of boolean
+	attributes.  Now attribute values are converted to strings for
+	most attributes and only treated as booleans for a predefined set
+	of attributes.  Update the doc-string accordingly.
+
 2010-02-26  Torsten Irländer <torsten.irlaender at intevation.de>
 
 	Moved logic for consistence checks and anonymisation into mode.case.

Modified: wasko/branches/2.0/mpulsweb/lib/renderer.py
===================================================================
--- wasko/branches/2.0/mpulsweb/lib/renderer.py	2010-02-26 09:04:49 UTC (rev 1837)
+++ wasko/branches/2.0/mpulsweb/lib/renderer.py	2010-02-26 10:35:37 UTC (rev 1838)
@@ -69,16 +69,26 @@
     return depth
 
 
-def tag(tagname, _close=False, **attrs):
+def tag(tagname, _close=False,
+        _booleans=('checked', 'disabled', 'multiple', 'selected'), **attrs):
     """Create an opening HTML tag with properly escaped attributes.
 
-    The tagname parameter gives the name of the tag.  The attributes are
-    given as keyword parameters.  The attribute value should be either
-    strings (byte-strings or unicode) in which case they will be
-    properly escaped.  The values may also be booleans, in which case
-    the attribute is included in the tag with only its name if the value
-    is true and completely omitted otherwise.
+    The tagname parameter gives the name of the tag.  The attributes and
+    their values are given as keyword parameters.  Most attributes have
+    string values.  Boolean attributes are treated as described below.
+    The values of string attributes are converted to strings if they are
+    not aleady strings (byte-strings or unicode) using these rules: None
+    is converted to the empty string, other values are converted using
+    the str function.  After value of string attributes will be properly
+    escaped as attribute values when inserted into the tag.
 
+    Boolean flags are treated differently: if the value of such an
+    attribute is true, the attribute is included in the tag without a
+    value, if the value is false, the attribute is omitted.  The boolean
+    attributes handled in this way are given by the _booleans parameter,
+    which is a sequence of attribute names.  Its default value is
+    ('checked', 'disabled', 'selected')
+
     To allow for attribute names that are also python keywords, a single
     trailing underscore character is removed from an attribute name if
     it is preset, so that e.g. the class attribute can be specified
@@ -94,13 +104,15 @@
     for name, value in attrs.items():
         if name.endswith("_"):
             name = name[:-1]
-        if isinstance(value, basestring):
+        if name in _booleans:
+            if value:
+                out.append(" " + name)
+        else:
+            if value is None:
+                value = ""
+            elif not isinstance(value, basestring):
+                value = str(value)
             out.append(" %s=%s" % (name, quoteattr(value)))
-        else:
-            if not value:
-                continue
-            else:
-                out.append(" " + name)
     if _close:
         out.append("/>")
     else:



More information about the Mpuls-commits mailing list