# HG changeset patch
# User cmlenz
# Date 1175529141 0
# Node ID 821fc97d3c0a84cdd40a8be55ef29f4de7fd9f9d
# Parent 57e8bd74671756e67fafc650bcc8550f620bd3b0
Fix for #107.
diff --git a/ChangeLog b/ChangeLog
--- a/ChangeLog
+++ b/ChangeLog
@@ -50,6 +50,9 @@
* `style` attributes are no longer allowed by the `HTMLSanitizer` by default.
If it is explicitly added to the set of safe attributes, and unicode escapes
in the attribute value are handled correctly.
+ * Namespace declarations on conditional elements (for example using a `py:if`
+ directive`) are no longer moved to the following element when the element
+ originally carrying the declaration is removed from the stream (ticket #107).
Version 0.3.6
diff --git a/genshi/output.py b/genshi/output.py
--- a/genshi/output.py
+++ b/genshi/output.py
@@ -412,6 +412,8 @@
ns_attrs = []
_push_ns_attr = ns_attrs.append
+ def _make_ns_attr(prefix, uri):
+ return u'xmlns%s' % (prefix and ':%s' % prefix or ''), uri
def _gen_prefix():
val = 0
@@ -467,10 +469,7 @@
prefix, uri = data
if uri not in namespaces:
prefix = prefixes.get(uri, [prefix])[-1]
- if not prefix:
- _push_ns_attr((u'xmlns', uri))
- else:
- _push_ns_attr((u'xmlns:%s' % prefix, uri))
+ _push_ns_attr(_make_ns_attr(prefix, uri))
_push_ns(prefix, uri)
elif kind is END_NS:
@@ -484,6 +483,10 @@
uri_prefixes.pop()
if not uri_prefixes:
del namespaces[uri]
+ if ns_attrs:
+ attr = _make_ns_attr(data, uri)
+ if attr in ns_attrs:
+ ns_attrs.remove(attr)
else:
yield kind, data, pos
diff --git a/genshi/template/tests/markup.py b/genshi/template/tests/markup.py
--- a/genshi/template/tests/markup.py
+++ b/genshi/template/tests/markup.py
@@ -217,6 +217,22 @@
42
""", str(tmpl.generate()))
+ def test_namespace_on_removed_elem(self):
+ """
+ Verify that a namespace declaration on an element that is removed from
+ the generated stream does not get pushed up to the next non-stripped
+ element (see ticket #107).
+ """
+ tmpl = MarkupTemplate("""
+
+ Size
+
+ """)
+ self.assertEqual("""
+
+
+ """, str(tmpl.generate()))
+
def test_include_in_loop(self):
dirname = tempfile.mkdtemp(suffix='genshi_test')
try: