@wordpress/a11y
Version:
Accessibility (a11y) utilities for WordPress.
32 lines (27 loc) • 908 B
text/typescript
let previousMessage = '';
/**
* Filter the message to be announced to the screenreader.
*
* @param message The message to be announced.
*
* @return The filtered message.
*/
export default function filterMessage( message: string ): string {
/*
* Strip HTML tags (if any) from the message string. Ideally, messages should
* be simple strings, carefully crafted for specific use with A11ySpeak.
* When re-using already existing strings this will ensure simple HTML to be
* stripped out and replaced with a space. Browsers will collapse multiple
* spaces natively.
*/
message = message.replace( /<[^<>]+>/g, ' ' );
/*
* Safari + VoiceOver don't announce repeated, identical strings. We use
* a `no-break space` to force them to think identical strings are different.
*/
if ( previousMessage === message ) {
message += '\u00A0';
}
previousMessage = message;
return message;
}