src/Range.php
/**
     * Inserts a new Node into at the start of the Range.
     *
     * @see https://dom.spec.whatwg.org/#dom-range-insertnode
     *
     * @throws \Rowbot\DOM\Exception\HierarchyRequestError
     */
Inserts a new Node into at the start of the Range.


Inserts a new Node into at the start of the Range .
public function insertNode(Node $node): void
    {
        if (
            $this->startNode instanceof ProcessingInstruction || $this->startNode instanceof Comment
            || ($this->startNode instanceof Text && $this->startNode->parentNode === null)
            || $this->startNode === $node
        ) {
            throw new HierarchyRequestError();
        }
        $referenceNode = null;
        if ($this->startNode instanceof Text) {
            $referenceNode = $this->startNode;
        } else {
            $referenceNode = $this->startNode->childNodes[$this->startOffset] ?? null;
        }
        $parent = !$referenceNode
            ? $this->startNode
            : $referenceNode->parentNode;
        $parent->ensurePreinsertionValidity($node, $referenceNode);
        if ($this->startNode instanceof Text) {
            $referenceNode = $this->startNode->splitText($this->startOffset);
        }
        if ($node === $referenceNode) {
            $referenceNode = $referenceNode->nextSibling;
        }
        if ($node->parentNode) {
            $node->removeNode();
        }
        $newOffset = !$referenceNode
            ? $parent->getLength()
            : $referenceNode->getTreeIndex();
        $newOffset += $node instanceof DocumentFragment
            ? $node->getLength()
            : 1;
        $parent->preinsertNode($node, $referenceNode);
        if ($this->startNode === $this->endNode && $this->startOffset === $this->endOffset) {
            $this->endNode = $parent;
            $this->endOffset = $newOffset;
        }
    }
public function insertNode ( Node $ node ) : void { if ( $ this -> startNode instanceof ProcessingInstruction || $ this -> startNode instanceof Comment || ( $ this -> startNode instanceof Text && $ this -> startNode -> parentNode === null ) || $ this -> startNode === $ node ) { throw new HierarchyRequestError ( ) ; } $ referenceNode = null ; if ( $ this -> startNode instanceof Text ) { $ referenceNode = $ this -> startNode ; } else { $ referenceNode = $ this -> startNode -> childNodes [ $ this -> startOffset ] ?? null ; } $ parent = ! $ referenceNode ? $ this -> startNode : $ referenceNode -> parentNode ; $ parent -> ensurePreinsertionValidity ( $ node , $ referenceNode ) ; if ( $ this -> startNode instanceof Text ) { $ referenceNode = $ this -> startNode -> splitText ( $ this -> startOffset ) ; } if ( $ node === $ referenceNode ) { $ referenceNode = $ referenceNode -> nextSibling ; } if ( $ node -> parentNode ) { $ node -> removeNode ( ) ; } $ newOffset = ! $ referenceNode ? $ parent -> getLength ( ) : $ referenceNode -> getTreeIndex ( ) ; $ newOffset += $ node instanceof DocumentFragment ? $ node -> getLength ( ) : 1 ; $ parent -> preinsertNode ( $ node , $ referenceNode ) ; if ( $ this -> startNode === $ this -> endNode && $ this -> startOffset === $ this -> endOffset ) { $ this -> endNode = $ parent ; $ this -> endOffset = $ newOffset ; } }
Inserts a new Node into at the start of the Range.
Inserts a new Node into at the start of the Range .