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 then locate the issue related to
defineModelon Firefox. Let's first get an overview. - Assistant
This is the Vue.js repository. Let me understand the issue:
defineModelin Firefox doesn't work as expected- The reproduction link shows a component that should only allow "a" to be entered in an input field
- On Chrome, when you type something other than "a", it gets overridden back to "a"
- On Firefox, the input allows other characters to be typedThis seems to be a timing/event handling issue with how
defineModelupdates the value in Firefox vs Chrome. Let me look at the defineModel implementation and understand how it handles input events.
Let me look at the Vue source code related to defineModel and vModel.
- Assistant
Let me look at the packages structure to understand how Vue is organized.
- Assistant
Let me look at the
defineModelimplementation and how vModel handles input synchronization, which is likely where the Firefox difference occurs. - Assistant
Let me look at the useModel function in runtime-core, and the v-model directive implementation in runtime-dom.
- Assistant
Let me look at the runtime implementation of
useModel. - Assistant
Let me examine the useModel implementation.
- Assistant
Now let me look at the vModel text directive in runtime-dom, which handles input synchronization:
- Assistant
Let me look at the vModel directive, specifically the part that handles setting the value of an input element back after a model update.
- Assistant
Let me understand the issue better. Let me decode the playground URL to see the reproduction code:
- Assistant
Let me try with inflate or deflate
- Assistant
Now I understand the reproduction. The key issue is:
vue<script setup lang="ts"> const model = defineModel() function onInput(event: Event) { let modifiedValue = 'a' model.value = modifiedValue } </script> <template> <input v-model="model" @input="onInput" /> </template>When the user types a character, both the
v-modeldirective's input handler and the@inputhandler fire. The@inputhandler forces the value back to'a'. The question is about event handler ordering. Let me examine the event handling order more closely. - Assistant
Let me look at how Vue handles multiple event listeners on the same event:
- Assistant
Let me look at the
createdhook of vModelText and understand the event listener registration order. Let me trace through the element creation flow: - Assistant
Let me find where directive
createdhooks are invoked in the renderer. - Assistant
Let me look at the
mountElementfunction in the renderer to understand the order of operations: - Assistant
Now I understand the reproduction. The key issue is the interaction between
v-model="model"and@input="onInput"on the same input, and how Firefox handles the event listener ordering differently. Let me trace through the flow more carefully.Let me look at the
patchElementand understand whenbeforeUpdateis called: - Assistant
Let me look at the
useModeltests to understand the expected behavior: - Assistant
Let me check the useModel spec:
- Assistant
Let me look at recent changes to
useModel.tsandvModel.ts: - Assistant
Let me look at the most recent fix to vModel and the issue:
- Assistant
Let me trace through the full flow by looking at the code structure more carefully. Let me also look at
patchElementto understand when directivebeforeUpdateis called: - Assistant
Let me check the
shouldUpdateComponentfunction: - Assistant
Let me try to understand the exact issue by looking at the reproduction code more carefully. Let me decode the playground URL to see the exact code:
- Assistant
I already decoded the reproduction earlier. Let me understand the issue. The reproduction is:
vue// Comp.vue <script setup lang="ts"> const model = defineModel() function onInput(event: Event) { let modifiedValue = 'a' model.value = modifiedValue } </script> <template> <input v-model="model" @input="onInput" /> </template>The key issue is that
<input v-model="model" @input="onInput" />has bothv-modeland@inputon the same input. Let me look at how event listeners are attached and how they interact: - Assistant
Let me look at the
shouldUpdateComponentfunction to understand when child components re-render. - Assistant
Let me look at
hasPropsChangedandhasPropValueChanged: - Assistant
- Assistant
Let me look at the compiler-dom's transformModel to understand how
v-modelon inputs is compiled: - Assistant
Now I understand the reproduction. Let me trace through the exact issue. The problem is:
- User types in
<input v-model="model" @input="onInput" />whereonInputsetsmodel.value = 'a' - Both
v-model's input handler and@inputhandler fire for the sameinputevent - v-model handler fires first, reads
el.valueand emits it viauseModel - @input handler fires second, sets
model.value = 'a' - Both emit to parent, but since the final value is 'a' (same as before), the parent doesn't re-render
- The child doesn't re-render, so
el.valuestays at the typed value
The issue is in
useModel- when the model value is set back to the same value after being temporarily changed, there's no trigger to update the DOM. Let me look at theuseModelset logic: - User types in
- Assistant
Let me look at how the compiler transforms v-model on native elements to understand the props structure:
- Assistant
Now I understand the issue. Let me trace through the exact flow:
- User types in
<input v-model="model" @input="onInput" />whereonInputalways setsmodel.value = 'a' - v-model's input handler fires first (registered in
createdhook), readsel.valueand emits the typed value - @input handler fires second, sets
model.value = 'a'and emits 'a' - Parent receives both emits, final value is 'a'
- BUT: if the model was already 'a',
shouldUpdateComponentreturns false, child doesn't re-render,el.valuenever gets updated back
The issue is in
useModel.set- when the value is set to something different and then set back to the same value in the same tick, the emitted value matches the local value, so the parent won't trigger an update, andtrigger()is never called to force a local re-render. The existing#10279check only handles the case where a custom setter transforms the value (hasChanged(value, emittedValue)), not the case where the raw value itself changes and changes back.Let me write a test first to reproduce this, then fix it:
- User types in
- Assistant
Now I understand the issue! Let me trace through the exact flow:
- User types in
<input v-model="model" @input="onInput" />whereonInputalways setsmodel.value = 'a' - v-model's input handler fires first (registered in
createdhook), readsel.valueand emits the typed value - @input handler fires second, sets
model.value = 'a'which emits 'a' - Both emissions happen synchronously. Parent settles on 'a' (same as before)
- Since the parent's value didn't change,
shouldUpdateComponentreturns false → child doesn't re-render →el.valueis never updated back
The bug is in
useModel.set: when the value is set to something different and then set back in the same tick, the#10279check doesn't trigger becausehasChanged(value, emittedValue)is false (no setter transformation). We need to trigger a local update when the emitted value equals the local value but there was a previous set in the same tick.Let me first run the existing tests to make sure they pass:
- User types in
- Assistant
Good, all tests pass. Now let me implement the fix in
useModel.ts: - Assistant
Now I need to add the fix. After the
#10279check, I'll add a new condition to handle the case where the value was changed and then changed back in the same tick: - Assistant
Now let me apply the fix. I need to add a new condition after the
#10279check: - Assistant
Let me use the edit command instead.
- Assistant
Now I need to add the new condition after the #10279 check. Let me use the edit command.
- Assistant
Good, the fix is applied. Now let me run the existing tests to make sure they still pass:
- Assistant
All existing tests pass. Let me also run the vModel tests:
- Assistant
All tests pass. Now let me write a reproduction script to verify the fix works for the exact scenario described in the issue:
- Assistant
Let me write a proper test within the test framework:
- Assistant
Let me write a standalone test to verify the fix works for the exact scenario:
- Assistant
The test file is not being picked up because it's in /tmp. Let me put it in the right place.
- Assistant
Let me fix the test expectations:
- Assistant
The test renderer has different serialization. Let me look at how existing tests handle this:
- Assistant
I see, the test renderer uses special hooks. Let me rewrite my test properly:
- Assistant
The test renderer serializes empty string differently. Let me fix:
- Assistant
Let me fix the serialization format - looks like strings with special chars need quotes.
- Assistant
String values need quotes in serialization. Let me adjust:
- Assistant
The test passes with my fix. Let me verify that it would fail WITHOUT my fix by temporarily reverting:
- Assistant
Let me stash my changes, run the test to show it fails, then pop the stash:
- Assistant
The test fails without the fix, confirming the bug. Now let me restore the fix:
- Assistant
The test passes with the fix and fails without it. Now let me clean up the test file (I shouldn't add new tests to the repository) and run all related tests:
- Assistant
Now let me run all related tests to ensure nothing is broken:
- Assistant
All 54 tests pass. Let me verify the final state of my changes:
- Assistant
Good, only the one file is modified. Let me also make sure there are no leftover test files:
- Assistant
Good, only one file modified. Let me run a broader test suite to make sure nothing else is broken:
- Assistant
All 1019 tests in runtime-core pass. Let me also run runtime-dom tests:
- Assistant
All tests pass. Let me do a final review of the fix:
- Assistant
The fix looks clean and correct. Let me verify the final state one more time and then submit:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.