import { isElementType } from '../misc/isElementType' ; import { getValue } from './getValue' ; enum maxLengthSupportedTypes { 'email' = 'email' , 'password' = 'password' , 'search' = 'search' , 'telephone' = 'telephone' , 'text' = 'text' , 'url' = 'url' } export function getSpaceUntilMaxLength ( element ) { const value = getValue ( element ) ; if ( value === null ) { return undefined ; } const maxLength = getSanitizedMaxLength ( element ) ; return maxLength ? maxLength - value . length : undefined ; } function getSanitizedMaxLength ( element ) { if ( ! supportsMaxLength ( element ) ) { return undefined ; } const attr = element . getAttribute ( 'maxlength' ) ?? '' ; return /^\d+$/ . test ( attr ) && Number ( attr ) >= 0 ? Number ( attr ) : undefined ; } function supportsMaxLength ( element ) { return ( isElementType ( element , 'textarea' ) || ( isElementType ( element , 'input' ) && Boolean ( maxLengthSupportedTypes [ element . type as keyof typeof maxLengthSupportedTypes ] ) ) ) ; }