php-readability icon indicating copy to clipboard operation
php-readability copied to clipboard

Iterate node lists with foreach

Open jtojnar opened this issue 11 months ago • 1 comments

DOMNodeList implements Traversable.

There are some for loops left but we cannot simply replace those: PHP follows the DOM specification, which requires that NodeList objects in the DOM are live. As a result, any operation that removes a node list member node from its parent (such as removeChild, replaceChild or appendChild) will cause the next node in the iterator to be skipped.

We could work around that by converting those node lists to static arrays using iterator_to_array but not sure if it is worth it.

Also add few cleanups I noticed.

jtojnar avatar Mar 16 '24 23:03 jtojnar

I have rebased this, deciding to skip the reverse for loops for now – the naïve port to iterator_to_array does not really work (see #91, possibly due to nested nodes being matched by xpath) . Maybe it would work with array_reverse but I will need too give the invariants deeper thought.

jtojnar avatar Mar 18 '24 22:03 jtojnar