From bdeb2612b367fb05c07d34750fe6576fa05b2298 Mon Sep 17 00:00:00 2001 From: Dustin Wilson Date: Thu, 7 Oct 2021 16:36:26 -0500 Subject: [PATCH] Adding property types and other PHP 7.4 stuff --- lib/Document.php | 26 +++++++++----------------- lib/DocumentFragment.php | 2 +- lib/Element.php | 4 ++-- lib/ElementMap.php | 4 ++-- lib/HTMLTemplateElement.php | 3 ++- lib/TokenList.php | 10 +++++----- 6 files changed, 21 insertions(+), 28 deletions(-) diff --git a/lib/Document.php b/lib/Document.php index d7d2049..0f8ed4c 100644 --- a/lib/Document.php +++ b/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]; diff --git a/lib/DocumentFragment.php b/lib/DocumentFragment.php index b14815b..72eb444 100644 --- a/lib/DocumentFragment.php +++ b/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) { diff --git a/lib/Element.php b/lib/Element.php index f142edc..12ecd1d 100644 --- a/lib/Element.php +++ b/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'); diff --git a/lib/ElementMap.php b/lib/ElementMap.php index dbf7700..cb51a7e 100644 --- a/lib/ElementMap.php +++ b/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 { diff --git a/lib/HTMLTemplateElement.php b/lib/HTMLTemplateElement.php index 7138f15..d89de56 100644 --- a/lib/HTMLTemplateElement.php +++ b/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 ?? ''); diff --git a/lib/TokenList.php b/lib/TokenList.php index d4cce98..d8af90e 100644 --- a/lib/TokenList.php +++ b/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 ]+/';