changeset 437:821fc97d3c0a trunk

Fix for #107.
author cmlenz
date Mon, 02 Apr 2007 15:52:21 +0000
parents 57e8bd746717
children 2c38ec4e2dff
files ChangeLog genshi/output.py genshi/template/tests/markup.py
diffstat 3 files changed, 26 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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
--- a/genshi/template/tests/markup.py
+++ b/genshi/template/tests/markup.py
@@ -217,6 +217,22 @@
           42
         </div>""", 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("""<?xml version="1.0"?>
+        <Test xmlns:py="http://genshi.edgewall.org/">
+          <Size py:if="0" xmlns:t="test">Size</Size>
+          <Item/>
+        </Test>""")
+        self.assertEqual("""<Test>
+          
+          <Item/>
+        </Test>""", str(tmpl.generate()))
+
     def test_include_in_loop(self):
         dirname = tempfile.mkdtemp(suffix='genshi_test')
         try:
Copyright (C) 2012-2017 Edgewall Software