changeset 368:94ff33bfe515 stable-0.3.x

Ported [425] to 0.3.x.
author cmlenz
date Wed, 22 Nov 2006 20:55:08 +0000
parents eb2ef86a2cec
children 0d749146842c
files genshi/output.py genshi/tests/output.py
diffstat 2 files changed, 32 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/genshi/output.py
+++ b/genshi/output.py
@@ -183,7 +183,10 @@
     _BOOLEAN_ATTRS = frozenset(['selected', 'checked', 'compact', 'declare',
                                 'defer', 'disabled', 'ismap', 'multiple',
                                 'nohref', 'noresize', 'noshade', 'nowrap'])
-    _PRESERVE_SPACE = frozenset([QName('pre'), QName('textarea')])
+    _PRESERVE_SPACE = frozenset([
+        QName('pre'), QName('http://www.w3.org/1999/xhtml}pre'),
+        QName('textarea'), QName('http://www.w3.org/1999/xhtml}textarea')
+    ])
 
     def __call__(self, stream):
         namespace = self.NAMESPACE
@@ -461,8 +464,8 @@
         @param noescape: a set or sequence of tag names for which text content
             should not be escaped
         
-        Both the `preserve` and `noescape` sets are expected to refer to
-        elements that cannot contain further child elements.
+        The `noescape` set is expected to refer to elements that cannot contain
+        further child elements (such as <style> or <script> in HTML documents).
         """
         if preserve is None:
             preserve = []
@@ -477,7 +480,7 @@
                  collapse_lines=re.compile('\n{2,}').sub):
         mjoin = Markup('').join
         preserve_elems = self.preserve
-        preserve = False
+        preserve = 0
         noescape_elems = self.noescape
         noescape = False
         escape_cdata = self.escape_cdata
@@ -502,15 +505,17 @@
                     yield TEXT, Markup(text), pos
 
                 if kind is START:
-                    tag, attrib = data
-                    if not preserve and (tag in preserve_elems or
-                                         attrib.get(space) == 'preserve'):
-                        preserve = True
+                    tag, attrs = data
+                    if preserve or (tag in preserve_elems or
+                                    attrs.get(space) == 'preserve'):
+                        preserve += 1
                     if not noescape and tag in noescape_elems:
                         noescape = True
 
                 elif kind is END:
-                    preserve = noescape = False
+                    noescape = False
+                    if preserve:
+                        preserve -= 1
 
                 elif kind is START_CDATA and not escape_cdata:
                     noescape = True
--- a/genshi/tests/output.py
+++ b/genshi/tests/output.py
@@ -89,6 +89,12 @@
         output = stream.render(XHTMLSerializer)
         self.assertEqual('<textarea name="foo">%s</textarea>' % content, output)
 
+    def test_pre_whitespace(self):
+        content = '\nHey <em>there</em>.  \n\n    I am indented.\n'
+        stream = XML('<pre>%s</pre>' % content)
+        output = stream.render(XHTMLSerializer)
+        self.assertEqual('<pre>%s</pre>' % content, output)
+
     def test_xml_space(self):
         text = '<foo xml:space="preserve"> Do not mess  \n\n with me </foo>'
         output = XML(text).render(XHTMLSerializer)
@@ -154,6 +160,18 @@
 
 class HTMLSerializerTestCase(unittest.TestCase):
 
+    def test_textarea_whitespace(self):
+        content = '\nHey there.  \n\n    I am indented.\n'
+        stream = XML('<textarea name="foo">%s</textarea>' % content)
+        output = stream.render(HTMLSerializer)
+        self.assertEqual('<textarea name="foo">%s</textarea>' % content, output)
+
+    def test_pre_whitespace(self):
+        content = '\nHey <em>there</em>.  \n\n    I am indented.\n'
+        stream = XML('<pre>%s</pre>' % content)
+        output = stream.render(HTMLSerializer)
+        self.assertEqual('<pre>%s</pre>' % content, output)
+
     def test_xml_space(self):
         text = '<foo xml:space="preserve"> Do not mess  \n\n with me </foo>'
         output = XML(text).render(HTMLSerializer)
Copyright (C) 2012-2017 Edgewall Software