| 116 | // The third argument (a keyboard to reuse) never crossed the wire and is |
| 117 | // ignored. |
| 118 | function type(element: Element, keys: string[], _optKeyboard?: unknown, optPersistModifiers?: boolean): void { |
| 119 | if (!holdsFocus(element)) { |
| 120 | if (!isInteractable(element)) { |
| 121 | throw botError(ELEMENT_NOT_INTERACTABLE, 'Element is not currently interactable and may not be manipulated') |
| 122 | } |
| 123 | scrollIntoView(element) |
| 124 | } |
| 125 | |
| 126 | const keyboard = new Keyboard() |
| 127 | keyboard.moveCursor(element) |
| 128 | |
| 129 | // A host whose shadow tree holds focus passes its keys to the field inside. |
| 130 | const receiver = holdsFocus(element) ? getActiveElementFrom(element) || element : element |
| 131 | // A read-only picker falls through to the keyboard, which fires the keys |
| 132 | // but edits nothing, as it does for a read-only text field. |
| 133 | if (isElement(receiver, 'INPUT') && PICKER_TYPES.has((receiver as HTMLInputElement).type) && isEditable(receiver)) { |
| 134 | if (setPickerValue(receiver as HTMLInputElement, keys.join(''))) { |
| 135 | return |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | for (const sequence of keys) { |
| 140 | for (const ch of Array.from(sequence)) { |
| 141 | if (ch === NULL) { |
| 142 | keyboard.releaseModifiers() |
| 143 | } else if ('\uE000' <= ch && ch <= '\uE03D') { |
| 144 | const key = WEBDRIVER_KEYS[ch] |
| 145 | if (!key) { |
| 146 | throw botError(INVALID_ARGUMENT, 'Unsupported WebDriver key: \\u' + ch.charCodeAt(0).toString(16)) |
| 147 | } |
| 148 | pressWebDriverKey(keyboard, key) |
| 149 | } else if (ALIASES[ch]) { |
| 150 | tap(keyboard, ALIASES[ch]) |
| 151 | } else { |
| 152 | typeCharacter(keyboard, ch) |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | if (!optPersistModifiers) { |
| 158 | keyboard.releaseModifiers() |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // A modifier toggles; anything else is pressed and released. |
| 163 | function pressWebDriverKey(keyboard: Keyboard, key: Key): void { |