Edgewall Software

Ticket #429: t429-fix.2.patch

File t429-fix.2.patch, 3.8 KB (added by cboos, 12 years ago)

second version, test inlined

  • genshi/output.py

    # HG changeset patch
    # Parent f5c2e2ec50f4b254f456b2a9447acc88e2805fdd
    #429: don't cache `(TEXT, Markup)` in serializers.
    
    Not only this is not needed, but this can also lead to wrong retrievals.
    
    For example, if the `u'…'` input is seen first, the `Markup('…')` output is cached. If later `Markup('…')` is seen, the retrieved value would have been `Markup('&…')`!
    
    Added a regression test for this example, `genshi.tests.output.cache_markup`.
    
    diff -r f5c2e2ec50f4 genshi/output.py
    a b class XMLSerializer(object): 
    239239        for filter_ in self.filters:
    240240            stream = filter_(stream)
    241241        for kind, data, pos in stream:
     242            if kind is TEXT and isinstance(data, Markup):
     243                yield data
     244                continue
    242245            cached = _get((kind, data))
    243246            if cached is not None:
    244247                yield cached
    class XHTMLSerializer(XMLSerializer): 
    345348        for filter_ in self.filters:
    346349            stream = filter_(stream)
    347350        for kind, data, pos in stream:
     351            if kind is TEXT and isinstance(data, Markup):
     352                yield data
     353                continue
    348354            cached = _get((kind, data))
    349355            if cached is not None:
    350356                yield cached
    class HTMLSerializer(XHTMLSerializer): 
    467473        for filter_ in self.filters:
    468474            stream = filter_(stream)
    469475        for kind, data, _ in stream:
     476            if kind is TEXT and isinstance(data, Markup):
     477                yield data
     478                continue
    470479            output = _get((kind, data))
    471480            if output is not None:
    472481                yield output
    class NamespaceFlattener(object): 
    658667        _gen_prefix = _gen_prefix().next
    659668
    660669        for kind, data, pos in stream:
     670            if kind is TEXT and isinstance(data, Markup):
     671                yield kind, data, pos
     672                continue
    661673            output = _get((kind, data))
    662674            if output is not None:
    663675                yield kind, output, pos
  • genshi/tests/output.py

    diff -r f5c2e2ec50f4 genshi/tests/output.py
    a b import doctest 
    1515import unittest
    1616import sys
    1717
    18 from genshi.core import Attrs, Stream, QName
     18from genshi.core import Attrs, Markup, QName, Stream
    1919from genshi.input import HTML, XML
    2020from genshi.output import DocType, XMLSerializer, XHTMLSerializer, \
    2121                          HTMLSerializer, EmptyTagFilter
    class XHTMLSerializerTestCase(unittest.T 
    390390                               encoding=None)
    391391        self.assertEqual('<!DOCTYPE html>\n<html></html>', output)
    392392
     393    def test_ignorable_space(self):
     394        text = '<foo> Mess  \n\n\n with me!  </foo>'
     395        output = XML(text).render(XMLSerializer, encoding=None)
     396        self.assertEqual('<foo> Mess\n with me!  </foo>', output)
     397
     398    def test_cache_markup(self):
     399        loc = (None, -1, -1)
     400        stream = Stream([(Stream.START, (QName('foo'), Attrs()), loc),
     401                         (Stream.TEXT, u'&hellip;', loc),
     402                         (Stream.END, QName('foo'), loc),
     403                         (Stream.START, (QName('bar'), Attrs()), loc),
     404                         (Stream.TEXT, Markup('&hellip;'), loc),
     405                         (Stream.END, QName('bar'), loc)])
     406        output = stream.render(XMLSerializer, encoding=None,
     407                               strip_whitespace=False)
     408        self.assertEqual('<foo>&amp;hellip;</foo><bar>&hellip;</bar>', output)
     409
    393410
    394411class HTMLSerializerTestCase(unittest.TestCase):
    395412