-
-
Notifications
You must be signed in to change notification settings - Fork 37
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
jtojnar
wants to merge
6
commits into
j0k3r:master
Choose a base branch
from
jtojnar:even-more-foreaches
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Commits on Mar 18, 2024
-
`DOMNode::$childNodes` always contained `DOMNodeList`.
Configuration menu - View commit details
-
Copy full SHA for f6d91bd - Browse repository at this point
Copy the full SHA f6d91bdView commit details -
Extract
for
-iterated items into variablesThis simplifies the code a bit and will make it slightly easier in case we decide to switch to `foreach` iteration.
Configuration menu - View commit details
-
Copy full SHA for 76908b1 - Browse repository at this point
Copy the full SHA 76908b1View commit details -
Configuration menu - View commit details
-
Copy full SHA for fbc9d67 - Browse repository at this point
Copy the full SHA fbc9d67View commit details -
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.
Configuration menu - View commit details
-
Copy full SHA for ed7d059 - Browse repository at this point
Copy the full SHA ed7d059View commit details -
Configuration menu - View commit details
-
Copy full SHA for 5166622 - Browse repository at this point
Copy the full SHA 5166622View commit details -
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.
Configuration menu - View commit details
-
Copy full SHA for 01e9b26 - Browse repository at this point
Copy the full SHA 01e9b26View commit details
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.