Skip to content

Commit 92024df

Browse files
committed
fixup! Use JSLikeHTMLElement in type hints
1 parent 063247b commit 92024df

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

src/Readability.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -987,9 +987,9 @@ protected function initializeNode(\DOMElement $node): void
987987
* Using a variety of metrics (content score, classname, element types), find the content that is
988988
* most likely to be the stuff a user wants to read. Then return it wrapped up in a div.
989989
*
990-
* @return \DOMElement|false
990+
* @return JSLikeHTMLElement|false
991991
*/
992-
protected function grabArticle(?\DOMElement $page = null)
992+
protected function grabArticle(?JSLikeHTMLElement $page = null)
993993
{
994994
if (!$page) {
995995
$page = $this->dom;
@@ -1166,7 +1166,7 @@ protected function grabArticle(?\DOMElement $page = null)
11661166
for ($c = $candidates->length - 1; $c >= 0; --$c) {
11671167
$node = $candidates->item($c);
11681168
// node should be readable but not inside of an article otherwise it's probably non-readable block
1169-
if ($node->hasAttribute('readability') && (int) $node->getAttributeNode('readability')->value < 40 && ($node->parentNode instanceof \DOMElement ? 0 !== strcasecmp($node->parentNode->tagName, 'article') : true)) {
1169+
if ($node->hasAttribute('readability') && (int) $node->getAttributeNode('readability')->value < 40 && ($node->parentNode instanceof JSLikeHTMLElement ? 0 !== strcasecmp($node->parentNode->tagName, 'article') : true)) {
11701170
$this->logger->debug('Removing unlikely candidate (using note) ' . $node->getNodePath() . ' by "' . $node->tagName . '" with readability ' . self::getContentScore($node));
11711171
$node->parentNode->removeChild($node);
11721172
}
@@ -1302,7 +1302,7 @@ protected function grabArticle(?\DOMElement $page = null)
13021302
if (0 === strcasecmp($tagName, 'td') || 0 === strcasecmp($tagName, 'tr')) {
13031303
$up = $topCandidate;
13041304

1305-
if ($up->parentNode instanceof \DOMElement) {
1305+
if ($up->parentNode instanceof JSLikeHTMLElement) {
13061306
$up = $up->parentNode;
13071307

13081308
if (0 === strcasecmp($up->tagName, 'table')) {
@@ -1327,19 +1327,19 @@ protected function grabArticle(?\DOMElement $page = null)
13271327
$siblingNode = $siblingNodes->item($s);
13281328
$siblingNodeName = $siblingNode->nodeName;
13291329
$append = false;
1330-
$this->logger->debug('Looking at sibling node: ' . $siblingNode->getNodePath() . (($siblingNode instanceof \DOMElement && $siblingNode->hasAttribute('readability')) ? (' with score ' . $siblingNode->getAttribute('readability')) : ''));
1330+
$this->logger->debug('Looking at sibling node: ' . $siblingNode->getNodePath() . (($siblingNode instanceof JSLikeHTMLElement && $siblingNode->hasAttribute('readability')) ? (' with score ' . $siblingNode->getAttribute('readability')) : ''));
13311331

13321332
if ($siblingNode->isSameNode($topCandidate)) {
13331333
$append = true;
13341334
} else {
13351335
$contentBonus = 0;
13361336

13371337
// Give a bonus if sibling nodes and top candidates have the same classname.
1338-
if ($siblingNode instanceof \DOMElement && $siblingNode->getAttribute('class') === $topCandidate->getAttribute('class') && '' !== $topCandidate->getAttribute('class')) {
1338+
if ($siblingNode instanceof JSLikeHTMLElement && $siblingNode->getAttribute('class') === $topCandidate->getAttribute('class') && '' !== $topCandidate->getAttribute('class')) {
13391339
$contentBonus += ((int) $topCandidate->getAttribute('readability')) * 0.2;
13401340
}
13411341

1342-
if ($siblingNode instanceof \DOMElement && $siblingNode->hasAttribute('readability') && (((int) $siblingNode->getAttribute('readability')) + $contentBonus) >= $siblingScoreThreshold) {
1342+
if ($siblingNode instanceof JSLikeHTMLElement && $siblingNode->hasAttribute('readability') && (((int) $siblingNode->getAttribute('readability')) + $contentBonus) >= $siblingScoreThreshold) {
13431343
$append = true;
13441344
} elseif (0 === strcasecmp($siblingNodeName, 'p')) {
13451345
$linkDensity = (int) $this->getLinkDensity($siblingNode);
@@ -1426,7 +1426,7 @@ protected function grabArticle(?\DOMElement $page = null)
14261426
* Get an element weight by attribute.
14271427
* Uses regular expressions to tell if this element looks good or bad.
14281428
*/
1429-
protected function weightAttribute(\DOMElement $element, string $attribute): int
1429+
protected function weightAttribute(JSLikeHTMLElement $element, string $attribute): int
14301430
{
14311431
if (!$element->hasAttribute($attribute)) {
14321432
return 0;
@@ -1470,7 +1470,7 @@ protected function reinitBody(): void
14701470
*
14711471
* @param callable(float): float $f
14721472
*/
1473-
private static function updateContentScore(\DOMElement $element, callable $f): void
1473+
private static function updateContentScore(JSLikeHTMLElement $element, callable $f): void
14741474
{
14751475
$readabilityAttr = $element->getAttributeNode('readability');
14761476
$prevScore = (float) $readabilityAttr->value;
@@ -1480,7 +1480,7 @@ private static function updateContentScore(\DOMElement $element, callable $f): v
14801480
/**
14811481
* Gets the content score for given element.
14821482
*/
1483-
private static function getContentScore(\DOMElement $element): float
1483+
private static function getContentScore(JSLikeHTMLElement $element): float
14841484
{
14851485
return $element->hasAttribute('readability') ? (float) $element->getAttribute('readability') : 0;
14861486
}
@@ -1552,11 +1552,11 @@ private function loadHtml(): void
15521552
$this->dom->registerNodeClass(\DOMElement::class, JSLikeHTMLElement::class);
15531553
}
15541554

1555-
private function getAncestors(\DOMElement $node, int $maxDepth = 0): array
1555+
private function getAncestors(JSLikeHTMLElement $node, int $maxDepth = 0): array
15561556
{
15571557
$ancestors = [];
15581558
$i = 0;
1559-
while ($node->parentNode instanceof \DOMElement) {
1559+
while ($node->parentNode instanceof JSLikeHTMLElement) {
15601560
$ancestors[] = $node->parentNode;
15611561
if (++$i === $maxDepth) {
15621562
break;
@@ -1589,10 +1589,10 @@ private function isPhrasingContent($node): bool
15891589
* Returns false if `$node` contains non-empty text nodes
15901590
* or if it contains no element with given tag or more than 1 element.
15911591
*/
1592-
private function hasSingleTagInsideElement(\DOMElement $node, string $tag): bool
1592+
private function hasSingleTagInsideElement(JSLikeHTMLElement $node, string $tag): bool
15931593
{
15941594
$childNodes = iterator_to_array($node->childNodes);
1595-
$children = array_filter($childNodes, fn ($childNode) => $childNode instanceof \DOMElement);
1595+
$children = array_filter($childNodes, fn ($childNode) => $childNode instanceof JSLikeHTMLElement);
15961596

15971597
// There should be exactly 1 element child with given tag
15981598
if (1 !== \count($children) || $children[0]->nodeName !== $tag) {
@@ -1613,7 +1613,7 @@ private function hasSingleTagInsideElement(\DOMElement $node, string $tag): bool
16131613
* Tidy must be configured to not clean the input for this function to
16141614
* work as expected, see $this->tidy_config['clean']
16151615
*/
1616-
private function isNodeVisible(\DOMElement $node): bool
1616+
private function isNodeVisible(JSLikeHTMLElement $node): bool
16171617
{
16181618
return !(
16191619
$node->hasAttribute('style')

0 commit comments

Comments
 (0)