Skip to content

Commit 6d36828

Browse files
authored
fix: track calls to HTMLInputElement.select() (#898)
1 parent f2e8f8e commit 6d36828

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/document/selection.ts

+13
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ export function prepareSelectionInterceptor(
6969
return {realArgs: v}
7070
},
7171
)
72+
73+
prepareInterceptor(
74+
element,
75+
'select',
76+
function interceptorImpl(this: HTMLInputElement | HTMLTextAreaElement) {
77+
this[UISelection] = {
78+
anchorOffset: 0,
79+
focusOffset: getUIValue(element).length,
80+
}
81+
82+
return {realArgs: [] as []}
83+
},
84+
)
7285
}
7386

7487
export function setUISelection(

tests/document/index.ts

+19
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,23 @@ test('clear UI selection if selection is programmatically set', async () => {
140140
element.selectionStart = 2
141141
expect(getUISelection(element)).toHaveProperty('startOffset', 2)
142142
expect(getUISelection(element)).toHaveProperty('endOffset', 2)
143+
144+
setUISelection(element, {anchorOffset: 1, focusOffset: 2})
145+
element.select()
146+
expect(getUISelection(element)).toHaveProperty('startOffset', 0)
147+
expect(getUISelection(element)).toHaveProperty('endOffset', 3)
148+
})
149+
150+
test('select input without selectionRange support', () => {
151+
const {element} = render<HTMLInputElement>(
152+
`<input type="number" value="123"/>`,
153+
)
154+
155+
prepare(element)
156+
157+
setUISelection(element, {focusOffset: 1})
158+
element.select()
159+
160+
expect(getUISelection(element)).toHaveProperty('startOffset', 0)
161+
expect(getUISelection(element)).toHaveProperty('endOffset', 3)
143162
})

0 commit comments

Comments
 (0)