Skip to content

Commit ec401ab

Browse files
committed
feat: allow setValue to set values to contenteditable divs
1 parent ffcd4ce commit ec401ab

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/domWrapper.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ export class DOMWrapper<NodeType extends Node> extends BaseWrapper<NodeType> {
136136
this.trigger('input')
137137
// trigger `change` for `v-model.lazy`
138138
return this.trigger('change')
139+
} else if (tagName === 'DIV' && this.attributes().contenteditable) {
140+
// for contenteditable elements, we set the innerHTML
141+
// and trigger input and change events
142+
// the value will be coerced to a string
143+
// and object types will be stringified (either default [object Object] or custom toString)])
144+
element.innerHTML = value
145+
this.trigger('input')
146+
return this.trigger('change')
139147
} else {
140148
throw Error(`wrapper.setValue() cannot be called on ${tagName}`)
141149
}

tests/setValue.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,25 @@ describe('setValue', () => {
343343
})
344344
})
345345
})
346+
347+
describe('on contenteditable elements', () => {
348+
it('sets element innerHTML', async () => {
349+
const richContent = '<h1>Rich title</h1><p>Rich description</p>'
350+
const onInput = vi.fn()
351+
const onChange = vi.fn()
352+
const Comp = defineComponent({
353+
setup() {
354+
return () => h('div', { contenteditable: 'true', onInput, onChange })
355+
}
356+
})
357+
358+
const wrapper = mount(Comp)
359+
const editable = wrapper.find<HTMLDivElement>('div')
360+
await editable.setValue(richContent)
361+
362+
expect(editable.element.innerHTML).toBe(richContent)
363+
expect(onInput).toHaveBeenCalledTimes(1)
364+
expect(onChange).toHaveBeenCalledTimes(1)
365+
})
366+
})
346367
})

0 commit comments

Comments
 (0)