Patch #102485 ] Check for legal children when adding children to a DOM node

This commit is contained in:
Andrew M. Kuchling 2000-12-31 03:50:23 +00:00
parent 1a4d77b252
commit 291ed4fb3f

View File

@ -18,6 +18,8 @@ import string
_string = string _string = string
del string del string
from xml.dom import HierarchyRequestErr
# localize the types, and allow support for Unicode values if available: # localize the types, and allow support for Unicode values if available:
import types import types
_TupleType = types.TupleType _TupleType = types.TupleType
@ -37,6 +39,7 @@ class Node(_Node):
_debug = 0 _debug = 0
_makeParentNodes = 1 _makeParentNodes = 1
debug = None debug = None
childNodeTypes = ()
def __init__(self): def __init__(self):
self.childNodes = [] self.childNodes = []
@ -99,6 +102,9 @@ class Node(_Node):
return self.childNodes[-1] return self.childNodes[-1]
def insertBefore(self, newChild, refChild): def insertBefore(self, newChild, refChild):
if newChild.nodeType not in self.childNodeTypes:
raise HierarchyRequestErr, \
"%s cannot be child of %s" % (repr(newChild), repr(self) )
if newChild.parentNode is not None: if newChild.parentNode is not None:
newChild.parentNode.removeChild(newChild) newChild.parentNode.removeChild(newChild)
if refChild is None: if refChild is None:
@ -119,6 +125,9 @@ class Node(_Node):
return newChild return newChild
def appendChild(self, node): def appendChild(self, node):
if node.nodeType not in self.childNodeTypes:
raise HierarchyRequestErr, \
"%s cannot be child of %s" % (repr(node), repr(self) )
if node.parentNode is not None: if node.parentNode is not None:
node.parentNode.removeChild(node) node.parentNode.removeChild(node)
if self.childNodes: if self.childNodes:
@ -134,6 +143,9 @@ class Node(_Node):
return node return node
def replaceChild(self, newChild, oldChild): def replaceChild(self, newChild, oldChild):
if newChild.nodeType not in self.childNodeTypes:
raise HierarchyRequestErr, \
"%s cannot be child of %s" % (repr(newChild), repr(self) )
if newChild.parentNode is not None: if newChild.parentNode is not None:
newChild.parentNode.removeChild(newChild) newChild.parentNode.removeChild(newChild)
if newChild is oldChild: if newChild is oldChild:
@ -250,6 +262,7 @@ class Attr(Node):
nodeType = Node.ATTRIBUTE_NODE nodeType = Node.ATTRIBUTE_NODE
attributes = None attributes = None
ownerElement = None ownerElement = None
childNodeTypes = (Node.TEXT_NODE, Node.ENTITY_REFERENCE_NODE)
def __init__(self, qName, namespaceURI="", localName=None, prefix=None): def __init__(self, qName, namespaceURI="", localName=None, prefix=None):
# skip setattr for performance # skip setattr for performance
@ -374,6 +387,9 @@ class Element(Node):
nodeType = Node.ELEMENT_NODE nodeType = Node.ELEMENT_NODE
nextSibling = None nextSibling = None
previousSibling = None previousSibling = None
childNodeTypes = (Node.ELEMENT_NODE, Node.PROCESSING_INSTRUCTION_NODE,
Node.COMMENT_NODE, Node.TEXT_NODE,
Node.CDATA_SECTION_NODE, Node.ENTITY_REFERENCE_NODE)
def __init__(self, tagName, namespaceURI="", prefix="", def __init__(self, tagName, namespaceURI="", prefix="",
localName=None): localName=None):
@ -508,6 +524,7 @@ class Comment(Node):
nodeType = Node.COMMENT_NODE nodeType = Node.COMMENT_NODE
nodeName = "#comment" nodeName = "#comment"
attributes = None attributes = None
childNodeTypes = ()
def __init__(self, data): def __init__(self, data):
Node.__init__(self) Node.__init__(self)
@ -519,6 +536,7 @@ class Comment(Node):
class ProcessingInstruction(Node): class ProcessingInstruction(Node):
nodeType = Node.PROCESSING_INSTRUCTION_NODE nodeType = Node.PROCESSING_INSTRUCTION_NODE
attributes = None attributes = None
childNodeTypes = ()
def __init__(self, target, data): def __init__(self, target, data):
Node.__init__(self) Node.__init__(self)
@ -532,6 +550,7 @@ class Text(Node):
nodeType = Node.TEXT_NODE nodeType = Node.TEXT_NODE
nodeName = "#text" nodeName = "#text"
attributes = None attributes = None
childNodeTypes = ()
def __init__(self, data): def __init__(self, data):
Node.__init__(self) Node.__init__(self)
@ -627,8 +646,13 @@ class Document(Node):
parentNode = None parentNode = None
implementation = DOMImplementation() implementation = DOMImplementation()
childNodeTypes = (Node.ELEMENT_NODE, Node.PROCESSING_INSTRUCTION_NODE,
Node.COMMENT_NODE, Node.DOCUMENT_TYPE_NODE)
def appendChild(self, node): def appendChild(self, node):
if node.nodeType not in self.childNodeTypes:
raise HierarchyRequestErr, \
"%s cannot be child of %s" % (repr(node), repr(self) )
if node.parentNode is not None: if node.parentNode is not None:
node.parentNode.removeChild(node) node.parentNode.removeChild(node)