DOM

DOM

Source
  1. If parent is not a Document, DocumentFragment, or Element node, then throw a "HierarchyRequestError" DOMException.

  2. If node is a host-including inclusive ancestor of parent, then throw a "HierarchyRequestError" DOMException.

  3. If child is non-null and its parent is not parent, then throw a "NotFoundError" DOMException.

  4. If node is not a DocumentFragment, DocumentType, Element, Text, ProcessingInstruction, or Comment node, then throw a "HierarchyRequestError" DOMException.

  5. If either node is a Text node and parent is a document, or node is a doctype and parent is not a document, then throw a "HierarchyRequestError" DOMException.

  6. If parent is a document, and any of the statements below, switched on node, are true, then throw a "HierarchyRequestError" DOMException.

    DocumentFragment node

    If node has more than one element child or has a Text node child.

    Otherwise, if node has one element child and either parent has an element child, child is a doctype, or child is non-null and a doctype is following child.

    element

    parent has an element child, child is a doctype, or child is non-null and a doctype is following child.

    doctype

    parent has a doctype child, child is non-null and an element is preceding child, or child is null and parent has an element child.

To pre-insert a node into a parent before a child, run these steps:

Specifications may define insertion steps for all or some nodes. The
algorithm is passed insertedNode, as indicated in the insert algorithm below.

Specifications may define children changed steps for all or some nodes. The algorithm is passed no argument and is called from insert, remove, and replace data.

To insert a node into a parent before a child, with an optional suppress observers flag, run these steps:

  1. Let nodes be node’s children, if node is a DocumentFragment node; otherwise « node ».

  2. Let count be nodes’s size.

  3. If count is 0, then return.

  4. If node is a DocumentFragment node, then:

  5. If child is non-null, then:

  6. Let previousSibling be child’s previous sibling or parent’s last child if child is null.

  7. For each node in nodes, in tree order:

    1. Adopt node into parent’s node document.

    2. If child is null, then append node to parent’s children.

    3. Otherwise, insert node into parent’s children before child’s index.

    4. If parent is a shadow host whose shadow root’s slot assignment is "named" and node is a slottable, then assign a slot for node.

    5. If parent’s root is a shadow root, and parent is a slot whose assigned nodes is the empty list, then run signal a slot change for parent.

    6. Run assign slottables for a tree with node’s root.

    7. For each shadow-including inclusive descendant inclusiveDescendant of node, in shadow-including tree order:

  8. If suppress observers flag is unset, then queue a tree mutation record for parent with nodes, « », previousSibling, and child.

  9. Run the children changed steps for parent.

To append a node to a parent, pre-insert node into parent before null.

To replace a child with node within a parent, run these steps:

  1. If parent is not a Document, DocumentFragment, or Element node, then throw a "HierarchyRequestError" DOMException.

  2. If node is a host-including inclusive ancestor of parent, then throw a "HierarchyRequestError" DOMException.

  3. If child’s parent is not parent, then throw a "NotFoundError" DOMException.

  4. If node is not a DocumentFragment, DocumentType, Element, Text, ProcessingInstruction, or Comment node, then throw a "HierarchyRequestError" DOMException.

  5. If either node is a Text node and parent is a document, or node is a doctype and parent is not a document, then throw a "HierarchyRequestError" DOMException.

  6. If parent is a document, and any of the statements below, switched on node, are true, then throw a "HierarchyRequestError" DOMException.

    DocumentFragment node

    If node has more than one element child or has a Text node child.

    Otherwise, if node has one element child and either parent has an element child that is not child or a doctype is following child.

    element

    parent has an element child that is not child or a doctype is following child.

    doctype

    parent has a doctype child that is not child, or an element is preceding child.

    The above statements differ from the pre-insert algorithm.

  7. Let referenceChild be child’s next sibling.

  8. If referenceChild is node, then set referenceChild to node’s next sibling.

  9. Let previousSibling be child’s previous sibling.

  10. Let removedNodes be the empty set.

  11. If child’s parent is non-null, then:

    1. Set removedNodes to « child ».

    2. Remove child with the suppress observers flag set.

    The above can only be false if child is node.

  12. Let nodes be node’s children if node is a DocumentFragment node; otherwise « node ».

  13. Insert node into parent before referenceChild with the suppress observers flag set.

  14. Queue a tree mutation record for parent with nodes, removedNodes, previousSibling, and referenceChild.

  15. Return child.

To replace all with a node within a parent, run these steps:

  1. Let removedNodes be parent’s children.

  2. Let addedNodes be the empty set.

  3. If node is a DocumentFragment node, then set addedNodes to node’s children.

  4. Otherwise, if node is non-null, set addedNodes to « node ».

  5. Remove all parent’s children, in tree order, with the suppress observers flag set.

  6. If node is non-null, then insert node into parent before null with the suppress observers flag set.

  7. If either addedNodes or removedNodes is not empty, then queue a tree mutation record for parent with addedNodes, removedNodes, null, and null.

This algorithm does not make any checks with regards to the node tree constraints. Specification authors need to use it wisely.

To a child from a parent, run these steps:

Specifications may define for all or some nodes. The
algorithm is passed removedNode, and optionally oldParent, as
indicated in the remove algorithm below.



Read Next page

Report Page