public function replace($newNode, $clone = true)
    {
        if ($this->node->parentNode === null) {
            throw new LogicException('Can not replace element without parent node');
        }

        if ($newNode instanceof Element) {
            $newNode = $newNode->getNode();
        }

        if (!$newNode instanceof DOMNode) {
            throw new InvalidArgumentException(sprintf('Argument 1 passed to %s must be an instance of %s or DOMNode, %s given', __METHOD__, __CLASS__, (is_object($newNode) ? get_class($newNode) : gettype($newNode))));
        }

        if ($clone) {
            $newNode = $newNode->cloneNode(true);
        }

        if ($newNode->ownerDocument === null || !$this->getDocument()->is($newNode->ownerDocument)) {
            $newNode = $this->node->ownerDocument->importNode($newNode, true);
        }

        $node = $this->node->parentNode->replaceChild($newNode, $this->node);

        return new Element($node);
    }
public function replace($newNode, $clone = true)
    {
        if ($this->node->parentNode === null) {
            throw new LogicException('Can not replace element without parent node');
        }

        if ($newNode instanceof Element) {
            $newNode = $newNode->getNode();
        }

        if (!$newNode instanceof DOMNode) {
            throw new InvalidArgumentException(sprintf('Argument 1 passed to %s must be an instance of %s or DOMNode, %s given', __METHOD__, __CLASS__, (is_object($newNode) ? get_class($newNode) : gettype($newNode))));
        }

        if ($clone) {
            $newNode = $newNode->cloneNode(true);
        }

        if ($newNode->ownerDocument === null || !$this->getDocument()->is($newNode->ownerDocument)) {
            $newNode = $this->node->ownerDocument->importNode($newNode, true);
        }

        $node = $this->node->parentNode->replaceChild($newNode, $this->node);

        return new Element($node);
    }
public function replace ( $ newNode , $ clone = true ) { if ( $ this -> node -> parentNode === null ) { throw new LogicException ( 'Can not replace element without parent node' ) ; } if ( $ newNode instanceof Element ) { $ newNode = $ newNode -> getNode ( ) ; } if ( ! $ newNode instanceof DOMNode ) { throw new InvalidArgumentException ( sprintf ( 'Argument 1 passed to %s must be an instance of %s or DOMNode, %s given' , __METHOD__ , __CLASS__ , ( is_object ( $ newNode ) ? get_class ( $ newNode ) : gettype ( $ newNode ) ) ) ) ; } if ( $ clone ) { $ newNode = $ newNode -> cloneNode ( true ) ; } if ( $ newNode -> ownerDocument === null || ! $ this -> getDocument ( ) -> is ( $ newNode -> ownerDocument ) ) { $ newNode = $ this -> node -> ownerDocument -> importNode ( $ newNode , true ) ; } $ node = $ this -> node -> parentNode -> replaceChild ( $ newNode , $ this -> node ) ; return new Element ( $ node ) ; }
Replaces a child.

@param \DOMNode|\DiDom\Element $newNode The new node
@param bool $clone Clone the node if true, otherwise move it

@return \DiDom\Element The node that has been replaced

@throws \LogicException if current node has no parent node
Replaces a child .