Skip to content

Commit 4f2d21e

Browse files
authored
fix: strip invalid attributes when paste html (#5304)
We never validated invalid attributes which cannot be rendered as jsx and will break the build. Here just strip them away.
1 parent 013a2b5 commit 4f2d21e

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

apps/builder/app/shared/html.test.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,23 @@ test("generate Image component instead of img element", () => {
334334
)
335335
);
336336
});
337+
338+
test("strip unsupported attribute names", () => {
339+
expect(
340+
generateFragmentFromHtml(`
341+
<button @click="open = true">Expand</button>
342+
<button x-on:click="open = !open">
343+
Toggle
344+
</button>
345+
`)
346+
).toEqual(
347+
renderTemplate(
348+
<>
349+
<ws.element ws:tag="button">Expand</ws.element>
350+
<ws.element ws:tag="button" x-on:click="open = !open">
351+
Toggle
352+
</ws.element>
353+
</>
354+
)
355+
);
356+
});

apps/builder/app/shared/html.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { ariaAttributes, attributesByTag } from "@webstudio-is/html-data";
1818
import { camelCaseProperty, parseCss } from "@webstudio-is/css-data";
1919
import { richTextContentTags } from "./content-model";
2020
import { setIsSubsetOf } from "./shim";
21+
import { isAttributeNameSafe } from "@webstudio-is/react-sdk";
2122

2223
type ElementNode = DefaultTreeAdapterMap["element"];
2324

@@ -169,6 +170,10 @@ export const generateFragmentFromHtml = (
169170
}
170171
instances.set(instance.id, instance);
171172
for (const attr of node.attrs) {
173+
// skip attributes which cannot be rendered in jsx
174+
if (!isAttributeNameSafe(attr.name)) {
175+
continue;
176+
}
172177
const id = `${instance.id}:${attr.name}`;
173178
const instanceId = instance.id;
174179
const name = attr.name;

0 commit comments

Comments
 (0)