# HG changeset patch # User cmlenz # Date 1149348567 0 # Node ID dbb08edbc61593e6b2a1c34adfd60bb0c853b159 # Parent 49364e784c47690f90adee1765e0a0149410b9d7 Improved `py:attrs` directive so that it removes existing attributes if they evaluate to `None` (AFAICT matching Kid behavior). diff --git a/markup/core.py b/markup/core.py --- 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: diff --git a/markup/template.py b/markup/template.py --- 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