mirror of
https://github.com/j0k3r/php-readability.git
synced 2026-09-27 06:26:17 +00:00
Merge pull request #123 from Kdecherf/fix/singletag-type-error
Fix a TypeError when tidy is disabled
This commit is contained in:
+5
-1
@@ -1003,7 +1003,11 @@ class Readability implements LoggerAwareInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($this->hasSingleTagInsideElement($node, 'p') && $this->getLinkDensity($node) < 0.25) {
|
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);
|
$node->parentNode->replaceChild($newNode, $node);
|
||||||
$nodesToScore[] = $newNode;
|
$nodesToScore[] = $newNode;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -507,6 +507,19 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$this->assertSame($expected, $method->invoke($readability, $node, $tag));
|
$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 = '<div><!-- comment --><code>some code snippet</code> ' . $text . '</div>';
|
||||||
|
|
||||||
|
$readability = $this->getReadability($html, 'http://0.0.0.0', 'libxml', false);
|
||||||
|
$res = $readability->init();
|
||||||
|
|
||||||
|
$this->assertTrue($res);
|
||||||
|
$this->assertStringContainsString('some code snippet', $readability->getContent()->getInnerHtml());
|
||||||
|
$this->assertStringContainsString('Padded real article text', $readability->getContent()->getInnerHtml());
|
||||||
|
}
|
||||||
|
|
||||||
public function testKeepFootnotes(): void
|
public function testKeepFootnotes(): void
|
||||||
{
|
{
|
||||||
// from https://www.schreibdichte.de/blog/feed-aggregator-und-spaeter-lesen-dienst-im-team
|
// from https://www.schreibdichte.de/blog/feed-aggregator-und-spaeter-lesen-dienst-im-team
|
||||||
|
|||||||
Reference in New Issue
Block a user