Skip to content

Clone into all descendant selectedcontent elements#12263

Open
josepharhar wants to merge 32 commits into
whatwg:mainfrom
josepharhar:selectedcontentredo
Open

Clone into all descendant selectedcontent elements#12263
josepharhar wants to merge 32 commits into
whatwg:mainfrom
josepharhar:selectedcontentredo

Conversation

@josepharhar

@josepharhar josepharhar commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

This PR changes several things:

  • Instead of the first selectedcontent element in DOM order staying up to date, now all descendant selectedcontent elements of the select element will be kept up to date.
  • Rewrites the selectedness setting algorithm to prevent setting selectedness of options which haven't had their insertion steps run yet in order to match implementations and to update selectedcontent elements.
  • Creates a list of selectedcontent elements to update before doing actual DOM operations.

Fixes #12096
Fixes #11880
Fixes #11883
Fixes #11825

(See WHATWG Working Mode: Changes for more details.)


/form-elements.html ( diff )

This PR improves the "clear a select's non-primary selectedcontent
elements" algorithm by making it create a list of selectedcontent
elements to modify separately from modifying them in order to prevent
the list of elements to change while iterating.

Fixes whatwg#11880
This PR makes sure that the contents of the selectedcontent element stay
up to date when the selected option is changed in the selectedness
setting algorithm.

This issue was found here:
web-platform-tests/wpt#55849 (comment)
In order to make the selectedness setting algorithm match
implementations, this PR makes the selectedness setting algorithm avoid
changing the selectedness of option elements which haven't ran their
insertion steps yet by checking whether the options have their cached
nearest ancestor select element assigned yet or not.

This was discussed here: whatwg#11825
…contentredo

Merged the PRs, now I need to do the selectedcontent rewrite to clone
into all descendant selectedcontent elements.

@annevk annevk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @josepharhar! Here's some minor editorial feedback to start. Will try to verify these changes by implementing them.

Comment thread source Outdated
Comment on lines +59315 to +59326
<li><p>Let <var>descendantSelectedcontents</var> be « ».</p></li>

<li><p>Let <var>option</var> be the first <code>option</code> in <var>select</var>'s <span
data-x="concept-select-option-list">list of options</span> whose <span
data-x="concept-option-selectedness">selectedness</span> is true, if any such <code>option</code>
exists, otherwise null.</p></li>
<li>
<p>For each <var>descendant</var> of <var>select</var>'s <span
data-x="descendant">descendants</span>:</p>

<li><p>If <var>option</var> is null, then run <span>clear a <code>selectedcontent</code></span>
given <var>selectedcontent</var>.</p></li>
<ol>
<li><p>If <var>descendant</var> is a <code>selectedcontent</code> element, then <span
data-x="list append">append</span> <var>descendant</var> to
<var>descendantSelectedcontents</var>.</p></li>
</ol>
</li>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need a loop for this. We can state this declaratively:

Let selectedContents be select's descendants that are selectedcontent elements, in tree order.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I have a more substantive question here. Why doesn't this skip disabled selectedcontent elements immediately?

I think the selectedcontent-nested.html test currently expects this, but it seems wasteful and best avoided.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need a loop for this. We can state this declaratively:

Done, thanks

Also, I have a more substantive question here. Why doesn't this skip disabled selectedcontent elements immediately?

Disabled selectedcontents will be skipped deeper in the algorithms called by this one - "clone an option into a selectedcontent" and "clear a selectedcontent" both check if the selectedcontent is disabled before modifying the DOM, and return early.

We could add a step to skip disabled selectedcontents here, but it would functionally be redundant, unless I'm misreading something. What do you think? Can we leave it as-is?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, yes I see how it would change the behavior to check if the selectedcontent elements are disabled before adding them to the list. I'll change the spec here and update the WPT.

Comment thread source Outdated
Comment thread source Outdated
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request Apr 8, 2026
This changes the behavior when selecting a new option in the case that
there are nested selectedcontent elements inside the select. Before, the
disabled selectedcontent would get cloned, but now it won't.

Context: whatwg/html#12263 (comment)

Bug: 458113204
Change-Id: I9afd550f21787b7daefb48c5a76712b5899e5b42
beckysiegel pushed a commit to chromium/chromium that referenced this pull request Apr 9, 2026
This changes the behavior when selecting a new option in the case that
there are nested selectedcontent elements inside the select. Before, the
disabled selectedcontent would get cloned, but now it won't.

Context: whatwg/html#12263 (comment)

Bug: 458113204
Change-Id: I9afd550f21787b7daefb48c5a76712b5899e5b42
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7737623
Reviewed-by: David Grogan <dgrogan@chromium.org>
Reviewed-by: Joey Arhar <jarhar@chromium.org>
Commit-Queue: Joey Arhar <jarhar@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1611875}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request Apr 9, 2026
This changes the behavior when selecting a new option in the case that
there are nested selectedcontent elements inside the select. Before, the
disabled selectedcontent would get cloned, but now it won't.

Context: whatwg/html#12263 (comment)

Bug: 458113204
Change-Id: I9afd550f21787b7daefb48c5a76712b5899e5b42
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7737623
Reviewed-by: David Grogan <dgrogan@chromium.org>
Reviewed-by: Joey Arhar <jarhar@chromium.org>
Commit-Queue: Joey Arhar <jarhar@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1611875}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request Apr 9, 2026
This changes the behavior when selecting a new option in the case that
there are nested selectedcontent elements inside the select. Before, the
disabled selectedcontent would get cloned, but now it won't.

Context: whatwg/html#12263 (comment)

Bug: 458113204
Change-Id: I9afd550f21787b7daefb48c5a76712b5899e5b42
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7737623
Reviewed-by: David Grogan <dgrogan@chromium.org>
Reviewed-by: Joey Arhar <jarhar@chromium.org>
Commit-Queue: Joey Arhar <jarhar@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1611875}
beckysiegel pushed a commit to chromium/chromium that referenced this pull request Apr 29, 2026
Modifying the DOM by updating selectedcontent elements during insertion
or removal steps is bad for security reasons. We already removed the
selectedcontent element removal steps which was one way this can happen,
but the option's InsertedInto and RemovedFrom methods may change which
option is selected, which will synchronously update the selectedcontent
element.

This patch fixes this by moving the selectedcontent updating to the
post-insertion steps for insertion, and by using a microtask to update
on removal if needed.

This matches the behavior in the spec PR: whatwg/html#12263

Bug: 458113204
Change-Id: I42bb94c6eace93445cfbc816529e42ca8a561b94
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7745871
Reviewed-by: David Baron <dbaron@chromium.org>
Reviewed-by: Joey Arhar <jarhar@chromium.org>
Commit-Queue: Joey Arhar <jarhar@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1622241}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request Apr 29, 2026
Modifying the DOM by updating selectedcontent elements during insertion
or removal steps is bad for security reasons. We already removed the
selectedcontent element removal steps which was one way this can happen,
but the option's InsertedInto and RemovedFrom methods may change which
option is selected, which will synchronously update the selectedcontent
element.

This patch fixes this by moving the selectedcontent updating to the
post-insertion steps for insertion, and by using a microtask to update
on removal if needed.

This matches the behavior in the spec PR: whatwg/html#12263

Bug: 458113204
Change-Id: I42bb94c6eace93445cfbc816529e42ca8a561b94
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7745871
Reviewed-by: David Baron <dbaron@chromium.org>
Reviewed-by: Joey Arhar <jarhar@chromium.org>
Commit-Queue: Joey Arhar <jarhar@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1622241}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request Apr 29, 2026
Modifying the DOM by updating selectedcontent elements during insertion
or removal steps is bad for security reasons. We already removed the
selectedcontent element removal steps which was one way this can happen,
but the option's InsertedInto and RemovedFrom methods may change which
option is selected, which will synchronously update the selectedcontent
element.

This patch fixes this by moving the selectedcontent updating to the
post-insertion steps for insertion, and by using a microtask to update
on removal if needed.

This matches the behavior in the spec PR: whatwg/html#12263

Bug: 458113204
Change-Id: I42bb94c6eace93445cfbc816529e42ca8a561b94
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7745871
Reviewed-by: David Baron <dbaron@chromium.org>
Reviewed-by: Joey Arhar <jarhar@chromium.org>
Commit-Queue: Joey Arhar <jarhar@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1622241}
@josepharhar

Copy link
Copy Markdown
Contributor Author

@annevk @jnjaeschke do yall have any more feedback on this PR?

lando-worker Bot pushed a commit to mozilla-firefox/firefox that referenced this pull request May 8, 2026
…nsertion/removal steps, a=testonly

Automatic update from web-platform-tests
Don't update selectedcontent in option insertion/removal steps

Modifying the DOM by updating selectedcontent elements during insertion
or removal steps is bad for security reasons. We already removed the
selectedcontent element removal steps which was one way this can happen,
but the option's InsertedInto and RemovedFrom methods may change which
option is selected, which will synchronously update the selectedcontent
element.

This patch fixes this by moving the selectedcontent updating to the
post-insertion steps for insertion, and by using a microtask to update
on removal if needed.

This matches the behavior in the spec PR: whatwg/html#12263

Bug: 458113204
Change-Id: I42bb94c6eace93445cfbc816529e42ca8a561b94
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7745871
Reviewed-by: David Baron <dbaron@chromium.org>
Reviewed-by: Joey Arhar <jarhar@chromium.org>
Commit-Queue: Joey Arhar <jarhar@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1622241}

--

wpt-commits: 8116dc1ced751ff3a62c58e020f3d7d0ad61553b
wpt-pr: 59528
fmasalha pushed a commit to fmasalha/firefox that referenced this pull request May 8, 2026
…nsertion/removal steps, a=testonly

Automatic update from web-platform-tests
Don't update selectedcontent in option insertion/removal steps

Modifying the DOM by updating selectedcontent elements during insertion
or removal steps is bad for security reasons. We already removed the
selectedcontent element removal steps which was one way this can happen,
but the option's InsertedInto and RemovedFrom methods may change which
option is selected, which will synchronously update the selectedcontent
element.

This patch fixes this by moving the selectedcontent updating to the
post-insertion steps for insertion, and by using a microtask to update
on removal if needed.

This matches the behavior in the spec PR: whatwg/html#12263

Bug: 458113204
Change-Id: I42bb94c6eace93445cfbc816529e42ca8a561b94
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7745871
Reviewed-by: David Baron <dbaron@chromium.org>
Reviewed-by: Joey Arhar <jarhar@chromium.org>
Commit-Queue: Joey Arhar <jarhar@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1622241}

--

wpt-commits: 8116dc1ced751ff3a62c58e020f3d7d0ad61553b
wpt-pr: 59528
@annevk annevk added the topic: select The <select> element label May 11, 2026
Comment thread source Outdated
Comment thread source Outdated
Comment thread source Outdated
Comment thread source Outdated
Comment thread source Outdated
Comment thread source Outdated
Comment thread source Outdated
Comment thread source Outdated
Comment thread source Outdated
Comment thread source Outdated
@annevk

annevk commented May 11, 2026

Copy link
Copy Markdown
Member

There's also an existing issue with moving option elements apparently. Those currently don't update the selectedcontent element either leading to stale state on both sides. Presumably that's not what we want?

@annevk annevk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The moving steps also haven't been addressed? And I believe the current set of algorithms would give us the Firefox outcome on #11825. Is that intentional?

Comment thread source Outdated
Comment thread source Outdated
Comment thread source Outdated
Comment thread source Outdated
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified-and-comments-removed that referenced this pull request May 12, 2026
…nsertion/removal steps, a=testonly

Automatic update from web-platform-tests
Don't update selectedcontent in option insertion/removal steps

Modifying the DOM by updating selectedcontent elements during insertion
or removal steps is bad for security reasons. We already removed the
selectedcontent element removal steps which was one way this can happen,
but the option's InsertedInto and RemovedFrom methods may change which
option is selected, which will synchronously update the selectedcontent
element.

This patch fixes this by moving the selectedcontent updating to the
post-insertion steps for insertion, and by using a microtask to update
on removal if needed.

This matches the behavior in the spec PR: whatwg/html#12263

Bug: 458113204
Change-Id: I42bb94c6eace93445cfbc816529e42ca8a561b94
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7745871
Reviewed-by: David Baron <dbaronchromium.org>
Reviewed-by: Joey Arhar <jarharchromium.org>
Commit-Queue: Joey Arhar <jarharchromium.org>
Cr-Commit-Position: refs/heads/main{#1622241}

--

wpt-commits: 8116dc1ced751ff3a62c58e020f3d7d0ad61553b
wpt-pr: 59528

UltraBlame original commit: 86fb81e4e646ca7e6da3fa87d0d72010474fb780
gecko-dev-updater pushed a commit to marco-c/gecko-dev-comments-removed that referenced this pull request May 12, 2026
…nsertion/removal steps, a=testonly

Automatic update from web-platform-tests
Don't update selectedcontent in option insertion/removal steps

Modifying the DOM by updating selectedcontent elements during insertion
or removal steps is bad for security reasons. We already removed the
selectedcontent element removal steps which was one way this can happen,
but the option's InsertedInto and RemovedFrom methods may change which
option is selected, which will synchronously update the selectedcontent
element.

This patch fixes this by moving the selectedcontent updating to the
post-insertion steps for insertion, and by using a microtask to update
on removal if needed.

This matches the behavior in the spec PR: whatwg/html#12263

Bug: 458113204
Change-Id: I42bb94c6eace93445cfbc816529e42ca8a561b94
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7745871
Reviewed-by: David Baron <dbaronchromium.org>
Reviewed-by: Joey Arhar <jarharchromium.org>
Commit-Queue: Joey Arhar <jarharchromium.org>
Cr-Commit-Position: refs/heads/main{#1622241}

--

wpt-commits: 8116dc1ced751ff3a62c58e020f3d7d0ad61553b
wpt-pr: 59528

UltraBlame original commit: 86fb81e4e646ca7e6da3fa87d0d72010474fb780
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified that referenced this pull request May 12, 2026
…nsertion/removal steps, a=testonly

Automatic update from web-platform-tests
Don't update selectedcontent in option insertion/removal steps

Modifying the DOM by updating selectedcontent elements during insertion
or removal steps is bad for security reasons. We already removed the
selectedcontent element removal steps which was one way this can happen,
but the option's InsertedInto and RemovedFrom methods may change which
option is selected, which will synchronously update the selectedcontent
element.

This patch fixes this by moving the selectedcontent updating to the
post-insertion steps for insertion, and by using a microtask to update
on removal if needed.

This matches the behavior in the spec PR: whatwg/html#12263

Bug: 458113204
Change-Id: I42bb94c6eace93445cfbc816529e42ca8a561b94
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7745871
Reviewed-by: David Baron <dbaronchromium.org>
Reviewed-by: Joey Arhar <jarharchromium.org>
Commit-Queue: Joey Arhar <jarharchromium.org>
Cr-Commit-Position: refs/heads/main{#1622241}

--

wpt-commits: 8116dc1ced751ff3a62c58e020f3d7d0ad61553b
wpt-pr: 59528

UltraBlame original commit: 86fb81e4e646ca7e6da3fa87d0d72010474fb780
@josepharhar

Copy link
Copy Markdown
Contributor Author

Thanks, I am working on moving steps right now for option and selectedcontent elements, and I'll update this PR accordingly soon.

And I believe the current set of algorithms would give us the Firefox outcome on #11825. Is that intentional?

I think this problem exists before and after this PR and this PR does not intend to change this behavior. Is there something in here in particular that it looks like this PR is changing with regards to this behavior?

@josepharhar

Copy link
Copy Markdown
Contributor Author

Ok, I pushed a change to make the moving steps for options and selectedcontent elements update selectedcontent elements if appropriate after a microtask.

@josepharhar

Copy link
Copy Markdown
Contributor Author

Is there anything else I can do to get this PR merged? I'd like to ship the changes in chromium.

@annevk

annevk commented Jun 5, 2026

Copy link
Copy Markdown
Member

The last two commits I pushed have behavioral changes:

  • It seemed that the post-connection steps didn't recalculate disabled. With that commit they do.
  • I also wanted to see what it would look like if we relied on microtasks entirely as that means we don't need post-connection steps. This looks quite attractive to me and from the earlier discussion I'm not sure we feel that strongly about it being synchronous. The main use cases are about being able to style it differently which this shouldn't impact at all.

Thoughts?

@josepharhar

Copy link
Copy Markdown
Contributor Author

Thanks Anne, this looks great! I am ok with using microtasks more often, and it looks like when actually choosing an option the selectedcontent element is still updated synchronously, right?

Comment thread source
</div>

<div algorithm>
<p>When an <code>option</code> element is popped off the <span>stack of open elements</span> of an

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@annevk is this not needed anymore because of the microtasks?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: select The <select> element

3 participants