-
Notifications
You must be signed in to change notification settings - Fork 19
PRO-824: Use Unicode-aware regex in ambiguous text check #1730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -23,12 +23,16 @@ const ambiguousPhrases = [ | |||||||||
| __( 'opens a new window', 'accessibility-checker' ), | ||||||||||
| ]; | ||||||||||
|
|
||||||||||
| const normalizedPhrases = ambiguousPhrases.map( | ||||||||||
| ( p ) => p.toLowerCase().replace( /[^\p{L}]+/gu, ' ' ).trim() | ||||||||||
| ); | ||||||||||
|
|
||||||||||
| const checkAmbiguousPhrase = ( text ) => { | ||||||||||
| if ( ! text ) { | ||||||||||
| return false; | ||||||||||
| } | ||||||||||
| text = text.toLowerCase().replace( /[^a-z]+/g, ' ' ).trim(); | ||||||||||
| return ambiguousPhrases.includes( text ); | ||||||||||
| text = text.toLowerCase().replace( /[^\p{L}]+/gu, ' ' ).trim(); | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For locales with accented translations, this drops combining marks instead of treating canonically equivalent text the same. For example, the Vietnamese translation for “continue” is Useful? React with 👍 / 👎. |
||||||||||
| return normalizedPhrases.includes( text ); | ||||||||||
|
Comment on lines
+34
to
+35
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apply the same Unicode-aware normalization (including
Suggested change
|
||||||||||
| }; | ||||||||||
|
|
||||||||||
| export default { | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using only
\p{L}(Letters) in the Unicode regex will strip combining diacritical marks (\p{M}), which are used in many languages (e.g., Hindi, Arabic, Thai, or even Latin languages when strings are in NFD/decomposed form, such as text copied from macOS). Stripping these marks will break words into fragments and cause matching to fail.To prevent this, we should:
\p{M}in the character class:/[^\p{L}\p{M}]+/gu..normalize()to ensure consistent Unicode normalization (NFC) before processing.