diff --git a/src/JSLikeHTMLElement.php b/src/JSLikeHTMLElement.php index 18116a6..23585ff 100644 --- a/src/JSLikeHTMLElement.php +++ b/src/JSLikeHTMLElement.php @@ -1,57 +1,41 @@ registerNodeClass('DOMElement', 'JSLikeHTMLElement'); - * $doc->loadHTML('

Para 1

Para 2

'); - * $elem = $doc->getElementsByTagName('div')->item(0); - * - * // print innerHTML - * echo $elem->innerHTML; // prints '

Para 1

Para 2

' - * echo "\n\n"; * - * // set innerHTML - * $elem->innerHTML = 'FiveFilters.org'; - * echo $elem->innerHTML; // prints 'FiveFilters.org' - * echo "\n\n"; + * ```php + * $doc = new DOMDocument(); + * $doc->loadHTML('

Para 1

Para 2

'); + * $elem = $doc->getElementsByTagName('div')->item(0); * - * // print document (with our changes) - * echo $doc->saveXML(); + * // Get inner HTML + * assert($elem->getInnerHtml() === '

Para 1

Para 2

'); * - * @author Keyvan Minoukadeh - http://www.keyvan.net - keyvan@keyvan.net + * // Set inner HTML + * $elem->setInnerHtml('FiveFilters.org'); + * assert($elem->getInnerHtml() === 'FiveFilters.org'); * - * @see http://fivefilters.org (the project this was written for) + * // print document (with our changes) + * echo $doc->saveXML(); + * ``` */ -class JSLikeHTMLElement extends \DOMElement +final class JSLikeHTMLElement extends \DOMElement { /** - * Used for setting innerHTML like it's done in JavaScript:. - * - * ```php - * $div->innerHTML = '

Chapter 2

The story begins...

'; - * ``` + * Sets inner HTML. */ - public function __set($name, $value) + public function setInnerHtml(string $value): void { - if ('innerHTML' !== $name) { - $trace = debug_backtrace(); - trigger_error('Undefined property via __set(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], \E_USER_NOTICE); - - return; - } - // first, empty the element if (isset($this->childNodes)) { for ($x = $this->childNodes->length - 1; $x >= 0; --$x) { @@ -81,7 +65,7 @@ class JSLikeHTMLElement extends \DOMElement $f = new \DOMDocument(); // Using will generate a warning, but so will bad HTML - // (and by this point, bad HTML is what we've got). + // (and by element point, bad HTML is what we've got). // We use it (and suppress the warning) because an HTML fragment will // be wrapped around tags which we don't really want to keep. // Note: despite the warning, if loadHTML succeeds it will return true. @@ -102,42 +86,18 @@ class JSLikeHTMLElement extends \DOMElement } /** - * Used for getting innerHTML like it's done in JavaScript:. - * - * ```php - * $string = $div->innerHTML; - * ``` + * Gets inner HTML. */ - public function __get($name) + public function getInnerHtml(): string { - if ('innerHTML' === $name) { - $inner = ''; + $inner = ''; - if (isset($this->childNodes)) { - foreach ($this->childNodes as $child) { - $inner .= $this->ownerDocument->saveXML($child); - } + if (isset($this->childNodes)) { + foreach ($this->childNodes as $child) { + $inner .= $this->ownerDocument->saveXML($child); } - - return $inner; } - $trace = debug_backtrace(); - trigger_error('Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], \E_USER_NOTICE); - } - - public function __toString() - { - return '[' . $this->tagName . ']'; - } - - public function getInnerHtml() - { - return $this->__get('innerHTML'); - } - - public function setInnerHtml($value) - { - return $this->__set('innerHTML', $value); + return $inner; } }