Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

fix: Correct single-quoted attribute handling in Custom Code (#3539)

Ivan Starkov60e7c064a7da
diff --git a/fixtures/webstudio-custom-template/app/__generated__/_index.tsx b/fixtures/webstudio-custom-template/app/__generated__/_index.tsx
index d06e071530..b0082b1554 100644
--- a/fixtures/webstudio-custom-template/app/__generated__/_index.tsx
+++ b/fixtures/webstudio-custom-template/app/__generated__/_index.tsx
@@ -92,9 +92,9 @@ const Style = ({ children, ...props }: Record<string, string | boolean>) => {
export const CustomCode = () => {
return (
<>
- <Script>{`console.log('HELLO')`}</Script>
- {`\n`}
- <meta property="saas:test" content="test"></meta>
+ <Script>{"console.log('HELLO')"}</Script>
+ {"\n"}
+ <meta property={"saas:test"} content={"test"}></meta>
</>
);
};
diff --git a/fixtures/webstudio-remix-vercel/app/__generated__/_index.tsx b/fixtures/webstudio-remix-vercel/app/__generated__/_index.tsx
index 4039b8330b..45c35c42b1 100644
--- a/fixtures/webstudio-remix-vercel/app/__generated__/_index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/__generated__/_index.tsx
@@ -65,8 +65,8 @@ const Style = ({ children, ...props }: Record<string, string | boolean>) => {
export const CustomCode = () => {
return (
<>
- <Script>{`console.log('KittyGuardedZone')`}</Script>
- {`\n`}
+ <Script>{"console.log('KittyGuardedZone')"}</Script>
+ {"\n"}
</>
);
};
diff --git a/packages/cli/src/html-to-jsx.test.ts b/packages/cli/src/html-to-jsx.test.ts
index 549f6c7da9..12c9154ad0 100644
--- a/packages/cli/src/html-to-jsx.test.ts
+++ b/packages/cli/src/html-to-jsx.test.ts
@@ -174,6 +174,16 @@ test("Support styles", async () => {
expect(await formatHtml(htmlCode)).toBe(await formatHtml(result));
});
+test("Supports symbols in attributes", async () => {
+ const htmlCode = `
+ <script data-cf-beacon='{"token": "124"}'>
+ </script>
+`;
+ const result = await convertHtmlToJsxAndRenderToHtml(htmlCode);
+
+ expect(await formatHtml(htmlCode)).toBe(await formatHtml(result));
+});
+
test("Real User Script Works", async () => {
const htmlCode = `
<script type="text/javascript">
diff --git a/packages/cli/src/html-to-jsx.ts b/packages/cli/src/html-to-jsx.ts
index 89e7151c09..7cebb5e359 100644
--- a/packages/cli/src/html-to-jsx.ts
+++ b/packages/cli/src/html-to-jsx.ts
@@ -96,6 +96,8 @@ const convertStyleString = (style: string) => {
return JSON.stringify(res);
};
+const escape = (value: string) => JSON.stringify(value);
+
const toAttrString = (name: string, value: string) => {
const attName = name.toLowerCase();
const jsxName = attName === "class" ? "className" : attName;
@@ -108,7 +110,7 @@ const toAttrString = (name: string, value: string) => {
return `${jsxName}={${convertStyleString(value)}}`;
}
- return `${jsxName}="${value}"`;
+ return `${jsxName}={${escape(value)}}`;
};
const attributesToString = (attributes: [string, string][]) =>
@@ -129,14 +131,6 @@ const convertTagName = (tagName: string) => {
return tag;
};
-const escapeValue = (value: string) =>
- value
- .replace(/\\/g, "\\\\")
- .replace(/`/g, "\\`")
- .replace(/\$/g, "\\$")
- .replace(/\r/g, "\\r")
- .replace(/\n/g, "\\n");
-
export const htmlToJsx = (html: string) => {
const parsedHtml = parseFragment(html, { scriptingEnabled: false });
@@ -145,9 +139,9 @@ export const htmlToJsx = (html: string) => {
for (const walkNode of walkChildNodes(parsedHtml)) {
switch (walkNode.type) {
case "text": {
- const escapedValue = escapeValue(walkNode.value);
+ const escapedValue = escape(walkNode.value);
- result += escapedValue ? "{`" + escapedValue + "`}" : "";
+ result += escapedValue ? "{" + escapedValue + "}" : "";
break;
}