[Checkins] SVN: z3c.form/trunk/ added a createCSSId function for making nice css ids from unicode strings.

Paul Carduner paulcarduner at gmail.com
Tue May 27 12:32:20 EDT 2008


Log message for revision 86991:
  added a createCSSId function for making nice css ids from unicode strings.

Changed:
  U   z3c.form/trunk/CHANGES.txt
  U   z3c.form/trunk/src/z3c/form/util.py
  U   z3c.form/trunk/src/z3c/form/util.txt

-=-
Modified: z3c.form/trunk/CHANGES.txt
===================================================================
--- z3c.form/trunk/CHANGES.txt	2008-05-27 15:57:47 UTC (rev 86990)
+++ z3c.form/trunk/CHANGES.txt	2008-05-27 16:32:18 UTC (rev 86991)
@@ -5,6 +5,10 @@
 Version 1.8.3 (????-??-??)
 --------------------------
 
+- Feature: The z3c.form.util module has a new function, createCSSId
+  that generates readable ids for use with css selectors from any
+  unicode string.
+
 - Bug: Display widgets did not set the style attribute if it was
   available, even though the input widgets did set the style attribute.
 

Modified: z3c.form/trunk/src/z3c/form/util.py
===================================================================
--- z3c.form/trunk/src/z3c/form/util.py	2008-05-27 15:57:47 UTC (rev 86990)
+++ z3c.form/trunk/src/z3c/form/util.py	2008-05-27 16:32:18 UTC (rev 86991)
@@ -18,6 +18,7 @@
 __docformat__ = "reStructuredText"
 import re
 import types
+import string
 import zope.interface
 import zope.contenttype
 
@@ -32,6 +33,11 @@
         return str(name).lower()
     return name.encode('utf-8').encode('hex')
 
+_acceptableChars = string.letters + string.digits + '_-'
+def createCSSId(name):
+    return str(''.join([((char in _acceptableChars and char) or
+                         char.encode('utf-8').encode('hex'))
+                        for char in name]))
 
 classTypes = type, types.ClassType
 

Modified: z3c.form/trunk/src/z3c/form/util.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/util.txt	2008-05-27 15:57:47 UTC (rev 86990)
+++ z3c.form/trunk/src/z3c/form/util.txt	2008-05-27 16:32:18 UTC (rev 86991)
@@ -31,7 +31,22 @@
   >>> util.createId(u'Ändern')
   'c383c2846e6465726e'
 
+``createCSSId(name)`` Function
+------------------------------
 
+This function takes any unicode name and coverts it into an id that
+can be easily referenced by CSS selectors.  Characters that are in the
+ascii alphabet, are numbers, or are '-' or '_' will be left the same.
+All other characters will be converted to ordinal numbers:
+
+  >>> util.createCSSId(u'NormalId')
+  'NormalId'
+  >>> util.createCSSId(u'عَرَ')
+  'c398c2b9c399c28ec398c2b1c399c28e'
+  >>> util.createCSSId(u'This has spaces')
+  'This20has20spaces'
+
+
 ``getWidgetById(form, id)`` Function
 ------------------------------------
 



More information about the Checkins mailing list