Skip to content

Commit

Permalink
Merge pull request #88 from jtojnar/has-single-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
j0k3r committed Mar 19, 2024
2 parents 2912276 + 677f3f0 commit cb6b6ac
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Readability.php
Expand Up @@ -1477,14 +1477,23 @@ private function isPhrasingContent($node): bool
);
}

/**
* Checks if `$node` has only whitespace and a single element with `$tag` for the tag name.
* Returns false if `$node` contains non-empty text nodes
* or if it contains no element with given tag or more than 1 element.
*/
private function hasSingleTagInsideElement(\DOMElement $node, string $tag): bool
{
if (1 !== $node->childNodes->length || $node->childNodes->item(0)->nodeName !== $tag) {
$childNodes = iterator_to_array($node->childNodes);
$children = array_filter($childNodes, fn ($childNode) => $childNode instanceof \DOMElement);

// There should be exactly 1 element child with given tag
if (1 !== \count($children) || $children[0]->nodeName !== $tag) {
return false;
}

$a = array_filter(
iterator_to_array($node->childNodes),
$childNodes,
fn ($childNode) => $childNode instanceof \DOMText && preg_match($this->regexps['hasContent'], $this->getInnerText($childNode))
);

Expand Down

0 comments on commit cb6b6ac

Please sign in to comment.