(
element: HTMLElement,
text: string,
init?: ClipboardEventInit,
{initialSelectionStart, initialSelectionEnd}: pasteOptions = {},
)
| 27 | } |
| 28 | |
| 29 | function paste( |
| 30 | element: HTMLElement, |
| 31 | text: string, |
| 32 | init?: ClipboardEventInit, |
| 33 | {initialSelectionStart, initialSelectionEnd}: pasteOptions = {}, |
| 34 | ) { |
| 35 | // TODO: implement for contenteditable |
| 36 | if (!isSupportedElement(element)) { |
| 37 | throw new TypeError( |
| 38 | `The given ${element.tagName} element is currently unsupported. |
| 39 | A PR extending this implementation would be very much welcome at https://github.com/testing-library/user-event`, |
| 40 | ) |
| 41 | } |
| 42 | |
| 43 | if (isDisabled(element)) { |
| 44 | return |
| 45 | } |
| 46 | |
| 47 | eventWrapper(() => element.focus()) |
| 48 | |
| 49 | // by default, a new element has it's selection start and end at 0 |
| 50 | // but most of the time when people call "paste", they expect it to paste |
| 51 | // at the end of the current input value. So, if the selection start |
| 52 | // and end are both the default of 0, then we'll go ahead and change |
| 53 | // them to the length of the current value. |
| 54 | // the only time it would make sense to pass the initialSelectionStart or |
| 55 | // initialSelectionEnd is if you have an input with a value and want to |
| 56 | // explicitely start typing with the cursor at 0. Not super common. |
| 57 | if (element.selectionStart === 0 && element.selectionEnd === 0) { |
| 58 | setSelectionRange( |
| 59 | element, |
| 60 | initialSelectionStart ?? element.value.length, |
| 61 | initialSelectionEnd ?? element.value.length, |
| 62 | ) |
| 63 | } |
| 64 | |
| 65 | fireEvent.paste(element, init) |
| 66 | |
| 67 | if (element.readOnly) { |
| 68 | return |
| 69 | } |
| 70 | |
| 71 | text = text.substr(0, getSpaceUntilMaxLength(element)) |
| 72 | |
| 73 | const {newValue, newSelectionStart} = calculateNewValue(text, element) |
| 74 | fireEvent.input(element, { |
| 75 | inputType: 'insertFromPaste', |
| 76 | target: {value: newValue}, |
| 77 | }) |
| 78 | setSelectionRange( |
| 79 | element, |
| 80 | |
| 81 | // TODO: investigate why the selection caused by invalid parameters was expected |
| 82 | ({ |
| 83 | newSelectionStart, |
| 84 | selectionEnd: newSelectionStart, |
| 85 | } as unknown) as number, |
| 86 | ({} as unknown) as number, |
nothing calls this directly
no test coverage detected