Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions packages/runtime-dom/__tests__/directives/vModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,52 @@ describe('vModel', () => {
expect(data.lazy).toEqual('foo')
})

it('should preserve unresolved trimmed text while focused in nested shadow roots', async () => {
const model = ref('')
const component = defineComponent({
render() {
return withVModel(
h('input', {
'onUpdate:modelValue': (value: string) => {
model.value = value
},
}),
model.value,
{
trim: true,
},
)
},
})

document.body.appendChild(root)
const outerShadowRoot = root.attachShadow({ mode: 'open' })
const innerHost = document.createElement('div')
outerShadowRoot.appendChild(innerHost)
const innerShadowRoot = innerHost.attachShadow({ mode: 'open' })

try {
render(h(component), innerShadowRoot)

const input = innerShadowRoot.querySelector('input') as HTMLInputElement
input.focus()

expect(document.activeElement).toBe(root)
expect(outerShadowRoot.activeElement).toBe(innerHost)
expect(innerShadowRoot.activeElement).toBe(input)

input.value = ' hello, world '
triggerEvent('input', input)
await nextTick()

expect(model.value).toEqual('hello, world')
expect(input.value).toEqual(' hello, world ')
} finally {
render(null, innerShadowRoot)
root.remove()
}
})

it('should work with range', async () => {
const component = defineComponent({
data() {
Expand Down
7 changes: 6 additions & 1 deletion packages/runtime-dom/src/directives/vModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ export const vModelText: ModelDirective<
return
}

if (document.activeElement === el && el.type !== 'range') {
const rootNode = el.getRootNode()
if (
(rootNode instanceof Document || rootNode instanceof ShadowRoot) &&
rootNode.activeElement === el &&
el.type !== 'range'
) {
// #8546
if (lazy && value === oldValue) {
return
Expand Down
Loading