JSLikeHTMLElement: Get rid of magic properties

Will make it easier to statically analyze with PHPStan.

This is a BC break.
pull/87/head
Jan Tojnar 2 years ago
parent 1acfc6fede
commit e9c9dbcc25
  1. 80
      src/JSLikeHTMLElement.php

@ -1,57 +1,41 @@
<?php <?php
// SPDX-FileCopyrightText: 2011 Keyvan Minoukadeh - http://www.keyvan.net - keyvan@keyvan.net
// SPDX-License-Identifier: Apache-2.0
namespace Readability; namespace Readability;
/** /**
* JavaScript-like HTML DOM Element. * Wrapper for DOMElement adding methods for accessing string representation of inner HTML contents.
* *
* This class extends PHP's DOMElement to allow * Inspired by JavaScript innerHTML property.
* users to get and set the innerHTML property of * https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML
* HTML elements in the same way it's done in
* JavaScript.
* *
* Example usage: * Example usage:
* require_once 'JSLikeHTMLElement.php'; *
* header('Content-Type: text/plain'); * ```php
* $doc = new DOMDocument(); * $doc = new DOMDocument();
* $doc->registerNodeClass('DOMElement', 'JSLikeHTMLElement');
* $doc->loadHTML('<div><p>Para 1</p><p>Para 2</p></div>'); * $doc->loadHTML('<div><p>Para 1</p><p>Para 2</p></div>');
* $elem = $doc->getElementsByTagName('div')->item(0); * $elem = $doc->getElementsByTagName('div')->item(0);
* *
* // print innerHTML * // Get inner HTML
* echo $elem->innerHTML; // prints '<p>Para 1</p><p>Para 2</p>' * assert($elem->getInnerHtml() === '<p>Para 1</p><p>Para 2</p>');
* echo "\n\n";
* *
* // set innerHTML * // Set inner HTML
* $elem->innerHTML = '<a href="http://fivefilters.org">FiveFilters.org</a>'; * $elem->setInnerHtml('<a href="http://fivefilters.org">FiveFilters.org</a>');
* echo $elem->innerHTML; // prints '<a href="http://fivefilters.org">FiveFilters.org</a>' * assert($elem->getInnerHtml() === '<a href="http://fivefilters.org">FiveFilters.org</a>');
* echo "\n\n";
* *
* // print document (with our changes) * // print document (with our changes)
* echo $doc->saveXML(); * 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 final class JSLikeHTMLElement extends \DOMElement
{ {
/** /**
* Used for setting innerHTML like it's done in JavaScript:. * Sets inner HTML.
*
* ```php
* $div->innerHTML = '<h2>Chapter 2</h2><p>The story begins...</p>';
* ```
*/ */
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 // first, empty the element
if (isset($this->childNodes)) { if (isset($this->childNodes)) {
for ($x = $this->childNodes->length - 1; $x >= 0; --$x) { for ($x = $this->childNodes->length - 1; $x >= 0; --$x) {
@ -81,7 +65,7 @@ class JSLikeHTMLElement extends \DOMElement
$f = new \DOMDocument(); $f = new \DOMDocument();
// Using <htmlfragment> will generate a warning, but so will bad HTML // Using <htmlfragment> 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 // 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. // 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. // Note: despite the warning, if loadHTML succeeds it will return true.
@ -102,15 +86,10 @@ class JSLikeHTMLElement extends \DOMElement
} }
/** /**
* Used for getting innerHTML like it's done in JavaScript:. * Gets inner HTML.
*
* ```php
* $string = $div->innerHTML;
* ```
*/ */
public function __get($name) public function getInnerHtml(): string
{ {
if ('innerHTML' === $name) {
$inner = ''; $inner = '';
if (isset($this->childNodes)) { if (isset($this->childNodes)) {
@ -121,23 +100,4 @@ class JSLikeHTMLElement extends \DOMElement
return $inner; 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);
}
} }

Loading…
Cancel
Save