Skip to content

Commit 049522e

Browse files
strakerWilcoFiers
andauthored
fix(color-contrast): ignore format unicode characters (#4102)
* fix(color-contrast): ignore format unicode characters * comment * typo * Apply suggestions from code review Co-authored-by: Wilco Fiers <WilcoFiers@users.noreply.github.com> --------- Co-authored-by: Wilco Fiers <WilcoFiers@users.noreply.github.com>
1 parent c6e07be commit 049522e

4 files changed

Lines changed: 130 additions & 76 deletions

File tree

lib/commons/text/has-unicode.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {
22
getUnicodeNonBmpRegExp,
33
getSupplementaryPrivateUseRegExp,
4-
getPunctuationRegExp
4+
getPunctuationRegExp,
5+
getCategoryFormatRegExp
56
} from './unicode';
67
import emojiRegexText from 'emoji-regex';
78

@@ -20,19 +21,22 @@ import emojiRegexText from 'emoji-regex';
2021
*/
2122
function hasUnicode(str, options) {
2223
const { emoji, nonBmp, punctuations } = options;
24+
let value = false;
25+
2326
if (emoji) {
24-
return emojiRegexText().test(str);
27+
value ||= emojiRegexText().test(str);
2528
}
2629
if (nonBmp) {
27-
return (
30+
value ||=
2831
getUnicodeNonBmpRegExp().test(str) ||
29-
getSupplementaryPrivateUseRegExp().test(str)
30-
);
32+
getSupplementaryPrivateUseRegExp().test(str) ||
33+
getCategoryFormatRegExp().test(str);
3134
}
3235
if (punctuations) {
33-
return getPunctuationRegExp().test(str);
36+
value ||= getPunctuationRegExp().test(str);
3437
}
35-
return false;
38+
39+
return value;
3640
}
3741

3842
export default hasUnicode;

lib/commons/text/remove-unicode.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {
22
getUnicodeNonBmpRegExp,
33
getSupplementaryPrivateUseRegExp,
4-
getPunctuationRegExp
4+
getPunctuationRegExp,
5+
getCategoryFormatRegExp
56
} from './unicode.js';
67
import emojiRegexText from 'emoji-regex';
78

@@ -25,8 +26,10 @@ function removeUnicode(str, options) {
2526
str = str.replace(emojiRegexText(), '');
2627
}
2728
if (nonBmp) {
28-
str = str.replace(getUnicodeNonBmpRegExp(), '');
29-
str = str.replace(getSupplementaryPrivateUseRegExp(), '');
29+
str = str
30+
.replace(getUnicodeNonBmpRegExp(), '')
31+
.replace(getSupplementaryPrivateUseRegExp(), '')
32+
.replace(getCategoryFormatRegExp(), '');
3033
}
3134
if (punctuations) {
3235
str = str.replace(getPunctuationRegExp(), '');

lib/commons/text/unicode.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,15 @@ export function getSupplementaryPrivateUseRegExp() {
8383
// ┏━━━━━━┻━━━━━━┓┏━━━━━━┻━━━━━━┓
8484
return /[\uDB80-\uDBBF][\uDC00-\uDFFF]/g;
8585
}
86+
87+
/**
88+
* Get regular expression for unicode format category.
89+
* When we drop IE11 we can instead use unicode character escape `/p{Cf}/gu`
90+
* Reference:
91+
* - https://www.compart.com/en/unicode/category/Cf
92+
*
93+
* @returns {RegExp}
94+
*/
95+
export function getCategoryFormatRegExp() {
96+
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;
97+
}

0 commit comments

Comments
 (0)