MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / clear

Function clear

javascript/atoms/typescript/clear.ts:39–66  ·  view source on GitHub ↗
(element: Element)

Source from the content-addressed store, hash-verified

37// bot.action.clear left behind in Firefox kept the caret alive in Firefox 3.
38// - The IE range-input and blur branches are gone.
39function clear(element: Element): void {
40 if (!isInteractable(element)) {
41 throw botError(ELEMENT_NOT_INTERACTABLE, 'Element is not currently interactable and may not be manipulated')
42 }
43 if (!isEditable(element)) {
44 throw botError(INVALID_ELEMENT_STATE, 'Element must be user-editable in order to clear it.')
45 }
46
47 if (isContentEditable(element)) {
48 if (element.textContent || element.children.length) {
49 focusOnElement(element)
50 element.textContent = ''
51 fire(element, EventType.INPUT, { inputType: 'deleteContentBackward', data: null })
52 unfocus(element)
53 }
54 return
55 }
56
57 const field = element as TextField
58 const badInput = !!(field as HTMLInputElement).validity?.badInput
59 if (field.value || badInput) {
60 focusOnElement(field)
61 field.value = ''
62 fire(field, EventType.INPUT, { inputType: 'deleteContentBackward', data: null })
63 fire(field, EventType.CHANGE, undefined)
64 unfocus(field)
65 }
66}
67
68// Focusing the body cannot take focus, so this blurs whatever has it.
69function unfocus(element: Element): void {

Callers

nothing calls this directly

Calls 7

isInteractableFunction · 0.85
botErrorFunction · 0.85
isEditableFunction · 0.85
isContentEditableFunction · 0.85
focusOnElementFunction · 0.85
fireFunction · 0.85
unfocusFunction · 0.85

Tested by

no test coverage detected