Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/document/UI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export type UIValueString = string & {[UIValue]: true}
export type UISelectionStart = number & {[UISelection]: true}

export function isUIValue(
value: string | UIValueString,
value: string | UIValueString | null,
): value is UIValueString {
return typeof value === 'object' && UIValue in value
return !!value && typeof value === 'object' && UIValue in value
}

export function isUISelectionStart(
Expand Down
15 changes: 9 additions & 6 deletions src/document/interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ export function prepareInterceptor<
then,
} = interceptorImpl.call(this, ...args)

const realFunc = ((!applyNative && objectDescriptor) ||
(prototypeDescriptor as PropertyDescriptor))[target] as (
const sourceDescriptor =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what was this change for?

!applyNative && objectDescriptor ? objectDescriptor : prototypeDescriptor
const realFunc = sourceDescriptor?.[target] as (
this: ElementType,
...a: unknown[]
) => unknown
Expand Down Expand Up @@ -102,18 +103,20 @@ export function prepareValueInterceptor(
'value',
function interceptorImpl(
this: HTMLInputElement | HTMLTextAreaElement,
v: UIValueString | string,
v: UIValueString | string | null,
) {
const isUI = isUIValue(v)
// `value` is a [LegacyNullToEmptyString] DOMString.
const value = v === null ? '' : v
const isUI = isUIValue(value)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value can never be null, so why did we add it to the signature for isUIValue?


if (isUI) {
startTrackValue(this)
}

return {
applyNative: !!isUI,
realArgs: sanitizeValue(this, v),
then: isUI ? undefined : () => trackOrSetValue(this, String(v)),
realArgs: sanitizeValue(this, value),
then: isUI ? undefined : () => trackOrSetValue(this, String(value)),
}
},
)
Expand Down
24 changes: 24 additions & 0 deletions tests/document/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,30 @@ test('reset UI selection if value is programmatically set', async () => {
expect(getUISelection(element)).toHaveProperty('startOffset', 6)
})

test('programmatic `null` value is treated as empty string', async () => {
const {element} = render<HTMLInputElement>(`<input value="abc"/>`)

prepare(element)

// HTMLInputElement.value is a [LegacyNullToEmptyString] DOMString.
// Callers still assign `null` at runtime; the IDL coerces it to `""`.
element.value = null as unknown as string

expect(element).toHaveValue('')
expect(getUIValue(element)).toBe('')
})

test('programmatic `null` value on number input is treated as empty', async () => {
const {element} = render<HTMLInputElement>(`<input type="number" value="1"/>`)

prepare(element)

element.value = null as unknown as string

expect(element).toHaveValue(null)
expect(getUIValue(element)).toBe('')
})

test('clear UI selection if selection is programmatically set', async () => {
const {element} = render<HTMLInputElement>(`<input/>`)

Expand Down