src/Range.php
/**
     * Wraps the content of Range in a new Node and inserts it in to the Document.
     *
     * @see https://dom.spec.whatwg.org/#dom-range-surroundcontents
     *
     * @throws \Rowbot\DOM\Exception\InvalidNodeTypeError
     * @throws \Rowbot\DOM\Exception\InvalidStateError
     */
Wraps the content of Range in a new Node and inserts it in to the Document.


Wraps the content of Range in a new Node and inserts it in to the Document .
public function surroundContents(Node $newParent): void
    {
        $commonAncestor = Node::getCommonAncestor($this->startNode, $this->endNode);
        if ($commonAncestor) {
            $tw = new TreeWalker($commonAncestor, NodeFilter::SHOW_ALL, function (Node $node): int {
                if (!$node instanceof Text && $this->isPartiallyContainedNode($node)) {
                    return NodeFilter::FILTER_ACCEPT;
                }
                return NodeFilter::FILTER_SKIP;
            });
            if ($tw->nextNode()) {
                throw new InvalidStateError();
            }
        }
        if (
            $newParent instanceof Document
            || $newParent instanceof DocumentType
            || $newParent instanceof DocumentFragment
        ) {
            throw new InvalidNodeTypeError();
        }
        $fragment = $this->extractContents();
        if ($newParent->hasChildNodes()) {
            $newParent->replaceAllNodes(null);
        }
        $this->insertNode($newParent);
        $newParent->appendChild($fragment);
        $this->selectNode($newParent);
    }
public function surroundContents ( Node $ newParent ) : void { $ commonAncestor = Node :: getCommonAncestor ( $ this -> startNode , $ this -> endNode ) ; if ( $ commonAncestor ) { $ tw = new TreeWalker ( $ commonAncestor , NodeFilter :: SHOW_ALL , function ( Node $ node ) : int { if ( ! $ node instanceof Text && $ this -> isPartiallyContainedNode ( $ node ) ) { return NodeFilter :: FILTER_ACCEPT ; } return NodeFilter :: FILTER_SKIP ; } ) ; if ( $ tw -> nextNode ( ) ) { throw new InvalidStateError ( ) ; } } if ( $ newParent instanceof Document || $ newParent instanceof DocumentType || $ newParent instanceof DocumentFragment ) { throw new InvalidNodeTypeError ( ) ; } $ fragment = $ this -> extractContents ( ) ; if ( $ newParent -> hasChildNodes ( ) ) { $ newParent -> replaceAllNodes ( null ) ; } $ this -> insertNode ( $ newParent ) ; $ newParent -> appendChild ( $ fragment ) ; $ this -> selectNode ( $ newParent ) ; }
Wraps the content of Range in a new Node and inserts it in to the Document.
Wraps the content of Range in a new Node and inserts it in to the Document .