Skip to content

Commit cc5c0c3

Browse files
add a way to remove formatting tags from strings and add underscores to list, see phetsims/joist#1038
1 parent 089ad07 commit cc5c0c3

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

js/accessibility/pdom/PDOMUtils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ const INS_TAG = 'INS';
6161
const SUB_TAG = 'SUB';
6262
const SUP_TAG = 'SUP';
6363
const BR_TAG = 'BR';
64+
const U_TAG = 'U';
6465

6566
// These browser tags define which elements are focusable by default. Used by ParallelDOM to decide when
6667
// to override tabindex on the primary sibling. See https://stackoverflow.com/questions/1599660/which-html-elements-can-receive-focus
6768
const DEFAULT_FOCUSABLE_TAGS = [ A_TAG, AREA_TAG, INPUT_TAG, SELECT_TAG, TEXTAREA_TAG, BUTTON_TAG, IFRAME_TAG ];
6869

6970
// collection of tags that are used for formatting text
7071
const FORMATTING_TAGS = [ BOLD_TAG, STRONG_TAG, I_TAG, EM_TAG, MARK_TAG, SMALL_TAG, DEL_TAG, INS_TAG, SUB_TAG,
71-
SUP_TAG, BR_TAG ];
72+
SUP_TAG, BR_TAG, U_TAG ];
7273

7374
// these elements do not have a closing tag, so they won't support features like innerHTML. This is how PhET treats
7475
// these elements, not necessary what is legal html.

js/nodes/RichText.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,7 @@ export default class RichText extends WidthSizable( Node ) {
17841784
* Stringifies an HTML subtree defined by the given element, but removing certain tags that we don't need for
17851785
* accessibility (like <a>, <span>, etc.), and adding in tags we do want (see ACCESSIBLE_TAGS).
17861786
*/
1787-
public static himalayaElementToAccessibleString( element: HimalayaNode ): string {
1787+
public static himalayaElementToAccessibleString( element: HimalayaNode, keepFormattingTags = true ): string {
17881788
if ( isHimalayaTextNode( element ) ) {
17891789
return richTextContentToString( element.content );
17901790
}
@@ -1797,11 +1797,18 @@ export default class RichText extends WidthSizable( Node ) {
17971797
content = StringUtils.wrapDirection( content, dir );
17981798
}
17991799

1800-
if ( _.includes( ACCESSIBLE_TAGS, element.tagName ) ) {
1800+
if ( keepFormattingTags && _.includes( ACCESSIBLE_TAGS, element.tagName ) ) {
18011801
return `<${element.tagName}>${content}</${element.tagName}>`;
18021802
}
18031803
else {
1804-
return content;
1804+
1805+
// We are removing all formatting tags. For breaks, add a space as that was the most likely intent.
1806+
if ( element.tagName === 'br' ) {
1807+
return ` ${content}`;
1808+
}
1809+
else {
1810+
return content;
1811+
}
18051812
}
18061813
}
18071814
else {
@@ -1813,13 +1820,13 @@ export default class RichText extends WidthSizable( Node ) {
18131820
* Transforms a given string with HTML markup into a string suitable for screen readers.
18141821
* Preserves basic styling tags while removing non-accessible markup.
18151822
*/
1816-
public static getAccessibleStringProperty( stringProperty: TReadOnlyProperty<string> ): TReadOnlyProperty<string> {
1823+
public static getAccessibleStringProperty( stringProperty: TReadOnlyProperty<string>, keepFormattingTags = true ): TReadOnlyProperty<string> {
18171824
return new DerivedStringProperty( [ stringProperty ], string => {
18181825
const rootElements: HimalayaNode[] = himalayaVar.parse( string );
18191826

18201827
let accessibleString = '';
18211828
rootElements.forEach( element => {
1822-
accessibleString += RichText.himalayaElementToAccessibleString( element );
1829+
accessibleString += RichText.himalayaElementToAccessibleString( element, keepFormattingTags );
18231830
} );
18241831

18251832
return accessibleString;

0 commit comments

Comments
 (0)