From a55990766be81a6808d5f1a30064a54ed4e4759f Mon Sep 17 00:00:00 2001 From: zepumph Date: Tue, 2 Nov 2021 18:25:47 -0600 Subject: [PATCH] strip br tags from voicing output using himalaya, https://github.com/phetsims/scenery/issues/1314 --- js/accessibility/voicing/voicingManager.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/js/accessibility/voicing/voicingManager.js b/js/accessibility/voicing/voicingManager.js index bd42b98..9b434e4 100644 --- a/js/accessibility/voicing/voicingManager.js +++ b/js/accessibility/voicing/voicingManager.js @@ -417,7 +417,7 @@ class VoicingManager extends Announcer { } // embedding marks (for i18n) impact the output, strip before speaking - const stringToSpeak = stripEmbeddingMarks( utterance.getTextToAlert( this.respectResponseCollectorProperties ) ); + const stringToSpeak = removeBrTags( stripEmbeddingMarks( utterance.getTextToAlert( this.respectResponseCollectorProperties ) ) ); const speechSynthUtterance = new SpeechSynthesisUtterance( stringToSpeak ); speechSynthUtterance.voice = this.voiceProperty.value; speechSynthUtterance.pitch = this.voicePitchProperty.value; @@ -617,6 +617,22 @@ class VoicingQueueElement { } +/** + * @param {Object} element - returned from himalaya parser, see documentation for details. + * @returns {boolean} + */ +const isNotBrTag = element => !( element.type.toLowerCase() === 'element' && element.tagName.toLowerCase() === 'br' ); + +/** + * Remove
or
tags from a string + * @param {string} string - plain text or html string + * @returns {string} + */ +function removeBrTags( string ) { + const parsedAndFiltered = himalaya.parse( string ).filter( isNotBrTag ); + return himalaya.stringify( parsedAndFiltered ); +} + const voicingManager = new VoicingManager(); // @public - Priority levels that can be used by Utterances providing the `announcerOptions.priority` option.