Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(document): track HTMLInputElement.setRangeText() #984

Merged
merged 1 commit into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/document/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {dispatchUIEvent} from '../event'
import {Config} from '../setup'
import {prepareSelectionInterceptor} from './selection'
import {prepareRangeTextInterceptor} from './setRangeText'
import {
clearInitialValue,
getInitialValue,
Expand Down Expand Up @@ -69,6 +70,7 @@ function prepareElement(el: Node | HTMLInputElement) {
if ('value' in el) {
prepareValueInterceptor(el)
prepareSelectionInterceptor(el)
prepareRangeTextInterceptor(el)
}

el[isPrepared] = isPrepared
Expand Down
7 changes: 7 additions & 0 deletions src/document/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,10 @@ export function getUISelection(
endOffset: Math.max(sel.anchorOffset, sel.focusOffset),
}
}

/** Flag the IDL selection as clean. This does not change the selection. */
export function setUISelectionClean(
element: HTMLInputElement | HTMLTextAreaElement,
) {
element[UISelection] = undefined
}
21 changes: 21 additions & 0 deletions src/document/setRangeText.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {prepareInterceptor} from './interceptor'
import {setUISelectionClean} from './selection'
import {setUIValueClean} from './value'

export function prepareRangeTextInterceptor(
element: HTMLInputElement | HTMLTextAreaElement,
) {
prepareInterceptor(
element,
'setRangeText',
function interceptorImpl(...realArgs) {
return {
realArgs,
then: () => {
setUIValueClean(element)
setUISelectionClean(element)
},
}
},
)
}
7 changes: 7 additions & 0 deletions src/document/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ export function getUIValue(element: HTMLInputElement | HTMLTextAreaElement) {
: String(element[UIValue])
}

/** Flag the IDL value as clean. This does not change the value.*/
export function setUIValueClean(
element: HTMLInputElement | HTMLTextAreaElement,
) {
element[UIValue] = undefined
}

export function clearInitialValue(
element: HTMLInputElement | HTMLTextAreaElement,
) {
Expand Down
19 changes: 19 additions & 0 deletions tests/document/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,22 @@ test('select input without selectionRange support', () => {
expect(getUISelection(element)).toHaveProperty('startOffset', 0)
expect(getUISelection(element)).toHaveProperty('endOffset', 3)
})

test('track changes to value and selection per setRangeText', () => {
const {element} = render<HTMLInputElement>(`<input/>`)
prepare(element)
setUIValue(element, 'abcd')
setUISelection(element, {focusOffset: 3})

element.setRangeText('X', 1, 2)
expect(element).toHaveValue('aXcd')
expect(element).toHaveProperty('selectionStart', 3)
expect(getUIValue(element)).toBe('aXcd')
expect(getUISelection(element)).toHaveProperty('focusOffset', 3)

element.setRangeText('Y', 1, 2, 'start')
expect(element).toHaveValue('aYcd')
expect(element).toHaveProperty('selectionEnd', 1)
expect(getUIValue(element)).toBe('aYcd')
expect(getUISelection(element)).toHaveProperty('focusOffset', 1)
})