| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2006-2008 Edgewall Software |
|---|
| 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 |
|---|
| 8 | # are also available at http://genshi.edgewall.org/wiki/License. |
|---|
| 9 | # |
|---|
| 10 | # This software consists of voluntary contributions made by many |
|---|
| 11 | # individuals. For the exact contribution history, see the revision |
|---|
| 12 | # history and logs, available at http://genshi.edgewall.org/log/. |
|---|
| 13 | |
|---|
| 14 | import doctest |
|---|
| 15 | import unittest |
|---|
| 16 | import sys |
|---|
| 17 | |
|---|
| 18 | from genshi.core import Attrs, Markup, QName, Stream |
|---|
| 19 | from genshi.input import HTML, XML |
|---|
| 20 | from genshi.output import DocType, XMLSerializer, XHTMLSerializer, \ |
|---|
| 21 | HTMLSerializer, EmptyTagFilter |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | class XMLSerializerTestCase(unittest.TestCase): |
|---|
| 25 | |
|---|
| 26 | def test_with_xml_decl(self): |
|---|
| 27 | stream = Stream([(Stream.XML_DECL, ('1.0', None, -1), (None, -1, -1))]) |
|---|
| 28 | output = stream.render(XMLSerializer, doctype='xhtml', encoding=None) |
|---|
| 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 | |
|---|
| 35 | def test_doctype_in_stream(self): |
|---|
| 36 | stream = Stream([(Stream.DOCTYPE, DocType.HTML_STRICT, (None, -1, -1))]) |
|---|
| 37 | output = stream.render(XMLSerializer, encoding=None) |
|---|
| 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), |
|---|
| 46 | (None, -1, -1))]) |
|---|
| 47 | output = stream.render(XMLSerializer, encoding=None) |
|---|
| 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): |
|---|
| 52 | stream = Stream([ |
|---|
| 53 | (Stream.DOCTYPE, |
|---|
| 54 | ('html', None, 'http://www.w3.org/TR/html4/strict.dtd'), |
|---|
| 55 | (None, -1, -1)) |
|---|
| 56 | ]) |
|---|
| 57 | output = stream.render(XMLSerializer, encoding=None) |
|---|
| 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), |
|---|
| 64 | (None, -1, -1))]) |
|---|
| 65 | output = stream.render(XMLSerializer, encoding=None) |
|---|
| 66 | self.assertEqual('<!DOCTYPE html>\n', output) |
|---|
| 67 | |
|---|
| 68 | def test_serializer_doctype(self): |
|---|
| 69 | stream = Stream([]) |
|---|
| 70 | output = stream.render(XMLSerializer, doctype=DocType.HTML_STRICT, |
|---|
| 71 | encoding=None) |
|---|
| 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): |
|---|
| 78 | stream = Stream([ |
|---|
| 79 | (Stream.DOCTYPE, ('html', None, None), (None, -1, -1)) |
|---|
| 80 | ]) |
|---|
| 81 | output = stream.render(XMLSerializer, doctype=DocType.HTML_STRICT, |
|---|
| 82 | encoding=None) |
|---|
| 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 | |
|---|
| 88 | def test_comment(self): |
|---|
| 89 | stream = Stream([(Stream.COMMENT, 'foo bar', (None, -1, -1))]) |
|---|
| 90 | output = stream.render(XMLSerializer, encoding=None) |
|---|
| 91 | self.assertEqual('<!--foo bar-->', output) |
|---|
| 92 | |
|---|
| 93 | def test_processing_instruction(self): |
|---|
| 94 | stream = Stream([(Stream.PI, ('python', 'x = 2'), (None, -1, -1))]) |
|---|
| 95 | output = stream.render(XMLSerializer, encoding=None) |
|---|
| 96 | self.assertEqual('<?python x = 2?>', output) |
|---|
| 97 | |
|---|
| 98 | def test_nested_default_namespaces(self): |
|---|
| 99 | stream = Stream([ |
|---|
| 100 | (Stream.START_NS, ('', 'http://example.org/'), (None, -1, -1)), |
|---|
| 101 | (Stream.START, (QName('http://example.org/}div'), Attrs()), (None, -1, -1)), |
|---|
| 102 | (Stream.TEXT, '\n ', (None, -1, -1)), |
|---|
| 103 | (Stream.START_NS, ('', 'http://example.org/'), (None, -1, -1)), |
|---|
| 104 | (Stream.START, (QName('http://example.org/}p'), Attrs()), (None, -1, -1)), |
|---|
| 105 | (Stream.END, QName('http://example.org/}p'), (None, -1, -1)), |
|---|
| 106 | (Stream.END_NS, '', (None, -1, -1)), |
|---|
| 107 | (Stream.TEXT, '\n ', (None, -1, -1)), |
|---|
| 108 | (Stream.START_NS, ('', 'http://example.org/'), (None, -1, -1)), |
|---|
| 109 | (Stream.START, (QName('http://example.org/}p'), Attrs()), (None, -1, -1)), |
|---|
| 110 | (Stream.END, QName('http://example.org/}p'), (None, -1, -1)), |
|---|
| 111 | (Stream.END_NS, '', (None, -1, -1)), |
|---|
| 112 | (Stream.TEXT, '\n ', (None, -1, -1)), |
|---|
| 113 | (Stream.END, QName('http://example.org/}div'), (None, -1, -1)), |
|---|
| 114 | (Stream.END_NS, '', (None, -1, -1)) |
|---|
| 115 | ]) |
|---|
| 116 | output = stream.render(XMLSerializer, encoding=None) |
|---|
| 117 | self.assertEqual("""<div xmlns="http://example.org/"> |
|---|
| 118 | <p/> |
|---|
| 119 | <p/> |
|---|
| 120 | </div>""", output) |
|---|
| 121 | |
|---|
| 122 | def test_nested_bound_namespaces(self): |
|---|
| 123 | stream = Stream([ |
|---|
| 124 | (Stream.START_NS, ('x', 'http://example.org/'), (None, -1, -1)), |
|---|
| 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 | ]) |
|---|
| 140 | output = stream.render(XMLSerializer, encoding=None) |
|---|
| 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([ |
|---|
| 148 | (Stream.START, (QName('div'), Attrs()), (None, -1, -1)), |
|---|
| 149 | (Stream.TEXT, '\n ', (None, -1, -1)), |
|---|
| 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 | ]) |
|---|
| 162 | output = stream.render(XMLSerializer, encoding=None) |
|---|
| 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)), |
|---|
| 172 | (Stream.START_NS, ('x', 'http://example.org/'), (None, -1, -1)), |
|---|
| 173 | (Stream.START, (QName('http://example.org/}p'), Attrs()), (None, -1, -1)), |
|---|
| 174 | (Stream.END, QName('http://example.org/}p'), (None, -1, -1)), |
|---|
| 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)), |
|---|
| 178 | (Stream.START, (QName('http://example.org/}p'), Attrs()), (None, -1, -1)), |
|---|
| 179 | (Stream.END, QName('http://example.org/}p'), (None, -1, -1)), |
|---|
| 180 | (Stream.END_NS, 'x', (None, -1, -1)), |
|---|
| 181 | (Stream.TEXT, '\n ', (None, -1, -1)), |
|---|
| 182 | (Stream.END, QName('div'), (None, -1, -1)), |
|---|
| 183 | ]) |
|---|
| 184 | output = stream.render(XMLSerializer, encoding=None) |
|---|
| 185 | self.assertEqual("""<div> |
|---|
| 186 | <x:p xmlns:x="http://example.org/"/> |
|---|
| 187 | <x:p xmlns:x="http://example.org/"/> |
|---|
| 188 | </div>""", output) |
|---|
| 189 | |
|---|
| 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>""" |
|---|
| 202 | output = XML(text).render(XMLSerializer, encoding=None) |
|---|
| 203 | self.assertEqual(text, output) |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | class XHTMLSerializerTestCase(unittest.TestCase): |
|---|
| 207 | |
|---|
| 208 | def test_xml_decl_dropped(self): |
|---|
| 209 | stream = Stream([(Stream.XML_DECL, ('1.0', None, -1), (None, -1, -1))]) |
|---|
| 210 | output = stream.render(XHTMLSerializer, doctype='xhtml', encoding=None) |
|---|
| 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', |
|---|
| 219 | drop_xml_decl=False, encoding=None) |
|---|
| 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 | |
|---|
| 226 | def test_xml_lang(self): |
|---|
| 227 | text = '<p xml:lang="en">English text</p>' |
|---|
| 228 | output = XML(text).render(XHTMLSerializer, encoding=None) |
|---|
| 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>' |
|---|
| 233 | output = XML(text).render(XHTMLSerializer, encoding=None) |
|---|
| 234 | self.assertEqual('<p xml:lang="en" lang="en">English text</p>', output) |
|---|
| 235 | |
|---|
| 236 | def test_textarea_whitespace(self): |
|---|
| 237 | content = '\nHey there. \n\n I am indented.\n' |
|---|
| 238 | stream = XML('<textarea name="foo">%s</textarea>' % content) |
|---|
| 239 | output = stream.render(XHTMLSerializer, encoding=None) |
|---|
| 240 | self.assertEqual('<textarea name="foo">%s</textarea>' % content, output) |
|---|
| 241 | |
|---|
| 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) |
|---|
| 245 | output = stream.render(XHTMLSerializer, encoding=None) |
|---|
| 246 | self.assertEqual('<pre>%s</pre>' % content, output) |
|---|
| 247 | |
|---|
| 248 | def test_xml_space(self): |
|---|
| 249 | text = '<foo xml:space="preserve"> Do not mess \n\n with me </foo>' |
|---|
| 250 | output = XML(text).render(XHTMLSerializer, encoding=None) |
|---|
| 251 | self.assertEqual('<foo> Do not mess \n\n with me </foo>', output) |
|---|
| 252 | |
|---|
| 253 | def test_empty_script(self): |
|---|
| 254 | text = """<html xmlns="http://www.w3.org/1999/xhtml"> |
|---|
| 255 | <script src="foo.js" /> |
|---|
| 256 | </html>""" |
|---|
| 257 | output = XML(text).render(XHTMLSerializer, encoding=None) |
|---|
| 258 | self.assertEqual("""<html xmlns="http://www.w3.org/1999/xhtml"> |
|---|
| 259 | <script src="foo.js"></script> |
|---|
| 260 | </html>""", output) |
|---|
| 261 | |
|---|
| 262 | def test_script_escaping(self): |
|---|
| 263 | text = """<script>/*<![CDATA[*/ |
|---|
| 264 | if (1 < 2) { alert("Doh"); } |
|---|
| 265 | /*]]>*/</script>""" |
|---|
| 266 | output = XML(text).render(XHTMLSerializer, encoding=None) |
|---|
| 267 | self.assertEqual(text, output) |
|---|
| 268 | |
|---|
| 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>""" |
|---|
| 273 | output = XML(text).render(XHTMLSerializer, encoding=None) |
|---|
| 274 | self.assertEqual(text, output) |
|---|
| 275 | |
|---|
| 276 | def test_style_escaping(self): |
|---|
| 277 | text = """<style>/*<![CDATA[*/ |
|---|
| 278 | html > body { display: none; } |
|---|
| 279 | /*]]>*/</style>""" |
|---|
| 280 | output = XML(text).render(XHTMLSerializer, encoding=None) |
|---|
| 281 | self.assertEqual(text, output) |
|---|
| 282 | |
|---|
| 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>""" |
|---|
| 287 | output = XML(text).render(XHTMLSerializer, encoding=None) |
|---|
| 288 | self.assertEqual(text, output) |
|---|
| 289 | |
|---|
| 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"> |
|---|
| 295 | <svg:polygon id="triangle" points="50,50 50,300 300,300"></svg:polygon> |
|---|
| 296 | </svg:svg> |
|---|
| 297 | </button> |
|---|
| 298 | </body> |
|---|
| 299 | </html>""" |
|---|
| 300 | output = XML(text).render(XHTMLSerializer, encoding=None) |
|---|
| 301 | self.assertEqual(text, output) |
|---|
| 302 | |
|---|
| 303 | def test_xhtml_namespace_prefix(self): |
|---|
| 304 | text = """<div xmlns="http://www.w3.org/1999/xhtml"> |
|---|
| 305 | <strong>Hello</strong> |
|---|
| 306 | </div>""" |
|---|
| 307 | output = XML(text).render(XHTMLSerializer, encoding=None) |
|---|
| 308 | self.assertEqual(text, output) |
|---|
| 309 | |
|---|
| 310 | def test_nested_default_namespaces(self): |
|---|
| 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 | ]) |
|---|
| 328 | output = stream.render(XHTMLSerializer, encoding=None) |
|---|
| 329 | self.assertEqual("""<div xmlns="http://example.org/"> |
|---|
| 330 | <p></p> |
|---|
| 331 | <p></p> |
|---|
| 332 | </div>""", output) |
|---|
| 333 | |
|---|
| 334 | def test_nested_bound_namespaces(self): |
|---|
| 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 | ]) |
|---|
| 352 | output = stream.render(XHTMLSerializer, encoding=None) |
|---|
| 353 | self.assertEqual("""<div xmlns:x="http://example.org/"> |
|---|
| 354 | <p></p> |
|---|
| 355 | <p></p> |
|---|
| 356 | </div>""", output) |
|---|
| 357 | |
|---|
| 358 | def test_html5_doctype(self): |
|---|
| 359 | stream = HTML(u'<html></html>') |
|---|
| 360 | output = stream.render(XHTMLSerializer, doctype=DocType.HTML5, |
|---|
| 361 | encoding=None) |
|---|
| 362 | self.assertEqual('<!DOCTYPE html>\n<html></html>', output) |
|---|
| 363 | |
|---|
| 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) |
|---|
| 368 | |
|---|
| 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 | |
|---|
| 382 | class HTMLSerializerTestCase(unittest.TestCase): |
|---|
| 383 | |
|---|
| 384 | def test_xml_lang(self): |
|---|
| 385 | text = '<p xml:lang="en">English text</p>' |
|---|
| 386 | output = XML(text).render(HTMLSerializer, encoding=None) |
|---|
| 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>' |
|---|
| 391 | output = XML(text).render(HTMLSerializer, encoding=None) |
|---|
| 392 | self.assertEqual('<p lang="en">English text</p>', output) |
|---|
| 393 | |
|---|
| 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) |
|---|
| 397 | output = stream.render(HTMLSerializer, encoding=None) |
|---|
| 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) |
|---|
| 403 | output = stream.render(HTMLSerializer, encoding=None) |
|---|
| 404 | self.assertEqual('<pre>%s</pre>' % content, output) |
|---|
| 405 | |
|---|
| 406 | def test_xml_space(self): |
|---|
| 407 | text = '<foo xml:space="preserve"> Do not mess \n\n with me </foo>' |
|---|
| 408 | output = XML(text).render(HTMLSerializer, encoding=None) |
|---|
| 409 | self.assertEqual('<foo> Do not mess \n\n with me </foo>', output) |
|---|
| 410 | |
|---|
| 411 | def test_empty_script(self): |
|---|
| 412 | text = '<script src="foo.js" />' |
|---|
| 413 | output = XML(text).render(HTMLSerializer, encoding=None) |
|---|
| 414 | self.assertEqual('<script src="foo.js"></script>', output) |
|---|
| 415 | |
|---|
| 416 | def test_script_escaping(self): |
|---|
| 417 | text = '<script>if (1 < 2) { alert("Doh"); }</script>' |
|---|
| 418 | output = XML(text).render(HTMLSerializer, encoding=None) |
|---|
| 419 | self.assertEqual('<script>if (1 < 2) { alert("Doh"); }</script>', |
|---|
| 420 | output) |
|---|
| 421 | |
|---|
| 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>""" |
|---|
| 426 | output = XML(text).render(HTMLSerializer, encoding=None) |
|---|
| 427 | self.assertEqual("""<script> |
|---|
| 428 | if (1 < 2) { alert("Doh"); } |
|---|
| 429 | </script>""", output) |
|---|
| 430 | |
|---|
| 431 | def test_style_escaping(self): |
|---|
| 432 | text = '<style>html > body { display: none; }</style>' |
|---|
| 433 | output = XML(text).render(HTMLSerializer, encoding=None) |
|---|
| 434 | self.assertEqual('<style>html > body { display: none; }</style>', |
|---|
| 435 | output) |
|---|
| 436 | |
|---|
| 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>""" |
|---|
| 441 | output = XML(text).render(HTMLSerializer, encoding=None) |
|---|
| 442 | self.assertEqual("""<style> |
|---|
| 443 | html > body { display: none; } |
|---|
| 444 | </style>""", output) |
|---|
| 445 | |
|---|
| 446 | def test_html5_doctype(self): |
|---|
| 447 | stream = HTML(u'<html></html>') |
|---|
| 448 | output = stream.render(HTMLSerializer, doctype=DocType.HTML5, |
|---|
| 449 | encoding=None) |
|---|
| 450 | self.assertEqual('<!DOCTYPE html>\n<html></html>', output) |
|---|
| 451 | |
|---|
| 452 | |
|---|
| 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 | |
|---|
| 471 | def suite(): |
|---|
| 472 | suite = unittest.TestSuite() |
|---|
| 473 | suite.addTest(unittest.makeSuite(XMLSerializerTestCase, 'test')) |
|---|
| 474 | suite.addTest(unittest.makeSuite(XHTMLSerializerTestCase, 'test')) |
|---|
| 475 | suite.addTest(unittest.makeSuite(HTMLSerializerTestCase, 'test')) |
|---|
| 476 | suite.addTest(unittest.makeSuite(EmptyTagFilterTestCase, 'test')) |
|---|
| 477 | suite.addTest(doctest.DocTestSuite(XMLSerializer.__module__)) |
|---|
| 478 | return suite |
|---|
| 479 | |
|---|
| 480 | if __name__ == '__main__': |
|---|
| 481 | unittest.main(defaultTest='suite') |
|---|