[CEReactions] step 3 pops the element queue before invoking it; all engines invoke first

Open Beginner friendly
#13,263 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
66/100
Issue type
Documentation
Clarity
Clearly specified
Activity status
Active
Tech stack
html
Domain
documentation

Research direction

Read the [CEReactions] section of the HTML Standard source and the linked "enqueue an element on the appropriate element queue" algorithm it references, then compare with the Blink, WebKit and Gecko entry points quoted in the issue. Edit step 3 to invoke the current element queue and pop it afterwards, adding a short note that the queue can grow while being invoked. Done means the wording matches the engines and the behaviour described for html/semantics/forms/the-select-element/customizable-select/selectedcontent-custom-element-reactions.html is what the text now specifies.

Written by the indexing model from the issue text.

Description

topic: custom elements
What is the issue with the HTML Standard?

[CEReactions] step 3 says:

  1. Pop the element queue from the custom element reactions stack, and invoke custom element reactions in that queue.

Read literally, the element queue is popped before its reactions are invoked. While those reactions run, the queue being processed is no longer the current element queue. Any reaction enqueued during the invocation therefore goes through enqueue an element on the appropriate element queue: if this was the outermost [CEReactions] scope the stack is now empty, so the element is added to the backup element queue and a microtask is queued to process it.

A concrete case: the invocation runs an upgrade reaction for a connected element, and the upgrade enqueues that element's connectedCallback. The callback reaction is added to the element's own custom element reaction queue, and the element is appended to the backup element queue. The callback runs right away, because the element's reaction queue is still being drained. But the element stays on the backup element queue with an empty reaction queue, and the "processing the backup element queue" flag stays set until that microtask runs. A later reaction from a path that has no [CEReactions] scope (the parser, user editing, a native microtask) is then appended behind the stale entry. When the backup element queue is processed, the stale element's later reactions run first, so callbacks come out in the wrong order.

All three engines instead invoke the queue while it is still the current element queue, and pop it afterwards. Reactions enqueued during the invocation therefore join that same queue, never the backup queue:

  • Blink: CustomElementReactionStack::PopInvokingReactions calls InvokeReactions(*queue), then stack_.pop_back().
  • WebKit: ~CustomElementReactionStack calls m_queue.processQueue() while s_currentProcessingStack is still this stack, then restores the previous one.
  • Gecko: CustomElementReactionsStack::PopAndInvokeElementQueue calls InvokeReactions, then RemoveLastElement, with the recursion depth still set.

We hit this in an engine that followed the text literally. html/semantics/forms/the-select-element/customizable-select/selectedcontent-custom-element-reactions.html ("Removing the selected option ... runs the clones' reactions after remove() returns") observes the order: Chrome passes it. Switching to invoke-then-pop fixed it.

Proposed change to step 3:

  1. Invoke custom element reactions in the current element queue, then pop it from the custom element reactions stack.

("Invoke custom element reactions" may need a note that the queue can grow while it is being invoked.)

Related, but not the same: #10944 and #10465.

Dominant language
HTML
Stars
9.5k
Forks
3.2k
Avg merge
4d 8h
Merged PRs (30d)
43

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from whatwg/html

All issues in whatwg/html

Similar issues

More Documentation issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.