protected function replaceNode($string)
    {
        if (empty($string)) {
            $this->node->parentNode->removeChild($this->node);

            return null;
        }

        $newDocument = new Document($string);

        if ($newDocument->outertext !== $string) {
            throw new RuntimeException('Not valid HTML fragment');
        }

        $newNode = $this->node->ownerDocument->importNode($newDocument->getDocument()->documentElement, true);

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

        return $this;
    }