Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/text-direction-ignores-emoji.md
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

Copy link
Copy Markdown

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 getTextDirection returns and how Home/End behaves in a code block, so CONTRIBUTING.md (Changesets) asks for a Verify: 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.

10 changes: 8 additions & 2 deletions packages/lexical/src/LexicalConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' +

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 ๐Ÿ‡ฎ๐Ÿ‡ฑ ืฉืœื•ื under dir="auto" left-to-right, while getTextDirection now returns 'rtl'. Cmd+Left/Right in a code block is then inverted on such a line. Regexes with the u flag that list those astral L ranges would close the gap; otherwise, a test pinning the current result would make the limitation deliberate.

'\uE000-\uFB1C' +
'\uFE10-\uFE6F\uFEFD-\uFFFF';

/**
* A RegExp matching text whose first strongly directional character is in
Expand Down
5 changes: 4 additions & 1 deletion packages/lexical/src/LexicalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,10 @@ export function getEditorPropertyFromDOMNode(node: Node | null): unknown {
return node ? node.__lexicalEditor : null;
}

/** Returns the text direction ('ltr' or 'rtl') of the given string, or null if it contains no strong directional characters. */
/**
* Returns the text direction ('ltr' or 'rtl') of the given string, or null if it contains no strong directional characters.
* Characters outside the Basic Multilingual Plane (eg. emoji) are treated as neutral.
*/
export function getTextDirection(text: string): 'ltr' | 'rtl' | null {
if (RTL_REGEX.test(text)) {
return 'rtl';
Expand Down
17 changes: 16 additions & 1 deletion packages/lexical/src/__tests__/unit/LexicalUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -202,6 +202,21 @@ describe('LexicalUtils tests', () => {
expect(getTextDirection(`\uFEFC`)).toBe('rtl');
});

test('getTextDirection() skips emoji: surrogate pairs and variation selectors (#5260)', () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: users see this change in code-block Cmd+Left/Right, through $getCodeLineDirection. A case beside the rtl code lines tests in packages/lexical-code/src/__tests__/unit/LexicalCodeNode.test.ts would cover it: a plaintext line ๐Ÿ˜€ ุชุฌุฑุจุฉ, caret at offset 3, then MOVE_TO_END should land at offset 0 (it lands at 8 on main).

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',
Expand Down
Loading