From 8bb837903ba8305c75fb1681ee8c9c4cb7449c5d Mon Sep 17 00:00:00 2001 From: Andres Rey Date: Sat, 19 Nov 2016 20:54:01 +0000 Subject: Fixed incorrect approach when counting nodeLists --- src/HTMLParser.php | 8 +++++++- src/Readability.php | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/HTMLParser.php b/src/HTMLParser.php index e99fb44..edbb8c6 100644 --- a/src/HTMLParser.php +++ b/src/HTMLParser.php @@ -451,7 +451,7 @@ class HTMLParser $kids = $this->dom->getElementsByTagName('body')->item(0)->childNodes; // Cannot be foreached, don't ask me why. - for ($i = 0; $i <= count($kids); $i++) { + for ($i = 0; $i < $kids->length; $i++) { $import = $topCandidate->importNode($kids->item($i), true); $topCandidate->firstChild->appendChild($import); } @@ -548,6 +548,12 @@ class HTMLParser $import = $articleContent->importNode($sibling->getDOMNode(), true); $articleContent->appendChild($import); + + // TODO Check node shifting! + // siblings is a reference to the children array, and + // sibling is removed from the array when we call appendChild(). + // As a result, we must revisit this index since the nodes + // have been shifted. } } diff --git a/src/Readability.php b/src/Readability.php index 3ba4538..d2dc9f5 100644 --- a/src/Readability.php +++ b/src/Readability.php @@ -296,7 +296,7 @@ class Readability extends Element implements ReadabilityInterface $new->appendChild($new->createElement($value)); $childs = $this->node->childNodes; - for ($i = 0; $i < count($childs); $i++) { + for ($i = 0; $i < $childs->length; $i++) { $import = $new->importNode($childs->item($i), true); $new->firstChild->appendChild($import); } -- cgit v1.2.3