-
Notifications
You must be signed in to change notification settings - Fork 3
Fix emoji false-positives in invisible character detection #377
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 |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import React from "react"; | ||
| import { act, render } from "@testing-library/react"; | ||
|
|
||
| import { useTextValidation } from "../useTextValidation"; | ||
|
|
||
| const HookWrapper: React.FC<{ value: string; callback: jest.Mock; callbackDelay?: number }> = ({ | ||
| value, | ||
| callback, | ||
| callbackDelay = 0, | ||
| }) => { | ||
| useTextValidation({ | ||
| value, | ||
| onChange: jest.fn(), | ||
| invisibleCharacterWarning: { callback, callbackDelay }, | ||
| }); | ||
| return null; | ||
| }; | ||
|
|
||
| describe("useTextValidation", () => { | ||
| beforeEach(() => { | ||
| jest.useFakeTimers(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.useRealTimers(); | ||
| }); | ||
|
|
||
| /** Render the hook with a controlled value and flush the debounce timer. */ | ||
| const runWithValue = (value: string, callbackDelay = 0) => { | ||
| const callback = jest.fn(); | ||
| render(<HookWrapper value={value} callback={callback} callbackDelay={callbackDelay} />); | ||
| act(() => { | ||
| jest.runAllTimers(); | ||
| }); | ||
| return callback; | ||
| }; | ||
|
|
||
| describe("invisible character detection", () => { | ||
| it("reports empty set for plain text", () => { | ||
| const callback = runWithValue("hello world"); | ||
| expect(callback).toHaveBeenCalledWith(new Set()); | ||
| }); | ||
|
|
||
| it("detects zero-width space (U+200B)", () => { | ||
| const callback = runWithValue("hello\u200Bworld"); | ||
| expect(callback).toHaveBeenCalledWith(new Set([0x200b])); | ||
| }); | ||
|
|
||
| it("detects zero-width non-joiner (U+200C)", () => { | ||
| const callback = runWithValue("hello\u200Cworld"); | ||
| expect(callback).toHaveBeenCalledWith(new Set([0x200c])); | ||
| }); | ||
| }); | ||
|
|
||
| describe("emoji false-positive prevention", () => { | ||
| it("does not flag ✔️ (base char + variation selector U+FE0F)", () => { | ||
| const callback = runWithValue("✔️"); | ||
| expect(callback).toHaveBeenCalledWith(new Set()); | ||
| }); | ||
|
|
||
| it("does not flag ZWJ sequence emoji 👨👩👧👦", () => { | ||
|
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. Same with the ZWJ. There are also legitimate use cases for it besides emojis. |
||
| const callback = runWithValue("👨👩👧👦"); | ||
| expect(callback).toHaveBeenCalledWith(new Set()); | ||
| }); | ||
|
|
||
| it("does not flag keycap emoji #️⃣", () => { | ||
| const callback = runWithValue("#️⃣"); | ||
| expect(callback).toHaveBeenCalledWith(new Set()); | ||
| }); | ||
| }); | ||
|
|
||
| describe("mixed content", () => { | ||
| it("detects ZWS while ignoring surrounding emoji", () => { | ||
| const callback = runWithValue("Check\u200B ✔️👨👩👧#️⃣"); | ||
| expect(callback).toHaveBeenCalledWith(new Set([0x200b])); | ||
| }); | ||
|
|
||
| it("reports empty set for text with only emoji", () => { | ||
| const callback = runWithValue("✔️ 👨👩👧👦#️⃣"); | ||
| expect(callback).toHaveBeenCalledWith(new Set()); | ||
| }); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,19 +44,28 @@ export const useTextValidation = <T>({ value, onChange, invisibleCharacterWarnin | |
| state.current.detectedCodePoints = new Set(); | ||
| }, []); | ||
| const detectionRegex = React.useMemo(() => chars.invisibleZeroWidthCharacters.createRegex(), []); | ||
| const segmenter = React.useMemo(() => new Intl.Segmenter(undefined, { granularity: "grapheme" }), []); | ||
| const emojiRegex = React.useMemo(() => new RegExp("\\p{Extended_Pictographic}|\\u20E3", "u"), []); | ||
|
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. Why is the
Contributor
Author
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.
|
||
|
|
||
| const detectIssues = React.useCallback( | ||
| (value: string): void => { | ||
| detectionRegex.lastIndex = 0; | ||
| let matchArray = detectionRegex.exec(value); | ||
| while (matchArray) { | ||
| const codePoint = matchArray[0].codePointAt(0); | ||
| if (codePoint) { | ||
| state.current.detectedCodePoints.add(codePoint); | ||
| for (const { segment } of segmenter.segment(value)) { | ||
| if (emojiRegex.test(segment)) { | ||
| // skip emoji clusters since they legitimately contain variation selectors, ZWJ, tags, etc. | ||
| } else { | ||
| detectionRegex.lastIndex = 0; | ||
| let matchArray = detectionRegex.exec(segment); | ||
| while (matchArray) { | ||
| const codePoint = matchArray[0].codePointAt(0); | ||
| if (codePoint) { | ||
| state.current.detectedCodePoints.add(codePoint); | ||
| } | ||
| matchArray = detectionRegex.exec(segment); | ||
| } | ||
| } | ||
| matchArray = detectionRegex.exec(value); | ||
| } | ||
| }, | ||
| [detectionRegex] | ||
| [detectionRegex, segmenter, emojiRegex] | ||
| ); | ||
| // Checks if the value contains any problematic characters with a small delay. | ||
| const checkValue = React.useCallback( | ||
|
|
||
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.
Maybe we should remove variation selectors completely from the black list. Emojis are not the only use case.