-
Notifications
You must be signed in to change notification settings - Fork 4k
/
filterMessage.js
30 lines (24 loc) · 753 Bytes
/
filterMessage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
let previousMessage = '';
/**
* Filter the message to be announced to the screenreader.
*
* @param {String} message The message to be announced.
*
* @returns {String}
*/
const filterMessage = function( message ) {
/*
* 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, ' ' );
if ( previousMessage === message ) {
message += '\u00A0';
}
previousMessage = message;
return message;
};
export default filterMessage;