Skip to content

feat: support setValue() on contenteditable elements - #2954

Closed
Revan0809 wants to merge 1 commit into
vuejs:mainfrom
Revan0809:feat/setvalue-contenteditable
Closed

Revan0809 wants to merge 1 commit into
vuejs:mainfrom
Revan0809:feat/setvalue-contenteditable

Conversation

@Revan0809

Copy link
Copy Markdown

Summary

wrapper.setValue() threw wrapper.setValue() cannot be called on ${tagName} for any element that wasn't INPUT, TEXTAREA, SELECT, or OPTION — so there was no way to drive a <div contenteditable> element, which is a common pattern for building rich-text editors on the web.

  • When the element carries a contenteditable attribute whose value isn't "false" (i.e. contenteditable, contenteditable="", contenteditable="true", or contenteditable="plaintext-only"), setValue() now sets innerHTML and triggers input/change, mirroring the existing INPUT/TEXTAREA branch.
  • An element with contenteditable="false" still throws, since that value explicitly marks it non-editable — matching the issue's proposed fix, extended slightly for correctness on that one edge case.

Closes #2390

Test plan

  • Added 4 tests: contenteditable="true", bare contenteditable, input/change event firing, and the contenteditable="false" case still throwing
  • Verified the 3 new positive-case tests actually catch the bug: reverting only domWrapper.ts while keeping the tests reproduces the exact wrapper.setValue() cannot be called on DIV error from the issue
  • npx vitest run tests/setValue.spec.ts — 29/29 pass
  • npx vue-tsc --noEmit -p tsconfig.volar.json — 0 errors
  • npx oxlint src/domWrapper.ts tests/setValue.spec.ts — 0 new issues (pre-existing vitest(require-mock-type-parameters) warnings on vi.fn() calls are present throughout this file already, confirmed unrelated to this change)

🤖 Generated with Claude Code

@netlify

netlify Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for vue-test-utils-docs ready!

Name Link
🔨 Latest commit fc329eb
🔍 Latest deploy log https://app.netlify.com/projects/vue-test-utils-docs/deploys/6ac1cf9028fc2d0008f3ee5d
😎 Deploy Preview https://deploy-preview-2954--vue-test-utils-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@cexbrayat cexbrayat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks the PR, but I think this has the same flaws than #2671 : we should check isContentEditable, and it lacks tests for other elements than divs, nested elements, other values of the contenteditable, etc...

@Revan0809
Revan0809 force-pushed the feat/setvalue-contenteditable branch from 9287d8e to fc329eb Compare October 4, 2026 04:01
@Revan0809

Copy link
Copy Markdown
Author

Thanks for the review, @cexbrayat — good catches, both addressed.

On isContentEditable: I looked into it, and this one needs a bit of nuance. Element.isContentEditable is not implemented by jsdom (confirmed empirically: 'isContentEditable' in document.createElement('div') is false in jsdom 30.1.1, the version this repo tests with). Since Vue Test Utils' consumers almost universally run their tests in jsdom (or happy-dom, which also doesn't implement it), relying on the real property would make this feature silently never work for them — not just hard to test here, but non-functional in practice.

So instead of the property, I rewrote it to mirror isContentEditable's own inheritance algorithm directly off attributes: element.closest('[contenteditable]') finds the nearest contenteditable ancestor-or-self, and its value determines editability for the whole subtree (any value other than "false" is editable, matching the spec). This gets the same correctness — any tag, not just DIV, and a child with no attribute of its own correctly inherits from an editable ancestor — using only closest()/getAttribute(), which jsdom supports fully.

On test coverage, added:

  • span and p elements (not just div)
  • contenteditable="plaintext-only"
  • A child with no contenteditable attribute of its own, inheriting from an editable ancestor
  • The same child with no editable ancestor at all (still throws)
  • The same child under an explicit contenteditable="false" ancestor (still throws)
  • A nearer editable ancestor correctly overriding a further non-editable one

35/35 tests pass, and I verified the two new inheritance-specific tests actually catch the original bug (they fail against the pre-rewrite code, since the old attribute-only check didn't look at ancestors at all).

wrapper.setValue() threw on any element that wasn't INPUT, TEXTAREA,
SELECT, or OPTION, so it had no way to drive a `<div contenteditable>`
rich-text editor, a common pattern for building such editors on the web.

An element is now editable (and setValue sets innerHTML and triggers
input/change) when its nearest contenteditable ancestor-or-self has a
value other than "false" -- mirroring Element.isContentEditable's
inheritance rules (any tag, and a child with no attribute of its own
inherits from an editable parent) without relying on the property
itself, which jsdom does not implement and would therefore never read
as editable for the vast majority of this library's consumers.

Fixes vuejs#2390

@cexbrayat cexbrayat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think happy-dom does implement isContentEditable (see https://github.com/capricorn86/happy-dom/blob/master/packages/happy-dom/src/nodes/html-element/HTMLElement.ts#L564-L581), and people may run their tests in a browser directly, using Vitest Browser Mode.
So I think we should use native isContentEditable when it returns a boolean.

@cexbrayat

Copy link
Copy Markdown
Member

Hi @Revan0809

if you're still interesting in landing this PR, would you mind take a look at my last comment ? Thanks!

@ValentinYoushkevich

Copy link
Copy Markdown
Contributor

@Revan0809 are you still planning to finish this one (the isContentEditable point from the last review)? If not, I'd be happy to take it over and address the review.

@ValentinYoushkevich

Copy link
Copy Markdown
Contributor

Opened #3033 that builds on this and addresses the review here.

@cexbrayat

Copy link
Copy Markdown
Member

Superseded by #3033

@cexbrayat cexbrayat closed this Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.setValue to work on [contenteditable=true] elements

3 participants