Skip to content

Commit

Permalink
Add a getAccessibleString function for #1669
Browse files Browse the repository at this point in the history
  • Loading branch information
jessegreenberg committed Nov 4, 2024
1 parent f2833aa commit de4f17b
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions js/nodes/RichText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,34 @@ export default class RichText extends WidthSizable( Node ) {
}
}

/**
* Transforms a given string with HTML markup into a string suitable for screen readers.
* Preserves basic styling tags while removing non-accessible markup.
*/
public static getAccessibleString( string: string | number | TReadOnlyProperty<string> ): string {

let parsable = '';
if ( typeof string === 'string' || typeof string === 'number' ) {
parsable = string.toString();
}
else {
parsable = string.value;
}
const rootElements = himalayaVar.parse( parsable );

let deconstructed = '';
rootElements.forEach( element => {
if ( isHimalayaTextNode( element ) ) {
deconstructed += element.content;
}
else if ( isHimalayaElementNode( element ) ) {
deconstructed += RichText.himalayaElementToAccessibleString( element );
}
} );

return deconstructed;
}

/**
* Main recursive function for constructing the RichText Node tree.
*
Expand Down

0 comments on commit de4f17b

Please sign in to comment.