/** * Load HTML or XML. * * @param string $string HTML or XML string or file path * @param bool $isFile Indicates that in first parameter was passed to the file path * @param string $type Type of document * @param int $options Additional parameters */ public function load($string, $isFile = false, $type = 'html', $options = 0) { if (!is_string($string)) { throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be string, %s given', __METHOD__, is_object($string) ? get_class($string) : gettype($string))); } if (!in_array(strtolower($type), ['xml', 'html'])) { throw new InvalidArgumentException(sprintf('Document type must be "xml" or "html", %s given', __METHOD__, is_object($type) ? get_class($type) : gettype($type))); } if (!is_integer($options)) { throw new InvalidArgumentException(sprintf('%s expects parameter 4 to be integer, %s given', __METHOD__, is_object($options) ? get_class($options) : gettype($options))); } $string = trim($string); if ($isFile) { $string = $this->loadFile($string); } if (substr($string, 0, 5) !== '<?xml') { $prolog = sprintf('<?xml version="1.0" encoding="%s"?>', $this->document->encoding); $string = $prolog . $string; } $this->type = strtolower($type); Errors::disable(); $this->type === 'xml' ? $this->document->loadXml($string, $options) : $this->document->loadHtml($string, $options); Errors::restore(); return $this; }
/** * Sets inner HTML. * * @param string $html * * @return Element */ public function setInnerHtml($html) { if (!is_string($html)) { throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be string, %s given', __METHOD__, is_object($html) ? get_class($html) : gettype($html))); } // remove all child nodes foreach ($this->node->childNodes as $node) { $this->node->removeChild($node); } if ($html !== '') { Errors::disable(); $html = "<htmlfragment>{$html}</htmlfragment>"; $document = new Document($html); $fragment = $document->first('htmlfragment')->getNode(); foreach ($fragment->childNodes as $node) { $newNode = $this->node->ownerDocument->importNode($node, true); $this->node->appendChild($newNode); } Errors::restore(); } return $this; }