license import { ModifierKeys } from '@angular/cdk/testing' ; import { PERIOD } from '@angular/cdk/keycodes' ; import { dispatchFakeEvent , dispatchKeyboardEvent } from './dispatch-events' ; import { triggerFocus } from './element-focus' ; /** Input types for which the value can be entered incrementally. */ const incrementalInputTypes = new Set ( [ 'text' , 'email' , 'hidden' , 'password' , 'search' , 'tel' , 'url' , ] ) ; docs-private export function isTextInput ( element ) { const nodeName = element . nodeName . toLowerCase ( ) ; return nodeName === 'input' || nodeName === 'textarea' ; } param element param keys docs-private export function typeInElement ( element , ... keys ) ; param element param modifiers param keys docs-private export function typeInElement ( element , modifiers , ... keys ) ; export function typeInElement ( element , ... modifiersAndKeys ) { const first = modifiersAndKeys [ 0 ] ; let modifiers ; let rest ; if ( typeof first !== 'string' && first . keyCode === undefined && first . key === undefined ) { modifiers = first ; rest = modifiersAndKeys . slice ( 1 ) ; } else { modifiers = { } ; rest = modifiersAndKeys ; } const isInput = isTextInput ( element ) ; const inputType = element . getAttribute ( 'type' ) || 'text' ; const keys = rest . map ( k => typeof k === 'string' ? k . split ( '' ) . map ( c => ( { keyCode : c . toUpperCase ( ) . charCodeAt ( 0 ) , key : c } ) ) : [ k ] ) . reduce ( ( arr , k ) => arr . concat ( k ) , [ ] ) ; const enterValueIncrementally = inputType === 'number' && keys . length > 0 ? keys . every ( key => key . key !== '.' && key . keyCode !== PERIOD ) : incrementalInputTypes . has ( inputType ) ; triggerFocus ( element ) ; if ( ! enterValueIncrementally ) { ( element as HTMLInputElement ) . value = keys . reduce ( ( value , key ) => value + ( key . key || '' ) , '' ) ; } for ( const key of keys ) { dispatchKeyboardEvent ( element , 'keydown' , key . keyCode , key . key , modifiers ) ; dispatchKeyboardEvent ( element , 'keypress' , key . keyCode , key . key , modifiers ) ; if ( isInput && key . key && key . key . length === 1 ) { if ( enterValueIncrementally ) { ( element as HTMLInputElement | HTMLTextAreaElement ) . value += key . key ; dispatchFakeEvent ( element , 'input' ) ; } } dispatchKeyboardEvent ( element , 'keyup' , key . keyCode , key . key , modifiers ) ; } if ( ! enterValueIncrementally ) { dispatchFakeEvent ( element , 'input' ) ; } } docs-private export function clearElement ( element ) { triggerFocus ( element as HTMLElement ) ; element . value = '' ; dispatchFakeEvent ( element , 'input' ) ; }