Skip to content

Commit cba568f

Browse files
matthewpematipico
andauthored
Fix multiline attribute values on components (#1135)
* Fix multiline attribute values on components Escape newlines in quoted attribute values when printing component props to avoid 'Unterminated string literal' errors from esbuild. Fixes #1047 * Update .changeset/fix-multiline-attribute.md Co-authored-by: Emanuele Stoppa <[email protected]> * Remove unrelated whitespace changes in printer_test.go --------- Co-authored-by: Emanuele Stoppa <[email protected]>
1 parent 6be1c52 commit cba568f

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@astrojs/compiler": patch
3+
---
4+
5+
Fixes the "Unterminated string literal" error when using multiline attribute values on components.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
[TestPrinter/multiline_class_attribute_on_component - 1]
3+
## Input
4+
5+
```
6+
<Component class="some-class
7+
another-class
8+
third-class">content</Component>
9+
```
10+
11+
## Output
12+
13+
```js
14+
import {
15+
Fragment,
16+
render as $$render,
17+
createAstro as $$createAstro,
18+
createComponent as $$createComponent,
19+
renderComponent as $$renderComponent,
20+
renderHead as $$renderHead,
21+
maybeRenderHead as $$maybeRenderHead,
22+
unescapeHTML as $$unescapeHTML,
23+
renderSlot as $$renderSlot,
24+
mergeSlots as $$mergeSlots,
25+
addAttribute as $$addAttribute,
26+
spreadAttributes as $$spreadAttributes,
27+
defineStyleVars as $$defineStyleVars,
28+
defineScriptVars as $$defineScriptVars,
29+
renderTransition as $$renderTransition,
30+
createTransitionScope as $$createTransitionScope,
31+
renderScript as $$renderScript,
32+
createMetadata as $$createMetadata
33+
} from "http://localhost:3000/";
34+
35+
export const $$metadata = $$createMetadata(import.meta.url, { modules: [], hydratedComponents: [], clientOnlyComponents: [], hydrationDirectives: new Set([]), hoisted: [] });
36+
37+
const $$Component = $$createComponent(($$result, $$props, $$slots) => {
38+
39+
return $$render`${$$renderComponent($$result,'Component',Component,{"class":"some-class\n another-class\n third-class"},{"default": () => $$render`content`,})}`;
40+
}, undefined, undefined);
41+
export default $$Component;
42+
```
43+
---

internal/printer/printer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ func (p *printer) printAttributesToObject(n *astro.Node) {
412412
p.printf(`"%s"`, a.Key)
413413
p.print(":")
414414
p.addSourceMapping(a.ValLoc)
415-
p.print(`"` + escapeDoubleQuote(a.Val) + `"`)
415+
p.print(`"` + escapeDoubleQuote(escapeNewlines(a.Val)) + `"`)
416416
case astro.EmptyAttribute:
417417
p.addSourceMapping(a.KeyLoc)
418418
p.printf(`"%s"`, a.Key)

internal/printer/printer_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,6 +2083,10 @@ import Analytics from '../components/Analytics.astro';
20832083
<title>{title}</title>
20842084
<meta name="description" content="a description" />`,
20852085
},
2086+
{
2087+
name: "multiline class attribute on component",
2088+
source: "<Component class=\"some-class\n another-class\n third-class\">content</Component>",
2089+
},
20862090
}
20872091
for _, tt := range tests {
20882092
if tt.only {

internal/printer/utils.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ func escapeDoubleQuote(str string) string {
9494
return strings.Replace(str, `"`, "\\\"", -1)
9595
}
9696

97+
func escapeNewlines(str string) string {
98+
str = strings.Replace(str, "\n", `\n`, -1)
99+
str = strings.Replace(str, "\r", `\r`, -1)
100+
return str
101+
}
102+
97103
func encodeDoubleQuote(str string) string {
98104
return strings.Replace(str, `"`, "&quot;", -1)
99105
}

0 commit comments

Comments
 (0)