annotate genshi/tests/output.py @ 347:ffa7dea6e8fd experimental-inline

cspeedups branch: Merged [423:426/trunk].
author cmlenz
date Fri, 10 Nov 2006 17:38:50 +0000
parents 06a25d0962af
children c199e9b95884 3eb30e4ece8c
rev   line source
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
2 #
66
59eb24184e9c Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents: 27
diff changeset
3 # Copyright (C) 2006 Edgewall Software
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
4 # All rights reserved.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
5 #
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
230
84168828b074 Renamed Markup to Genshi in repository.
cmlenz
parents: 212
diff changeset
8 # are also available at http://genshi.edgewall.org/wiki/License.
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
9 #
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
230
84168828b074 Renamed Markup to Genshi in repository.
cmlenz
parents: 212
diff changeset
12 # history and logs, available at http://genshi.edgewall.org/log/.
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
13
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
14 import doctest
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
15 import unittest
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
16 import sys
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
17
314
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
18 from genshi.core import Attrs, Stream, QName
230
84168828b074 Renamed Markup to Genshi in repository.
cmlenz
parents: 212
diff changeset
19 from genshi.input import HTML, XML
84168828b074 Renamed Markup to Genshi in repository.
cmlenz
parents: 212
diff changeset
20 from genshi.output import DocType, XMLSerializer, XHTMLSerializer, \
212
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 178
diff changeset
21 HTMLSerializer, EmptyTagFilter
85
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
22
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
23
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
24 class XMLSerializerTestCase(unittest.TestCase):
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
25
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
26 def test_doctype_in_stream(self):
314
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
27 stream = Stream([(Stream.DOCTYPE, DocType.HTML_STRICT, (None, -1, -1))])
85
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
28 output = stream.render(XMLSerializer)
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
29 self.assertEqual('<!DOCTYPE html PUBLIC '
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
30 '"-//W3C//DTD HTML 4.01//EN" '
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
31 '"http://www.w3.org/TR/html4/strict.dtd">\n',
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
32 output)
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
33
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
34 def test_doctype_in_stream_no_sysid(self):
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
35 stream = Stream([(Stream.DOCTYPE,
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
36 ('html', '-//W3C//DTD HTML 4.01//EN', None),
314
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
37 (None, -1, -1))])
85
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
38 output = stream.render(XMLSerializer)
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
39 self.assertEqual('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">\n',
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
40 output)
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
41
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
42 def test_doctype_in_stream_no_pubid(self):
314
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
43 stream = Stream([
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
44 (Stream.DOCTYPE,
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
45 ('html', None, 'http://www.w3.org/TR/html4/strict.dtd'),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
46 (None, -1, -1))
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
47 ])
85
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
48 output = stream.render(XMLSerializer)
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
49 self.assertEqual('<!DOCTYPE html SYSTEM '
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
50 '"http://www.w3.org/TR/html4/strict.dtd">\n',
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
51 output)
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
52
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
53 def test_doctype_in_stream_no_pubid_or_sysid(self):
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
54 stream = Stream([(Stream.DOCTYPE, ('html', None, None),
314
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
55 (None, -1, -1))])
85
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
56 output = stream.render(XMLSerializer)
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
57 self.assertEqual('<!DOCTYPE html>\n', output)
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
58
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
59 def test_serializer_doctype(self):
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
60 stream = Stream([])
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
61 output = stream.render(XMLSerializer, doctype=DocType.HTML_STRICT)
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
62 self.assertEqual('<!DOCTYPE html PUBLIC '
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
63 '"-//W3C//DTD HTML 4.01//EN" '
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
64 '"http://www.w3.org/TR/html4/strict.dtd">\n',
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
65 output)
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
66
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
67 def test_doctype_one_and_only(self):
314
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
68 stream = Stream([
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
69 (Stream.DOCTYPE, ('html', None, None), (None, -1, -1))
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
70 ])
85
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
71 output = stream.render(XMLSerializer, doctype=DocType.HTML_STRICT)
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
72 self.assertEqual('<!DOCTYPE html PUBLIC '
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
73 '"-//W3C//DTD HTML 4.01//EN" '
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
74 '"http://www.w3.org/TR/html4/strict.dtd">\n',
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
75 output)
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
76
89
80386d62814f Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents: 85
diff changeset
77 def test_comment(self):
314
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
78 stream = Stream([(Stream.COMMENT, 'foo bar', (None, -1, -1))])
89
80386d62814f Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents: 85
diff changeset
79 output = stream.render(XMLSerializer)
80386d62814f Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents: 85
diff changeset
80 self.assertEqual('<!--foo bar-->', output)
80386d62814f Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents: 85
diff changeset
81
105
71f3db26eecb Include processing instructions in serialized streams.
cmlenz
parents: 89
diff changeset
82 def test_processing_instruction(self):
314
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
83 stream = Stream([(Stream.PI, ('python', 'x = 2'), (None, -1, -1))])
105
71f3db26eecb Include processing instructions in serialized streams.
cmlenz
parents: 89
diff changeset
84 output = stream.render(XMLSerializer)
71f3db26eecb Include processing instructions in serialized streams.
cmlenz
parents: 89
diff changeset
85 self.assertEqual('<?python x = 2?>', output)
71f3db26eecb Include processing instructions in serialized streams.
cmlenz
parents: 89
diff changeset
86
313
d72d842e1083 Handle expressions containing non-ASCII strings as arguments for `py:with`, `py:def`, and `py:for`.
cmlenz
parents: 280
diff changeset
87 def test_nested_default_namespaces(self):
314
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
88 stream = Stream([
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
89 (Stream.START_NS, ('', 'http://example.org/'), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
90 (Stream.START, (QName('div'), Attrs()), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
91 (Stream.TEXT, '\n ', (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
92 (Stream.START_NS, ('', 'http://example.org/'), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
93 (Stream.START, (QName('p'), Attrs()), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
94 (Stream.END, QName('p'), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
95 (Stream.END_NS, '', (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
96 (Stream.TEXT, '\n ', (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
97 (Stream.START_NS, ('', 'http://example.org/'), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
98 (Stream.START, (QName('p'), Attrs()), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
99 (Stream.END, QName('p'), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
100 (Stream.END_NS, '', (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
101 (Stream.TEXT, '\n ', (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
102 (Stream.END, QName('div'), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
103 (Stream.END_NS, '', (None, -1, -1))
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
104 ])
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
105 output = stream.render(XMLSerializer)
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
106 self.assertEqual("""<div xmlns="http://example.org/">
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
107 <p/>
313
d72d842e1083 Handle expressions containing non-ASCII strings as arguments for `py:with`, `py:def`, and `py:for`.
cmlenz
parents: 280
diff changeset
108 <p/>
d72d842e1083 Handle expressions containing non-ASCII strings as arguments for `py:with`, `py:def`, and `py:for`.
cmlenz
parents: 280
diff changeset
109 </div>""", output)
d72d842e1083 Handle expressions containing non-ASCII strings as arguments for `py:with`, `py:def`, and `py:for`.
cmlenz
parents: 280
diff changeset
110
d72d842e1083 Handle expressions containing non-ASCII strings as arguments for `py:with`, `py:def`, and `py:for`.
cmlenz
parents: 280
diff changeset
111 def test_nested_bound_namespaces(self):
314
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
112 stream = Stream([
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
113 (Stream.START_NS, ('x', 'http://example.org/'), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
114 (Stream.START, (QName('div'), Attrs()), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
115 (Stream.TEXT, '\n ', (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
116 (Stream.START_NS, ('x', 'http://example.org/'), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
117 (Stream.START, (QName('p'), Attrs()), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
118 (Stream.END, QName('p'), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
119 (Stream.END_NS, 'x', (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
120 (Stream.TEXT, '\n ', (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
121 (Stream.START_NS, ('x', 'http://example.org/'), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
122 (Stream.START, (QName('p'), Attrs()), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
123 (Stream.END, QName('p'), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
124 (Stream.END_NS, 'x', (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
125 (Stream.TEXT, '\n ', (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
126 (Stream.END, QName('div'), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
127 (Stream.END_NS, 'x', (None, -1, -1))
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
128 ])
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
129 output = stream.render(XMLSerializer)
313
d72d842e1083 Handle expressions containing non-ASCII strings as arguments for `py:with`, `py:def`, and `py:for`.
cmlenz
parents: 280
diff changeset
130 self.assertEqual("""<div xmlns:x="http://example.org/">
d72d842e1083 Handle expressions containing non-ASCII strings as arguments for `py:with`, `py:def`, and `py:for`.
cmlenz
parents: 280
diff changeset
131 <p/>
314
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
132 <p/>
313
d72d842e1083 Handle expressions containing non-ASCII strings as arguments for `py:with`, `py:def`, and `py:for`.
cmlenz
parents: 280
diff changeset
133 </div>""", output)
d72d842e1083 Handle expressions containing non-ASCII strings as arguments for `py:with`, `py:def`, and `py:for`.
cmlenz
parents: 280
diff changeset
134
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
135
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 105
diff changeset
136 class XHTMLSerializerTestCase(unittest.TestCase):
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 105
diff changeset
137
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 105
diff changeset
138 def test_textarea_whitespace(self):
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 105
diff changeset
139 content = '\nHey there. \n\n I am indented.\n'
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 123
diff changeset
140 stream = XML('<textarea name="foo">%s</textarea>' % content)
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 105
diff changeset
141 output = stream.render(XHTMLSerializer)
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 105
diff changeset
142 self.assertEqual('<textarea name="foo">%s</textarea>' % content, output)
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 105
diff changeset
143
347
ffa7dea6e8fd cspeedups branch: Merged [423:426/trunk].
cmlenz
parents: 314
diff changeset
144 def test_pre_whitespace(self):
ffa7dea6e8fd cspeedups branch: Merged [423:426/trunk].
cmlenz
parents: 314
diff changeset
145 content = '\nHey <em>there</em>. \n\n I am indented.\n'
ffa7dea6e8fd cspeedups branch: Merged [423:426/trunk].
cmlenz
parents: 314
diff changeset
146 stream = XML('<pre>%s</pre>' % content)
ffa7dea6e8fd cspeedups branch: Merged [423:426/trunk].
cmlenz
parents: 314
diff changeset
147 output = stream.render(XHTMLSerializer)
ffa7dea6e8fd cspeedups branch: Merged [423:426/trunk].
cmlenz
parents: 314
diff changeset
148 self.assertEqual('<pre>%s</pre>' % content, output)
ffa7dea6e8fd cspeedups branch: Merged [423:426/trunk].
cmlenz
parents: 314
diff changeset
149
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 105
diff changeset
150 def test_xml_space(self):
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 105
diff changeset
151 text = '<foo xml:space="preserve"> Do not mess \n\n with me </foo>'
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 123
diff changeset
152 output = XML(text).render(XHTMLSerializer)
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 105
diff changeset
153 self.assertEqual(text, output)
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 105
diff changeset
154
177
553866249cb0 * Minor fix for the XHTML serializer (the local namespace var got clobbered)
cmlenz
parents: 160
diff changeset
155 def test_empty_script(self):
178
ba7556e3a835 Fix bug in XHTML serialization: all elements were allowed to be written out as empty if the namespace was set.
cmlenz
parents: 177
diff changeset
156 text = """<html xmlns="http://www.w3.org/1999/xhtml">
ba7556e3a835 Fix bug in XHTML serialization: all elements were allowed to be written out as empty if the namespace was set.
cmlenz
parents: 177
diff changeset
157 <script src="foo.js" />
ba7556e3a835 Fix bug in XHTML serialization: all elements were allowed to be written out as empty if the namespace was set.
cmlenz
parents: 177
diff changeset
158 </html>"""
177
553866249cb0 * Minor fix for the XHTML serializer (the local namespace var got clobbered)
cmlenz
parents: 160
diff changeset
159 output = XML(text).render(XHTMLSerializer)
178
ba7556e3a835 Fix bug in XHTML serialization: all elements were allowed to be written out as empty if the namespace was set.
cmlenz
parents: 177
diff changeset
160 self.assertEqual("""<html xmlns="http://www.w3.org/1999/xhtml">
ba7556e3a835 Fix bug in XHTML serialization: all elements were allowed to be written out as empty if the namespace was set.
cmlenz
parents: 177
diff changeset
161 <script src="foo.js"></script>
ba7556e3a835 Fix bug in XHTML serialization: all elements were allowed to be written out as empty if the namespace was set.
cmlenz
parents: 177
diff changeset
162 </html>""", output)
177
553866249cb0 * Minor fix for the XHTML serializer (the local namespace var got clobbered)
cmlenz
parents: 160
diff changeset
163
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 123
diff changeset
164 def test_script_escaping(self):
143
3d4c214c979a CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
165 text = """<script>/*<![CDATA[*/
3d4c214c979a CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
166 if (1 < 2) { alert("Doh"); }
3d4c214c979a CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
167 /*]]>*/</script>"""
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 123
diff changeset
168 output = XML(text).render(XHTMLSerializer)
143
3d4c214c979a CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
169 self.assertEqual(text, output)
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 123
diff changeset
170
280
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
171 def test_script_escaping_with_namespace(self):
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
172 text = """<script xmlns="http://www.w3.org/1999/xhtml">/*<![CDATA[*/
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
173 if (1 < 2) { alert("Doh"); }
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
174 /*]]>*/</script>"""
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
175 output = XML(text).render(XHTMLSerializer)
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
176 self.assertEqual(text, output)
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
177
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 123
diff changeset
178 def test_style_escaping(self):
143
3d4c214c979a CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
179 text = """<style>/*<![CDATA[*/
3d4c214c979a CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
180 html > body { display: none; }
3d4c214c979a CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
181 /*]]>*/</style>"""
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 123
diff changeset
182 output = XML(text).render(XHTMLSerializer)
143
3d4c214c979a CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
183 self.assertEqual(text, output)
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 123
diff changeset
184
280
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
185 def test_style_escaping_with_namespace(self):
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
186 text = """<style xmlns="http://www.w3.org/1999/xhtml">/*<![CDATA[*/
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
187 html > body { display: none; }
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
188 /*]]>*/</style>"""
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
189 output = XML(text).render(XHTMLSerializer)
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
190 self.assertEqual(text, output)
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
191
158
3f23fafeef99 * Add test case for SVG content embedded in an HTML document.
cmlenz
parents: 143
diff changeset
192 def test_embedded_svg(self):
3f23fafeef99 * Add test case for SVG content embedded in an HTML document.
cmlenz
parents: 143
diff changeset
193 text = """<html xmlns="http://www.w3.org/1999/xhtml" xmlns:svg="http://www.w3.org/2000/svg">
3f23fafeef99 * Add test case for SVG content embedded in an HTML document.
cmlenz
parents: 143
diff changeset
194 <body>
3f23fafeef99 * Add test case for SVG content embedded in an HTML document.
cmlenz
parents: 143
diff changeset
195 <button>
3f23fafeef99 * Add test case for SVG content embedded in an HTML document.
cmlenz
parents: 143
diff changeset
196 <svg:svg width="600px" height="400px">
160
d19e8a2c549e Attribute order in parsed XML is now preserved.
cmlenz
parents: 158
diff changeset
197 <svg:polygon id="triangle" points="50,50 50,300 300,300" />
158
3f23fafeef99 * Add test case for SVG content embedded in an HTML document.
cmlenz
parents: 143
diff changeset
198 </svg:svg>
3f23fafeef99 * Add test case for SVG content embedded in an HTML document.
cmlenz
parents: 143
diff changeset
199 </button>
3f23fafeef99 * Add test case for SVG content embedded in an HTML document.
cmlenz
parents: 143
diff changeset
200 </body>
3f23fafeef99 * Add test case for SVG content embedded in an HTML document.
cmlenz
parents: 143
diff changeset
201 </html>"""
3f23fafeef99 * Add test case for SVG content embedded in an HTML document.
cmlenz
parents: 143
diff changeset
202 output = XML(text).render(XHTMLSerializer)
3f23fafeef99 * Add test case for SVG content embedded in an HTML document.
cmlenz
parents: 143
diff changeset
203 self.assertEqual(text, output)
3f23fafeef99 * Add test case for SVG content embedded in an HTML document.
cmlenz
parents: 143
diff changeset
204
177
553866249cb0 * Minor fix for the XHTML serializer (the local namespace var got clobbered)
cmlenz
parents: 160
diff changeset
205 def test_xhtml_namespace_prefix(self):
553866249cb0 * Minor fix for the XHTML serializer (the local namespace var got clobbered)
cmlenz
parents: 160
diff changeset
206 text = """<html:div xmlns:html="http://www.w3.org/1999/xhtml">
553866249cb0 * Minor fix for the XHTML serializer (the local namespace var got clobbered)
cmlenz
parents: 160
diff changeset
207 <html:strong>Hello</html:strong>
553866249cb0 * Minor fix for the XHTML serializer (the local namespace var got clobbered)
cmlenz
parents: 160
diff changeset
208 </html:div>"""
553866249cb0 * Minor fix for the XHTML serializer (the local namespace var got clobbered)
cmlenz
parents: 160
diff changeset
209 output = XML(text).render(XHTMLSerializer)
553866249cb0 * Minor fix for the XHTML serializer (the local namespace var got clobbered)
cmlenz
parents: 160
diff changeset
210 self.assertEqual(text, output)
553866249cb0 * Minor fix for the XHTML serializer (the local namespace var got clobbered)
cmlenz
parents: 160
diff changeset
211
313
d72d842e1083 Handle expressions containing non-ASCII strings as arguments for `py:with`, `py:def`, and `py:for`.
cmlenz
parents: 280
diff changeset
212 def test_nested_default_namespaces(self):
314
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
213 stream = Stream([
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
214 (Stream.START_NS, ('', 'http://example.org/'), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
215 (Stream.START, (QName('div'), Attrs()), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
216 (Stream.TEXT, '\n ', (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
217 (Stream.START_NS, ('', 'http://example.org/'), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
218 (Stream.START, (QName('p'), Attrs()), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
219 (Stream.END, QName('p'), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
220 (Stream.END_NS, '', (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
221 (Stream.TEXT, '\n ', (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
222 (Stream.START_NS, ('', 'http://example.org/'), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
223 (Stream.START, (QName('p'), Attrs()), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
224 (Stream.END, QName('p'), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
225 (Stream.END_NS, '', (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
226 (Stream.TEXT, '\n ', (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
227 (Stream.END, QName('div'), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
228 (Stream.END_NS, '', (None, -1, -1))
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
229 ])
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
230 output = stream.render(XHTMLSerializer)
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
231 self.assertEqual("""<div xmlns="http://example.org/">
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
232 <p></p>
313
d72d842e1083 Handle expressions containing non-ASCII strings as arguments for `py:with`, `py:def`, and `py:for`.
cmlenz
parents: 280
diff changeset
233 <p></p>
d72d842e1083 Handle expressions containing non-ASCII strings as arguments for `py:with`, `py:def`, and `py:for`.
cmlenz
parents: 280
diff changeset
234 </div>""", output)
d72d842e1083 Handle expressions containing non-ASCII strings as arguments for `py:with`, `py:def`, and `py:for`.
cmlenz
parents: 280
diff changeset
235
d72d842e1083 Handle expressions containing non-ASCII strings as arguments for `py:with`, `py:def`, and `py:for`.
cmlenz
parents: 280
diff changeset
236 def test_nested_bound_namespaces(self):
314
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
237 stream = Stream([
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
238 (Stream.START_NS, ('x', 'http://example.org/'), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
239 (Stream.START, (QName('div'), Attrs()), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
240 (Stream.TEXT, '\n ', (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
241 (Stream.START_NS, ('x', 'http://example.org/'), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
242 (Stream.START, (QName('p'), Attrs()), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
243 (Stream.END, QName('p'), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
244 (Stream.END_NS, 'x', (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
245 (Stream.TEXT, '\n ', (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
246 (Stream.START_NS, ('x', 'http://example.org/'), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
247 (Stream.START, (QName('p'), Attrs()), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
248 (Stream.END, QName('p'), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
249 (Stream.END_NS, 'x', (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
250 (Stream.TEXT, '\n ', (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
251 (Stream.END, QName('div'), (None, -1, -1)),
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
252 (Stream.END_NS, 'x', (None, -1, -1))
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
253 ])
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
254 output = stream.render(XHTMLSerializer)
313
d72d842e1083 Handle expressions containing non-ASCII strings as arguments for `py:with`, `py:def`, and `py:for`.
cmlenz
parents: 280
diff changeset
255 self.assertEqual("""<div xmlns:x="http://example.org/">
314
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
256 <p></p>
06a25d0962af Improved the unit tests for nested namespaces in serialization.
cmlenz
parents: 313
diff changeset
257 <p></p>
313
d72d842e1083 Handle expressions containing non-ASCII strings as arguments for `py:with`, `py:def`, and `py:for`.
cmlenz
parents: 280
diff changeset
258 </div>""", output)
d72d842e1083 Handle expressions containing non-ASCII strings as arguments for `py:with`, `py:def`, and `py:for`.
cmlenz
parents: 280
diff changeset
259
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 105
diff changeset
260
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 105
diff changeset
261 class HTMLSerializerTestCase(unittest.TestCase):
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 105
diff changeset
262
347
ffa7dea6e8fd cspeedups branch: Merged [423:426/trunk].
cmlenz
parents: 314
diff changeset
263 def test_textarea_whitespace(self):
ffa7dea6e8fd cspeedups branch: Merged [423:426/trunk].
cmlenz
parents: 314
diff changeset
264 content = '\nHey there. \n\n I am indented.\n'
ffa7dea6e8fd cspeedups branch: Merged [423:426/trunk].
cmlenz
parents: 314
diff changeset
265 stream = XML('<textarea name="foo">%s</textarea>' % content)
ffa7dea6e8fd cspeedups branch: Merged [423:426/trunk].
cmlenz
parents: 314
diff changeset
266 output = stream.render(HTMLSerializer)
ffa7dea6e8fd cspeedups branch: Merged [423:426/trunk].
cmlenz
parents: 314
diff changeset
267 self.assertEqual('<textarea name="foo">%s</textarea>' % content, output)
ffa7dea6e8fd cspeedups branch: Merged [423:426/trunk].
cmlenz
parents: 314
diff changeset
268
ffa7dea6e8fd cspeedups branch: Merged [423:426/trunk].
cmlenz
parents: 314
diff changeset
269 def test_pre_whitespace(self):
ffa7dea6e8fd cspeedups branch: Merged [423:426/trunk].
cmlenz
parents: 314
diff changeset
270 content = '\nHey <em>there</em>. \n\n I am indented.\n'
ffa7dea6e8fd cspeedups branch: Merged [423:426/trunk].
cmlenz
parents: 314
diff changeset
271 stream = XML('<pre>%s</pre>' % content)
ffa7dea6e8fd cspeedups branch: Merged [423:426/trunk].
cmlenz
parents: 314
diff changeset
272 output = stream.render(HTMLSerializer)
ffa7dea6e8fd cspeedups branch: Merged [423:426/trunk].
cmlenz
parents: 314
diff changeset
273 self.assertEqual('<pre>%s</pre>' % content, output)
ffa7dea6e8fd cspeedups branch: Merged [423:426/trunk].
cmlenz
parents: 314
diff changeset
274
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 105
diff changeset
275 def test_xml_space(self):
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 105
diff changeset
276 text = '<foo xml:space="preserve"> Do not mess \n\n with me </foo>'
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 123
diff changeset
277 output = XML(text).render(HTMLSerializer)
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 105
diff changeset
278 self.assertEqual('<foo> Do not mess \n\n with me </foo>', output)
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 105
diff changeset
279
177
553866249cb0 * Minor fix for the XHTML serializer (the local namespace var got clobbered)
cmlenz
parents: 160
diff changeset
280 def test_empty_script(self):
553866249cb0 * Minor fix for the XHTML serializer (the local namespace var got clobbered)
cmlenz
parents: 160
diff changeset
281 text = '<script src="foo.js" />'
553866249cb0 * Minor fix for the XHTML serializer (the local namespace var got clobbered)
cmlenz
parents: 160
diff changeset
282 output = XML(text).render(XHTMLSerializer)
553866249cb0 * Minor fix for the XHTML serializer (the local namespace var got clobbered)
cmlenz
parents: 160
diff changeset
283 self.assertEqual('<script src="foo.js"></script>', output)
553866249cb0 * Minor fix for the XHTML serializer (the local namespace var got clobbered)
cmlenz
parents: 160
diff changeset
284
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 123
diff changeset
285 def test_script_escaping(self):
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 123
diff changeset
286 text = '<script>if (1 &lt; 2) { alert("Doh"); }</script>'
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 123
diff changeset
287 output = XML(text).render(HTMLSerializer)
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 123
diff changeset
288 self.assertEqual('<script>if (1 < 2) { alert("Doh"); }</script>',
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 123
diff changeset
289 output)
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 123
diff changeset
290
280
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
291 def test_script_escaping_with_namespace(self):
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
292 text = """<script xmlns="http://www.w3.org/1999/xhtml">
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
293 if (1 &lt; 2) { alert("Doh"); }
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
294 </script>"""
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
295 output = XML(text).render(HTMLSerializer)
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
296 self.assertEqual("""<script>
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
297 if (1 < 2) { alert("Doh"); }
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
298 </script>""", output)
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
299
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 123
diff changeset
300 def test_style_escaping(self):
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 123
diff changeset
301 text = '<style>html &gt; body { display: none; }</style>'
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 123
diff changeset
302 output = XML(text).render(HTMLSerializer)
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 123
diff changeset
303 self.assertEqual('<style>html > body { display: none; }</style>',
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 123
diff changeset
304 output)
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 123
diff changeset
305
280
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
306 def test_style_escaping_with_namespace(self):
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
307 text = """<style xmlns="http://www.w3.org/1999/xhtml">
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
308 html &gt; body { display: none; }
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
309 </style>"""
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
310 output = XML(text).render(HTMLSerializer)
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
311 self.assertEqual("""<style>
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
312 html > body { display: none; }
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
313 </style>""", output)
ce848f7c41f1 The content of `<script>` and `<style>` elements is no longer escaped when serializing to HTML but declaring the XHTML namespace in the template.
cmlenz
parents: 230
diff changeset
314
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 105
diff changeset
315
212
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 178
diff changeset
316 class EmptyTagFilterTestCase(unittest.TestCase):
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 178
diff changeset
317
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 178
diff changeset
318 def test_empty(self):
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 178
diff changeset
319 stream = XML('<elem></elem>') | EmptyTagFilter()
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 178
diff changeset
320 self.assertEqual([EmptyTagFilter.EMPTY], [ev[0] for ev in stream])
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 178
diff changeset
321
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 178
diff changeset
322 def test_text_content(self):
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 178
diff changeset
323 stream = XML('<elem>foo</elem>') | EmptyTagFilter()
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 178
diff changeset
324 self.assertEqual([Stream.START, Stream.TEXT, Stream.END],
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 178
diff changeset
325 [ev[0] for ev in stream])
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 178
diff changeset
326
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 178
diff changeset
327 def test_elem_content(self):
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 178
diff changeset
328 stream = XML('<elem><sub /><sub /></elem>') | EmptyTagFilter()
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 178
diff changeset
329 self.assertEqual([Stream.START, EmptyTagFilter.EMPTY,
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 178
diff changeset
330 EmptyTagFilter.EMPTY, Stream.END],
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 178
diff changeset
331 [ev[0] for ev in stream])
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 178
diff changeset
332
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 178
diff changeset
333
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
334 def suite():
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
335 suite = unittest.TestSuite()
85
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
336 suite.addTest(unittest.makeSuite(XMLSerializerTestCase, 'test'))
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 105
diff changeset
337 suite.addTest(unittest.makeSuite(XHTMLSerializerTestCase, 'test'))
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 105
diff changeset
338 suite.addTest(unittest.makeSuite(HTMLSerializerTestCase, 'test'))
212
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 178
diff changeset
339 suite.addTest(unittest.makeSuite(EmptyTagFilterTestCase, 'test'))
85
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 66
diff changeset
340 suite.addTest(doctest.DocTestSuite(XMLSerializer.__module__))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
341 return suite
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
342
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
343 if __name__ == '__main__':
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
344 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software