Repository navigation
[lexical] Bug Fix: Don't treat surrogate pairs (emoji) as left-to-right #9462
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: main
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,5 @@ | ||
| --- | ||
| 'lexical': patch | ||
| --- | ||
|
|
||
| [fix] `getTextDirection` no longer reads an emoji or another character outside the Basic Multilingual Plane as left-to-right, so a line that starts with an emoji followed by right-to-left text is right-to-left | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,10 +111,16 @@ export const COMPOSITION_START_CHAR: string = IS_FIREFOX | |
| ? NON_BREAKING_SPACE | ||
| : COMPOSITION_SUFFIX; | ||
| const RTL = '\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC'; | ||
| // The surrogate range \uD800-\uDFFF is left out of LTR: these regexes match | ||
| // UTF-16 code units, and a surrogate pair (such as an emoji) is not a strong | ||
| // left-to-right character, so it must not decide the direction. | ||
| // Neither are the variation selectors \uFE00-\uFE0F, such as the \uFE0F that | ||
| // follows many emoji (e.g. โค๏ธ). | ||
| const LTR = | ||
| 'A-Za-z\u00C0-\u00D6\u00D8-\u00F6' + | ||
| '\u00F8-\u02B8\u0300-\u0590\u0800-\u1FFF\u200E\u2C00-\uFB1C' + | ||
| '\uFE00-\uFE6F\uFEFD-\uFFFF'; | ||
| '\u00F8-\u02B8\u0300-\u0590\u0800-\u1FFF\u200E\u2C00-\uD7FF' + | ||
|
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. Optional: dropping the whole surrogate range also makes strong-LTR characters outside the BMP neutral. Regional-indicator flags (U+1F1E6โU+1F1FF) and CJK Extension B (U+20000) are Bidi_Class L, so browsers lay out |
||
| '\uE000-\uFB1C' + | ||
| '\uFE10-\uFE6F\uFEFD-\uFFFF'; | ||
|
|
||
| /** | ||
| * A RegExp matching text whose first strongly directional character is in | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -190,7 +190,7 @@ describe('LexicalUtils tests', () => { | |
| expect(getTextDirection('\u200E')).toBe('ltr'); | ||
| expect(getTextDirection('\u2C00')).toBe('ltr'); | ||
| expect(getTextDirection('\uFB1C')).toBe('ltr'); | ||
| expect(getTextDirection('\uFE00')).toBe('ltr'); | ||
| expect(getTextDirection('\uFE10')).toBe('ltr'); | ||
| expect(getTextDirection('\uFE6F')).toBe('ltr'); | ||
| expect(getTextDirection('\uFEFD')).toBe('ltr'); | ||
| expect(getTextDirection('\uFFFF')).toBe('ltr'); | ||
|
|
@@ -202,6 +202,21 @@ describe('LexicalUtils tests', () => { | |
| expect(getTextDirection(`\uFEFC`)).toBe('rtl'); | ||
| }); | ||
|
|
||
| test('getTextDirection() skips emoji: surrogate pairs and variation selectors (#5260)', () => { | ||
|
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. Optional: users see this change in code-block Cmd+Left/Right, through |
||
| expect(getTextDirection('๐')).toBe(null); | ||
| expect(getTextDirection('๐ ืฉืืื')).toBe('rtl'); | ||
| expect(getTextDirection('๐ ู ุฑุญุจุง')).toBe('rtl'); | ||
| expect(getTextDirection('๐๐ ืฉืืื')).toBe('rtl'); | ||
| expect(getTextDirection('๐ hello')).toBe('ltr'); | ||
| expect(getTextDirection('โค๏ธ ืฉืืื')).toBe('rtl'); | ||
| expect(getTextDirection('1๏ธโฃ ืฉืืื')).toBe('rtl'); | ||
| expect(getTextDirection('โ๏ธ ู ุฑุญุจุง')).toBe('rtl'); | ||
| expect(getTextDirection('\uFE0F')).toBe(null); | ||
| expect(getTextDirection('ืฉืืื ๐')).toBe('rtl'); | ||
| expect(getTextDirection('\uD7FF')).toBe('ltr'); | ||
| expect(getTextDirection('\uE000')).toBe('ltr'); | ||
| }); | ||
|
|
||
| test('isExactShortcutMatch() matches by event.key for single-letter', () => { | ||
| const eventWithoutUppercase = new KeyboardEvent('keydown', { | ||
| code: 'KeyZ', | ||
|
|
||
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.
Optional: this fix changes what
getTextDirectionreturns and how Home/End behaves in a code block, so CONTRIBUTING.md (Changesets) asks for aVerify:paragraph, which lists the entry under "Behavior changes". For example:Verify: getTextDirection('๐ ืฉืืื') returns 'rtl'; in a code block, Cmd+Left/Right (Home/End outside macOS) on a line that starts with an emoji followed by Hebrew or Arabic moves the caret the way it does on a line of only Hebrew or Arabic.