From 5bc17010f5bea7918a4f78d81d25719a001b1965 Mon Sep 17 00:00:00 2001 From: Philipp Fritsche Date: Fri, 1 Apr 2022 06:11:04 +0000 Subject: [PATCH] fix: track calls to `HTMLInputElement.select()` --- src/document/selection.ts | 13 +++++++++++++ tests/document/index.ts | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/document/selection.ts b/src/document/selection.ts index 5830150a..bb31d5a2 100644 --- a/src/document/selection.ts +++ b/src/document/selection.ts @@ -69,6 +69,19 @@ export function prepareSelectionInterceptor( return {realArgs: v} }, ) + + prepareInterceptor( + element, + 'select', + function interceptorImpl(this: HTMLInputElement | HTMLTextAreaElement) { + this[UISelection] = { + anchorOffset: 0, + focusOffset: getUIValue(element).length, + } + + return {realArgs: [] as []} + }, + ) } export function setUISelection( diff --git a/tests/document/index.ts b/tests/document/index.ts index 10e4c564..72c68603 100644 --- a/tests/document/index.ts +++ b/tests/document/index.ts @@ -140,4 +140,23 @@ test('clear UI selection if selection is programmatically set', async () => { element.selectionStart = 2 expect(getUISelection(element)).toHaveProperty('startOffset', 2) expect(getUISelection(element)).toHaveProperty('endOffset', 2) + + setUISelection(element, {anchorOffset: 1, focusOffset: 2}) + element.select() + expect(getUISelection(element)).toHaveProperty('startOffset', 0) + expect(getUISelection(element)).toHaveProperty('endOffset', 3) +}) + +test('select input without selectionRange support', () => { + const {element} = render( + ``, + ) + + prepare(element) + + setUISelection(element, {focusOffset: 1}) + element.select() + + expect(getUISelection(element)).toHaveProperty('startOffset', 0) + expect(getUISelection(element)).toHaveProperty('endOffset', 3) })