import { fireEvent } from '@testing-library/dom' ; import { prepareSelectionInterceptor } from './selection' ; import { getInitialValue , prepareValueInterceptor , setInitialValue , } from './value' ; const isPrepared = Symbol ( 'Node prepared with document state workarounds' ) ; declare global { interface Node { [ isPrepared ] ? : typeof isPrepared ; } } export function prepareDocument ( document ) { if ( document [ isPrepared ] ) { return ; } document . addEventListener ( 'focus' , e => { const el = e . target as Node ; prepareElement ( el ) ; } , { capture : true , passive : true , } ) ; if ( document . activeElement ) { prepareElement ( document . activeElement ) ; } document . addEventListener ( 'blur' , e => { const el = e . target as HTMLInputElement ; const initialValue = getInitialValue ( el ) ; if ( typeof initialValue === 'string' && el . value !== initialValue ) { fireEvent . change ( el ) ; } } , { capture : true , passive : true , } ) ; document [ isPrepared ] = isPrepared ; } function prepareElement ( el ) { if ( 'value' in el ) { setInitialValue ( el ) ; } if ( el [ isPrepared ] ) { return ; } if ( 'value' in el ) { prepareValueInterceptor ( el ) ; prepareSelectionInterceptor ( el ) ; } el [ isPrepared ] = isPrepared ; } export { getUIValue , setUIValue , startTrackValue , endTrackValue } from './value' ; export { getUISelection , setUISelection } from './selection' ; export type { UISelectionRange } from './selection' ;