@@ -375,20 +375,27 @@ def append_child(self, new_child: _AnyNode) -> _AnyNode:
375
375
- `WRONG_DOCUMENT_ERR`: Raised if `new_child` was created from a different document than the one that created this node.
376
376
- `NO_MODIFICATION_ALLOWED_ERR`: Raised if this node is readonly.
377
377
"""
378
+ # `HIERARCHY_REQUEST_ERR` should be checked on subclasses by overriding.
378
379
if self ._read_only :
379
380
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 :
381
382
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.
385
387
if new_child .node_type == NodeType .DOCUMENT_FRAGMENT_NODE :
386
388
grand_child_node : _AnyNode
387
389
for grand_child_node in new_child .child_nodes :
388
390
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 )
392
399
return new_child
393
400
394
401
def has_child_nodes (self ) -> bool :
0 commit comments