changeset 939:f15334b65cf8 trunk

Don't cache (TEXT, Markup) events in serializers. This is not needed and since Markup instances compare equal to the same non-Markup string this can lead to incorrect cached output being retrieved. Fixes #429. This is patch t429-fix.2.patch from that ticket. It includes an additional unrelated test to check that the WhitespaceFilter actually removes ignorable whitespace.
author hodgestar
date Sun, 12 Jun 2011 00:41:35 +0000
parents 8d0f693081b5
children 417787b9b9a7
files genshi/output.py genshi/tests/output.py
diffstat 2 files changed, 30 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/genshi/output.py
+++ b/genshi/output.py
@@ -239,6 +239,9 @@
         for filter_ in self.filters:
             stream = filter_(stream)
         for kind, data, pos in stream:
+            if kind is TEXT and isinstance(data, Markup):
+                yield data
+                continue
             cached = _get((kind, data))
             if cached is not None:
                 yield cached
@@ -345,6 +348,9 @@
         for filter_ in self.filters:
             stream = filter_(stream)
         for kind, data, pos in stream:
+            if kind is TEXT and isinstance(data, Markup):
+                yield data
+                continue
             cached = _get((kind, data))
             if cached is not None:
                 yield cached
@@ -467,6 +473,9 @@
         for filter_ in self.filters:
             stream = filter_(stream)
         for kind, data, _ in stream:
+            if kind is TEXT and isinstance(data, Markup):
+                yield data
+                continue
             output = _get((kind, data))
             if output is not None:
                 yield output
@@ -658,6 +667,9 @@
         _gen_prefix = _gen_prefix().next
 
         for kind, data, pos in stream:
+            if kind is TEXT and isinstance(data, Markup):
+                yield kind, data, pos
+                continue
             output = _get((kind, data))
             if output is not None:
                 yield kind, output, pos
--- a/genshi/tests/output.py
+++ b/genshi/tests/output.py
@@ -15,7 +15,7 @@
 import unittest
 import sys
 
-from genshi.core import Attrs, Stream, QName
+from genshi.core import Attrs, Markup, QName, Stream
 from genshi.input import HTML, XML
 from genshi.output import DocType, XMLSerializer, XHTMLSerializer, \
                           HTMLSerializer, EmptyTagFilter
@@ -361,6 +361,23 @@
                                encoding=None)
         self.assertEqual('<!DOCTYPE html>\n<html></html>', output)
 
+    def test_ignorable_space(self):
+        text = '<foo> Mess  \n\n\n with me!  </foo>'
+        output = XML(text).render(XMLSerializer, encoding=None)
+        self.assertEqual('<foo> Mess\n with me!  </foo>', output)
+
+    def test_cache_markup(self):
+        loc = (None, -1, -1)
+        stream = Stream([(Stream.START, (QName('foo'), Attrs()), loc),
+                         (Stream.TEXT, u'&hellip;', loc),
+                         (Stream.END, QName('foo'), loc),
+                         (Stream.START, (QName('bar'), Attrs()), loc),
+                         (Stream.TEXT, Markup('&hellip;'), loc),
+                         (Stream.END, QName('bar'), loc)])
+        output = stream.render(XMLSerializer, encoding=None, 
+                               strip_whitespace=False)
+        self.assertEqual('<foo>&amp;hellip;</foo><bar>&hellip;</bar>', output)
+
 
 class HTMLSerializerTestCase(unittest.TestCase):
 
Copyright (C) 2012-2017 Edgewall Software