Skip to content

Commit ed53b54

Browse files
committed
Create method Node.append_child() (#16)
1 parent 35a5143 commit ed53b54

File tree

1 file changed

+31
-0
lines changed
  • w3/python/core/fundamental_interface

1 file changed

+31
-0
lines changed

w3/python/core/fundamental_interface/Node.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,37 @@ def insert_before(self,
301301
new_child._set_parent_node(self)
302302
return new_child
303303

304+
def append_child(self, new_child: _AnyNode) -> _AnyNode:
305+
"""Adds the node `new_child` to the end of the list of children of this node.
306+
307+
If the `new_child` is already in the tree, it is first removed.
308+
309+
Args:
310+
new_child: The node to add. If it is a `DocumentFragment` object, the entire contents of the document fragment are moved into the child list of this node
311+
312+
Returns:
313+
The node added.
314+
315+
Raises:
316+
DOMException:
317+
- `HIERARCHY_REQUEST_ERR`: Raised if this node is of a type that does not allow children of the type of the `new_child` node, or if the node to append is one of this node's ancestors.
318+
- `WRONG_DOCUMENT_ERR`: Raised if `new_child` was created from a different document than the one that created this node.
319+
- `NO_MODIFICATION_ALLOWED_ERR`: Raised if this node is readonly.
320+
"""
321+
if self._read_only:
322+
raise DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR)
323+
if self.owner_document is not new_child.owner_document:
324+
raise DOMException(DOMException.WRONG_DOCUMENT_ERR)
325+
# `HIERARCHY_REQUEST_ERR` should be checked on subclasses by overriding.
326+
if new_child.node_type == NodeType.DOCUMENT_FRAGMENT_NODE:
327+
grand_child_node: _AnyNode
328+
for grand_child_node in new_child.child_nodes:
329+
self.append_child(grand_child_node)
330+
else:
331+
new_child._set_parent_node(self)
332+
self.child_nodes.append(new_child)
333+
return new_child
334+
304335
def has_child_nodes(self) -> bool:
305336
"""This is a convenience method to allow easy determination of whether a node has any children.
306337

0 commit comments

Comments
 (0)