changeset 5:dbb08edbc615 trunk

Improved `py:attrs` directive so that it removes existing attributes if they evaluate to `None` (AFAICT matching Kid behavior).
author cmlenz
date Sat, 03 Jun 2006 15:29:27 +0000
parents 49364e784c47
children 71e8e645fe81
files markup/core.py markup/template.py
diffstat 2 files changed, 15 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/markup/core.py
+++ b/markup/core.py
@@ -142,6 +142,12 @@
                 return value
         return default
 
+    def remove(self, name):
+        for idx, (attr, _) in enumerate(self):
+            if attr == name:
+                del self[idx]
+                break
+
     def set(self, name, value):
         for idx, (attr, _) in enumerate(self):
             if attr == name:
--- a/markup/template.py
+++ b/markup/template.py
@@ -30,7 +30,6 @@
    nodes, making match templates more powerful while keeping the syntax simple
 
 Todo items:
- * XPath support needs a real implementation
  * Improved error reporting
  * Support for using directives as elements and not just as attributes, reducing
    the need for wrapper elements with py:strip=""
@@ -210,12 +209,15 @@
         kind, (tag, attrib), pos  = stream.next()
         attrs = self.expr.evaluate(ctxt)
         if attrs:
-            attrib = attrib[:]
-            for name, value in attrs.items():
-                if value is not None:
-                    value = unicode(value).strip()
-                    attrib.append((name, value))
-        yield kind, (tag, Attributes(attrib)), pos
+            attrib = Attributes(attrib[:])
+            if not isinstance(attrs, list): # assume it's a dict
+                attrs = attrs.items()
+            for name, value in attrs:
+                if value is None:
+                    attrib.remove(name)
+                else:
+                    attrib.set(name, unicode(value).strip())
+        yield kind, (tag, attrib), pos
         for event in stream:
             yield event
 
Copyright (C) 2012-2017 Edgewall Software