[whatwg/dom] Handle NodeIterator node removal during filtering (PR #1479)

The `NodeIterator` `traverse` algorithm read the reference into local variables, ran the filter (which can execute author code), and then wrote those locals back. Filtering can remove nodes and run the `NodeIterator` pre-remove steps, but those adjusted only the stored reference, not the in-progress traversal position held in `traverse`'s locals. As a result a node removed during its own filtering could leave the reference pointing at a detached node, and traversal could continue from there.

This models a reference as a *node pointer* (a tuple of a node and a pointer before) and gives each `NodeIterator` a *candidate reference* node pointer that holds the in-progress traversal position. `traverse` advances the candidate reference, sets the reference to it only once a node is accepted, and returns the node passed to the filter. The pre-remove steps adjust both the reference and, while traversing, the candidate reference.

`referenceNode` continues to report the last accepted node during filtering.

Matches Gecko, Blink, and WebKit (originally found via Acid3).

Tests: https://github.com/web-platform-tests/wpt/pull/61035


<!--
    This comment and the below content is programmatically generated.
    You may add a comma-separated list of anchors you'd like a
    direct link to below (e.g. #idl-serializers, #idl-sequence):

    Don't remove this comment or modify anything below this line.
    If you don't want a preview generated for this pull request,
    just replace the whole of this comment's content by "no preview"
    and remove what's below.
-->
***

### :boom: Error: 422 Unprocessable Entity :boom: ###

[PR Preview](https://github.com/tobie/pr-preview#pr-preview) failed to build. _(Last tried on Jul 2, 2026, 11:34 AM UTC)_.

<details>
<summary>More</summary>


PR Preview relies on a number of web services to run. There seems to be an issue with the following one:

:rotating_light: [Spec Generator](https://www.w3.org/publications/spec-generator/) - Spec Generator is the web service used to build bikeshed/ReSpec specs

:link: [Related URL](https://www.w3.org/publications/spec-generator/?type=bikeshed-spec&output=html&url=https%3A%2F%2Fraw.githubusercontent.com%2Fwhatwg%2Fdom%2F42f20c1aa0d93add9298ea8112a28f01f196097a%2Fdom.bs&force=1&md-status=LS-PR&md-Text-Macro=PR-NUMBER%201479)

**Error output:**

```json
[
    {
        "lineNum": "2369:17",
        "messageType": "link",
        "text": "Multiple possible 'dfn' local refs for 'node'.\nRandomly chose one of them; other instances might get a different random choice."
    },
    {
        "lineNum": "2973:3",
        "messageType": "link",
        "text": "Multiple possible 'dfn' local refs for 'node'.\nRandomly chose one of them; other instances might get a different random choice."
    },
    {
        "lineNum": null,
        "messageType": "failure",
        "text": "Did not generate, due to errors exceeding the allowed error level."
    }
]
```

_This seems to be an issue with the [Spec Generator](https://www.w3.org/publications/spec-generator/) service. PR Preview doesn't manage this service and so has no control over it. If you've identified an issue with it, you can [report the issue to the maintainers of Spec Generator](https://github.com/w3c/spec-generator/issues/new) directly. Please be courteous. Thank you!_

_If you don't have enough information above to solve the error by yourself or if the issue doesn't seem related to Spec Generator, you can [file an issue with PR Preview](https://github.com/tobie/pr-preview/issues/new?title=Unidentified%20Error&body=See%20whatwg/dom%231479.)._

</details>

You can view, comment on, or merge this pull request online at:

  https://github.com/whatwg/dom/pull/1479

-- Commit Summary --

  * Handle NodeIterator node removal during filtering

-- File Changes --

    M dom.bs (134)

-- Patch Links --

https://github.com/whatwg/dom/pull/1479.patch
https://github.com/whatwg/dom/pull/1479.diff

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/pull/1479
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/dom/pull/1479@github.com>

Received on Thursday, 2 July 2026 11:34:24 UTC