diff --git a/src/CssInliner.php b/src/CssInliner.php index 4417fd5f..ace1de64 100644 --- a/src/CssInliner.php +++ b/src/CssInliner.php @@ -417,7 +417,7 @@ private function normalizeStyleAttributesOfAllNodes(): void private function getAllNodesWithStyleAttribute(): \DOMNodeList { $query = '//*[@style]'; - $matches = $this->getXPath()->query($query); + $matches = $this->xPath->query($query); \assert($matches instanceof \DOMNodeList); /** @var \DOMNodeList<\DOMElement> $matches */ @@ -467,7 +467,7 @@ private static function normalizePropertyNameCallback(array $matches): string */ private function getCssFromAllStyleNodes(): string { - $styleNodes = $this->getXPath()->query('//style'); + $styleNodes = $this->xPath->query('//style'); if ($styleNodes === false) { return ''; } @@ -518,7 +518,7 @@ private function getNodesToExclude(): array private function querySelectorAll(string $selectors, array $options = []): \DOMNodeList { try { - $result = $this->getXPath()->query($this->getCssSelectorConverter()->toXPath($selectors)); + $result = $this->xPath->query($this->getCssSelectorConverter()->toXPath($selectors)); \assert($result instanceof \DOMNodeList); } catch (ParseException $exception) { $alwaysThrowParseException = $options[self::QSA_ALWAYS_THROW_PARSE_EXCEPTION] ?? false; diff --git a/src/HtmlProcessor/AbstractHtmlProcessor.php b/src/HtmlProcessor/AbstractHtmlProcessor.php index 67bdf82e..ef1f8194 100644 --- a/src/HtmlProcessor/AbstractHtmlProcessor.php +++ b/src/HtmlProcessor/AbstractHtmlProcessor.php @@ -50,9 +50,9 @@ abstract class AbstractHtmlProcessor private $domDocument; /** - * @var \DOMXPath|null + * @var \DOMXPath */ - private $xPath = null; + protected $xPath; /** * The constructor. @@ -124,19 +124,6 @@ private function setDomDocument(\DOMDocument $domDocument): void $this->xPath = new \DOMXPath($domDocument); } - /** - * @throws \UnexpectedValueException - */ - protected function getXPath(): \DOMXPath - { - if (!$this->xPath instanceof \DOMXPath) { - $message = self::class . '::setDomDocument() has not yet been called on ' . static::class; - throw new \UnexpectedValueException($message, 1617819086); - } - - return $this->xPath; - } - /** * Renders the normalized and processed HTML. * diff --git a/src/HtmlProcessor/CssToAttributeConverter.php b/src/HtmlProcessor/CssToAttributeConverter.php index 496d5ae6..62e8f9a0 100644 --- a/src/HtmlProcessor/CssToAttributeConverter.php +++ b/src/HtmlProcessor/CssToAttributeConverter.php @@ -77,7 +77,7 @@ public function convertCssToVisualAttributes(): self */ private function getAllNodesWithStyleAttribute(): \DOMNodeList { - $result = $this->getXPath()->query('//*[@style]'); + $result = $this->xPath->query('//*[@style]'); \assert($result instanceof \DOMNodeList); /** @var \DOMNodeList<\DOMElement> $result */ diff --git a/src/HtmlProcessor/HtmlPruner.php b/src/HtmlProcessor/HtmlPruner.php index 624567ab..1081f996 100644 --- a/src/HtmlProcessor/HtmlPruner.php +++ b/src/HtmlProcessor/HtmlPruner.php @@ -32,7 +32,7 @@ final class HtmlPruner extends AbstractHtmlProcessor */ public function removeElementsWithDisplayNone(): self { - $elementsWithStyleDisplayNone = $this->getXPath()->query(self::DISPLAY_NONE_MATCHER); + $elementsWithStyleDisplayNone = $this->xPath->query(self::DISPLAY_NONE_MATCHER); \assert($elementsWithStyleDisplayNone instanceof \DOMNodeList); if ($elementsWithStyleDisplayNone->length === 0) { return $this; @@ -65,7 +65,7 @@ public function removeElementsWithDisplayNone(): self public function removeRedundantClasses(array $classesToKeep = []): self { /** @var \DOMNodeList<\DOMElement> $elementsWithClassAttribute */ - $elementsWithClassAttribute = $this->getXPath()->query('//*[@class]'); + $elementsWithClassAttribute = $this->xPath->query('//*[@class]'); if ($classesToKeep !== []) { $this->removeClassesFromElements($elementsWithClassAttribute, $classesToKeep);