mirror of
https://github.com/j0k3r/php-readability.git
synced 2026-09-27 06:26:17 +00:00
PHP-CS-Fixer 3.95.0 added `void_return` rule to `@Symfony:risky` ruleset. https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/releases/tag/v3.95.0 https://cs.symfony.com/doc/rules/function_notation/void_return.html `void` type declaration is available since PHP 7.1: https://www.php.net/manual/en/language.types.void.php Magic methods can have type annotations but they must be compatible with the default ones (not enforced prior to PHP 8.0): https://php.watch/versions/8.0/magic-method-signatures Let’s just type all the methods. This could technically be a BC break if someone is extending the `JSLikeHTMLElement` class, overriding the methods that previously did not have return type hint without adding it, and calling `registerNodeClass` on the public `dom` property but I think it is pretty unlikely – at least there seem to be no public instances on GitHub.
146 lines
4.4 KiB
PHP
146 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace Readability;
|
|
|
|
/**
|
|
* JavaScript-like HTML DOM Element.
|
|
*
|
|
* This class extends PHP's DOMElement to allow
|
|
* users to get and set the innerHTML property of
|
|
* HTML elements in the same way it's done in
|
|
* JavaScript.
|
|
*
|
|
* Example usage:
|
|
* require_once 'JSLikeHTMLElement.php';
|
|
* header('Content-Type: text/plain');
|
|
* $doc = new DOMDocument();
|
|
* $doc->registerNodeClass('DOMElement', 'JSLikeHTMLElement');
|
|
* $doc->loadHTML('<div><p>Para 1</p><p>Para 2</p></div>');
|
|
* $elem = $doc->getElementsByTagName('div')->item(0);
|
|
*
|
|
* // print innerHTML
|
|
* echo $elem->innerHTML; // prints '<p>Para 1</p><p>Para 2</p>'
|
|
* echo "\n\n";
|
|
*
|
|
* // set innerHTML
|
|
* $elem->innerHTML = '<a href="http://fivefilters.org">FiveFilters.org</a>';
|
|
* echo $elem->innerHTML; // prints '<a href="http://fivefilters.org">FiveFilters.org</a>'
|
|
* echo "\n\n";
|
|
*
|
|
* // print document (with our changes)
|
|
* echo $doc->saveXML();
|
|
*
|
|
* @author Keyvan Minoukadeh - http://www.keyvan.net - keyvan@keyvan.net
|
|
*
|
|
* @see http://fivefilters.org (the project this was written for)
|
|
*/
|
|
class JSLikeHTMLElement extends \DOMElement
|
|
{
|
|
/**
|
|
* Used for setting innerHTML like it's done in JavaScript:.
|
|
*
|
|
* ```php
|
|
* $div->innerHTML = '<h2>Chapter 2</h2><p>The story begins...</p>';
|
|
* ```
|
|
*/
|
|
public function __set(string $name, 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) {
|
|
$this->removeChild($this->childNodes->item($x));
|
|
}
|
|
}
|
|
|
|
// $value holds our new inner HTML
|
|
$value = trim($value);
|
|
if (empty($value)) {
|
|
return;
|
|
}
|
|
|
|
// ensure bad entity won't generate warning
|
|
$previousError = libxml_use_internal_errors(true);
|
|
|
|
$f = $this->ownerDocument->createDocumentFragment();
|
|
|
|
// appendXML() expects well-formed markup (XHTML)
|
|
$result = $f->appendXML($value);
|
|
if ($result) {
|
|
if ($f->hasChildNodes()) {
|
|
$this->appendChild($f);
|
|
}
|
|
} else {
|
|
// $value is probably ill-formed
|
|
$f = new \DOMDocument();
|
|
|
|
// 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('<meta charset="utf-8"><htmlfragment>' . $value . '</htmlfragment>');
|
|
|
|
if ($result) {
|
|
$import = $f->getElementsByTagName('htmlfragment')->item(0);
|
|
|
|
foreach ($import->childNodes as $child) {
|
|
$importedNode = $this->ownerDocument->importNode($child, true);
|
|
$this->appendChild($importedNode);
|
|
}
|
|
}
|
|
}
|
|
|
|
libxml_clear_errors();
|
|
libxml_use_internal_errors($previousError);
|
|
}
|
|
|
|
/**
|
|
* Used for getting innerHTML like it's done in JavaScript:.
|
|
*
|
|
* ```php
|
|
* $string = $div->innerHTML;
|
|
* ```
|
|
*/
|
|
public function __get(string $name): string
|
|
{
|
|
if ('innerHTML' === $name) {
|
|
$inner = '';
|
|
|
|
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);
|
|
|
|
return '';
|
|
}
|
|
|
|
public function __toString(): string
|
|
{
|
|
return '[' . $this->tagName . ']';
|
|
}
|
|
|
|
public function getInnerHtml(): string
|
|
{
|
|
return $this->__get('innerHTML');
|
|
}
|
|
|
|
public function setInnerHtml(string $value): void
|
|
{
|
|
$this->__set('innerHTML', $value);
|
|
}
|
|
}
|