diff --git a/src/Readability.php b/src/Readability.php index e5f661a..f586909 100644 --- a/src/Readability.php +++ b/src/Readability.php @@ -1003,7 +1003,11 @@ class Readability implements LoggerAwareInterface } if ($this->hasSingleTagInsideElement($node, 'p') && $this->getLinkDensity($node) < 0.25) { - $newNode = $node->childNodes->item(0); + // In some cases when tidy is disabled the first item may not be a DOMElement so we apply a filter + $newNode = array_values(array_filter( + iterator_to_array($node->childNodes), + static fn ($childNode) => $childNode instanceof \DOMElement + ))[0]; $node->parentNode->replaceChild($newNode, $node); $nodesToScore[] = $newNode; } diff --git a/tests/ReadabilityTest.php b/tests/ReadabilityTest.php index e0844a1..cdaa69e 100644 --- a/tests/ReadabilityTest.php +++ b/tests/ReadabilityTest.php @@ -507,6 +507,19 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase $this->assertSame($expected, $method->invoke($readability, $node, $tag)); } + public function testDivToPElementWithComment(): void + { + $text = str_repeat('Padded real article text to reach decent length for content scoring threshold here. ', 8); + $html = '
some code snippet ' . $text . '