Open Bug 1891259 Opened 2 years ago Updated 8 months ago

event.preventDefault() is not respected when called in microtask

Categories

(Core :: DOM: Events, defect)

Firefox 126
defect

Tracking

()

REOPENED

People

(Reporter: nick.gard.dev, Assigned: smaug)

References

(Blocks 1 open bug)

Details

Attachments

(1 file)

User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:126.0) Gecko/20100101 Firefox/126.0

Steps to reproduce:

There is a minimal test case on this pen: https://codepen.io/NickGard/pen/mdgLoZv?editors=1011

The code is:

<input id="X" is="dumb-thing">
<script>
X.addEventListener("beforeinput", (e) => {
  console.log("beforeinput");
  // when called here, preventDefault() correctly prevents "input" event from dispatching
  // event.preventDefault();

  queueMicrotask(() => {
    // this is too late to call preventDefault() in Firefox.
    // Safari and Chrome still prevent the "input" event from dispatching when called here
    event.preventDefault();
  });
});
X.addEventListener("input", (e) => {
  console.log("input");
});
</script>

Actual results:

The "input" event handler is still triggered, logging "input" in the console.

Expected results:

The "input" event handler should not have been triggered.

The Bugbug bot thinks this bug should belong to the 'Core::DOM: Events' component, and is moving the bug to that component. Please correct in case you think the bot is wrong.

Component: Untriaged → DOM: Events
Product: Firefox → Core

After exploring this a bit more with other events' default behaviors, all of the ones I've tested are correctly prevented from occurring when event.preventDefault() is called in a microtask. For instance, the following code correctly stops characters from being entered into the input and precludes the "beforeinput" and "input" events from dispatching.

<input id="X">
<script>
  X.addEventListener('keydown', event => {
    queueMicrotask(() => {
      event.preventDefault();
    });
  });
</script>

It seems that "beforeinput" is the only one that doesn't work in a microtask.

Right, because I assume Gecko dispatches beforeinput inside another (C++) event listener.

Assignee: nobody → smaug
Severity: -- → S3
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true
Pushed by opettay@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/13c25f58aaca beforeInputEvent.preventDefault() is not respected when called in microtask, r=edgar

Backed out for causing xpcshell failures on test_ext_scripting_contentScripts.js

Flags: needinfo?(smaug)
Depends on: 1892775
Pushed by opettay@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/ebcc80d71094 beforeInputEvent.preventDefault() is not respected when called in microtask, r=edgar
Status: ASSIGNED → RESOLVED
Closed: 2 years ago
Resolution: --- → FIXED
Target Milestone: --- → 127 Branch
Regressions: 1893226

Given bug 1893226, should we back this out until we can sort that regression out?

Backed out for breaking handful of things on slack

Backout link: https://hg.mozilla.org/mozilla-central/rev/2d59d3cfc93fa569342ae78098d774b23b75c810

Status: RESOLVED → REOPENED
Resolution: FIXED → ---
Target Milestone: 127 Branch → ---
Flags: needinfo?(smaug)

There is an r+ patch which didn't land and no activity in this bug for 2 weeks.
:smaug, could you have a look please?
If you still have some work to do, you can add an action "Plan Changes" in Phabricator.
For more information, please visit BugBot documentation.

Flags: needinfo?(smaug)
Flags: needinfo?(echen)
Flags: needinfo?(smaug)
Flags: needinfo?(echen)

Has there been any progress on fixing this in a way that doesn't cause a regression like the one in Slack?

I've been rather busy with other things, but this is very high up in my todo list.

Is there any update on fixing this? This has broken oninput state modifications in Svelte 5 for us. Compare https://svelte.dev/playground/bf28bf223e4746a199f15ca80385487d?version=5.39.2 in Firefox vs. Chrome or Edge.

You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: