diff options
author | Andres Rey <[email protected]> | 2017-11-09 19:12:47 +0000 |
---|---|---|
committer | Andres Rey <[email protected]> | 2017-11-09 19:12:47 +0000 |
commit | 9c8febc1e381aa642c634b757b4c32e12f444ba6 (patch) | |
tree | 1322363bcac8d791f40625e7dc3f6aba146a4b3b | |
parent | 0940c192d8f9c74e734a6a90c553f3510aa99b40 (diff) |
Remove reverse traversing when scanning for brs and convert the DOMNodeList to an array before looping over it
-rw-r--r-- | src/HTMLParser.php | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/src/HTMLParser.php b/src/HTMLParser.php index 1223891..6da5828 100644 --- a/src/HTMLParser.php +++ b/src/HTMLParser.php @@ -265,11 +265,15 @@ class HTMLParser */ private function prepDocument(DOMDocument $dom) { - $brs = $dom->getElementsByTagName('br'); - $length = $brs->length; - for ($i = 0; $i < $length; $i++) { - /** @var \DOMNode $br */ - $br = $brs->item($length - 1 - $i); + /* + * DOMNodeList must be converted to an array before looping over it. + * This is done to avoid node shifting when removing nodes. + * + * Reverse traversing cannot be done here because we need to find brs that are right next to other brs. + * (If we go the other way around we need to search for previous nodes forcing the creation of new functions + * that will be used only here) + */ + foreach(iterator_to_array($dom->getElementsByTagName('br')) as $br){ $next = $br->nextSibling; /* |