summaryrefslogtreecommitdiffstats
path: root/Lib/xml/dom/pulldom.py
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2001-03-13 10:50:13 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2001-03-13 10:50:13 (GMT)
commit126f2f62db541565bebc83859ac329836d7d3f00 (patch)
tree5b5f01713f08c3af3db0f4d5563099448ff01caa /Lib/xml/dom/pulldom.py
parenta8f7e5976126c4d0a3acef512e75a87b560ac2ec (diff)
downloadcpython-126f2f62db541565bebc83859ac329836d7d3f00.zip
cpython-126f2f62db541565bebc83859ac329836d7d3f00.tar.gz
cpython-126f2f62db541565bebc83859ac329836d7d3f00.tar.bz2
Patch #407965: Improve Level 2 conformance of minidom
- addition of a DocumentFragment implementation and createDocumentFragment method - proper setting of ownerDocument for all nodes - setting of namespaceURI to None in Element as a class attribute - addition of setAttributeNodeNS and removeAttributeNodeNS as aliases for setAttributeNode and removeAttributeNode - support for inheriting from DOMImplementation to extend it with additional features (to override the Document class) in pulldom: - support for nodes (comment and PI) that occur before he document element; that became necessary as pulldom now delays creation of the document until it has the document element.
Diffstat (limited to 'Lib/xml/dom/pulldom.py')
-rw-r--r--Lib/xml/dom/pulldom.py36
1 files changed, 29 insertions, 7 deletions
diff --git a/Lib/xml/dom/pulldom.py b/Lib/xml/dom/pulldom.py
index b573ba0..d7280ee 100644
--- a/Lib/xml/dom/pulldom.py
+++ b/Lib/xml/dom/pulldom.py
@@ -33,6 +33,7 @@ class PullDOM(xml.sax.ContentHandler):
pass
self._ns_contexts = [{}] # contains uri -> prefix dicts
self._current_context = self._ns_contexts[-1]
+ self.pending_events = []
def pop(self):
result = self.elementStack[-1]
@@ -115,15 +116,22 @@ class PullDOM(xml.sax.ContentHandler):
self.lastEvent = self.lastEvent[1]
def comment(self, s):
- node = self.document.createComment(s)
- self.lastEvent[1] = [(COMMENT, node), None]
- self.lastEvent = self.lastEvent[1]
+ if self.document:
+ node = self.document.createComment(s)
+ self.lastEvent[1] = [(COMMENT, node), None]
+ self.lastEvent = self.lastEvent[1]
+ else:
+ event = [(COMMENT, s), None]
+ self.pending_events.append(event)
def processingInstruction(self, target, data):
- node = self.document.createProcessingInstruction(target, data)
-
- self.lastEvent[1] = [(PROCESSING_INSTRUCTION, node), None]
- self.lastEvent = self.lastEvent[1]
+ if self.document:
+ node = self.document.createProcessingInstruction(target, data)
+ self.lastEvent[1] = [(PROCESSING_INSTRUCTION, node), None]
+ self.lastEvent = self.lastEvent[1]
+ else:
+ event = [(PROCESSING_INSTRUCTION, target, data), None]
+ self.pending_events.append(event)
def ignorableWhitespace(self, chars):
node = self.document.createTextNode(chars)
@@ -148,6 +156,20 @@ class PullDOM(xml.sax.ContentHandler):
self.lastEvent[1] = [(START_DOCUMENT, node), None]
self.lastEvent = self.lastEvent[1]
self.push(node)
+ # Put everything we have seen so far into the document
+ for e in self.pending_events:
+ if e[0][0] == PROCESSING_INSTRUCTION:
+ _,target,data = e[0]
+ n = self.document.createProcessingInstruction(target, data)
+ e[0] = (PROCESSING_INSTRUCTION, n)
+ elif e[0][0] == COMMENT:
+ n = self.document.createComment(e[0][1])
+ e[0] = (COMMENT, n)
+ else:
+ raise AssertionError("Unknown pending event ",e[0][0])
+ self.lastEvent[1] = e
+ self.lastEvent = e
+ self.pending_events = None
return node.firstChild
def endDocument(self):