Skip to content

Commit 21835b1

Browse files
committed
fix(compiler): keep spread raw-text element children and quotes raw
Element children of a spread raw-text element such as `<xmp {...p}>` now keep their characters in the Rust compiler, matching Babel. In both compilers a quote opens an attribute value only after `=`, and `<plaintext>` is treated as raw text to the end of the template.
1 parent 0b6c99b commit 21835b1

9 files changed

Lines changed: 144 additions & 15 deletions

File tree

‎packages/babel-plugin/src/ssr/template.ts‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ export function createTemplate(
152152

153153
type AsciiState =
154154
| { kind: "text" }
155-
| { kind: "tag"; raw?: string }
155+
// Only a quote right after `=` opens a quoted value.
156+
| { kind: "tag"; raw?: string; value?: boolean }
156157
| { kind: "quoted"; quote: string; raw?: string }
157158
// Comments, CDATA and bogus comments, kept as written up to `end`.
158159
| { kind: "preserve"; end: string }
@@ -167,7 +168,9 @@ export const rawTextElements = new Set([
167168
"iframe",
168169
"noembed",
169170
"noframes",
170-
"xmp"
171+
"xmp",
172+
// `<plaintext>` has no end tag.
173+
"plaintext"
171174
]);
172175
// Latin-1 already keeps a V8 string one-byte, and `&#128;`-`&#159;` decode to other characters.
173176
const nonLatin1 = /[^\x00-\xff]/;
@@ -212,7 +215,13 @@ function asciiParts(parts: string[], state: AsciiState): string[] {
212215
const name = tagName.exec(part)![0].toLowerCase();
213216
state = {
214217
kind: "tag",
215-
raw: !closing && rawTextElements.has(name) ? `</${name}` : undefined
218+
raw:
219+
closing || !rawTextElements.has(name)
220+
? undefined
221+
: name === "plaintext"
222+
? ""
223+
: `</${name}`,
224+
value: false
216225
};
217226
} else if (closing) {
218227
state = { kind: "preserve", end: ">" };
@@ -221,9 +230,19 @@ function asciiParts(parts: string[], state: AsciiState): string[] {
221230
}
222231
out += len === ch.length ? toAscii(ch) : part.slice(i, i + len);
223232
} else if (state.kind === "tag") {
224-
if (ch === '"' || ch === "'") state = { kind: "quoted", quote: ch, raw: state.raw };
233+
if (state.value && (ch === '"' || ch === "'"))
234+
state = { kind: "quoted", quote: ch, raw: state.raw };
225235
else if (ch === ">")
226-
state = state.raw ? { kind: "raw", close: state.raw, escaped: false } : { kind: "text" };
236+
state =
237+
state.raw !== undefined
238+
? { kind: "raw", close: state.raw, escaped: false }
239+
: { kind: "text" };
240+
else
241+
state = {
242+
kind: "tag",
243+
raw: state.raw,
244+
value: ch === "=" || (!!state.value && /[\t\n\f\r ]/.test(ch))
245+
};
227246
out += ch;
228247
} else if (state.kind === "quoted") {
229248
if (ch === state.quote) state = { kind: "tag", raw: state.raw };
@@ -240,6 +259,7 @@ function asciiParts(parts: string[], state: AsciiState): string[] {
240259
state = { ...state, escaped: true };
241260
else if (
242261
!state.escaped &&
262+
state.close !== "" &&
243263
part.slice(i, end).toLowerCase() === state.close &&
244264
endsTagName(part[end])
245265
)

‎packages/babel-plugin/test/__ssr_fixtures__/textInterpolation/code.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,17 @@ const nonAsciiCdata = (
113113
<text innerHTML={"<![CDATA[→]]>"} />
114114
</svg>
115115
);
116+
const nonAsciiSpreadXmpElement = (
117+
<xmp {...spread}>
118+
<span>→</span>
119+
</xmp>
120+
);
121+
const nonAsciiSpreadScriptElement = (
122+
<script {...spread}>
123+
<span>→</span>
124+
</script>
125+
);
126+
const nonAsciiUnquotedQuote = (
127+
<div innerHTML={`<style data-x=a'b>.a::after{content:"→"}</style>`} />
128+
);
129+
const nonAsciiPlaintext = <div innerHTML={"<plaintext><p>→</p>"} />;

‎packages/babel-plugin/test/__ssr_fixtures__/textInterpolation/output.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ var _tmpl$ = "<span>Hello </span>",
2828
_tmpl$23 = '<p title="caf\xE9">caf\xE9 \x80\x91</p>',
2929
_tmpl$24 = "\u2192",
3030
_tmpl$25 = '<script>h = "<!--<script></script>\u2192";</script>',
31-
_tmpl$26 = "<svg><text><![CDATA[\u2192]]></text></svg>";
31+
_tmpl$26 = "<svg><text><![CDATA[\u2192]]></text></svg>",
32+
_tmpl$27 = "<span>\u2192</span>",
33+
_tmpl$28 = '<div><style data-x=a\'b>.a::after{content:"\u2192"}</style></div>',
34+
_tmpl$29 = "<div><plaintext><p>\u2192</p></div>";
3235
var _sk$ = k => k === "title" || k === "data-static";
3336
const trailing = _$ssr(_tmpl$);
3437
const leading = _$ssr(_tmpl$2);
@@ -127,3 +130,7 @@ const latin1AndC1 = _$ssr(_tmpl$23);
127130
const nonAsciiSpreadXmp = _$ssrElement("xmp", spread, _$ssr(_tmpl$24), false);
128131
const nonAsciiEscapedScript = _$ssr(_tmpl$25);
129132
const nonAsciiCdata = _$ssr(_tmpl$26);
133+
const nonAsciiSpreadXmpElement = _$ssrElement("xmp", spread, _$ssr(_tmpl$27), false);
134+
const nonAsciiSpreadScriptElement = _$ssrElement("script", spread, _$ssr(_tmpl$27), false);
135+
const nonAsciiUnquotedQuote = _$ssr(_tmpl$28);
136+
const nonAsciiPlaintext = _$ssr(_tmpl$29);

‎packages/babel-plugin/test/__ssr_hydratable_fixtures__/textInterpolation/code.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,17 @@ const nonAsciiCdata = (
111111
<text innerHTML={"<![CDATA[→]]>"} />
112112
</svg>
113113
);
114+
const nonAsciiSpreadXmpElement = (
115+
<xmp {...spread}>
116+
<span>→</span>
117+
</xmp>
118+
);
119+
const nonAsciiSpreadScriptElement = (
120+
<script {...spread}>
121+
<span>→</span>
122+
</script>
123+
);
124+
const nonAsciiUnquotedQuote = (
125+
<div innerHTML={`<style data-x=a'b>.a::after{content:"→"}</style>`} />
126+
);
127+
const nonAsciiPlaintext = <div innerHTML={"<plaintext><p>→</p>"} />;

‎packages/babel-plugin/test/__ssr_hydratable_fixtures__/textInterpolation/output.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ var _tmpl$ = ["<span", ">Hello </span>"],
3232
_tmpl$22 = ["<p", ' title="caf\xE9">caf\xE9 \x80\x91</p>'],
3333
_tmpl$23 = "\u2192",
3434
_tmpl$24 = ["<script", '>h = "<!--<script></script>\u2192";</script>'],
35-
_tmpl$25 = ["<svg", "><text><![CDATA[\u2192]]></text></svg>"];
35+
_tmpl$25 = ["<svg", "><text><![CDATA[\u2192]]></text></svg>"],
36+
_tmpl$26 = "<span>\u2192</span>",
37+
_tmpl$27 = ["<div", '><style data-x=a\'b>.a::after{content:"\u2192"}</style></div>'],
38+
_tmpl$28 = ["<div", "><plaintext><p>\u2192</p></div>"];
3639
var _sk$ = k => k === "title" || k === "data-static";
3740
var _v$ = _$ssrHydrationKey();
3841
const trailing = _$ssr(_tmpl$, _v$);
@@ -157,3 +160,9 @@ var _v$37 = _$ssrHydrationKey();
157160
const nonAsciiEscapedScript = _$ssr(_tmpl$24, _v$37);
158161
var _v$38 = _$ssrHydrationKey();
159162
const nonAsciiCdata = _$ssr(_tmpl$25, _v$38);
163+
const nonAsciiSpreadXmpElement = _$ssrElement("xmp", spread, () => _$ssr(_tmpl$26), true);
164+
const nonAsciiSpreadScriptElement = _$ssrElement("script", spread, () => _$ssr(_tmpl$26), true);
165+
var _v$39 = _$ssrHydrationKey();
166+
const nonAsciiUnquotedQuote = _$ssr(_tmpl$27, _v$39);
167+
var _v$40 = _$ssrHydrationKey();
168+
const nonAsciiPlaintext = _$ssr(_tmpl$28, _v$40);

‎packages/compiler/__tests__/fixtures/ssr-hydratable/textInterpolation/output.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ var _tmpl$24 = ["<p", " title=\"café\">café €‘</p>"];
7373
var _tmpl$25 = "→";
7474
var _tmpl$26 = ["<script", ">h = \"<!--<script><\/script>→\";<\/script>"];
7575
var _tmpl$27 = ["<svg", "><text><![CDATA[→]]></text></svg>"];
76+
var _tmpl$28 = "<span>→</span>";
77+
var _tmpl$29 = ["<div", "><style data-x=a'b>.a::after{content:\"→\"}</style></div>"];
78+
var _tmpl$30 = ["<div", "><plaintext><p>→</p></div>"];
7679
var _sk$ = (k) => k === "title" || k === "data-static";
7780
var _v$ = _$ssrHydrationKey();
7881
const trailing = _$ssr(_tmpl$, _v$);
@@ -165,3 +168,13 @@ var _v$39 = _$ssrHydrationKey();
165168
const nonAsciiEscapedScript = _$ssr(_tmpl$26, _v$39);
166169
var _v$40 = _$ssrHydrationKey();
167170
const nonAsciiCdata = _$ssr(_tmpl$27, _v$40);
171+
const nonAsciiSpreadXmpElement = _$ssrElement("xmp", spread, () => {
172+
return _$ssr(_tmpl$28);
173+
}, true);
174+
const nonAsciiSpreadScriptElement = _$ssrElement("script", spread, () => {
175+
return _$ssr(_tmpl$28);
176+
}, true);
177+
var _v$41 = _$ssrHydrationKey();
178+
const nonAsciiUnquotedQuote = _$ssr(_tmpl$29, _v$41);
179+
var _v$42 = _$ssrHydrationKey();
180+
const nonAsciiPlaintext = _$ssr(_tmpl$30, _v$42);

‎packages/compiler/__tests__/fixtures/ssr/textInterpolation/output.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ var _tmpl$25 = "<p title=\"café\">café €‘</p>";
4141
var _tmpl$26 = "→";
4242
var _tmpl$27 = "<script>h = \"<!--<script><\/script>→\";<\/script>";
4343
var _tmpl$28 = "<svg><text><![CDATA[→]]></text></svg>";
44+
var _tmpl$29 = "<span>→</span>";
45+
var _tmpl$30 = "<div><style data-x=a'b>.a::after{content:\"→\"}</style></div>";
46+
var _tmpl$31 = "<div><plaintext><p>→</p></div>";
4447
var _sk$ = (k) => k === "title" || k === "data-static";
4548
const trailing = _$ssr(_tmpl$);
4649
const leading = _$ssr(_tmpl$2);
@@ -96,3 +99,7 @@ const latin1AndC1 = _$ssr(_tmpl$25);
9699
const nonAsciiSpreadXmp = _$ssrElement("xmp", spread, _$ssr(_tmpl$26), false);
97100
const nonAsciiEscapedScript = _$ssr(_tmpl$27);
98101
const nonAsciiCdata = _$ssr(_tmpl$28);
102+
const nonAsciiSpreadXmpElement = _$ssrElement("xmp", spread, _$ssr(_tmpl$29), false);
103+
const nonAsciiSpreadScriptElement = _$ssrElement("script", spread, _$ssr(_tmpl$29), false);
104+
const nonAsciiUnquotedQuote = _$ssr(_tmpl$30);
105+
const nonAsciiPlaintext = _$ssr(_tmpl$31);

‎packages/compiler/src/shared/utils.rs‎

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -775,16 +775,24 @@ pub(crate) fn ascii_attribute_markup(markup: String) -> String {
775775
if is_one_byte(&markup) {
776776
return markup;
777777
}
778-
ascii_parts(&[markup], AsciiState::Tag { raw: None })
779-
.pop()
780-
.unwrap_or_default()
778+
ascii_parts(
779+
&[markup],
780+
AsciiState::Tag {
781+
raw: None,
782+
value: false,
783+
},
784+
)
785+
.pop()
786+
.unwrap_or_default()
781787
}
782788

783789
#[derive(Clone, Copy, PartialEq)]
784790
enum AsciiState {
785791
Text,
786792
Tag {
787793
raw: Option<&'static str>,
794+
// Only a quote right after `=` opens a quoted value.
795+
value: bool,
788796
},
789797
Quoted {
790798
quote: char,
@@ -809,6 +817,8 @@ pub(crate) fn raw_text_close(name: &str) -> Option<&'static str> {
809817
"noembed" => Some("</noembed"),
810818
"noframes" => Some("</noframes"),
811819
"xmp" => Some("</xmp"),
820+
// `<plaintext>` has no end tag.
821+
"plaintext" => Some(""),
812822
_ => None,
813823
}
814824
}
@@ -855,6 +865,7 @@ fn ascii_parts(parts: &[String], mut state: AsciiState) -> std::vec::Vec<String>
855865
.to_ascii_lowercase();
856866
state = AsciiState::Tag {
857867
raw: if closing { None } else { raw_text_close(&name) },
868+
value: false,
858869
};
859870
} else if closing {
860871
state = AsciiState::Preserve(">");
@@ -867,20 +878,24 @@ fn ascii_parts(parts: &[String], mut state: AsciiState) -> std::vec::Vec<String>
867878
out.push_str(&rest[..len]);
868879
}
869880
}
870-
AsciiState::Tag { raw } => {
871-
if ch == '"' || ch == '\'' {
881+
AsciiState::Tag { raw, value } => {
882+
if value && (ch == '"' || ch == '\'') {
872883
state = AsciiState::Quoted { quote: ch, raw };
873884
} else if ch == '>' {
874885
state = raw.map_or(AsciiState::Text, |close| AsciiState::Raw {
875886
close,
876887
escaped: false,
877888
});
889+
} else if ch == '=' || (value && ch.is_ascii_whitespace()) {
890+
state = AsciiState::Tag { raw, value: true };
891+
} else {
892+
state = AsciiState::Tag { raw, value: false };
878893
}
879894
out.push(ch);
880895
}
881896
AsciiState::Quoted { quote, raw } => {
882897
if ch == quote {
883-
state = AsciiState::Tag { raw };
898+
state = AsciiState::Tag { raw, value: false };
884899
}
885900
push_ascii(&mut out, ch);
886901
}
@@ -898,11 +913,15 @@ fn ascii_parts(parts: &[String], mut state: AsciiState) -> std::vec::Vec<String>
898913
escaped: true,
899914
};
900915
} else if !escaped
916+
&& !close.is_empty()
901917
&& rest.len() >= close.len()
902918
&& rest.as_bytes()[..close.len()].eq_ignore_ascii_case(close.as_bytes())
903919
&& ends_tag_name(rest[close.len()..].chars().next())
904920
{
905-
state = AsciiState::Tag { raw: None };
921+
state = AsciiState::Tag {
922+
raw: None,
923+
value: false,
924+
};
906925
}
907926
out.push(ch);
908927
}
@@ -1025,6 +1044,22 @@ mod ascii_template_tests {
10251044
);
10261045
}
10271046

1047+
#[test]
1048+
fn quote_opens_a_value_only_after_equals() {
1049+
assert_eq!(
1050+
run(&["<style data-x=a'b>.a::after{content:\"→\"}</style><p title = '→'>→"]),
1051+
["<style data-x=a'b>.a::after{content:\"→\"}</style><p title = '&#8594;'>&#8594;"]
1052+
);
1053+
}
1054+
1055+
#[test]
1056+
fn plaintext_stays_raw_to_the_end() {
1057+
assert_eq!(
1058+
run(&["<p>→</p><plaintext><p>→</p></plaintext>→"]),
1059+
["<p>&#8594;</p><plaintext><p>→</p></plaintext>→"]
1060+
);
1061+
}
1062+
10281063
#[test]
10291064
fn astral_characters_use_one_reference() {
10301065
assert_eq!(run(&["<p>😀</p>"]), ["<p>&#128512;</p>"]);

‎packages/compiler/src/ssr/transform.rs‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ pub(crate) struct AstSsrTransform<'a, 'source> {
139139
/// Native elements marked pass-through by `escapeExpression` (Babel's
140140
/// `wontEscape`), identified by span.
141141
wont_escape_spans: std::vec::Vec<Span>,
142+
/// Native children of a spread raw-text element, whose templates keep their characters.
143+
raw_text_spans: std::vec::Vec<Span>,
142144
/// Span of the JSX root currently being lowered via the visitor entry.
143145
/// Babel keeps a raw `this` in the tag callee of the root element of each
144146
/// `transformJSX` call; only descendants use the `_self$` capture.
@@ -298,6 +300,7 @@ impl<'a, 'source> AstSsrTransform<'a, 'source> {
298300
component_child_depth: 0,
299301
var_scope_stack: std::vec::Vec::new(),
300302
wont_escape_spans: std::vec::Vec::new(),
303+
raw_text_spans: std::vec::Vec::new(),
301304
jsx_root_span: None,
302305
deferring: false,
303306
iife_callee_addrs: std::vec::Vec::new(),
@@ -922,7 +925,11 @@ impl<'a, 'source> AstSsrTransform<'a, 'source> {
922925
)));
923926
}
924927
}
925-
let template_local = self.register_template(&template.parts);
928+
let template_local = if self.raw_text_spans.contains(&element.span) {
929+
self.register_raw_template(&template.parts)
930+
} else {
931+
self.register_template(&template.parts)
932+
};
926933
// A pass-through element (already inside `_$escape`) with a static
927934
// template needs no `ssr()` wrapper — the raw HTML template reference
928935
// suffices (Babel returns the `_tmpl$` id directly).
@@ -1929,6 +1936,9 @@ impl<'a, 'source> AstSsrTransform<'a, 'source> {
19291936
// Nested natives render their own template/ssrElement —
19301937
// no escaping or markers (Babel: `child.exprs` is empty
19311938
// for templates, `spreadElement` skips both).
1939+
if crate::shared::utils::raw_text_close(tag_name).is_some() {
1940+
self.raw_text_spans.push(child_element.span);
1941+
}
19321942
nodes.push(self.lower_element_impl(child_element, false)?);
19331943
}
19341944
JSXChild::ExpressionContainer(container) => {

0 commit comments

Comments
 (0)