[Python-checkins] CVS: python/dist/src/Lib/xml/dom minidom.py,1.27,1.28 pulldom.py,1.19,1.20

Martin v. L?wis loewis@usw-pr-cvs1.sourceforge.net
Tue, 13 Mar 2001 02:50:15 -0800


Update of /cvsroot/python/python/dist/src/Lib/xml/dom
In directory usw-pr-cvs1:/tmp/cvs-serv23893

Modified Files:
	minidom.py pulldom.py 
Log Message:
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.


Index: minidom.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/xml/dom/minidom.py,v
retrieving revision 1.27
retrieving revision 1.28
diff -C2 -r1.27 -r1.28
*** minidom.py	2001/02/22 14:04:09	1.27
--- minidom.py	2001/03/13 10:50:13	1.28
***************
*** 39,46 ****
      debug = None
      childNodeTypes = ()
  
      def __init__(self):
          self.childNodes = []
!         self.parentNode = None
          if Node._debug:
              index = repr(id(self)) + repr(self.__class__)
--- 39,47 ----
      debug = None
      childNodeTypes = ()
+     namespaceURI = None # this is non-null only for elements and attributes
  
      def __init__(self):
          self.childNodes = []
!         self.parentNode = self.ownerDocument = None
          if Node._debug:
              index = repr(id(self)) + repr(self.__class__)
***************
*** 108,111 ****
--- 109,117 ----
  
      def insertBefore(self, newChild, refChild):
+         if newChild.nodeType == self.DOCUMENT_FRAGMENT_NODE:
+             for c in newChild.childNodes:
+                 self.insertBefore(c, refChild)
+             ### The DOM does not clearly specify what to return in this case
+             return newChild
          if newChild.nodeType not in self.childNodeTypes:
              raise HierarchyRequestErr, \
***************
*** 131,134 ****
--- 137,145 ----
  
      def appendChild(self, node):
+         if node.nodeType == self.DOCUMENT_FRAGMENT_NODE:
+             for c in node.childNodes:
+                 self.appendChild(c)
+             ### The DOM does not clearly specify what to return in this case
+             return node
          if node.nodeType not in self.childNodeTypes:
              raise HierarchyRequestErr, \
***************
*** 149,152 ****
--- 160,167 ----
  
      def replaceChild(self, newChild, oldChild):
+         if newChild.nodeType == self.DOCUMENT_FRAGMENT_NODE:
+             refChild = oldChild.nextSibling
+             self.removeChild(oldChild)
+             return self.insertBefore(newChild, refChild)
          if newChild.nodeType not in self.childNodeTypes:
              raise HierarchyRequestErr, \
***************
*** 234,238 ****
  
      def unlink(self):
!         self.parentNode = None
          for child in self.childNodes:
              child.unlink()
--- 249,253 ----
  
      def unlink(self):
!         self.parentNode = self.ownerDocument = None
          for child in self.childNodes:
              child.unlink()
***************
*** 271,274 ****
--- 286,304 ----
      return rc
  
+ class DocumentFragment(Node):
+     nodeType = Node.DOCUMENT_FRAGMENT_NODE
+     nodeName = "#document-fragment"
+     nodeValue = None
+     attributes = None
+     parentNode = None
+     childNodeTypes = (Node.ELEMENT_NODE,
+                       Node.TEXT_NODE,
+                       Node.CDATA_SECTION_NODE,
+                       Node.ENTITY_REFERENCE_NODE,
+                       Node.PROCESSING_INSTRUCTION_NODE,
+                       Node.COMMENT_NODE,
+                       Node.NOTATION_NODE)
+ 
+ 
  class Attr(Node):
      nodeType = Node.ATTRIBUTE_NODE
***************
*** 410,414 ****
                        Node.CDATA_SECTION_NODE, Node.ENTITY_REFERENCE_NODE)
  
!     def __init__(self, tagName, namespaceURI="", prefix="",
                   localName=None):
          Node.__init__(self)
--- 440,444 ----
                        Node.CDATA_SECTION_NODE, Node.ENTITY_REFERENCE_NODE)
  
!     def __init__(self, tagName, namespaceURI=None, prefix="",
                   localName=None):
          Node.__init__(self)
***************
*** 495,498 ****
--- 525,530 ----
              return old
  
+     setAttributeNodeNS = setAttributeNode
+ 
      def removeAttribute(self, name):
          attr = self._attrs[name]
***************
*** 508,511 ****
--- 540,545 ----
          del self._attrsNS[(node.namespaceURI, node.localName)]
  
+     removeAttributeNodeNS = removeAttributeNode
+ 
      def hasAttribute(self, name):
          return self._attrs.has_key(name)
***************
*** 652,656 ****
              raise xml.dom.WrongDocumentErr(
                  "doctype object owned by another DOM tree")
!         doc = Document()
          if doctype is None:
              doctype = self.createDocumentType(qualifiedName, None, None)
--- 686,690 ----
              raise xml.dom.WrongDocumentErr(
                  "doctype object owned by another DOM tree")
!         doc = self._createDocument()
          if doctype is None:
              doctype = self.createDocumentType(qualifiedName, None, None)
***************
*** 672,676 ****
          element = doc.createElementNS(namespaceURI, qualifiedName)
          doc.appendChild(element)
!         doctype.parentNode = doc
          doc.doctype = doctype
          doc.implementation = self
--- 706,710 ----
          element = doc.createElementNS(namespaceURI, qualifiedName)
          doc.appendChild(element)
!         doctype.parentNode = doctype.ownerDocument = doc
          doc.doctype = doctype
          doc.implementation = self
***************
*** 683,686 ****
--- 717,723 ----
          return doctype
  
+     # internal
+     def _createDocument(self):
+         return Document()
  
  class Document(Node):
***************
*** 691,694 ****
--- 728,732 ----
      doctype = None
      parentNode = None
+     previousSibling = nextSibling = None
  
      implementation = DOMImplementation()
***************
*** 728,751 ****
              self.doctype = None
          Node.unlink(self)
- 
-     createElement = Element
- 
-     createTextNode = Text
- 
-     createComment = Comment
- 
-     createProcessingInstruction = ProcessingInstruction
  
!     createAttribute = Attr
  
      def createElementNS(self, namespaceURI, qualifiedName):
          prefix, localName = _nssplit(qualifiedName)
!         return self.createElement(qualifiedName, namespaceURI,
!                                   prefix, localName)
  
      def createAttributeNS(self, namespaceURI, qualifiedName):
          prefix, localName = _nssplit(qualifiedName)
!         return self.createAttribute(qualifiedName, namespaceURI,
!                                     localName, prefix)
  
      def getElementsByTagNameNS(self, namespaceURI, localName):
--- 766,811 ----
              self.doctype = None
          Node.unlink(self)
  
!     def createDocumentFragment(self):
!         d = DocumentFragment()
!         d.ownerDoc = self
!         return d
! 
!     def createElement(self, tagName):
!         e = Element(tagName)
!         e.ownerDocument = self
!         return e
! 
!     def createTextNode(self, data):
!         t = Text(data)
!         t.ownerDocument = self
!         return t
! 
!     def createComment(self, data):
!         c = Comment(data)
!         c.ownerDocument = self
!         return c
! 
!     def createProcessingInstruction(self, target, data):
!         p = ProcessingInstruction(target, data)
!         p.ownerDocument = self
!         return p
! 
!     def createAttribute(self, qName):
!         a = Attr(qName)
!         a.ownerDocument = self
!         return a
  
      def createElementNS(self, namespaceURI, qualifiedName):
          prefix, localName = _nssplit(qualifiedName)
!         e = Element(qualifiedName, namespaceURI, prefix, localName)
!         e.ownerDocument = self
!         return e
  
      def createAttributeNS(self, namespaceURI, qualifiedName):
          prefix, localName = _nssplit(qualifiedName)
!         a = Attr(qualifiedName, namespaceURI, localName, prefix)
!         a.ownerDocument = self
!         return a
  
      def getElementsByTagNameNS(self, namespaceURI, localName):

Index: pulldom.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/xml/dom/pulldom.py,v
retrieving revision 1.19
retrieving revision 1.20
diff -C2 -r1.19 -r1.20
*** pulldom.py	2001/02/06 01:16:06	1.19
--- pulldom.py	2001/03/13 10:50:13	1.20
***************
*** 34,37 ****
--- 34,38 ----
          self._ns_contexts = [{}] # contains uri -> prefix dicts
          self._current_context = self._ns_contexts[-1]
+         self.pending_events = []
  
      def pop(self):
***************
*** 116,128 ****
  
      def comment(self, s):
!         node = self.document.createComment(s)
!         self.lastEvent[1] = [(COMMENT, node), None]
!         self.lastEvent = self.lastEvent[1]
  
      def processingInstruction(self, target, data):
!         node = self.document.createProcessingInstruction(target, data)
! 
!         self.lastEvent[1] = [(PROCESSING_INSTRUCTION, node), None]
!         self.lastEvent = self.lastEvent[1]
  
      def ignorableWhitespace(self, chars):
--- 117,136 ----
  
      def comment(self, s):
!         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):
!         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):
***************
*** 149,152 ****
--- 157,174 ----
          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