Skip to content
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

fix(color-contrast): ignore format unicode characters #4102

Merged
merged 4 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions lib/commons/text/has-unicode.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
getUnicodeNonBmpRegExp,
getSupplementaryPrivateUseRegExp,
getPunctuationRegExp
getPunctuationRegExp,
getCategoryFormatRegExp
} from './unicode';
import emojiRegexText from 'emoji-regex';

Expand All @@ -20,19 +21,22 @@ import emojiRegexText from 'emoji-regex';
*/
function hasUnicode(str, options) {
const { emoji, nonBmp, punctuations } = options;
let value = false;

if (emoji) {
return emojiRegexText().test(str);
value ||= emojiRegexText().test(str);
}
if (nonBmp) {
return (
value ||=
getUnicodeNonBmpRegExp().test(str) ||
getSupplementaryPrivateUseRegExp().test(str)
);
getSupplementaryPrivateUseRegExp().test(str) ||
getCategoryFormatRegExp().test(str);
}
if (punctuations) {
return getPunctuationRegExp().test(str);
value ||= getPunctuationRegExp().test(str);
}
return false;

return value;
}

export default hasUnicode;
9 changes: 6 additions & 3 deletions lib/commons/text/remove-unicode.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
getUnicodeNonBmpRegExp,
getSupplementaryPrivateUseRegExp,
getPunctuationRegExp
getPunctuationRegExp,
getCategoryFormatRegExp
} from './unicode.js';
import emojiRegexText from 'emoji-regex';

Expand All @@ -25,8 +26,10 @@ function removeUnicode(str, options) {
str = str.replace(emojiRegexText(), '');
}
if (nonBmp) {
str = str.replace(getUnicodeNonBmpRegExp(), '');
str = str.replace(getSupplementaryPrivateUseRegExp(), '');
str = str
.replace(getUnicodeNonBmpRegExp(), '')
.replace(getSupplementaryPrivateUseRegExp(), '')
.replace(getCategoryFormatRegExp(), '');
}
if (punctuations) {
str = str.replace(getPunctuationRegExp(), '');
Expand Down
12 changes: 12 additions & 0 deletions lib/commons/text/unicode.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,15 @@ export function getSupplementaryPrivateUseRegExp() {
// ┏━━━━━━┻━━━━━━┓┏━━━━━━┻━━━━━━┓
return /[\uDB80-\uDBBF][\uDC00-\uDFFF]/g;
}

/**
* Get regular expression for unicode format category.
* When we drop IE11 we can instead use unicode character escape `/p{Cf}/gu`
* Reference:
* - https://www.compart.com/en/unicode/category/Cf
*
* @returns {RegExp}
*/
export function getCategoryFormatRegExp() {
return /[\xAD\u0600-\u0605\u061C\u06DD\u070F\u08E2\u180E\u200B-\u200F\u202A-\u202E\u2060-\u2064\u2066-\u206F\uFEFF\uFFF9-\uFFFB]|\uD804[\uDCBD\uDCCD]|\uD80D[\uDC30-\uDC38]|\uD82F[\uDCA0-\uDCA3]|\uD834[\uDD73-\uDD7A]|\uDB40[\uDC01\uDC20-\uDC7F]/g;
}