Skip to content

Commit 613f662

Browse files
committed
Add support back to Input elements
Note: selectionStart/End only applies to input elements of certain types so it could be null and throws. This fallbacks to 0. https://html.spec.whatwg.org/#do-not-apply
1 parent 148d2ac commit 613f662

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

src/text.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export function insertText(textarea: HTMLTextAreaElement, text: string): void {
2-
const beginning = textarea.value.substring(0, textarea.selectionStart)
3-
const remaining = textarea.value.substring(textarea.selectionEnd)
1+
export function insertText(textarea: HTMLInputElement | HTMLTextAreaElement, text: string): void {
2+
const beginning = textarea.value.substring(0, textarea.selectionStart || 0)
3+
const remaining = textarea.value.substring(textarea.selectionEnd || 0)
44

55
const newline = beginning.length === 0 || beginning.match(/\n$/) ? '' : '\n'
66
const textBeforeCursor = beginning + newline + text

0 commit comments

Comments
 (0)