Flake\Core\DOM\HTMLElement::setInnerHTML PHP Method

setInnerHTML() public method

public setInnerHTML ( $sHtml )
    function setInnerHTML($sHtml)
    {
        // first, empty the element
        for ($x = $this->childNodes->length - 1; $x >= 0; $x--) {
            $this->removeChild($this->childNodes->item($x));
        }
        // $value holds our new inner HTML
        if ($sHtml != '') {
            $f = $this->ownerDocument->createDocumentFragment();
            // appendXML() expects well-formed markup (XHTML)
            $result = @$f->appendXML($sHtml);
            // @ to suppress PHP warnings
            if ($result) {
                if ($f->hasChildNodes()) {
                    $this->appendChild($f);
                }
            } else {
                // $value is probably ill-formed
                $f = new \DOMDocument();
                $sHtml = mb_convert_encoding($sHtml, 'HTML-ENTITIES', 'UTF-8');
                // Using <htmlfragment> will generate a warning, but so will bad HTML
                // (and by this point, bad HTML is what we've got).
                // We use it (and suppress the warning) because an HTML fragment will
                // be wrapped around <html><body> tags which we don't really want to keep.
                // Note: despite the warning, if loadHTML succeeds it will return true.
                $result = @$f->loadHTML('<htmlfragment>' . $sHtml . '</htmlfragment>');
                if ($result) {
                    $import = $f->getElementsByTagName('htmlfragment')->item(0);
                    foreach ($import->childNodes as $child) {
                        $importedNode = $this->ownerDocument->importNode($child, true);
                        $this->appendChild($importedNode);
                    }
                } else {
                    // oh well, we tried, we really did. :(
                    // this element is now empty
                }
            }
        }
    }