changeset 535:4599602f00e5

span macro: refactor some code, make whitelist a param, clean up
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Tue, 22 Jun 2010 17:05:57 +0200
parents 7bcfe0b3823a
children c937dca76e96
files data/plugin/macro/span.py
diffstat 1 files changed, 16 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/data/plugin/macro/span.py	Tue Jun 22 16:56:31 2010 +0200
+++ b/data/plugin/macro/span.py	Tue Jun 22 17:05:57 2010 +0200
@@ -37,13 +37,15 @@
 
 Dependencies = []
 
-from MoinMoin.wikiutil import required_arg
 
-def make_style_safe(style):
-    """ make html 'style' attribute value safe """
+def make_style_safe(style, whitelist=None):
+    """
+    make html 'style' attribute value safe, only accept property names in whitelist,
+    if whitelist is None, use builtin WHITELIST
+    """
     # whitelist of safe style attributes, taken from:
     # http://validator.w3.org/feed/docs/warning/DangerousStyleAttr.html
-    whitelist = ("azimuth,background,background-color,border,border-bottom,"
+    WHITELIST = ("azimuth,background,background-color,border,border-bottom,"
                  "border-bottom-color,border-bottom-style,border-bottom-width,"
                  "border-collapse,border-color,border-left,border-left-color,"
                  "border-left-style,border-left-width,border-right,border-right-color,"
@@ -65,13 +67,14 @@
         split the declarations into property, value tuples,
         remove all surrounding whitespace
         """
-        decls = [decl.split(u':', 1) for decl in style.split(u';')]
-        # remove spaces from property and value
-        result = []
-        for decl in decls:
+        decls = []
+        for decl in style.split(u';'):
+            decl = decl.split(u':', 1)
             if len(decl) == 2:
-                result.append((decl[0].strip(), decl[1].strip()))
-        return result
+                prop = decl[0].strip()
+                val = decl[1].strip()
+                result.append((prop, val))
+        return decls
 
     def style_join(decls):
         """
@@ -81,14 +84,15 @@
         style = u'; '.join(decls)
         return style
 
-    def decl_filter(decls):
+    def decl_filter(decls, whitelist):
         """
         filter a list of prop, value tuples, only let whitelisted props through
         """
+        whitelist = whitelist or WHITELIST
         return [(prop, val) for prop, val in decls if prop in whitelist]
 
     decls = style_split(style)
-    decls = decl_filter(decls)
+    decls = decl_filter(decls, whitelist)
     style = style_join(decls)
     return style