Merge pull request #172 from alibaba/fix/contenteditable

fix: contenteditable
This commit is contained in:
Simon
2026-03-09 19:19:23 +08:00
committed by GitHub
2 changed files with 16 additions and 25 deletions

View File

@@ -101,41 +101,29 @@ const nativeTextAreaValueSetter = Object.getOwnPropertyDescriptor(
'value' 'value'
)!.set! )!.set!
/**
* create a synthetic keyboard event
* with key keycode code
*/
export async function createSyntheticInputEvent(elem: HTMLElement, key: string) {
elem.dispatchEvent(new KeyboardEvent('keydown', { bubbles: true, cancelable: true, key }))
await waitFor(0.01)
if (elem instanceof HTMLInputElement || elem instanceof HTMLTextAreaElement) {
elem.dispatchEvent(new Event('beforeinput', { bubbles: true }))
await waitFor(0.01)
elem.dispatchEvent(new Event('input', { bubbles: true }))
await waitFor(0.01)
}
elem.dispatchEvent(new KeyboardEvent('keyup', { bubbles: true, cancelable: true, key }))
}
export async function inputTextElement(element: HTMLElement, text: string) { export async function inputTextElement(element: HTMLElement, text: string) {
if (!(element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement)) { const isContentEditable = element.isContentEditable
throw new Error('Element is not an input or textarea') if (
!(element instanceof HTMLInputElement) &&
!(element instanceof HTMLTextAreaElement) &&
!isContentEditable
) {
throw new Error('Element is not an input, textarea, or contenteditable')
} }
await clickElement(element) await clickElement(element)
if (element instanceof HTMLTextAreaElement) { if (isContentEditable) {
element.innerText = text
} else if (element instanceof HTMLTextAreaElement) {
nativeTextAreaValueSetter.call(element, text) nativeTextAreaValueSetter.call(element, text)
} else { } else {
nativeInputValueSetter.call(element, text) nativeInputValueSetter.call(element, text)
} }
const inputEvent = new Event('input', { bubbles: true }) element.dispatchEvent(new Event('input', { bubbles: true }))
element.dispatchEvent(inputEvent)
await waitFor(0.1) // Wait to ensure input event processing completes await waitFor(0.1)
blurLastClickedElement() blurLastClickedElement()
} }

View File

@@ -140,10 +140,13 @@ export function flatTreeToString(flatTree: FlatDomTree, includeAttributes?: stri
// for jump check // for jump check
'target', 'target',
// absolute 定位的下拉菜单 // absolute position dropdown menu
'aria-haspopup', 'aria-haspopup',
'aria-controls', 'aria-controls',
'aria-owns', 'aria-owns',
// content editable
'contenteditable',
] ]
const includeAttrs = [...(includeAttributes || []), ...DEFAULT_INCLUDE_ATTRIBUTES] const includeAttrs = [...(includeAttributes || []), ...DEFAULT_INCLUDE_ATTRIBUTES]