Skip to content

Commit 2fcf3a0

Browse files
committed
#16 Add comment for Node.append_child()
1 parent 7c5ed53 commit 2fcf3a0

File tree

1 file changed

+14
-7
lines changed
  • w3/python/core/fundamental_interface

1 file changed

+14
-7
lines changed

w3/python/core/fundamental_interface/Node.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -375,20 +375,27 @@ def append_child(self, new_child: _AnyNode) -> _AnyNode:
375375
- `WRONG_DOCUMENT_ERR`: Raised if `new_child` was created from a different document than the one that created this node.
376376
- `NO_MODIFICATION_ALLOWED_ERR`: Raised if this node is readonly.
377377
"""
378+
# `HIERARCHY_REQUEST_ERR` should be checked on subclasses by overriding.
378379
if self._read_only:
379380
raise DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR)
380-
if self.owner_document is not new_child.owner_document:
381+
if new_child.owner_document is not self.owner_document:
381382
raise DOMException(DOMException.WRONG_DOCUMENT_ERR)
382-
# `HIERARCHY_REQUEST_ERR` should be checked on subclasses by overriding. # `HIERARCHY_REQUEST_ERR` should be checked on subclasses by overriding.
383-
if new_child in self.child_nodes:
384-
self.child_nodes.remove(new_child)
383+
# If `new_child` is a `DocumentFragment` object,
384+
# all of its children are appended, in the same order.
385+
#
386+
# This is done by recursive calls.
385387
if new_child.node_type == NodeType.DOCUMENT_FRAGMENT_NODE:
386388
grand_child_node: _AnyNode
387389
for grand_child_node in new_child.child_nodes:
388390
self.append_child(grand_child_node)
389-
else:
390-
new_child._set_parent_node(self)
391-
self.child_nodes.append(new_child)
391+
return new_child
392+
# If the `new_child` is already in the tree,
393+
# it is first removed.
394+
if new_child in self.child_nodes:
395+
self.child_nodes.remove(new_child)
396+
# Otherwise, simply append `new_child` using the methods of `NodeList(list)`.
397+
self.child_nodes.append(new_child)
398+
new_child._set_parent_node(self)
392399
return new_child
393400

394401
def has_child_nodes(self) -> bool:

0 commit comments

Comments
 (0)