src/Text.php
/**
     * Splits the text at the given offset.
     *
     * @see https://dom.spec.whatwg.org/#dom-text-splittext
     *
     * @throws \Rowbot\DOM\Exception\IndexSizeError
     */
Splits the text at the given offset.


Splits the text at the given offset .
public function splitText(int $offset): self
    {
        $length = $this->getLength();
        if ($offset > $length) {
            throw new IndexSizeError();
        }
        $count = $length - $offset;
        $newData = $this->substringData($offset, $count);
        $newNode = new Text($this->nodeDocument, $newData);
        if ($this->parentNode) {
            $this->parentNode->insertNode($newNode, $this->nextSibling);
            $treeIndex = $this->getTreeIndex();
            $ranges = [];
            foreach (Range::getRangeCollection() as $range) {
                $startNode = $range->startContainer;
                $endNode = $range->endContainer;
                if (
                    $startNode === $this
                    || $startNode === $this->parentNode
                    || $endNode === $this
                    || $endNode === $this->parentNode
                ) {
                    $ranges[] = [$range, $startNode, $endNode];
                }
            }
            foreach ($ranges as [$range, $startNode, $endNode]) {
                $startOffset = $range->startOffset;
                if ($startNode === $this && $startOffset > $offset) {
                    $range->setStartInternal($newNode, $startOffset - $offset);
                }
            }
            foreach ($ranges as [$range, $startNode, $endNode]) {
                $endOffset = $range->endOffset;
                if ($endNode === $this && $endOffset > $offset) {
                    $range->setEndInternal($newNode, $endOffset - $offset);
                }
            }
            foreach ($ranges as [$range, $startNode, $endNode]) {
                $startOffset = $range->startOffset;
                if ($startNode === $this->parentNode && $startOffset === $treeIndex + 1) {
                    $range->setStartInternal($startNode, $startOffset + 1);
                }
            }
            foreach ($ranges as [$range, $startNode, $endNode]) {
                $endOffset = $range->endOffset;
                if ($endNode === $this->parentNode && $endOffset === $treeIndex + 1) {
                    $range->setEndInternal($endNode, $endOffset + 1);
                }
            }
            unset($ranges);
        }
        $this->doReplaceData($offset, $count, '');
        return $newNode;
    }
public function splitText ( int $ offset ) : self { $ length = $ this -> getLength ( ) ; if ( $ offset > $ length ) { throw new IndexSizeError ( ) ; } $ count = $ length - $ offset ; $ newData = $ this -> substringData ( $ offset , $ count ) ; $ newNode = new Text ( $ this -> nodeDocument , $ newData ) ; if ( $ this -> parentNode ) { $ this -> parentNode -> insertNode ( $ newNode , $ this -> nextSibling ) ; $ treeIndex = $ this -> getTreeIndex ( ) ; $ ranges = [ ] ; foreach ( Range :: getRangeCollection ( ) as $ range ) { $ startNode = $ range -> startContainer ; $ endNode = $ range -> endContainer ; if ( $ startNode === $ this || $ startNode === $ this -> parentNode || $ endNode === $ this || $ endNode === $ this -> parentNode ) { $ ranges [ ] = [ $ range , $ startNode , $ endNode ] ; } } foreach ( $ ranges as [ $ range , $ startNode , $ endNode ] ) { $ startOffset = $ range -> startOffset ; if ( $ startNode === $ this && $ startOffset > $ offset ) { $ range -> setStartInternal ( $ newNode , $ startOffset - $ offset ) ; } } foreach ( $ ranges as [ $ range , $ startNode , $ endNode ] ) { $ endOffset = $ range -> endOffset ; if ( $ endNode === $ this && $ endOffset > $ offset ) { $ range -> setEndInternal ( $ newNode , $ endOffset - $ offset ) ; } } foreach ( $ ranges as [ $ range , $ startNode , $ endNode ] ) { $ startOffset = $ range -> startOffset ; if ( $ startNode === $ this -> parentNode && $ startOffset === $ treeIndex + 1 ) { $ range -> setStartInternal ( $ startNode , $ startOffset + 1 ) ; } } foreach ( $ ranges as [ $ range , $ startNode , $ endNode ] ) { $ endOffset = $ range -> endOffset ; if ( $ endNode === $ this -> parentNode && $ endOffset === $ treeIndex + 1 ) { $ range -> setEndInternal ( $ endNode , $ endOffset + 1 ) ; } } unset ( $ ranges ) ; } $ this -> doReplaceData ( $ offset , $ count , '' ) ; return $ newNode ; }
Splits the text at the given offset.
Splits the text at the given offset .