@@ -38,10 +38,11 @@ class Node(_Node):
3838 _makeParentNodes = 1
3939 debug = None
4040 childNodeTypes = ()
41+ namespaceURI = None # this is non-null only for elements and attributes
4142
4243 def __init__ (self ):
4344 self .childNodes = []
44- self .parentNode = None
45+ self .parentNode = self . ownerDocument = None
4546 if Node ._debug :
4647 index = repr (id (self )) + repr (self .__class__ )
4748 Node .allnodes [index ] = repr (self .__dict__ )
@@ -107,6 +108,11 @@ def _get_lastChild(self):
107108 return self .childNodes [- 1 ]
108109
109110 def insertBefore (self , newChild , refChild ):
111+ if newChild .nodeType == self .DOCUMENT_FRAGMENT_NODE :
112+ for c in newChild .childNodes :
113+ self .insertBefore (c , refChild )
114+ ### The DOM does not clearly specify what to return in this case
115+ return newChild
110116 if newChild .nodeType not in self .childNodeTypes :
111117 raise HierarchyRequestErr , \
112118 "%s cannot be child of %s" % (repr (newChild ), repr (self ))
@@ -130,6 +136,11 @@ def insertBefore(self, newChild, refChild):
130136 return newChild
131137
132138 def appendChild (self , node ):
139+ if node .nodeType == self .DOCUMENT_FRAGMENT_NODE :
140+ for c in node .childNodes :
141+ self .appendChild (c )
142+ ### The DOM does not clearly specify what to return in this case
143+ return node
133144 if node .nodeType not in self .childNodeTypes :
134145 raise HierarchyRequestErr , \
135146 "%s cannot be child of %s" % (repr (node ), repr (self ))
@@ -148,6 +159,10 @@ def appendChild(self, node):
148159 return node
149160
150161 def replaceChild (self , newChild , oldChild ):
162+ if newChild .nodeType == self .DOCUMENT_FRAGMENT_NODE :
163+ refChild = oldChild .nextSibling
164+ self .removeChild (oldChild )
165+ return self .insertBefore (newChild , refChild )
151166 if newChild .nodeType not in self .childNodeTypes :
152167 raise HierarchyRequestErr , \
153168 "%s cannot be child of %s" % (repr (newChild ), repr (self ))
@@ -233,7 +248,7 @@ def isSameNode(self, other):
233248 # minidom-specific API:
234249
235250 def unlink (self ):
236- self .parentNode = None
251+ self .parentNode = self . ownerDocument = None
237252 for child in self .childNodes :
238253 child .unlink ()
239254 self .childNodes = None
@@ -270,6 +285,21 @@ def _getElementsByTagNameNSHelper(parent, nsURI, localName, rc):
270285 _getElementsByTagNameNSHelper (node , nsURI , localName , rc )
271286 return rc
272287
288+ class DocumentFragment (Node ):
289+ nodeType = Node .DOCUMENT_FRAGMENT_NODE
290+ nodeName = "#document-fragment"
291+ nodeValue = None
292+ attributes = None
293+ parentNode = None
294+ childNodeTypes = (Node .ELEMENT_NODE ,
295+ Node .TEXT_NODE ,
296+ Node .CDATA_SECTION_NODE ,
297+ Node .ENTITY_REFERENCE_NODE ,
298+ Node .PROCESSING_INSTRUCTION_NODE ,
299+ Node .COMMENT_NODE ,
300+ Node .NOTATION_NODE )
301+
302+
273303class Attr (Node ):
274304 nodeType = Node .ATTRIBUTE_NODE
275305 attributes = None
@@ -409,7 +439,7 @@ class Element(Node):
409439 Node .COMMENT_NODE , Node .TEXT_NODE ,
410440 Node .CDATA_SECTION_NODE , Node .ENTITY_REFERENCE_NODE )
411441
412- def __init__ (self , tagName , namespaceURI = "" , prefix = "" ,
442+ def __init__ (self , tagName , namespaceURI = None , prefix = "" ,
413443 localName = None ):
414444 Node .__init__ (self )
415445 self .tagName = self .nodeName = tagName
@@ -494,6 +524,8 @@ def setAttributeNode(self, attr):
494524 # it doesn't represent a change, and should not be returned.
495525 return old
496526
527+ setAttributeNodeNS = setAttributeNode
528+
497529 def removeAttribute (self , name ):
498530 attr = self ._attrs [name ]
499531 self .removeAttributeNode (attr )
@@ -507,6 +539,8 @@ def removeAttributeNode(self, node):
507539 del self ._attrs [node .name ]
508540 del self ._attrsNS [(node .namespaceURI , node .localName )]
509541
542+ removeAttributeNodeNS = removeAttributeNode
543+
510544 def hasAttribute (self , name ):
511545 return self ._attrs .has_key (name )
512546
@@ -651,7 +685,7 @@ def createDocument(self, namespaceURI, qualifiedName, doctype):
651685 if doctype and doctype .parentNode is not None :
652686 raise xml .dom .WrongDocumentErr (
653687 "doctype object owned by another DOM tree" )
654- doc = Document ()
688+ doc = self . _createDocument ()
655689 if doctype is None :
656690 doctype = self .createDocumentType (qualifiedName , None , None )
657691 if not qualifiedName :
@@ -671,7 +705,7 @@ def createDocument(self, namespaceURI, qualifiedName, doctype):
671705 "illegal use of prefix without namespaces" )
672706 element = doc .createElementNS (namespaceURI , qualifiedName )
673707 doc .appendChild (element )
674- doctype .parentNode = doc
708+ doctype .parentNode = doctype . ownerDocument = doc
675709 doc .doctype = doctype
676710 doc .implementation = self
677711 return doc
@@ -682,6 +716,9 @@ def createDocumentType(self, qualifiedName, publicId, systemId):
682716 doctype .systemId = systemId
683717 return doctype
684718
719+ # internal
720+ def _createDocument (self ):
721+ return Document ()
685722
686723class Document (Node ):
687724 nodeType = Node .DOCUMENT_NODE
@@ -690,6 +727,7 @@ class Document(Node):
690727 attributes = None
691728 doctype = None
692729 parentNode = None
730+ previousSibling = nextSibling = None
693731
694732 implementation = DOMImplementation ()
695733 childNodeTypes = (Node .ELEMENT_NODE , Node .PROCESSING_INSTRUCTION_NODE ,
@@ -728,25 +766,47 @@ def unlink(self):
728766 self .doctype = None
729767 Node .unlink (self )
730768
731- createElement = Element
769+ def createDocumentFragment (self ):
770+ d = DocumentFragment ()
771+ d .ownerDoc = self
772+ return d
773+
774+ def createElement (self , tagName ):
775+ e = Element (tagName )
776+ e .ownerDocument = self
777+ return e
732778
733- createTextNode = Text
779+ def createTextNode (self , data ):
780+ t = Text (data )
781+ t .ownerDocument = self
782+ return t
734783
735- createComment = Comment
784+ def createComment (self , data ):
785+ c = Comment (data )
786+ c .ownerDocument = self
787+ return c
736788
737- createProcessingInstruction = ProcessingInstruction
789+ def createProcessingInstruction (self , target , data ):
790+ p = ProcessingInstruction (target , data )
791+ p .ownerDocument = self
792+ return p
738793
739- createAttribute = Attr
794+ def createAttribute (self , qName ):
795+ a = Attr (qName )
796+ a .ownerDocument = self
797+ return a
740798
741799 def createElementNS (self , namespaceURI , qualifiedName ):
742800 prefix , localName = _nssplit (qualifiedName )
743- return self .createElement (qualifiedName , namespaceURI ,
744- prefix , localName )
801+ e = Element (qualifiedName , namespaceURI , prefix , localName )
802+ e .ownerDocument = self
803+ return e
745804
746805 def createAttributeNS (self , namespaceURI , qualifiedName ):
747806 prefix , localName = _nssplit (qualifiedName )
748- return self .createAttribute (qualifiedName , namespaceURI ,
749- localName , prefix )
807+ a = Attr (qualifiedName , namespaceURI , localName , prefix )
808+ a .ownerDocument = self
809+ return a
750810
751811 def getElementsByTagNameNS (self , namespaceURI , localName ):
752812 _getElementsByTagNameNSHelper (self , namespaceURI , localName )
0 commit comments