test-utils icon indicating copy to clipboard operation
test-utils copied to clipboard

.setValue to work on `[contenteditable=true]` elements

Open renatodeleao opened this issue 1 year ago • 4 comments

Problem

<div contenteditable> is now widely adopted as the way to make Rich text editors on the web. I've noticed that we cannot set the value of these elementes using .setValue as it throws an error, and have to resort to manual workarounds.

Solution

when field.setValue is applied and field is <div contenteditable> element, internally VTU should override Elements textContent or innerHTML prop, depending on the value of attribute value is plaintext-only or 'true'. Since this is not widely supported, just going with innerHTML is probably easier.

https://github.com/vuejs/test-utils/blob/05f76b63b7a45fc05c0ec840dbaf179fa0d750ab/src/domWrapper.ts#L125-L131

else if (this.attributes().contenteditable) { 
   element.innerHTML = value 
  
   this.trigger('input') 
   // trigger `change` for `v-model.lazy` 
   return this.trigger('change') 
}

Additional context

  • https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/contenteditable
  • Not sure if being to naive or not and overlooking something. Let me know!

Cheers ✌️

renatodeleao avatar Mar 27 '24 13:03 renatodeleao

Hi @renatodeleao

That could be a good idea. Are you willing to open a PR that implements and documents this?

cexbrayat avatar Apr 03 '24 08:04 cexbrayat

@cexbrayat sure I can draft a PR. I'll let you know.

renatodeleao avatar Apr 03 '24 09:04 renatodeleao

If there is still interest, here is how it might look:

https://github.com/lmiller1990/vuejs-composition-api-course/blob/9b203f089f924508c0d1083ecd06e54e2d75ee61/tests/unit/PostWriter.spec.ts#L16-L27

lmiller1990 avatar Jun 03 '24 11:06 lmiller1990