Browse Source

Adding property types and other PHP 7.4 stuff

wrapper-classes
Dustin Wilson 3 years ago
parent
commit
bdeb2612b3
  1. 26
      lib/Document.php
  2. 2
      lib/DocumentFragment.php
  3. 4
      lib/Element.php
  4. 4
      lib/ElementMap.php
  5. 3
      lib/HTMLTemplateElement.php
  6. 10
      lib/TokenList.php

26
lib/Document.php

@ -14,12 +14,12 @@ use MensBeam\HTML\Parser,
class Document extends \DOMDocument {
use DocumentOrElement, MagicProperties, ParentNode, Walk;
protected $_body = null;
protected ?Element $_body = null;
/** Nonstandard */
protected $_documentEncoding = null;
protected $_quirksMode = Parser::NO_QUIRKS_MODE;
protected ?string $_documentEncoding = null;
protected int $_quirksMode = Parser::NO_QUIRKS_MODE;
/** Nonstandard */
protected $_xpath = null;
protected ?\DOMXPath $_xpath = null;
// List of elements that are treated as block elements for the purposes of
// output formatting when serializing
@ -838,24 +838,16 @@ class Document extends \DOMDocument {
$node = $node->content;
}
$templates = $node->walk(function($n) {
if ($n instanceof Element && !$n instanceof HTMLTemplateElement && $this->isHTMLNamespace($n) && strtolower($n->nodeName) === 'template') {
return true;
}
});
// Yes, it seems weird to unpack a generator like this, but there is a need to
// iterate through them in reverse so nested templates can be handled properly.
// Also, this is slightly faster than using XPath to look for the templates;
// they would also need to be unpacked because the NodeList is live and would
// create an infinite loop if not unpacked to an array.
// TODO: Once support for 7.4 is okay this entire middle section here can be
// replaced with a spread operator above.
$temp = [];
foreach ($templates as $template) {
$temp[] = $template;
}
$templates = $temp;
$templates = [ ...$node->walk(function($n) {
if ($n instanceof Element && !$n instanceof HTMLTemplateElement && $this->isHTMLNamespace($n) && strtolower($n->nodeName) === 'template') {
return true;
}
}) ];
for ($templatesLength = count($templates), $i = $templatesLength - 1; $i >= 0; $i--) {
$template = $templates[$i];

2
lib/DocumentFragment.php

@ -12,7 +12,7 @@ namespace MensBeam\HTML\DOM;
class DocumentFragment extends \DOMDocumentFragment {
use MagicProperties, ParentNode, Walk;
protected $_host = null;
protected ?\WeakReference $_host = null;
protected function __get_host(): ?\DOMNode {
if ($this->_host === null) {

4
lib/Element.php

@ -13,10 +13,10 @@ use MensBeam\HTML\Parser;
class Element extends \DOMElement {
use DocumentOrElement, MagicProperties, Moonwalk, ParentNode, ToString, Walk;
protected $_classList;
protected ?TokenList $_classList = null;
protected function __get_classList(): ?TokenList {
protected function __get_classList(): TokenList {
// Only create the class list if it is actually used.
if ($this->_classList === null) {
$this->_classList = new TokenList($this, 'class');

4
lib/ElementMap.php

@ -18,8 +18,8 @@ class ElementMap {
// List of documents is necessary because when Document objects are destructed
// it's not possible to check for a document's existence without triggering a
// fatal error. Keeping document references around fixes that.
protected static $documents = [];
protected static $elements = [];
protected static array $documents = [];
protected static array $elements = [];
public static function add(Element $element): bool {

3
lib/HTMLTemplateElement.php

@ -10,7 +10,8 @@ namespace MensBeam\HTML\DOM;
/** Class specifically for template elements to handle its content property. */
class HTMLTemplateElement extends Element {
public $content = null;
public DocumentFragment $content;
public function __construct(Document $ownerDocument, string $qualifiedName, ?string $namespace = null) {
parent::__construct($qualifiedName, null, $namespace ?? '');

10
lib/TokenList.php

@ -11,14 +11,14 @@ namespace MensBeam\HTML\DOM;
class TokenList implements \ArrayAccess, \Countable, \Iterator {
use MagicProperties;
protected $localName;
protected $element;
protected string $localName;
protected \WeakReference $element;
protected $_length = 0;
protected $position = 0;
protected int $_length = 0;
protected int $position = 0;
# A DOMTokenList object has an associated token set (a set), which is initially
# empty.
protected $tokenSet = [];
protected array $tokenSet = [];
private const ASCII_WHITESPACE_REGEX = '/[\t\n\x0c\r ]+/';

Loading…
Cancel
Save