diff options
Diffstat (limited to 'Lib/xml/dom')
| -rw-r--r-- | Lib/xml/dom/minidom.py | 28 | ||||
| -rw-r--r-- | Lib/xml/dom/pulldom.py | 2 |
2 files changed, 24 insertions, 6 deletions
diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py index d9c04d4..f23ad05 100644 --- a/Lib/xml/dom/minidom.py +++ b/Lib/xml/dom/minidom.py @@ -268,6 +268,14 @@ class Node(xml.dom.Node): self.previousSibling = None self.nextSibling = None + # A Node is its own context manager, to ensure that an unlink() call occurs. + # This is similar to how a file object works. + def __enter__(self): + return self + + def __exit__(self, et, ev, tb): + self.unlink() + defproperty(Node, "firstChild", doc="First child node, or None.") defproperty(Node, "lastChild", doc="Last child node, or None.") defproperty(Node, "localName", doc="Namespace-local name of this node.") @@ -828,10 +836,16 @@ class Element(Node): _write_data(writer, attrs[a_name].value) writer.write("\"") if self.childNodes: - writer.write(">%s"%(newl)) - for node in self.childNodes: - node.writexml(writer,indent+addindent,addindent,newl) - writer.write("%s</%s>%s" % (indent,self.tagName,newl)) + writer.write(">") + if (len(self.childNodes) == 1 and + self.childNodes[0].nodeType == Node.TEXT_NODE): + self.childNodes[0].writexml(writer, '', '', '') + else: + writer.write(newl) + for node in self.childNodes: + node.writexml(writer, indent+addindent, addindent, newl) + writer.write(indent) + writer.write("</%s>%s" % (self.tagName, newl)) else: writer.write("/>%s"%(newl)) @@ -913,6 +927,10 @@ class Childless: raise xml.dom.NotFoundErr( self.nodeName + " nodes do not have children") + def normalize(self): + # For childless nodes, normalize() has nothing to do. + pass + def replaceChild(self, newChild, oldChild): raise xml.dom.HierarchyRequestErr( self.nodeName + " nodes do not have children") @@ -1049,7 +1067,7 @@ class Text(CharacterData): return newText def writexml(self, writer, indent="", addindent="", newl=""): - _write_data(writer, "%s%s%s"%(indent, self.data, newl)) + _write_data(writer, "%s%s%s" % (indent, self.data, newl)) # DOM Level 3 (WD 9 April 2002) diff --git a/Lib/xml/dom/pulldom.py b/Lib/xml/dom/pulldom.py index 81a36b0..d5ac8b2 100644 --- a/Lib/xml/dom/pulldom.py +++ b/Lib/xml/dom/pulldom.py @@ -326,7 +326,7 @@ def parse(stream_or_string, parser=None, bufsize=None): if bufsize is None: bufsize = default_bufsize if isinstance(stream_or_string, str): - stream = open(stream_or_string) + stream = open(stream_or_string, 'rb') else: stream = stream_or_string if not parser: |
