Skip to content

Commit

Permalink
fix(document): do not track value on HTMLSelectElement (#989)
Browse files Browse the repository at this point in the history
* fix(document): do not track `value` on `HTMLSelectElement`

* throw error when trying to track non-existent property
  • Loading branch information
ph-fritsche authored Jul 15, 2022
1 parent c88865d commit 77a7fa8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/document/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {dispatchUIEvent} from '../event'
import {Config} from '../setup'
import {isElementType} from '../utils'
import {prepareSelectionInterceptor} from './selection'
import {prepareRangeTextInterceptor} from './setRangeText'
import {
Expand All @@ -24,7 +25,7 @@ export function prepareDocument(document: Document) {
document.addEventListener(
'focus',
e => {
const el = e.target as Node
const el = e.target as Element

prepareElement(el)
},
Expand Down Expand Up @@ -62,12 +63,12 @@ export function prepareDocument(document: Document) {
document[isPrepared] = isPrepared
}

function prepareElement(el: Node | HTMLInputElement) {
function prepareElement(el: Element) {
if (el[isPrepared]) {
return
}

if ('value' in el) {
if (isElementType(el, ['input', 'textarea'])) {
prepareValueInterceptor(el)
prepareSelectionInterceptor(el)
prepareRangeTextInterceptor(el)
Expand Down
7 changes: 5 additions & 2 deletions src/document/interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Params<Prop> = Prop extends anyFunc ? Parameters<Prop> : [Prop]
type ImplReturn<Prop> = Prop extends anyFunc ? Parameters<Prop> : Prop

export function prepareInterceptor<
ElementType extends Node,
ElementType extends Element,
PropName extends keyof ElementType,
>(
element: ElementType,
Expand Down Expand Up @@ -39,11 +39,14 @@ export function prepareInterceptor<

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

/* istanbul ignore if */
if (
typeof prototypeDescriptor?.[target] !== 'function' ||
(prototypeDescriptor[target] as Interceptable)[Interceptor]
) {
return
throw new Error(
`Element ${element.tagName} does not implement "${String(propName)}".`,
)
}

function intercept(
Expand Down
4 changes: 3 additions & 1 deletion src/document/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ function sanitizeValue(
return String(v)
}

export function prepareValueInterceptor(element: HTMLInputElement) {
export function prepareValueInterceptor(
element: HTMLInputElement | HTMLTextAreaElement,
) {
prepareInterceptor(element, 'value', valueInterceptor)
}

Expand Down

0 comments on commit 77a7fa8

Please sign in to comment.