Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fc643c3

Browse files
committed
Bug fix to namespace handling in XMLGenerator (now adds declarations).
Bug fixes to XMLFilterBase (wrong ignorableWhitespace signature and did not inherit set*Handler methods from XMLReader.)
1 parent 9a580c4 commit fc643c3

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

Lib/xml/sax/saxutils.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55

66
import handler
7-
7+
import xmlreader
88

99
def escape(data, entities={}):
1010
"""Escape &, <, and > in a string of data.
@@ -31,6 +31,7 @@ def __init__(self, out=None, encoding="iso-8859-1"):
3131
self._out = out
3232
self._ns_contexts = [{}] # contains uri -> prefix dicts
3333
self._current_context = self._ns_contexts[-1]
34+
self._undeclared_ns_maps = []
3435
self._encoding = encoding
3536

3637
# ContentHandler methods
@@ -42,9 +43,11 @@ def startDocument(self):
4243
def startPrefixMapping(self, prefix, uri):
4344
self._ns_contexts.append(self._current_context.copy())
4445
self._current_context[uri] = prefix
46+
self._undeclared_ns_maps.append((prefix, uri))
4547

4648
def endPrefixMapping(self, prefix):
47-
del self._current_context[-1]
49+
self._current_context = self._ns_contexts[-1]
50+
del self._ns_contexts[-1]
4851

4952
def startElement(self, name, attrs):
5053
self._out.write('<' + name)
@@ -58,6 +61,11 @@ def endElement(self, name):
5861
def startElementNS(self, name, qname, attrs):
5962
name = self._current_context[name[0]] + ":" + name[1]
6063
self._out.write('<' + name)
64+
65+
for pair in self._undeclared_ns_maps:
66+
self._out.write(' xmlns:%s="%s"' % pair)
67+
self._undeclared_ns_maps = []
68+
6169
for (name, value) in attrs.items():
6270
name = self._current_context[name[0]] + ":" + name[1]
6371
self._out.write(' %s="%s"' % (name, escape(value)))
@@ -77,7 +85,7 @@ def processingInstruction(self, target, data):
7785
self._out.write('<?%s %s?>' % (target, data))
7886

7987

80-
class XMLFilterBase:
88+
class XMLFilterBase(xmlreader.XMLReader):
8189
"""This class is designed to sit between an XMLReader and the
8290
client application's event handlers. By default, it does nothing
8391
but pass requests up to the reader and events on to the handlers
@@ -128,8 +136,8 @@ def endElementNS(self, name, qname):
128136
def characters(self, content):
129137
self._cont_handler.characters(content)
130138

131-
def ignorableWhitespace(self, chars, start, end):
132-
self._cont_handler.ignorableWhitespace(chars, start, end)
139+
def ignorableWhitespace(self, chars):
140+
self._cont_handler.ignorableWhitespace(chars)
133141

134142
def processingInstruction(self, target, data):
135143
self._cont_handler.processingInstruction(target, data)

0 commit comments

Comments
 (0)