1
# regression test for SAX 2.0 -*- coding: iso-8859-1 -*-
2
# $Id: test_sax.py 66203 2008-09-04 02:22:52Z benjamin.peterson $
1
# regression test for SAX 2.0
2
# $Id: test_sax.py 85862 2010-10-27 18:54:06Z antoine.pitrou $
4
4
from xml.sax import make_parser, ContentHandler, \
5
5
SAXException, SAXReaderNotAvailable, SAXParseException
11
11
from xml.sax.saxutils import XMLGenerator, escape, unescape, quoteattr, \
13
13
from xml.sax.expatreader import create_parser
14
from xml.sax.handler import feature_namespaces
14
15
from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl
15
16
from io import StringIO
16
17
from test.support import findfile, run_unittest
106
107
"<Donald Duck & Co>")
108
109
def test_escape_extra(self):
109
self.assertEquals(escape("Hei p� deg", {"�" : "å"}),
110
self.assertEquals(escape("Hei på deg", {"å" : "å"}),
110
111
"Hei på deg")
118
119
"<Donald Duck & Co>")
120
121
def test_unescape_extra(self):
121
self.assertEquals(unescape("Hei p� deg", {"�" : "å"}),
122
self.assertEquals(unescape("Hei på deg", {"å" : "å"}),
122
123
"Hei på deg")
124
125
def test_unescape_amp_extra(self):
288
289
self.assertEquals(result.getvalue(),
289
290
start+'<my:a xmlns:my="qux" b="c"></my:a>')
292
def test_5027_1(self):
293
# The xml prefix (as in xml:lang below) is reserved and bound by
294
# definition to http://www.w3.org/XML/1998/namespace. XMLGenerator had
295
# a bug whereby a KeyError is thrown because this namespace is missing
298
# This test demonstrates the bug by parsing a document.
300
'<?xml version="1.0"?>'
301
'<a:g1 xmlns:a="http://example.com/ns">'
302
'<a:g2 xml:lang="en">Hello</a:g2>'
305
parser = make_parser()
306
parser.setFeature(feature_namespaces, True)
308
gen = XMLGenerator(result)
309
parser.setContentHandler(gen)
310
parser.parse(test_xml)
312
self.assertEquals(result.getvalue(),
314
'<a:g1 xmlns:a="http://example.com/ns">'
315
'<a:g2 xml:lang="en">Hello</a:g2>'
318
def test_5027_2(self):
319
# The xml prefix (as in xml:lang below) is reserved and bound by
320
# definition to http://www.w3.org/XML/1998/namespace. XMLGenerator had
321
# a bug whereby a KeyError is thrown because this namespace is missing
324
# This test demonstrates the bug by direct manipulation of the
327
gen = XMLGenerator(result)
330
gen.startPrefixMapping('a', 'http://example.com/ns')
331
gen.startElementNS(('http://example.com/ns', 'g1'), 'g1', {})
332
lang_attr = {('http://www.w3.org/XML/1998/namespace', 'lang'): 'en'}
333
gen.startElementNS(('http://example.com/ns', 'g2'), 'g2', lang_attr)
334
gen.characters('Hello')
335
gen.endElementNS(('http://example.com/ns', 'g2'), 'g2')
336
gen.endElementNS(('http://example.com/ns', 'g1'), 'g1')
337
gen.endPrefixMapping('a')
340
self.assertEquals(result.getvalue(),
342
'<a:g1 xmlns:a="http://example.com/ns">'
343
'<a:g2 xml:lang="en">Hello</a:g2>'
292
347
class XMLFilterBaseTest(unittest.TestCase):
293
348
def test_filter_basic(self):