| [2] | 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | # |
|---|
| [865] | 3 | # Copyright (C) 2006-2008 Edgewall Software |
|---|
| [2] | 4 | # All rights reserved. |
|---|
| 5 | # |
|---|
| 6 | # This software is licensed as described in the file COPYING, which |
|---|
| 7 | # you should have received as part of this distribution. The terms |
|---|
| [287] | 8 | # are also available at http://genshi.edgewall.org/wiki/License. |
|---|
| [2] | 9 | # |
|---|
| 10 | # This software consists of voluntary contributions made by many |
|---|
| 11 | # individuals. For the exact contribution history, see the revision |
|---|
| [287] | 12 | # history and logs, available at http://genshi.edgewall.org/log/. |
|---|
| [2] | 13 | |
|---|
| 14 | import doctest |
|---|
| 15 | import unittest |
|---|
| 16 | import sys |
|---|
| 17 | |
|---|
| [1164] | 18 | from genshi.core import Attrs, Markup, QName, Stream |
|---|
| [287] | 19 | from genshi.input import HTML, XML |
|---|
| 20 | from genshi.output import DocType, XMLSerializer, XHTMLSerializer, \ |
|---|
| [263] | 21 | HTMLSerializer, EmptyTagFilter |
|---|
| [2] | 22 | |
|---|
| [90] | 23 | |
|---|
| 24 | class XMLSerializerTestCase(unittest.TestCase): |
|---|
| 25 | |
|---|
| [853] | 26 | def test_with_xml_decl(self): |
|---|
| [787] | 27 | stream = Stream([(Stream.XML_DECL, ('1.0', None, -1), (None, -1, -1))]) |
|---|
| [1085] | 28 | output = stream.render(XMLSerializer, doctype='xhtml', encoding=None) |
|---|
| [787] | 29 | self.assertEqual('<?xml version="1.0"?>\n' |
|---|
| 30 | '<!DOCTYPE html PUBLIC ' |
|---|
| 31 | '"-//W3C//DTD XHTML 1.0 Strict//EN" ' |
|---|
| 32 | '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n', |
|---|
| 33 | output) |
|---|
| 34 | |
|---|
| [90] | 35 | def test_doctype_in_stream(self): |
|---|
| [388] | 36 | stream = Stream([(Stream.DOCTYPE, DocType.HTML_STRICT, (None, -1, -1))]) |
|---|
| [1085] | 37 | output = stream.render(XMLSerializer, encoding=None) |
|---|
| [90] | 38 | self.assertEqual('<!DOCTYPE html PUBLIC ' |
|---|
| 39 | '"-//W3C//DTD HTML 4.01//EN" ' |
|---|
| 40 | '"http://www.w3.org/TR/html4/strict.dtd">\n', |
|---|
| 41 | output) |
|---|
| 42 | |
|---|
| 43 | def test_doctype_in_stream_no_sysid(self): |
|---|
| 44 | stream = Stream([(Stream.DOCTYPE, |
|---|
| 45 | ('html', '-//W3C//DTD HTML 4.01//EN', None), |
|---|
| [388] | 46 | (None, -1, -1))]) |
|---|
| [1085] | 47 | output = stream.render(XMLSerializer, encoding=None) |
|---|
| [90] | 48 | self.assertEqual('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">\n', |
|---|
| 49 | output) |
|---|
| 50 | |
|---|
| 51 | def test_doctype_in_stream_no_pubid(self): |
|---|
| [388] | 52 | stream = Stream([ |
|---|
| 53 | (Stream.DOCTYPE, |
|---|
| 54 | ('html', None, 'http://www.w3.org/TR/html4/strict.dtd'), |
|---|
| 55 | (None, -1, -1)) |
|---|
| 56 | ]) |
|---|
| [1085] | 57 | output = stream.render(XMLSerializer, encoding=None) |
|---|
| [90] | 58 | self.assertEqual('<!DOCTYPE html SYSTEM ' |
|---|
| 59 | '"http://www.w3.org/TR/html4/strict.dtd">\n', |
|---|
| 60 | output) |
|---|
| 61 | |
|---|
| 62 | def test_doctype_in_stream_no_pubid_or_sysid(self): |
|---|
| 63 | stream = Stream([(Stream.DOCTYPE, ('html', None, None), |
|---|
| [388] | 64 | (None, -1, -1))]) |
|---|
| [1085] | 65 | output = stream.render(XMLSerializer, encoding=None) |
|---|
| [90] | 66 | self.assertEqual('<!DOCTYPE html>\n', output) |
|---|
| 67 | |
|---|
| 68 | def test_serializer_doctype(self): |
|---|
| 69 | stream = Stream([]) |
|---|
| [1085] | 70 | output = stream.render(XMLSerializer, doctype=DocType.HTML_STRICT, |
|---|
| 71 | encoding=None) |
|---|
| [90] | 72 | self.assertEqual('<!DOCTYPE html PUBLIC ' |
|---|
| 73 | '"-//W3C//DTD HTML 4.01//EN" ' |
|---|
| 74 | '"http://www.w3.org/TR/html4/strict.dtd">\n', |
|---|
| 75 | output) |
|---|
| 76 | |
|---|
| 77 | def test_doctype_one_and_only(self): |
|---|
| [388] | 78 | stream = Stream([ |
|---|
| 79 | (Stream.DOCTYPE, ('html', None, None), (None, -1, -1)) |
|---|
| 80 | ]) |
|---|
| [1085] | 81 | output = stream.render(XMLSerializer, doctype=DocType.HTML_STRICT, |
|---|
| 82 | encoding=None) |
|---|
| [90] | 83 | self.assertEqual('<!DOCTYPE html PUBLIC ' |
|---|
| 84 | '"-//W3C//DTD HTML 4.01//EN" ' |
|---|
| 85 | '"http://www.w3.org/TR/html4/strict.dtd">\n', |
|---|
| 86 | output) |
|---|
| 87 | |
|---|
| [94] | 88 | def test_comment(self): |
|---|
| [388] | 89 | stream = Stream([(Stream.COMMENT, 'foo bar', (None, -1, -1))]) |
|---|
| [1085] | 90 | output = stream.render(XMLSerializer, encoding=None) |
|---|
| [94] | 91 | self.assertEqual('<!--foo bar-->', output) |
|---|
| [90] | 92 | |
|---|
| [122] | 93 | def test_processing_instruction(self): |
|---|
| [388] | 94 | stream = Stream([(Stream.PI, ('python', 'x = 2'), (None, -1, -1))]) |
|---|
| [1085] | 95 | output = stream.render(XMLSerializer, encoding=None) |
|---|
| [122] | 96 | self.assertEqual('<?python x = 2?>', output) |
|---|
| [94] | 97 | |
|---|
| [387] | 98 | def test_nested_default_namespaces(self): |
|---|
| [388] | 99 | stream = Stream([ |
|---|
| 100 | (Stream.START_NS, ('', 'http://example.org/'), (None, -1, -1)), |
|---|
| [494] | 101 | (Stream.START, (QName('http://example.org/}div'), Attrs()), (None, -1, -1)), |
|---|
| [388] | 102 | (Stream.TEXT, '\n ', (None, -1, -1)), |
|---|
| 103 | (Stream.START_NS, ('', 'http://example.org/'), (None, -1, -1)), |
|---|
| [494] | 104 | (Stream.START, (QName('http://example.org/}p'), Attrs()), (None, -1, -1)), |
|---|
| 105 | (Stream.END, QName('http://example.org/}p'), (None, -1, -1)), |
|---|
| [388] | 106 | (Stream.END_NS, '', (None, -1, -1)), |
|---|
| 107 | (Stream.TEXT, '\n ', (None, -1, -1)), |
|---|
| 108 | (Stream.START_NS, ('', 'http://example.org/'), (None, -1, -1)), |
|---|
| [494] | 109 | (Stream.START, (QName('http://example.org/}p'), Attrs()), (None, -1, -1)), |
|---|
| 110 | (Stream.END, QName('http://example.org/}p'), (None, -1, -1)), |
|---|
| [388] | 111 | (Stream.END_NS, '', (None, -1, -1)), |
|---|
| 112 | (Stream.TEXT, '\n ', (None, -1, -1)), |
|---|
| [494] | 113 | (Stream.END, QName('http://example.org/}div'), (None, -1, -1)), |
|---|
| [388] | 114 | (Stream.END_NS, '', (None, -1, -1)) |
|---|
| 115 | ]) |
|---|
| [1085] | 116 | output = stream.render(XMLSerializer, encoding=None) |
|---|
| [388] | 117 | self.assertEqual("""<div xmlns="http://example.org/"> |
|---|
| [387] | 118 | <p/> |
|---|
| [388] | 119 | <p/> |
|---|
| [387] | 120 | </div>""", output) |
|---|
| [122] | 121 | |
|---|
| [387] | 122 | def test_nested_bound_namespaces(self): |
|---|
| [388] | 123 | stream = Stream([ |
|---|
| 124 | (Stream.START_NS, ('x', 'http://example.org/'), (None, -1, -1)), |
|---|
| [494] | 125 | (Stream.START, (QName('http://example.org/}div'), Attrs()), (None, -1, -1)), |
|---|
| 126 | (Stream.TEXT, '\n ', (None, -1, -1)), |
|---|
| 127 | (Stream.START_NS, ('x', 'http://example.org/'), (None, -1, -1)), |
|---|
| 128 | (Stream.START, (QName('http://example.org/}p'), Attrs()), (None, -1, -1)), |
|---|
| 129 | (Stream.END, QName('http://example.org/}p'), (None, -1, -1)), |
|---|
| 130 | (Stream.END_NS, 'x', (None, -1, -1)), |
|---|
| 131 | (Stream.TEXT, '\n ', (None, -1, -1)), |
|---|
| 132 | (Stream.START_NS, ('x', 'http://example.org/'), (None, -1, -1)), |
|---|
| 133 | (Stream.START, (QName('http://example.org/}p'), Attrs()), (None, -1, -1)), |
|---|
| 134 | (Stream.END, QName('http://example.org/}p'), (None, -1, -1)), |
|---|
| 135 | (Stream.END_NS, 'x', (None, -1, -1)), |
|---|
| 136 | (Stream.TEXT, '\n ', (None, -1, -1)), |
|---|
| 137 | (Stream.END, QName('http://example.org/}div'), (None, -1, -1)), |
|---|
| 138 | (Stream.END_NS, 'x', (None, -1, -1)) |
|---|
| 139 | ]) |
|---|
| [1085] | 140 | output = stream.render(XMLSerializer, encoding=None) |
|---|
| [494] | 141 | self.assertEqual("""<x:div xmlns:x="http://example.org/"> |
|---|
| 142 | <x:p/> |
|---|
| 143 | <x:p/> |
|---|
| 144 | </x:div>""", output) |
|---|
| 145 | |
|---|
| 146 | def test_multiple_default_namespaces(self): |
|---|
| 147 | stream = Stream([ |
|---|
| [388] | 148 | (Stream.START, (QName('div'), Attrs()), (None, -1, -1)), |
|---|
| 149 | (Stream.TEXT, '\n ', (None, -1, -1)), |
|---|
| [494] | 150 | (Stream.START_NS, ('', 'http://example.org/'), (None, -1, -1)), |
|---|
| 151 | (Stream.START, (QName('http://example.org/}p'), Attrs()), (None, -1, -1)), |
|---|
| 152 | (Stream.END, QName('http://example.org/}p'), (None, -1, -1)), |
|---|
| 153 | (Stream.END_NS, '', (None, -1, -1)), |
|---|
| 154 | (Stream.TEXT, '\n ', (None, -1, -1)), |
|---|
| 155 | (Stream.START_NS, ('', 'http://example.org/'), (None, -1, -1)), |
|---|
| 156 | (Stream.START, (QName('http://example.org/}p'), Attrs()), (None, -1, -1)), |
|---|
| 157 | (Stream.END, QName('http://example.org/}p'), (None, -1, -1)), |
|---|
| 158 | (Stream.END_NS, '', (None, -1, -1)), |
|---|
| 159 | (Stream.TEXT, '\n ', (None, -1, -1)), |
|---|
| 160 | (Stream.END, QName('div'), (None, -1, -1)), |
|---|
| 161 | ]) |
|---|
| [1085] | 162 | output = stream.render(XMLSerializer, encoding=None) |
|---|
| [494] | 163 | self.assertEqual("""<div> |
|---|
| 164 | <p xmlns="http://example.org/"/> |
|---|
| 165 | <p xmlns="http://example.org/"/> |
|---|
| 166 | </div>""", output) |
|---|
| 167 | |
|---|
| 168 | def test_multiple_bound_namespaces(self): |
|---|
| 169 | stream = Stream([ |
|---|
| 170 | (Stream.START, (QName('div'), Attrs()), (None, -1, -1)), |
|---|
| 171 | (Stream.TEXT, '\n ', (None, -1, -1)), |
|---|
| [388] | 172 | (Stream.START_NS, ('x', 'http://example.org/'), (None, -1, -1)), |
|---|
| [494] | 173 | (Stream.START, (QName('http://example.org/}p'), Attrs()), (None, -1, -1)), |
|---|
| 174 | (Stream.END, QName('http://example.org/}p'), (None, -1, -1)), |
|---|
| [388] | 175 | (Stream.END_NS, 'x', (None, -1, -1)), |
|---|
| 176 | (Stream.TEXT, '\n ', (None, -1, -1)), |
|---|
| 177 | (Stream.START_NS, ('x', 'http://example.org/'), (None, -1, -1)), |
|---|
| [494] | 178 | (Stream.START, (QName('http://example.org/}p'), Attrs()), (None, -1, -1)), |
|---|
| 179 | (Stream.END, QName('http://example.org/}p'), (None, -1, -1)), |
|---|
| [388] | 180 | (Stream.END_NS, 'x', (None, -1, -1)), |
|---|
| 181 | (Stream.TEXT, '\n ', (None, -1, -1)), |
|---|
| 182 | (Stream.END, QName('div'), (None, -1, -1)), |
|---|
| 183 | ]) |
|---|
| [1085] | 184 | output = stream.render(XMLSerializer, encoding=None) |
|---|
| [494] | 185 | self.assertEqual("""<div> |
|---|
| 186 | <x:p xmlns:x="http://example.org/"/> |
|---|
| 187 | <x:p xmlns:x="http://example.org/"/> |
|---|
| [387] | 188 | </div>""", output) |
|---|
| 189 | |
|---|
| [502] | 190 | def test_atom_with_xhtml(self): |
|---|
| 191 | text = """<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"> |
|---|
| 192 | <id>urn:uuid:c60843aa-0da8-4fa6-bbe5-98007bc6774e</id> |
|---|
| 193 | <updated>2007-01-28T11:36:02.807108-06:00</updated> |
|---|
| 194 | <title type="xhtml"> |
|---|
| 195 | <div xmlns="http://www.w3.org/1999/xhtml">Example</div> |
|---|
| 196 | </title> |
|---|
| 197 | <subtitle type="xhtml"> |
|---|
| 198 | <div xmlns="http://www.w3.org/1999/xhtml">Bla bla bla</div> |
|---|
| 199 | </subtitle> |
|---|
| 200 | <icon/> |
|---|
| 201 | </feed>""" |
|---|
| [1085] | 202 | output = XML(text).render(XMLSerializer, encoding=None) |
|---|
| [502] | 203 | self.assertEqual(text, output) |
|---|
| [387] | 204 | |
|---|
| [502] | 205 | |
|---|
| [154] | 206 | class XHTMLSerializerTestCase(unittest.TestCase): |
|---|
| 207 | |
|---|
| [853] | 208 | def test_xml_decl_dropped(self): |
|---|
| 209 | stream = Stream([(Stream.XML_DECL, ('1.0', None, -1), (None, -1, -1))]) |
|---|
| [1085] | 210 | output = stream.render(XHTMLSerializer, doctype='xhtml', encoding=None) |
|---|
| [853] | 211 | self.assertEqual('<!DOCTYPE html PUBLIC ' |
|---|
| 212 | '"-//W3C//DTD XHTML 1.0 Strict//EN" ' |
|---|
| 213 | '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n', |
|---|
| 214 | output) |
|---|
| 215 | |
|---|
| 216 | def test_xml_decl_included(self): |
|---|
| 217 | stream = Stream([(Stream.XML_DECL, ('1.0', None, -1), (None, -1, -1))]) |
|---|
| 218 | output = stream.render(XHTMLSerializer, doctype='xhtml', |
|---|
| [1085] | 219 | drop_xml_decl=False, encoding=None) |
|---|
| [853] | 220 | self.assertEqual('<?xml version="1.0"?>\n' |
|---|
| 221 | '<!DOCTYPE html PUBLIC ' |
|---|
| 222 | '"-//W3C//DTD XHTML 1.0 Strict//EN" ' |
|---|
| 223 | '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n', |
|---|
| 224 | output) |
|---|
| 225 | |
|---|
| [628] | 226 | def test_xml_lang(self): |
|---|
| 227 | text = '<p xml:lang="en">English text</p>' |
|---|
| [1085] | 228 | output = XML(text).render(XHTMLSerializer, encoding=None) |
|---|
| [628] | 229 | self.assertEqual('<p lang="en" xml:lang="en">English text</p>', output) |
|---|
| 230 | |
|---|
| 231 | def test_xml_lang_nodup(self): |
|---|
| 232 | text = '<p xml:lang="en" lang="en">English text</p>' |
|---|
| [1085] | 233 | output = XML(text).render(XHTMLSerializer, encoding=None) |
|---|
| [628] | 234 | self.assertEqual('<p xml:lang="en" lang="en">English text</p>', output) |
|---|
| 235 | |
|---|
| [154] | 236 | def test_textarea_whitespace(self): |
|---|
| 237 | content = '\nHey there. \n\n I am indented.\n' |
|---|
| [181] | 238 | stream = XML('<textarea name="foo">%s</textarea>' % content) |
|---|
| [1085] | 239 | output = stream.render(XHTMLSerializer, encoding=None) |
|---|
| [154] | 240 | self.assertEqual('<textarea name="foo">%s</textarea>' % content, output) |
|---|
| 241 | |
|---|
| [425] | 242 | def test_pre_whitespace(self): |
|---|
| 243 | content = '\nHey <em>there</em>. \n\n I am indented.\n' |
|---|
| 244 | stream = XML('<pre>%s</pre>' % content) |
|---|
| [1085] | 245 | output = stream.render(XHTMLSerializer, encoding=None) |
|---|
| [425] | 246 | self.assertEqual('<pre>%s</pre>' % content, output) |
|---|
| 247 | |
|---|
| [154] | 248 | def test_xml_space(self): |
|---|
| 249 | text = '<foo xml:space="preserve"> Do not mess \n\n with me </foo>' |
|---|
| [1085] | 250 | output = XML(text).render(XHTMLSerializer, encoding=None) |
|---|
| [805] | 251 | self.assertEqual('<foo> Do not mess \n\n with me </foo>', output) |
|---|
| [154] | 252 | |
|---|
| [222] | 253 | def test_empty_script(self): |
|---|
| [223] | 254 | text = """<html xmlns="http://www.w3.org/1999/xhtml"> |
|---|
| 255 | <script src="foo.js" /> |
|---|
| 256 | </html>""" |
|---|
| [1085] | 257 | output = XML(text).render(XHTMLSerializer, encoding=None) |
|---|
| [223] | 258 | self.assertEqual("""<html xmlns="http://www.w3.org/1999/xhtml"> |
|---|
| 259 | <script src="foo.js"></script> |
|---|
| 260 | </html>""", output) |
|---|
| [222] | 261 | |
|---|
| [181] | 262 | def test_script_escaping(self): |
|---|
| [184] | 263 | text = """<script>/*<![CDATA[*/ |
|---|
| 264 | if (1 < 2) { alert("Doh"); } |
|---|
| 265 | /*]]>*/</script>""" |
|---|
| [1085] | 266 | output = XML(text).render(XHTMLSerializer, encoding=None) |
|---|
| [184] | 267 | self.assertEqual(text, output) |
|---|
| [154] | 268 | |
|---|
| [345] | 269 | def test_script_escaping_with_namespace(self): |
|---|
| 270 | text = """<script xmlns="http://www.w3.org/1999/xhtml">/*<![CDATA[*/ |
|---|
| 271 | if (1 < 2) { alert("Doh"); } |
|---|
| 272 | /*]]>*/</script>""" |
|---|
| [1085] | 273 | output = XML(text).render(XHTMLSerializer, encoding=None) |
|---|
| [345] | 274 | self.assertEqual(text, output) |
|---|
| 275 | |
|---|
| [181] | 276 | def test_style_escaping(self): |
|---|
| [184] | 277 | text = """<style>/*<![CDATA[*/ |
|---|
| 278 | html > body { display: none; } |
|---|
| 279 | /*]]>*/</style>""" |
|---|
| [1085] | 280 | output = XML(text).render(XHTMLSerializer, encoding=None) |
|---|
| [184] | 281 | self.assertEqual(text, output) |
|---|
| [181] | 282 | |
|---|
| [345] | 283 | def test_style_escaping_with_namespace(self): |
|---|
| 284 | text = """<style xmlns="http://www.w3.org/1999/xhtml">/*<![CDATA[*/ |
|---|
| 285 | html > body { display: none; } |
|---|
| 286 | /*]]>*/</style>""" |
|---|
| [1085] | 287 | output = XML(text).render(XHTMLSerializer, encoding=None) |
|---|
| [345] | 288 | self.assertEqual(text, output) |
|---|
| 289 | |
|---|
| [200] | 290 | def test_embedded_svg(self): |
|---|
| 291 | text = """<html xmlns="http://www.w3.org/1999/xhtml" xmlns:svg="http://www.w3.org/2000/svg"> |
|---|
| 292 | <body> |
|---|
| 293 | <button> |
|---|
| 294 | <svg:svg width="600px" height="400px"> |
|---|
| [502] | 295 | <svg:polygon id="triangle" points="50,50 50,300 300,300"></svg:polygon> |
|---|
| [200] | 296 | </svg:svg> |
|---|
| 297 | </button> |
|---|
| 298 | </body> |
|---|
| 299 | </html>""" |
|---|
| [1085] | 300 | output = XML(text).render(XHTMLSerializer, encoding=None) |
|---|
| [200] | 301 | self.assertEqual(text, output) |
|---|
| [181] | 302 | |
|---|
| [222] | 303 | def test_xhtml_namespace_prefix(self): |
|---|
| [502] | 304 | text = """<div xmlns="http://www.w3.org/1999/xhtml"> |
|---|
| 305 | <strong>Hello</strong> |
|---|
| 306 | </div>""" |
|---|
| [1085] | 307 | output = XML(text).render(XHTMLSerializer, encoding=None) |
|---|
| [222] | 308 | self.assertEqual(text, output) |
|---|
| [200] | 309 | |
|---|
| [387] | 310 | def test_nested_default_namespaces(self): |
|---|
| [388] | 311 | stream = Stream([ |
|---|
| 312 | (Stream.START_NS, ('', 'http://example.org/'), (None, -1, -1)), |
|---|
| 313 | (Stream.START, (QName('div'), Attrs()), (None, -1, -1)), |
|---|
| 314 | (Stream.TEXT, '\n ', (None, -1, -1)), |
|---|
| 315 | (Stream.START_NS, ('', 'http://example.org/'), (None, -1, -1)), |
|---|
| 316 | (Stream.START, (QName('p'), Attrs()), (None, -1, -1)), |
|---|
| 317 | (Stream.END, QName('p'), (None, -1, -1)), |
|---|
| 318 | (Stream.END_NS, '', (None, -1, -1)), |
|---|
| 319 | (Stream.TEXT, '\n ', (None, -1, -1)), |
|---|
| 320 | (Stream.START_NS, ('', 'http://example.org/'), (None, -1, -1)), |
|---|
| 321 | (Stream.START, (QName('p'), Attrs()), (None, -1, -1)), |
|---|
| 322 | (Stream.END, QName('p'), (None, -1, -1)), |
|---|
| 323 | (Stream.END_NS, '', (None, -1, -1)), |
|---|
| 324 | (Stream.TEXT, '\n ', (None, -1, -1)), |
|---|
| 325 | (Stream.END, QName('div'), (None, -1, -1)), |
|---|
| 326 | (Stream.END_NS, '', (None, -1, -1)) |
|---|
| 327 | ]) |
|---|
| [1085] | 328 | output = stream.render(XHTMLSerializer, encoding=None) |
|---|
| [388] | 329 | self.assertEqual("""<div xmlns="http://example.org/"> |
|---|
| [387] | 330 | <p></p> |
|---|
| [388] | 331 | <p></p> |
|---|
| [387] | 332 | </div>""", output) |
|---|
| [222] | 333 | |
|---|
| [387] | 334 | def test_nested_bound_namespaces(self): |
|---|
| [388] | 335 | stream = Stream([ |
|---|
| 336 | (Stream.START_NS, ('x', 'http://example.org/'), (None, -1, -1)), |
|---|
| 337 | (Stream.START, (QName('div'), Attrs()), (None, -1, -1)), |
|---|
| 338 | (Stream.TEXT, '\n ', (None, -1, -1)), |
|---|
| 339 | (Stream.START_NS, ('x', 'http://example.org/'), (None, -1, -1)), |
|---|
| 340 | (Stream.START, (QName('p'), Attrs()), (None, -1, -1)), |
|---|
| 341 | (Stream.END, QName('p'), (None, -1, -1)), |
|---|
| 342 | (Stream.END_NS, 'x', (None, -1, -1)), |
|---|
| 343 | (Stream.TEXT, '\n ', (None, -1, -1)), |
|---|
| 344 | (Stream.START_NS, ('x', 'http://example.org/'), (None, -1, -1)), |
|---|
| 345 | (Stream.START, (QName('p'), Attrs()), (None, -1, -1)), |
|---|
| 346 | (Stream.END, QName('p'), (None, -1, -1)), |
|---|
| 347 | (Stream.END_NS, 'x', (None, -1, -1)), |
|---|
| 348 | (Stream.TEXT, '\n ', (None, -1, -1)), |
|---|
| 349 | (Stream.END, QName('div'), (None, -1, -1)), |
|---|
| 350 | (Stream.END_NS, 'x', (None, -1, -1)) |
|---|
| 351 | ]) |
|---|
| [1085] | 352 | output = stream.render(XHTMLSerializer, encoding=None) |
|---|
| [387] | 353 | self.assertEqual("""<div xmlns:x="http://example.org/"> |
|---|
| [388] | 354 | <p></p> |
|---|
| 355 | <p></p> |
|---|
| [387] | 356 | </div>""", output) |
|---|
| 357 | |
|---|
| [540] | 358 | def test_html5_doctype(self): |
|---|
| [1157] | 359 | stream = HTML(u'<html></html>') |
|---|
| [1085] | 360 | output = stream.render(XHTMLSerializer, doctype=DocType.HTML5, |
|---|
| 361 | encoding=None) |
|---|
| [540] | 362 | self.assertEqual('<!DOCTYPE html>\n<html></html>', output) |
|---|
| [387] | 363 | |
|---|
| [1164] | 364 | def test_ignorable_space(self): |
|---|
| 365 | text = '<foo> Mess \n\n\n with me! </foo>' |
|---|
| 366 | output = XML(text).render(XMLSerializer, encoding=None) |
|---|
| 367 | self.assertEqual('<foo> Mess\n with me! </foo>', output) |
|---|
| [540] | 368 | |
|---|
| [1164] | 369 | def test_cache_markup(self): |
|---|
| 370 | loc = (None, -1, -1) |
|---|
| 371 | stream = Stream([(Stream.START, (QName('foo'), Attrs()), loc), |
|---|
| 372 | (Stream.TEXT, u'…', loc), |
|---|
| 373 | (Stream.END, QName('foo'), loc), |
|---|
| 374 | (Stream.START, (QName('bar'), Attrs()), loc), |
|---|
| 375 | (Stream.TEXT, Markup('…'), loc), |
|---|
| 376 | (Stream.END, QName('bar'), loc)]) |
|---|
| 377 | output = stream.render(XMLSerializer, encoding=None, |
|---|
| 378 | strip_whitespace=False) |
|---|
| 379 | self.assertEqual('<foo>&hellip;</foo><bar>…</bar>', output) |
|---|
| 380 | |
|---|
| 381 | |
|---|
| [154] | 382 | class HTMLSerializerTestCase(unittest.TestCase): |
|---|
| 383 | |
|---|
| [628] | 384 | def test_xml_lang(self): |
|---|
| 385 | text = '<p xml:lang="en">English text</p>' |
|---|
| [1085] | 386 | output = XML(text).render(HTMLSerializer, encoding=None) |
|---|
| [628] | 387 | self.assertEqual('<p lang="en">English text</p>', output) |
|---|
| 388 | |
|---|
| 389 | def test_xml_lang_nodup(self): |
|---|
| 390 | text = '<p lang="en" xml:lang="en">English text</p>' |
|---|
| [1085] | 391 | output = XML(text).render(HTMLSerializer, encoding=None) |
|---|
| [628] | 392 | self.assertEqual('<p lang="en">English text</p>', output) |
|---|
| 393 | |
|---|
| [425] | 394 | def test_textarea_whitespace(self): |
|---|
| 395 | content = '\nHey there. \n\n I am indented.\n' |
|---|
| 396 | stream = XML('<textarea name="foo">%s</textarea>' % content) |
|---|
| [1085] | 397 | output = stream.render(HTMLSerializer, encoding=None) |
|---|
| [425] | 398 | self.assertEqual('<textarea name="foo">%s</textarea>' % content, output) |
|---|
| 399 | |
|---|
| 400 | def test_pre_whitespace(self): |
|---|
| 401 | content = '\nHey <em>there</em>. \n\n I am indented.\n' |
|---|
| 402 | stream = XML('<pre>%s</pre>' % content) |
|---|
| [1085] | 403 | output = stream.render(HTMLSerializer, encoding=None) |
|---|
| [425] | 404 | self.assertEqual('<pre>%s</pre>' % content, output) |
|---|
| 405 | |
|---|
| [154] | 406 | def test_xml_space(self): |
|---|
| 407 | text = '<foo xml:space="preserve"> Do not mess \n\n with me </foo>' |
|---|
| [1085] | 408 | output = XML(text).render(HTMLSerializer, encoding=None) |
|---|
| [154] | 409 | self.assertEqual('<foo> Do not mess \n\n with me </foo>', output) |
|---|
| 410 | |
|---|
| [222] | 411 | def test_empty_script(self): |
|---|
| 412 | text = '<script src="foo.js" />' |
|---|
| [1085] | 413 | output = XML(text).render(HTMLSerializer, encoding=None) |
|---|
| [222] | 414 | self.assertEqual('<script src="foo.js"></script>', output) |
|---|
| 415 | |
|---|
| [181] | 416 | def test_script_escaping(self): |
|---|
| 417 | text = '<script>if (1 < 2) { alert("Doh"); }</script>' |
|---|
| [1085] | 418 | output = XML(text).render(HTMLSerializer, encoding=None) |
|---|
| [181] | 419 | self.assertEqual('<script>if (1 < 2) { alert("Doh"); }</script>', |
|---|
| 420 | output) |
|---|
| [154] | 421 | |
|---|
| [345] | 422 | def test_script_escaping_with_namespace(self): |
|---|
| 423 | text = """<script xmlns="http://www.w3.org/1999/xhtml"> |
|---|
| 424 | if (1 < 2) { alert("Doh"); } |
|---|
| 425 | </script>""" |
|---|
| [1085] | 426 | output = XML(text).render(HTMLSerializer, encoding=None) |
|---|
| [345] | 427 | self.assertEqual("""<script> |
|---|
| 428 | if (1 < 2) { alert("Doh"); } |
|---|
| 429 | </script>""", output) |
|---|
| 430 | |
|---|
| [181] | 431 | def test_style_escaping(self): |
|---|
| 432 | text = '<style>html > body { display: none; }</style>' |
|---|
| [1085] | 433 | output = XML(text).render(HTMLSerializer, encoding=None) |
|---|
| [181] | 434 | self.assertEqual('<style>html > body { display: none; }</style>', |
|---|
| 435 | output) |
|---|
| 436 | |
|---|
| [345] | 437 | def test_style_escaping_with_namespace(self): |
|---|
| 438 | text = """<style xmlns="http://www.w3.org/1999/xhtml"> |
|---|
| 439 | html > body { display: none; } |
|---|
| 440 | </style>""" |
|---|
| [1085] | 441 | output = XML(text).render(HTMLSerializer, encoding=None) |
|---|
| [345] | 442 | self.assertEqual("""<style> |
|---|
| 443 | html > body { display: none; } |
|---|
| 444 | </style>""", output) |
|---|
| [181] | 445 | |
|---|
| [540] | 446 | def test_html5_doctype(self): |
|---|
| [1157] | 447 | stream = HTML(u'<html></html>') |
|---|
| [1085] | 448 | output = stream.render(HTMLSerializer, doctype=DocType.HTML5, |
|---|
| 449 | encoding=None) |
|---|
| [540] | 450 | self.assertEqual('<!DOCTYPE html>\n<html></html>', output) |
|---|
| [345] | 451 | |
|---|
| [540] | 452 | |
|---|
| [263] | 453 | class EmptyTagFilterTestCase(unittest.TestCase): |
|---|
| 454 | |
|---|
| 455 | def test_empty(self): |
|---|
| 456 | stream = XML('<elem></elem>') | EmptyTagFilter() |
|---|
| 457 | self.assertEqual([EmptyTagFilter.EMPTY], [ev[0] for ev in stream]) |
|---|
| 458 | |
|---|
| 459 | def test_text_content(self): |
|---|
| 460 | stream = XML('<elem>foo</elem>') | EmptyTagFilter() |
|---|
| 461 | self.assertEqual([Stream.START, Stream.TEXT, Stream.END], |
|---|
| 462 | [ev[0] for ev in stream]) |
|---|
| 463 | |
|---|
| 464 | def test_elem_content(self): |
|---|
| 465 | stream = XML('<elem><sub /><sub /></elem>') | EmptyTagFilter() |
|---|
| 466 | self.assertEqual([Stream.START, EmptyTagFilter.EMPTY, |
|---|
| 467 | EmptyTagFilter.EMPTY, Stream.END], |
|---|
| 468 | [ev[0] for ev in stream]) |
|---|
| 469 | |
|---|
| 470 | |
|---|
| [2] | 471 | def suite(): |
|---|
| 472 | suite = unittest.TestSuite() |
|---|
| [90] | 473 | suite.addTest(unittest.makeSuite(XMLSerializerTestCase, 'test')) |
|---|
| [154] | 474 | suite.addTest(unittest.makeSuite(XHTMLSerializerTestCase, 'test')) |
|---|
| 475 | suite.addTest(unittest.makeSuite(HTMLSerializerTestCase, 'test')) |
|---|
| [263] | 476 | suite.addTest(unittest.makeSuite(EmptyTagFilterTestCase, 'test')) |
|---|
| [90] | 477 | suite.addTest(doctest.DocTestSuite(XMLSerializer.__module__)) |
|---|
| [2] | 478 | return suite |
|---|
| 479 | |
|---|
| 480 | if __name__ == '__main__': |
|---|
| 481 | unittest.main(defaultTest='suite') |
|---|