Skip to content

feat: allow setValue to set values to contenteditable divs #2671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions src/domWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ export class DOMWrapper<NodeType extends Node> extends BaseWrapper<NodeType> {
this.trigger('input')
// trigger `change` for `v-model.lazy`
return this.trigger('change')
} else if (tagName === 'DIV' && this.attributes().contenteditable) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK contenteditable is available on all tags, not only on divs

and the presence of the attribute might not be enough, as I think in <div contenteditable="true"><div id="inner"></div></div>, #inner is also editable.

I think using the property isContentEditable might be more reliable

// for contenteditable elements, we set the innerHTML
// and trigger input and change events
// the value will be coerced to a string
// and object types will be stringified (either default [object Object] or custom toString)])
element.innerHTML = value
this.trigger('input')
return this.trigger('change')
} else {
throw Error(`wrapper.setValue() cannot be called on ${tagName}`)
}
Expand Down
21 changes: 21 additions & 0 deletions tests/setValue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,25 @@ describe('setValue', () => {
})
})
})

describe('on contenteditable elements', () => {
it('sets element innerHTML', async () => {
const richContent = '<h1>Rich title</h1><p>Rich description</p>'
const onInput = vi.fn()
const onChange = vi.fn()
const Comp = defineComponent({
setup() {
return () => h('div', { contenteditable: 'true', onInput, onChange })
}
})

const wrapper = mount(Comp)
const editable = wrapper.find<HTMLDivElement>('div')
await editable.setValue(richContent)

expect(editable.element.innerHTML).toBe(richContent)
expect(onInput).toHaveBeenCalledTimes(1)
expect(onChange).toHaveBeenCalledTimes(1)
})
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to have more tests, with other elements than div, other values than true, nested elements with parents that are editable or not, etc.

})