Skip to content

Commit 77a7fa8

Browse files
authored
fix(document): do not track value on HTMLSelectElement (#989)
* fix(document): do not track `value` on `HTMLSelectElement` * throw error when trying to track non-existent property
1 parent c88865d commit 77a7fa8

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

src/document/index.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {dispatchUIEvent} from '../event'
22
import {Config} from '../setup'
3+
import {isElementType} from '../utils'
34
import {prepareSelectionInterceptor} from './selection'
45
import {prepareRangeTextInterceptor} from './setRangeText'
56
import {
@@ -24,7 +25,7 @@ export function prepareDocument(document: Document) {
2425
document.addEventListener(
2526
'focus',
2627
e => {
27-
const el = e.target as Node
28+
const el = e.target as Element
2829

2930
prepareElement(el)
3031
},
@@ -62,12 +63,12 @@ export function prepareDocument(document: Document) {
6263
document[isPrepared] = isPrepared
6364
}
6465

65-
function prepareElement(el: Node | HTMLInputElement) {
66+
function prepareElement(el: Element) {
6667
if (el[isPrepared]) {
6768
return
6869
}
6970

70-
if ('value' in el) {
71+
if (isElementType(el, ['input', 'textarea'])) {
7172
prepareValueInterceptor(el)
7273
prepareSelectionInterceptor(el)
7374
prepareRangeTextInterceptor(el)

src/document/interceptor.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type Params<Prop> = Prop extends anyFunc ? Parameters<Prop> : [Prop]
1010
type ImplReturn<Prop> = Prop extends anyFunc ? Parameters<Prop> : Prop
1111

1212
export function prepareInterceptor<
13-
ElementType extends Node,
13+
ElementType extends Element,
1414
PropName extends keyof ElementType,
1515
>(
1616
element: ElementType,
@@ -39,11 +39,14 @@ export function prepareInterceptor<
3939

4040
const target = prototypeDescriptor?.set ? 'set' : 'value'
4141

42+
/* istanbul ignore if */
4243
if (
4344
typeof prototypeDescriptor?.[target] !== 'function' ||
4445
(prototypeDescriptor[target] as Interceptable)[Interceptor]
4546
) {
46-
return
47+
throw new Error(
48+
`Element ${element.tagName} does not implement "${String(propName)}".`,
49+
)
4750
}
4851

4952
function intercept(

src/document/value.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ function sanitizeValue(
5757
return String(v)
5858
}
5959

60-
export function prepareValueInterceptor(element: HTMLInputElement) {
60+
export function prepareValueInterceptor(
61+
element: HTMLInputElement | HTMLTextAreaElement,
62+
) {
6163
prepareInterceptor(element, 'value', valueInterceptor)
6264
}
6365

0 commit comments

Comments
 (0)