Skip to content

Commit ac2cae5

Browse files
committed
[compiler] Fix for string attribute values with emoji
If a JSX attribute value is a string that contains unicode or other characters that need special escaping, we wrap the attribute value in an expression container. However, our unicode to detect this only handled the basic unicode character plane, not the "astral" plane which includes emojis. This PR updates the regex to detect such extended characters and also use an expression container. ghstack-source-id: 6d9c8e4dd22285077108e2fa53d66154d1b781fb Pull Request resolved: facebook#33096
1 parent 66de8e5 commit ac2cae5

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -2327,9 +2327,12 @@ function codegenInstructionValue(
23272327
* u0080 to u009F: C1 control codes
23282328
* u00A0 to uFFFF: All non-basic Latin characters
23292329
* https://en.wikipedia.org/wiki/List_of_Unicode_characters#Control_codes
2330+
*
2331+
* u010000 to u10FFFF: Astral plane characters
2332+
* https://mathiasbynens.be/notes/javascript-unicode
23302333
*/
23312334
const STRING_REQUIRES_EXPR_CONTAINER_PATTERN =
2332-
/[\u{0000}-\u{001F}\u{007F}\u{0080}-\u{FFFF}]|"|\\/u;
2335+
/[\u{0000}-\u{001F}\u{007F}\u{0080}-\u{FFFF}\u{010000}-\u{10FFFF}]|"|\\/u;
23332336
function codegenJsxAttribute(
23342337
cx: Context,
23352338
attribute: JsxAttribute,

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/jsx-string-attribute-expression-container.expect.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ function Component() {
1111
<Text value={'Lauren'} />
1212
<Text value={'சத்யா'} />
1313
<Text value={'Sathya'} />
14+
<Text value={'welcome 👋'} />
1415
</div>
1516
);
1617
}
@@ -42,6 +43,7 @@ function Component() {
4243
<Text value="Lauren" />
4344
<Text value={"\u0B9A\u0BA4\u0BCD\u0BAF\u0BBE"} />
4445
<Text value="Sathya" />
46+
<Text value={"welcome \uD83D\uDC4B"} />
4547
</div>
4648
);
4749
$[0] = t0;
@@ -74,4 +76,4 @@ export const FIXTURE_ENTRYPOINT = {
7476
7577
### Eval output
7678
(kind: ok) <div><span>
77-
</span><span>A E</span><span>나은</span><span>Lauren</span><span>சத்யா</span><span>Sathya</span></div>
79+
</span><span>A E</span><span>나은</span><span>Lauren</span><span>சத்யா</span><span>Sathya</span><span>welcome 👋</span></div>

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/jsx-string-attribute-expression-container.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function Component() {
77
<Text value={'Lauren'} />
88
<Text value={'சத்யா'} />
99
<Text value={'Sathya'} />
10+
<Text value={'welcome 👋'} />
1011
</div>
1112
);
1213
}

0 commit comments

Comments
 (0)