Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/CssInliner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down Expand Up @@ -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 '';
}
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 2 additions & 15 deletions src/HtmlProcessor/AbstractHtmlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ abstract class AbstractHtmlProcessor
private $domDocument;

/**
* @var \DOMXPath|null
* @var \DOMXPath
Copy link
Copy Markdown
Collaborator

@JakeQZ JakeQZ May 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although this is not set by the constructor itself, since the constructor is both final and private, the property does not need to be nullable. So this change is valid.

*/
private $xPath = null;
protected $xPath;

/**
* The constructor.
Expand Down Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion src/HtmlProcessor/CssToAttributeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
4 changes: 2 additions & 2 deletions src/HtmlProcessor/HtmlPruner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down