-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix(react): escape newlines in JSX quoted attribute values #11728
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 2 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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| export default /*#__PURE__*/ React.createElement(A, { | ||
| className: b, | ||
| header: "C", | ||
| subheader: "D E" | ||
| subheader: "D\n E" | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| function Component() { | ||
| return /*#__PURE__*/ React.createElement("div", { | ||
| name: "A B" | ||
| name: "A\n B" | ||
| }); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| function test() { | ||
| return /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement(A, { | ||
| b: "\\ " | ||
| b: "\\\n " | ||
| })); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2192,9 +2192,10 @@ fn transform_jsx_attr_str(v: &Wtf8) -> Wtf8Buf { | |
| '\u{000c}' => buf.push_str("\\f"), | ||
| ' ' => buf.push_char(' '), | ||
|
|
||
| '\n' | '\r' | '\t' => { | ||
| '\n' => buf.push_char('\n'), | ||
|
Member
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. This change makes multiline JSX attribute strings diverge from Babel semantics. Babel applies |
||
| '\r' => buf.push_char('\r'), | ||
| '\t' => { | ||
| buf.push_char(' '); | ||
|
|
||
| while let Some(next) = iter.peek() { | ||
|
Member
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. Multiline JSX attribute values still normalize Because this arm does So the newline fix is correct, but tab-indented multiline attrs are still semantically changed. Could we preserve |
||
| if next.to_char() == Some(' ') { | ||
| iter.next(); | ||
|
Comment on lines
+2197
to
2201
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.
When a quoted JSX attribute spans lines in a tab-indented file, this branch still rewrites Useful? React with 👍 / 👎. |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| // Newline in quoted JSX attribute value should be escaped, not collapsed to space | ||
| // https://github.com/swc-project/swc/issues/11550 | ||
| const hello = <div data-anything="bruh | ||
| bruh">hello</div>; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| // Newline in quoted JSX attribute value should be escaped, not collapsed to space | ||
| // https://github.com/swc-project/swc/issues/11550 | ||
| const hello = /*#__PURE__*/ React.createElement("div", { | ||
| "data-anything": "bruh\nbruh" | ||
| }, "hello"); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| export default /*#__PURE__*/ React.createElement(A, { | ||
| className: b, | ||
| header: "C", | ||
| subheader: "D E" | ||
| subheader: "D\n E" | ||
| }); |
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.
Nice fix for LF, but this exposes a CRLF regression. In
read_jsx_str(crates/swc_ecma_parser/src/lexer/mod.rs), whenread_jsx_new_line(false)consumes\r\n,chunk_startis still set viacur_pos + BytePos(ch.len_utf8() as _)(line 1505), so the\ngets re-sliced and appended again. I reproduced this by changing the fixture input newline to CRLF: the emitted value becomes"bruh\\r\\n\\nbruh"(extra LF). Could we advancechunk_startto the current lexer position after consuming the newline sequence and add a CRLF fixture?