ISSUE DESCRIPTION defineModel on Firefox does not work as expected Vue version 3.5.17…
- User
ISSUE DESCRIPTION
defineModel on Firefox does not work as expected
Vue version
3.5.17
Link to minimal reproduction
Steps to reproduce
- Use firefox
- Enter anything in the field. It should be blocked and only show "a", but it wont on firefox.
What is expected?
It should only keep "a" and nothing else should "make it through"
What is actually happening?
Seems like either a race condition or firefox not updating at the same time as Chrome? no clue.
System Info
shellSystem: OS: Windows 11 10.0.26100 CPU: (20) x64 Intel(R) Core(TM) i5-14600K Memory: 12.60 GB / 31.77 GB Binaries: Node: 22.16.0 - C:\nvm4w\nodejs\node.EXE npm: 10.9.2 - C:\nvm4w\nodejs\npm.CMD Browsers: Edge: Chromium (134.0.3124.85) Internet Explorer: 11.0.26100.1882 -- Its not showing my actual browser: Firefox 139.0.4Any additional comments?
No response
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use to restore the file to its original state. You can also reset all changes in the repository using command. Additionally, you can use and to temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call ) using or commands. However you don't need to use or before submitting your changes. In fact, only submits currently unstaged changes because it uses to get the changes that need to be submitted.
- Assistant
I'll start by exploring the repository to understand its structure and locate the relevant files for the Vue3.5.17 codebase. The issue is about
defineModelnot working as expected on Firefox. The minimal reproduction link shows a component that should restrict input to only "a". Let's first check the repository. - Assistant
Let me explore the repository structure more broadly.
- Assistant
Let me understand the issue. The problem is with
defineModelon Firefox. The reproduction shows a component that should restrict input to only "a", but on Firefox, other characters make it through. This seems like a race condition with how the value is being updated.Let me look at the defineModel implementation and how v-model works on input elements.
- Assistant
Let me look at the reproduction link to understand the issue better. The problem seems to be related to how
v-modelhandles input events on Firefox. Let me search for the relevant code. - Assistant
Let me decode the playground link to understand the reproduction better. Let me also look at how v-model handles input events.
- Assistant
Now let me decode the playground link to understand the reproduction case:
- Assistant
Let me try with node to decode this.
- Assistant
Now I understand the reproduction. The issue is:
- Comp.vue uses
defineModel()withv-model="model"and@input="onInput"on the same<input>element - The
onInputfunction always setsmodel.value = 'a', effectively restricting the value to only "a" - On Chrome this works, but on Firefox other characters make it through
This is likely a race condition in the ordering of event listeners between the v-model directive's internal
inputhandler and the user's@inputhandler. Let me investigate how events are registered. - Comp.vue uses
- Assistant
Let me look at how Vue handles event listeners, particularly in the runtime-dom event handling code. The issue is about the order in which event listeners fire when you have both
v-modeland@inputon the same element.The v-model directive adds a native
inputevent listener increated, and the@inputhandler is added as a prop. The order in which they fire depends on how Vue handles this.Let me look at the event patching code in runtime-dom.
- Assistant
Now I understand the reproduction. It's a component using
defineModel()withv-modelon a native<input>and an additional@inputhandler. Let me understand the issue by looking at the flow. Let me check howv-modelon native inputs interacts with@inputhandlers. - Assistant
Now let me look at the baseTransform for v-model to understand what props it generates:
- Assistant
Let me understand the flow more carefully. The issue is about event listener ordering. Let me look at how Vue processes directives and event props:
- Assistant
Let me look at how
patchProphandles event props in the context of v-model and @input: - Assistant
Let me look at how events are registered - particularly how
v-modeland@inputlisteners are registered on native elements. - Assistant
Let me look at the renderer to understand how directive hooks are called vs prop patching.
- Assistant
Let me look at the
mountElementfunction in the renderer to understand the order of operations. - Assistant
Now I see the order. In
mountElement:invokeDirectiveHook('created')is called first (which registers v-model'sinputlistener)- Then props are patched (which registers
@inputlistener)
Let me look at
patchElementto understand the update flow: - Assistant
Now let me look at the
useModelimplementation which powersdefineModel: - Assistant
Let me look at how the
useModelfunction works in the component internal instance: - Assistant
Let me read the useModel implementation:
- Assistant
Now I understand the reproduction case. Let me look at the key issue: the
vModelTextdirective'sbeforeUpdatehook and how it handles the case where the element is focused and the model value is being constrained. Let me look at how the event handlers interact: - Assistant
Let me look at the key issue. I need to understand the interaction between
v-modeland@inputon native inputs when combined withdefineModel. Let me check theshouldUpdateComponentlogic: - Assistant
Let me look at the shouldUpdateComponent function:
- Assistant
Let me look at the shouldUpdateComponent function to understand when components get re-rendered:
- Assistant
Now I understand the reproduction. The issue involves:
- A child component with
defineModel()that has bothv-model="model"and@input="onInput"on an<input>element - The
onInputfunction always setsmodel.value = 'a' - This works in Chrome but not in Firefox
The issue likely involves the ordering of event listeners between the v-model directive's internal
inputhandler and the user's@inputhandler, or how Firefox handles the update cycle differently.Let me investigate how events are registered and the timing:
- A child component with
- Assistant
Let me look at recent changes related to v-model, events, or Firefox:
- Assistant
Let me look at the recent commit about array event handler dispatch:
- Assistant
Let me check the event handler dispatch optimization more closely.
- Assistant
Let me look at how the event listener registration works and what might differ between browsers. Let me check the order of operations:
- Assistant
Let me understand the reproduction better by looking at how
v-modeland@inputinteract on native elements. Let me check the flow: - Assistant
Now I need to understand the exact issue. Let me look at how
v-modeland@inputinteract on the same native input element. The key issue is the order of event listener execution and the model value flow. Let me trace through the flow carefully.First, let me check how the compiled template would look:
- Assistant
Let me build the project first and then test.
- Assistant
Let me try to build the project first.
- Assistant
Let me try a more targeted approach. Let me write a test to reproduce the issue:
- Assistant
Now let me look at how the compiled template works and understand the interaction between
v-modeland@inputon the same native element: - Assistant
I changed directory to /tmp accidentally. Let me go back to /core.
- Assistant
Now I have a clear picture of the issue. Let me trace through the problem carefully:
The reproduction has
<input v-model="model" @input="onInput" />whereonInputalways setsmodel.value = 'a'.When the user types a character:
- v-model directive's
inputlistener fires (registered first increatedhook) - readsel.valueand calls the assigner with the typed value @inputhandler fires (registered second via prop patching) - setsmodel.value = 'a'
The key issue is: the
@inputhandler runs AFTER the v-model directive's handler, but the model value assignment from step 1 already triggered a synchronous emit to the parent. On subsequent keystrokes, the model value ends up back at 'a' (same as before), soshouldUpdateComponentmay prevent the child from re-rendering.But the issue says it works on Chrome but NOT Firefox. This suggests a difference in event listener ordering. Let me check if Firefox might dispatch event listeners in a different order.
Let me look at the
patchEventinvoker more carefully: - v-model directive's
- Assistant
Now I understand the issue. Let me trace through what happens when
<input v-model="model" @input="onInput" />processes an input event:- The v-model directive's listener (registered first in
createdhook) readsel.valueand calls the assigner - The
@inputhandler (registered second via prop patching) setsmodel.value = 'a'
Both modify the model value synchronously. The problem is: when
model.valueends up at the same value ('a') as before the event, the parent'sshouldUpdateComponentreturns false, so the child doesn't re-render, andvModelText.beforeUpdatenever fires to resetel.value. This causes the DOM to be out of sync.Let me now reproduce this issue with a test and then implement a fix:
- The v-model directive's listener (registered first in
- Assistant
Let me look at the existing test to understand the testing patterns used.
- Assistant
Now let me understand the exact issue better and implement a fix. The problem is:
- On
<input v-model="model" @input="onInput" />, when the user types:- v-model directive's handler fires first, reads
el.value, emits it to parent @inputhandler fires second, setsmodel.value = 'a', emits 'a' to parent
- v-model directive's handler fires first, reads
- The parent's value goes from 'a' → typedValue → 'a'
- Since the final value matches the previous value,
shouldUpdateComponentreturns false - The child doesn't re-render, so
vModelText.beforeUpdatenever fires el.valuestays as the typed text, not 'a'
Let me implement the fix in the
vModelTextdirective: - On
- Assistant
Let me see the full vModelText directive:
- Assistant
Now I have a clear understanding. Let me implement the fix. The core issue is that when
v-modeland@inputare on the same native input, and@inputoverrides the model value, the directive'sbeforeUpdatemay not fire because the child component doesn't re-render (the model value ends up the same).The fix: in the v-model directive's event handler, after calling the assigner, schedule a
nextTickto syncel.valuewith the directive's binding value. We need to store the binding value on the element for later access. - Assistant
Now let me implement the fix. I'll modify the
vModelTextdirective to store the binding value and syncel.valuein anextTickafter the event handler: - Assistant
Now let me implement the fix:
- Assistant
Let me apply the changes directly:
- Assistant
Now let me apply the changes. First, add the new symbol key:
- Assistant
Let me use the edit command differently.
- Assistant
Good, now let me modify the
createdhook to destructurevalueand add thenextTicksync: - Assistant
I see there's a duplicate
})on line 86. Let me fix that: - Assistant
I see a duplicate line. Let me fix it:
- Assistant
Good, the duplicate is fixed. Now let me update the
mountedhook to store the binding value: - Assistant
Good. Now let me update the
beforeUpdatehook to store the binding value: - Assistant
The edit broke the
beforeUpdatefunction signature. Let me fix it: - Assistant
Good, now let me verify the entire vModelText directive looks correct:
- Assistant
The code looks correct. Now let me run the existing vModel tests to make sure I haven't broken anything:
- Assistant
There's one test failure. Let me look at that test to understand it:
- Assistant
Let me read this test to understand it:
- Assistant
I see the issue - the
nextTickcallback needs to respect the same rules asbeforeUpdate(like preserving unresolved trimmed text while focused). Let me also add a flag to track whetherbeforeUpdatehas already handled the sync: - Assistant
Let me use a version counter approach to track whether
beforeUpdatehas fired. This way thenextTickonly syncs when the component didn't re-render: - Assistant
Now I need to update the event handler to use the version counter. Let me also need to add a blank line after the symbol declarations:
- Assistant
Now let me update the event handler to capture the version before firing and check it in
nextTick: - Assistant
I see another duplicate
})on line 93. Let me fix that: - Assistant
There's another duplicate line (93-94). Let me fix it:
- Assistant
Good. Now I need to add the version counter increment in
beforeUpdate: - Assistant
Now I need to also initialize the
updateKeycounter in thecreatedhook: - Assistant
Good. Now let me run the tests again:
- Assistant
All 28 tests pass! Let me also run the broader test suite:
- Assistant
All 231 runtime-dom tests pass! Let me also run the runtime-core tests:
- Assistant
All 1019 runtime-core tests pass! Let me also run the compiler-sfc defineModel tests:
- Assistant
Let me also verify the final state of the file to make sure it looks correct:
- Assistant
Let me verify the full file structure including the symbol declarations:
- Assistant
Let me run the full test suite to make sure everything is good:
- Assistant
The 13 failed tests are all e2e tests that require a browser (puppeteer), which can't run in this environment. All unit tests pass. Let me verify:
- Assistant
All failures are e2e tests that require a browser. All unit tests pass. Let me also quickly run the
useModeltest: - Assistant
All tests pass. Let me check the diff to review my changes:
- Assistant
The diff looks clean and correct. Let me now verify the changes by looking at the complete vModelText directive one more time:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.