src/Range.php
/**
     * Removes the contents of the Range from the Document.
     *
     * @see https://dom.spec.whatwg.org/#dom-range-deletecontents
     */
Removes the contents of the Range from the Document.
Removes the contents of the Range from the Document .
public function deleteContents(): void
    {
        if ($this->startNode === $this->endNode && $this->startOffset === $this->endOffset) {
            return;
        }
        $originalStartNode = $this->startNode;
        $originalStartOffset = $this->startOffset;
        $originalEndNode = $this->endNode;
        $originalEndOffset = $this->endOffset;
        if (
            $originalStartNode === $originalEndNode
            && ($originalStartNode instanceof Text
                || $originalStartNode instanceof ProcessingInstruction
                || $originalStartNode instanceof Comment)
        ) {
            $originalStartNode->doReplaceData(
                $originalStartOffset,
                $originalEndOffset - $originalStartOffset,
                ''
            );
            return;
        }
        $nodesToRemove = array();
        $tw = new TreeWalker(
            $originalStartNode,
            NodeFilter::SHOW_ALL,
            function (Node $node) {
                if (
                    $this->isFullyContainedNode($node)
                    && !$this->isFullyContainedNode($node->parentNode)
                ) {
                    return NodeFilter::FILTER_ACCEPT;
                }
                return NodeFilter::FILTER_SKIP;
            }
        );
        while ($node = $tw->nextNode()) {
            $nodesToRemove[] = $node;
        }
        if ($originalStartNode->contains($originalEndNode)) {
            $newNode = $originalStartNode;
            $newOffset = $originalStartOffset;
        } else {
            $referenceNode = $originalStartNode;
            while ($referenceNode) {
                if ($referenceNode->contains($originalEndNode)) {
                    break;
                }
                $referenceNode = $referenceNode->parentNode;
            }
            $newNode = $referenceNode->parentNode;
            $newOffset = $referenceNode->getTreeIndex() + 1;
        }
        if (
            $originalStartNode instanceof Text
            || $originalStartNode instanceof ProcessingInstruction
            || $originalStartNode instanceof Comment
        ) {
            $originalStartNode->doReplaceData(
                $originalStartOffset,
                $originalStartNode->length - $originalStartOffset,
                ''
            );
        }
        foreach ($nodesToRemove as $node) {
            $node->removeNode();
        }
        if (
            $originalEndNode instanceof Text
            || $originalEndNode instanceof ProcessingInstruction
            || $originalEndNode instanceof Comment
        ) {
            $originalEndNode->doReplaceData(0, $originalEndOffset, '');
        }
        $this->startNode = $newNode;
        $this->startOffset = $newOffset;
        $this->endNode = $newNode;
        $this->endOffset = $newOffset;
    }
public function deleteContents ( ) : void { if ( $ this -> startNode === $ this -> endNode && $ this -> startOffset === $ this -> endOffset ) { return ; } $ originalStartNode = $ this -> startNode ; $ originalStartOffset = $ this -> startOffset ; $ originalEndNode = $ this -> endNode ; $ originalEndOffset = $ this -> endOffset ; if ( $ originalStartNode === $ originalEndNode && ( $ originalStartNode instanceof Text || $ originalStartNode instanceof ProcessingInstruction || $ originalStartNode instanceof Comment ) ) { $ originalStartNode -> doReplaceData ( $ originalStartOffset , $ originalEndOffset - $ originalStartOffset , '' ) ; return ; } $ nodesToRemove = array ( ) ; $ tw = new TreeWalker ( $ originalStartNode , NodeFilter :: SHOW_ALL , function ( Node $ node ) { if ( $ this -> isFullyContainedNode ( $ node ) && ! $ this -> isFullyContainedNode ( $ node -> parentNode ) ) { return NodeFilter :: FILTER_ACCEPT ; } return NodeFilter :: FILTER_SKIP ; } ) ; while ( $ node = $ tw -> nextNode ( ) ) { $ nodesToRemove [ ] = $ node ; } if ( $ originalStartNode -> contains ( $ originalEndNode ) ) { $ newNode = $ originalStartNode ; $ newOffset = $ originalStartOffset ; } else { $ referenceNode = $ originalStartNode ; while ( $ referenceNode ) { if ( $ referenceNode -> contains ( $ originalEndNode ) ) { break ; } $ referenceNode = $ referenceNode -> parentNode ; } $ newNode = $ referenceNode -> parentNode ; $ newOffset = $ referenceNode -> getTreeIndex ( ) + 1 ; } if ( $ originalStartNode instanceof Text || $ originalStartNode instanceof ProcessingInstruction || $ originalStartNode instanceof Comment ) { $ originalStartNode -> doReplaceData ( $ originalStartOffset , $ originalStartNode -> length - $ originalStartOffset , '' ) ; } foreach ( $ nodesToRemove as $ node ) { $ node -> removeNode ( ) ; } if ( $ originalEndNode instanceof Text || $ originalEndNode instanceof ProcessingInstruction || $ originalEndNode instanceof Comment ) { $ originalEndNode -> doReplaceData ( 0 , $ originalEndOffset , '' ) ; } $ this -> startNode = $ newNode ; $ this -> startOffset = $ newOffset ; $ this -> endNode = $ newNode ; $ this -> endOffset = $ newOffset ; }
Removes the contents of the Range from the Document.
Removes the contents of the Range from the Document .