Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterate more node lists with foreach #91

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from

Commits on Mar 18, 2024

  1. Remove pointless stdClass

    `DOMNode::$childNodes` always contained `DOMNodeList`.
    jtojnar committed Mar 18, 2024
    Configuration menu
    Copy the full SHA
    f6d91bd View commit details
    Browse the repository at this point in the history
  2. Extract for-iterated items into variables

    This simplifies the code a bit and will make it slightly easier in case we decide to switch to `foreach` iteration.
    jtojnar committed Mar 18, 2024
    Configuration menu
    Copy the full SHA
    76908b1 View commit details
    Browse the repository at this point in the history
  3. Remove dead iteration code

    This was forgotten in b580cf2.
    jtojnar committed Mar 18, 2024
    Configuration menu
    Copy the full SHA
    fbc9d67 View commit details
    Browse the repository at this point in the history
  4. Iterate node lists with foreach

    `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.
    jtojnar committed Mar 18, 2024
    Configuration menu
    Copy the full SHA
    ed7d059 View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    5166622 View commit details
    Browse the repository at this point in the history
  6. Iterate more node lists with foreach

    PHP follows the DOM specification, which requires that NodeList objects
    in the DOM are live. As a result, any operation that removes a NodeList
    node from its parent (e.g. removeChild, replaceChild or appendChild)
    will cause the next node in the iterator to be skipped.
    
    Let’s convert those node lists to arrays to make them static to allow
    us to use foreach and drop the index decrementing.
    
    This will make the code a bit clearer
    at the cost of slightly more work being performed.
    jtojnar committed Mar 18, 2024
    Configuration menu
    Copy the full SHA
    01e9b26 View commit details
    Browse the repository at this point in the history