protected function replaceChild($string)
    {
        if (!empty($string)) {
            $newDocument = new Document($string);

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

        foreach ($this->node->childNodes as $node) {
            $this->node->removeChild($node);
        }

        if (!empty($string)) {
            $newNode = $this->node->ownerDocument->importNode($newDocument->getDocument()->documentElement, true);
            $this->node->appendChild($newNode);
        }

        return $this;
    }