summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/HTMLParser.php14
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;
/*