diff options
-rw-r--r-- | src/Nodes/NodeTrait.php | 17 | ||||
-rw-r--r-- | src/Readability.php | 4 |
2 files changed, 19 insertions, 2 deletions
diff --git a/src/Nodes/NodeTrait.php b/src/Nodes/NodeTrait.php index 2fbd240..ccc82ab 100644 --- a/src/Nodes/NodeTrait.php +++ b/src/Nodes/NodeTrait.php @@ -557,4 +557,21 @@ trait NodeTrait $count -= ($count - $nodes->length); } } + + /** + * Mimics JS's firstElementChild property. PHP only has firstChild which could be any type of DOMNode. Use this + * function to get the first one that is an DOMElement node. + * + * @return \DOMElement|null + */ + public function getFirstElementChild() + { + foreach ($this->childNodes as $node) { + if ($node instanceof \DOMElement) { + return $node; + } + } + + return null; + } } diff --git a/src/Readability.php b/src/Readability.php index f31ed75..1c24633 100644 --- a/src/Readability.php +++ b/src/Readability.php @@ -1281,9 +1281,9 @@ class Readability /** @var DOMNode $table */ $tbody = $table->hasSingleTagInsideElement('tbody') ? $table->childNodes[0] : $table; if ($tbody->hasSingleTagInsideElement('tr')) { - $row = $tbody->firstChild; + $row = $tbody->getFirstElementChild(); if ($row->hasSingleTagInsideElement('td')) { - $cell = $row->firstChild; + $cell = $row->getFirstElementChild(); $cell = NodeUtility::setNodeTag($cell, (array_reduce(iterator_to_array($cell->childNodes), function ($carry, $node) { return $node->isPhrasingContent() && $carry; }, true)) ? 'p' : 'div'); |