mirror of
https://github.com/j0k3r/php-readability.git
synced 2026-09-27 06:26:17 +00:00
Fix "Undefined array key 0" error
If a matching element isn't the first child (e.g. preceded by a whitespace text node), it may sit at a key other than 0 after some cleanup, leading to a "Undefined array key 0" error. This fix is now covered by 6 tests. Fixes #96 Signed-off-by: Kevin Decherf <kevin@kdecherf.com>
This commit is contained in:
+8
-1
@@ -1483,8 +1483,15 @@ class Readability implements LoggerAwareInterface
|
||||
$childNodes = iterator_to_array($node->childNodes);
|
||||
$children = array_filter($childNodes, static fn ($childNode) => $childNode instanceof \DOMElement);
|
||||
|
||||
// array_first() has been added in PHP 8.5, failing back for older versions
|
||||
if (!\function_exists('array_first')) {
|
||||
$firstChild = $children ? $children[array_key_first($children)] : null;
|
||||
} else {
|
||||
$firstChild = array_first($children);
|
||||
}
|
||||
|
||||
// There should be exactly 1 element child with given tag
|
||||
if (1 !== \count($children) || $children[0]->nodeName !== $tag) {
|
||||
if (1 !== \count($children) || $firstChild->nodeName !== $tag) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user