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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
experimental: support paste html (#5185)
Bogdan Chadkin59b6471c4f07
diff --git a/apps/builder/app/builder/features/ai/apply-operations.ts b/apps/builder/app/builder/features/ai/apply-operations.tsindex 269b709d32..7bcf7b8f57 100644--- a/apps/builder/app/builder/features/ai/apply-operations.ts+++ b/apps/builder/app/builder/features/ai/apply-operations.ts@@ -10,7 +10,6 @@ import { serverSyncStore } from "~/shared/sync";import { isBaseBreakpoint } from "~/shared/breakpoints";import {deleteInstanceMutable,- findClosestInsertable,insertWebstudioFragmentAt,updateWebstudioData,type Insertable,@@ -76,11 +75,10 @@ const insertTemplateByOp = (const instanceSelector = computeSelectorForInstanceId(operation.addTo);if (instanceSelector) {- let insertable: Insertable = {+ const insertable: Insertable = {parentSelector: instanceSelector,position: operation.addAtIndex + 1,};- insertable = findClosestInsertable(fragment, insertable) ?? insertable;insertWebstudioFragmentAt(fragment, insertable);return rootInstanceIds;}diff --git a/apps/builder/app/builder/features/command-panel/command-panel.tsx b/apps/builder/app/builder/features/command-panel/command-panel.tsxindex 394d7efb44..9120acfb1d 100644--- a/apps/builder/app/builder/features/command-panel/command-panel.tsx+++ b/apps/builder/app/builder/features/command-panel/command-panel.tsx@@ -38,7 +38,6 @@ import {$selectedBreakpointId,} from "~/shared/nano-states";import {- findClosestInsertable,getComponentTemplateData,getInstanceLabel,insertWebstudioFragmentAt,@@ -207,10 +206,7 @@ const ComponentOptionsGroup = ({ options }: { options: ComponentOption[] }) => {closeCommandPanel();const fragment = getComponentTemplateData(component);if (fragment) {- const insertable = findClosestInsertable(fragment);- if (insertable) {- insertWebstudioFragmentAt(fragment, insertable);- }+ insertWebstudioFragmentAt(fragment);}}}>diff --git a/apps/builder/app/builder/features/components/components.tsx b/apps/builder/app/builder/features/components/components.tsxindex 066d8e13c9..0dd04bd9c8 100644--- a/apps/builder/app/builder/features/components/components.tsx+++ b/apps/builder/app/builder/features/components/components.tsx@@ -37,7 +37,6 @@ import {$registeredTemplates,} from "~/shared/nano-states";import {- findClosestInsertable,getComponentTemplateData,getInstanceLabel,insertWebstudioFragmentAt,@@ -218,10 +217,7 @@ export const ComponentsPanel = ({const handleInsert = (component: string) => {const fragment = getComponentTemplateData(component);if (fragment) {- const insertable = findClosestInsertable(fragment);- if (insertable) {- insertWebstudioFragmentAt(fragment, insertable);- }+ insertWebstudioFragmentAt(fragment);}onClose();};diff --git a/apps/builder/app/builder/features/workspace/canvas-tools/outline/block-utils.ts b/apps/builder/app/builder/features/workspace/canvas-tools/outline/block-utils.tsindex 6c2a93e096..12cdf3493f 100644--- a/apps/builder/app/builder/features/workspace/canvas-tools/outline/block-utils.ts+++ b/apps/builder/app/builder/features/workspace/canvas-tools/outline/block-utils.ts@@ -13,6 +13,7 @@ import {} from "~/shared/instance-utils";import {$instances,+ $project,$registeredComponentMetas,$textEditingInstanceSelector,findBlockChildSelector,@@ -69,7 +70,11 @@ const getInsertionIndex = (};export const insertListItemAt = (listItemSelector: InstanceSelector) => {+ const project = $project.get();const instances = $instances.get();+ if (project === undefined) {+ return;+ }const parentSelector = listItemSelector.slice(1);@@ -111,6 +116,7 @@ export const insertListItemAt = (listItemSelector: InstanceSelector) => {...data,startingInstanceId: target.parentSelector[0],}),+ projectId: project.id,});const newRootInstanceId = newInstanceIds.get(fragment.instances[0].id);if (newRootInstanceId === undefined) {@@ -141,7 +147,11 @@ export const insertTemplateAt = (anchor: InstanceSelector,insertBefore: boolean) => {+ const project = $project.get();const instances = $instances.get();+ if (project === undefined) {+ return;+ }const fragment = extractWebstudioFragment(getWebstudioData(),@@ -173,6 +183,7 @@ export const insertTemplateAt = (...data,startingInstanceId: target.parentSelector[0],}),+ projectId: project.id,});const newRootInstanceId = newInstanceIds.get(fragment.instances[0].id);if (newRootInstanceId === undefined) {diff --git a/apps/builder/app/builder/shared/commands.ts b/apps/builder/app/builder/shared/commands.tsindex ccf3b04896..119f66dab5 100644--- a/apps/builder/app/builder/shared/commands.ts+++ b/apps/builder/app/builder/shared/commands.ts@@ -16,6 +16,7 @@ import {$isContentMode,$registeredComponentMetas,findBlockSelector,+ $project,} from "~/shared/nano-states";import {$breakpointsMenuView,@@ -439,6 +440,10 @@ export const { emitCommand, subscribeCommands } = createCommandsEmitter({name: "duplicateInstance",defaultHotkeys: ["meta+d", "ctrl+d"],handler: () => {+ const project = $project.get();+ if (project === undefined) {+ return;+ }if ($isDesignMode.get() === false) {builderApi.toast.info("Duplicating is only allowed in design mode.");return;@@ -462,6 +467,7 @@ export const { emitCommand, subscribeCommands } = createCommandsEmitter({...data,startingInstanceId: parentItem.instanceSelector[0],}),+ projectId: project.id,});const newRootInstanceId = newInstanceIds.get(selectedItem.instance.iddiff --git a/apps/builder/app/shared/content-model.ts b/apps/builder/app/shared/content-model.tsindex b3a5cac2d3..e5dddcf1c6 100644--- a/apps/builder/app/shared/content-model.ts+++ b/apps/builder/app/shared/content-model.ts@@ -398,7 +398,7 @@ export const isTreeSatisfyingContentModel = ({return isSatisfying;};-const richTextContentTags = new Set<undefined | string>([+export const richTextContentTags = new Set<undefined | string>(["sup","sub","b",diff --git a/apps/builder/app/shared/copy-paste.test.tsx b/apps/builder/app/shared/copy-paste.test.tsxindex 4c81607085..510e8ea8e0 100644--- a/apps/builder/app/shared/copy-paste.test.tsx+++ b/apps/builder/app/shared/copy-paste.test.tsx@@ -129,12 +129,14 @@ test("insert instances with slots", () => {data,fragment,availableVariables: [],+ projectId: "",});expect(data.instances.size).toEqual(4);insertWebstudioFragmentCopy({data,fragment,availableVariables: [],+ projectId: "",});expect(data.instances.size).toEqual(5);expect(Array.from(data.instances.values())).toEqual([@@ -164,6 +166,7 @@ test("insert instances with multiple roots", () => {data,fragment,availableVariables: [],+ projectId: "",});expect(data.instances.size).toEqual(5);});@@ -185,6 +188,7 @@ test("should add :root local styles", () => {data: newProject,fragment,availableVariables: [],+ projectId: "",});expect(toCss(newProject)).toEqual(stripIndent(`@@ -223,6 +227,7 @@ test("should merge :root local styles", () => {data: newProject,fragment,availableVariables: [],+ projectId: "",});expect(toCss(newProject)).toEqual(stripIndent(`@@ -252,6 +257,7 @@ test("should copy local styles of duplicated instance", () => {data: project,fragment,availableVariables: [],+ projectId: "",});const newInstanceId = Array.from(project.instances.keys()).at(-1);expect(toCss(project)).toEqual(@@ -317,6 +323,7 @@ describe("props", () => {data,fragment,availableVariables: [],+ projectId: "",});expect(Array.from(data.props.values())).toEqual([expect.objectContaining({@@ -345,6 +352,7 @@ describe("props", () => {data,fragment,availableVariables: [],+ projectId: "",});expect(Array.from(data.props.values())).toEqual([expect.objectContaining({@@ -437,6 +445,7 @@ describe("variables", () => {data,fragment,availableVariables: [],+ projectId: "",});const [newDataSourceId] = data.dataSources.keys();expect(Array.from(data.dataSources.values())).toEqual([@@ -488,6 +497,7 @@ describe("variables", () => {data,fragment,availableVariables: [],+ projectId: "",});expect(Array.from(data.dataSources.values())).toEqual([expect.objectContaining({@@ -540,6 +550,7 @@ describe("variables", () => {...data,startingInstanceId: "bodyId",}),+ projectId: "",});const newInstanceId = Array.from(data.instances.keys()).at(-1) ?? "";expect(newInstanceId).not.toEqual("boxId");@@ -636,6 +647,7 @@ describe("resources", () => {...data,startingInstanceId: "bodyId",}),+ projectId: "",});const newInstanceId = Array.from(data.instances.keys()).at(-1);expect(newInstanceId).not.toEqual("boxId");@@ -724,6 +736,7 @@ describe("resources", () => {...data,startingInstanceId: "bodyId",}),+ projectId: "",});const newInstanceId = Array.from(data.instances.keys()).at(-1);expect(newInstanceId).not.toEqual("boxId");@@ -767,6 +780,7 @@ describe("resources", () => {data,fragment,availableVariables: [],+ projectId: "",});const [newPropResourceId, newVariableResourceId] = data.resources.keys();const [newBoxVariableId] = data.dataSources.keys();@@ -833,6 +847,7 @@ describe("resources", () => {data,fragment,availableVariables: [],+ projectId: "",});expect(Array.from(data.dataSources.values())).toEqual([expect.objectContaining({diff --git a/apps/builder/app/shared/copy-paste/init-copy-paste.ts b/apps/builder/app/shared/copy-paste/init-copy-paste.tsindex 16eaabf1af..af4dcb0e9e 100644--- a/apps/builder/app/shared/copy-paste/init-copy-paste.ts+++ b/apps/builder/app/shared/copy-paste/init-copy-paste.ts@@ -4,6 +4,7 @@ import {$textEditingInstanceSelector,} from "../nano-states";import { instanceText, instanceJson } from "./plugin-instance";+import { html } from "./plugin-html";import * as markdown from "./plugin-markdown";import * as webflow from "./plugin-webflow/plugin-webflow";import { builderApi } from "../builder-api";@@ -132,7 +133,7 @@ const initPlugins = ({export const initCopyPaste = ({ signal }: { signal: AbortSignal }) => {initPlugins({- plugins: [instanceJson, instanceText, markdown, webflow],+ plugins: [instanceJson, instanceText, html, markdown, webflow],signal,});};diff --git a/apps/builder/app/shared/copy-paste/plugin-html.test.tsx b/apps/builder/app/shared/copy-paste/plugin-html.test.tsxnew file mode 100644index 0000000000..531fd92ce5--- /dev/null+++ b/apps/builder/app/shared/copy-paste/plugin-html.test.tsx@@ -0,0 +1,65 @@+import { expect, test } from "vitest";+import { setEnv } from "@webstudio-is/feature-flags";+import { renderData, ws } from "@webstudio-is/template";+import { createDefaultPages } from "@webstudio-is/project-build";+import type { Project } from "@webstudio-is/project";+import { registerContainers } from "../sync";+import { $instances, $pages, $project } from "../nano-states";+import { $awareness } from "../awareness";+import { html } from "./plugin-html";++setEnv("*");+registerContainers();++test("paste html fragment", () => {+ const data = renderData(+ <ws.element ws:tag="body" ws:id="bodyId">+ <ws.element ws:tag="div" ws:id="divId"></ws.element>+ </ws.element>+ );+ $project.set({ id: "" } as Project);+ $instances.set(data.instances);+ $pages.set(+ createDefaultPages({ rootInstanceId: "bodyId", homePageId: "pageId" })+ );+ $awareness.set({ pageId: "pageId", instanceSelector: ["divId", "bodyId"] });+ expect(+ html.onPaste?.(`+ <section>+ <h1>It works</h1>+ </section>+ `)+ ).toEqual(true);+ const [_bodyId, _divId, sectionId, headingId] = $instances.get().keys();+ expect(sectionId).toBeTruthy();+ expect(headingId).toBeTruthy();+ expect($instances.get()).toEqual(+ renderData(+ <ws.element ws:tag="body" ws:id="bodyId">+ <ws.element ws:tag="div" ws:id="divId">+ <ws.element ws:tag="section" ws:id={sectionId}>+ <ws.element ws:tag="h1" ws:id={headingId}>+ It works+ </ws.element>+ </ws.element>+ </ws.element>+ </ws.element>+ ).instances+ );+});++test("ignore html without any tags", () => {+ const data = renderData(+ <ws.element ws:tag="body" ws:id="bodyId">+ <ws.element ws:tag="div" ws:id="divId"></ws.element>+ </ws.element>+ );+ $project.set({ id: "" } as Project);+ $instances.set(data.instances);+ $pages.set(+ createDefaultPages({ rootInstanceId: "bodyId", homePageId: "pageId" })+ );+ $awareness.set({ pageId: "pageId", instanceSelector: ["divId", "bodyId"] });+ expect(html.onPaste?.(`It works`)).toEqual(false);+ expect($instances.get()).toEqual(data.instances);+});diff --git a/apps/builder/app/shared/copy-paste/plugin-html.ts b/apps/builder/app/shared/copy-paste/plugin-html.tsnew file mode 100644index 0000000000..7e1d45b59b--- /dev/null+++ b/apps/builder/app/shared/copy-paste/plugin-html.ts@@ -0,0 +1,15 @@+import { isFeatureEnabled } from "@webstudio-is/feature-flags";+import { generateFragmentFromHtml } from "../html";+import { insertWebstudioFragmentAt } from "../instance-utils";+import type { Plugin } from "./init-copy-paste";++export const html: Plugin = {+ mimeType: "text/plain",+ onPaste: (html: string) => {+ if (!isFeatureEnabled("element")) {+ return false;+ }+ const fragment = generateFragmentFromHtml(html);+ return insertWebstudioFragmentAt(fragment);+ },+};diff --git a/apps/builder/app/shared/copy-paste/plugin-instance.ts b/apps/builder/app/shared/copy-paste/plugin-instance.tsindex 493f1a6f9b..9006b09df7 100644--- a/apps/builder/app/shared/copy-paste/plugin-instance.ts+++ b/apps/builder/app/shared/copy-paste/plugin-instance.ts@@ -9,7 +9,11 @@ import {isComponentDetachable,portalComponent,} from "@webstudio-is/sdk";-import { $selectedInstanceSelector, $instances } from "../nano-states";+import {+ $selectedInstanceSelector,+ $instances,+ $project,+} from "../nano-states";import type { InstanceSelector } from "../tree-utils";import {deleteInstanceMutable,@@ -152,9 +156,9 @@ const findPasteTarget = (data: InstanceData): undefined | Insertable => {};const onPaste = (clipboardData: string) => {+ const project = $project.get();const fragment = parse(clipboardData);-- if (fragment === undefined) {+ if (fragment === undefined || project === undefined) {return false;}@@ -171,6 +175,7 @@ const onPaste = (clipboardData: string) => {...data,startingInstanceId: pasteTarget.parentSelector[0],}),+ projectId: project.id,});const newRootInstanceId = newInstanceIds.get(fragment.instances[0].id);if (newRootInstanceId === undefined) {diff --git a/apps/builder/app/shared/copy-paste/plugin-markdown.ts b/apps/builder/app/shared/copy-paste/plugin-markdown.tsindex ed7918f675..b647d5fa28 100644--- a/apps/builder/app/shared/copy-paste/plugin-markdown.ts+++ b/apps/builder/app/shared/copy-paste/plugin-markdown.ts@@ -7,10 +7,7 @@ import type {Instance,WebstudioFragment,} from "@webstudio-is/sdk";-import {- findClosestInsertable,- insertWebstudioFragmentAt,-} from "../instance-utils";+import { insertWebstudioFragmentAt } from "../instance-utils";import { $breakpoints } from "../nano-states";import { isBaseBreakpoint } from "../breakpoints";import { denormalizeSrcProps } from "./asset-upload";@@ -213,12 +210,7 @@ export const onPaste = async (clipboardData: string) => {return false;}fragment = await denormalizeSrcProps(fragment);- const insertable = findClosestInsertable(fragment);- if (insertable === undefined) {- return false;- }- insertWebstudioFragmentAt(fragment, insertable);- return true;+ return insertWebstudioFragmentAt(fragment);};export const __testing__ = {diff --git a/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.ts b/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.tsindex 3380c3b7af..40292b5c1c 100644--- a/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.ts+++ b/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.ts@@ -166,8 +166,9 @@ const parse = (clipboardData: string) => {};export const onPaste = async (clipboardData: string) => {+ const project = $project.get();const wfData = parse(clipboardData);- if (wfData === undefined) {+ if (wfData === undefined || project === undefined) {return false;}@@ -190,6 +191,7 @@ export const onPaste = async (clipboardData: string) => {...data,startingInstanceId: insertable.parentSelector[0],}),+ projectId: project.id,});const children = fragment.childrendiff --git a/apps/builder/app/shared/html.test.tsx b/apps/builder/app/shared/html.test.tsxnew file mode 100644index 0000000000..2831db3489--- /dev/null+++ b/apps/builder/app/shared/html.test.tsx@@ -0,0 +1,192 @@+import { expect, test } from "vitest";+import { renderTemplate, ws } from "@webstudio-is/template";+import { generateFragmentFromHtml } from "./html";++test("generate instances from html", () => {+ expect(+ generateFragmentFromHtml(`+ <main>+ <section>+ <h1>It works!</h1>+ <p>Webstudio is great.</p>+ <ul>+ <li>one</li>+ <li>two</li>+ </ul>+ </section>+ </main>+ `)+ ).toEqual(+ renderTemplate(+ <ws.element ws:tag="main">+ <ws.element ws:tag="section">+ <ws.element ws:tag="h1">It works!</ws.element>+ <ws.element ws:tag="p">Webstudio is great.</ws.element>+ <ws.element ws:tag="ul">+ <ws.element ws:tag="li">one</ws.element>+ <ws.element ws:tag="li">two</ws.element>+ </ws.element>+ </ws.element>+ </ws.element>+ )+ );+});++test("generate multiple root instances from html", () => {+ expect(+ generateFragmentFromHtml(`+ <section>+ <h1>One</h1>+ </section>+ <section>+ <h1>Two</h1>+ </section>+ `)+ ).toEqual(+ renderTemplate(+ <>+ <ws.element ws:tag="section">+ <ws.element ws:tag="h1">One</ws.element>+ </ws.element>+ <ws.element ws:tag="section">+ <ws.element ws:tag="h1">Two</ws.element>+ </ws.element>+ </>+ )+ );+});++test("handle broken html", () => {+ expect(+ generateFragmentFromHtml(`+ <section>+ <h1>One</h1>+ </section attribute="value">+ `)+ ).toEqual(+ renderTemplate(+ <>+ <ws.element ws:tag="section">+ <ws.element ws:tag="h1">One</ws.element>+ </ws.element>+ </>+ )+ );+});++test("handle non-html", () => {+ expect(generateFragmentFromHtml("")).toEqual(renderTemplate(<></>));+ expect(generateFragmentFromHtml("It works!")).toEqual(renderTemplate(<></>));+});++test("ignore custom elements", () => {+ expect(+ generateFragmentFromHtml(`+ <custom-element>+ <div></div>+ </custom-element>+ <section></section>+ `)+ ).toEqual(renderTemplate(<ws.element ws:tag="section"></ws.element>));+});++test("ignore not allowed tags", () => {+ expect(+ generateFragmentFromHtml(`+ <style>style</style>+ <script>script</script>+ <template>template</template>+ <section></section>+ `)+ ).toEqual(renderTemplate(<ws.element ws:tag="section"></ws.element>));+});++test("generate props from html attributes", () => {+ expect(+ generateFragmentFromHtml(`+ <form action="/my-action">+ <button class="my-button">My Button</button>+ </form>+ `)+ ).toEqual(+ renderTemplate(+ <ws.element ws:tag="form" action="/my-action">+ <ws.element ws:tag="button" class="my-button">+ My Button+ </ws.element>+ </ws.element>+ )+ );+});++test("generate props from number and boolean html attributes", () => {+ expect(+ generateFragmentFromHtml(`+ <button autofocus tabindex="-1">My Button</button>+ `)+ ).toEqual(+ renderTemplate(+ <ws.element ws:tag="button" autofocus={true} tabindex={-1}>+ My Button+ </ws.element>+ )+ );+});++test("generate props from number and boolean aria attributes", () => {+ expect(+ generateFragmentFromHtml(`+ <button aria-expanded aria-valuemin="100">My Button</button>+ `)+ ).toEqual(+ renderTemplate(+ <ws.element ws:tag="button" aria-expanded={true} aria-valuemin={100}>+ My Button+ </ws.element>+ )+ );+});++test("wrap text with span when spotted outside of rich text", () => {+ expect(+ generateFragmentFromHtml(`+ <div>div<article>article</article></div>+ `)+ ).toEqual(+ renderTemplate(+ <ws.element ws:tag="div">+ <ws.element ws:tag="span">div</ws.element>+ <ws.element ws:tag="article">article</ws.element>+ </ws.element>+ )+ );+ expect(+ generateFragmentFromHtml(`+ <div>div<b><img></b></div>+ `)+ ).toEqual(+ renderTemplate(+ <ws.element ws:tag="div">+ <ws.element ws:tag="span">div</ws.element>+ <ws.element ws:tag="b">+ <ws.element ws:tag="img"></ws.element>+ </ws.element>+ </ws.element>+ )+ );+});++test("do not wrap text with span when spotted near link", () => {+ expect(+ generateFragmentFromHtml(`+ <div>div<a>link</a></div>+ `)+ ).toEqual(+ renderTemplate(+ <ws.element ws:tag="div">+ div+ <ws.element ws:tag="a">link</ws.element>+ </ws.element>+ )+ );+});diff --git a/apps/builder/app/shared/html.ts b/apps/builder/app/shared/html.tsnew file mode 100644index 0000000000..bc8f290d89--- /dev/null+++ b/apps/builder/app/shared/html.ts@@ -0,0 +1,178 @@+import {+ type DefaultTreeAdapterMap,+ defaultTreeAdapter,+ parseFragment,+} from "parse5";+import {+ type WebstudioFragment,+ type Instance,+ elementComponent,+ Prop,+ tags,+} from "@webstudio-is/sdk";+import { ariaAttributes, attributesByTag } from "@webstudio-is/html-data";+import { richTextContentTags } from "./content-model";+import { setIsSubsetOf } from "./shim";++type ElementNode = DefaultTreeAdapterMap["element"];++const spaceRegex = /^\s*$/;++const getAttributeType = (+ attribute: (typeof ariaAttributes)[number]+): "string" | "boolean" | "number" => {+ if (attribute.type === "string" || attribute.type === "select") {+ return "string";+ }+ if (attribute.type === "number" || attribute.type === "boolean") {+ return attribute.type;+ }+ attribute.type satisfies never;+ throw Error("Unknown type");+};++const getAttributeTypes = () => {+ const attributeTypes = new Map<string, "string" | "number" | "boolean">();+ for (const attribute of ariaAttributes) {+ attributeTypes.set(attribute.name, getAttributeType(attribute));+ }+ for (const attribute of attributesByTag["*"] ?? []) {+ attributeTypes.set(attribute.name, getAttributeType(attribute));+ }+ for (const [tag, attributes] of Object.entries(attributesByTag)) {+ if (attributes) {+ for (const attribute of attributes) {+ attributeTypes.set(+ `${tag}:${attribute.name}`,+ getAttributeType(attribute)+ );+ }+ }+ }+ return attributeTypes;+};++const findContentTags = (element: ElementNode, tags = new Set<string>()) => {+ for (const childNode of element.childNodes) {+ if (defaultTreeAdapter.isElementNode(childNode)) {+ tags.add(childNode.tagName);+ findContentTags(childNode, tags);+ }+ }+ return tags;+};++export const generateFragmentFromHtml = (html: string): WebstudioFragment => {+ const attributeTypes = getAttributeTypes();+ const instances = new Map<Instance["id"], Instance>();+ const props: Prop[] = [];+ let lastId = -1;+ const getNewId = () => {+ lastId += 1;+ return lastId.toString();+ };+ const convertElementToInstance = (node: ElementNode) => {+ if (!tags.includes(node.tagName)) {+ return;+ }+ const instance: Instance = {+ type: "instance",+ id: getNewId(),+ component: elementComponent,+ tag: node.tagName,+ children: [],+ };+ instances.set(instance.id, instance);+ for (const attr of node.attrs) {+ const id = `${instance.id}:${attr.name}`;+ const instanceId = instance.id;+ const name = attr.name;+ // cast props to types extracted from html and aria specs+ const type =+ attributeTypes.get(`${node.tagName}:${name}`) ??+ attributeTypes.get(name) ??+ "string";+ if (type === "string") {+ props.push({ id, instanceId, name, type, value: attr.value });+ continue;+ }+ if (type === "number") {+ props.push({ id, instanceId, name, type, value: Number(attr.value) });+ continue;+ }+ if (type === "boolean") {+ props.push({ id, instanceId, name, type, value: true });+ continue;+ }+ (type) satisfies never;+ }+ const contentTags = findContentTags(node);+ const hasNonRichTextContent = !setIsSubsetOf(+ contentTags,+ richTextContentTags+ );+ for (const childNode of node.childNodes) {+ if (defaultTreeAdapter.isElementNode(childNode)) {+ const child = convertElementToInstance(childNode);+ if (child) {+ instance.children.push(child);+ }+ }+ if (defaultTreeAdapter.isTextNode(childNode)) {+ if (spaceRegex.test(childNode.value)) {+ continue;+ }+ let child: Instance["children"][number] = {+ type: "text",+ value: childNode.value,+ };+ // when element has content elements other than supported by rich text+ // wrap its text children with span, for example+ // <div>+ // text+ // <article></article>+ // </div>+ // is converted into+ // <div>+ // <span>text</span>+ // <article></article>+ // </div>+ if (hasNonRichTextContent) {+ const span: Instance = {+ type: "instance",+ id: getNewId(),+ component: elementComponent,+ tag: "span",+ children: [child],+ };+ instances.set(span.id, span);+ child = { type: "id", value: span.id };+ }+ instance.children.push(child);+ }+ }+ return { type: "id" as const, value: instance.id };+ };+ const documentFragment = parseFragment(html, { scriptingEnabled: false });+ const children: Instance["children"] = [];+ for (const childNode of documentFragment.childNodes) {+ if (defaultTreeAdapter.isElementNode(childNode)) {+ const child = convertElementToInstance(childNode);+ if (child) {+ children.push(child);+ }+ }+ }+ return {+ children,+ instances: Array.from(instances.values()),+ props,+ dataSources: [],+ resources: [],+ styleSourceSelections: [],+ styleSources: [],+ styles: [],+ breakpoints: [],+ assets: [],+ };+};diff --git a/apps/builder/app/shared/instance-utils.test.tsx b/apps/builder/app/shared/instance-utils.test.tsxindex 0e9759f41e..e04b6e97f2 100644--- a/apps/builder/app/shared/instance-utils.test.tsx+++ b/apps/builder/app/shared/instance-utils.test.tsx@@ -1050,6 +1050,7 @@ describe("insert webstudio fragment copy", () => {assets: [createImageAsset("asset1", "name", "another_project")],},availableVariables: [],+ projectId: "current_project",});expect(Array.from(data.assets.values())).toEqual([createImageAsset("asset1", "name", "current_project"),@@ -1068,6 +1069,7 @@ describe("insert webstudio fragment copy", () => {],},availableVariables: [],+ projectId: "current_project",});expect(Array.from(data.assets.values())).toEqual([// preserve any user changes@@ -1102,6 +1104,7 @@ describe("insert webstudio fragment copy", () => {],},availableVariables: [],+ projectId: "",});expect(Array.from(data.breakpoints.values())).toEqual([{ id: "existing_base", label: "base" },@@ -1141,6 +1144,7 @@ describe("insert webstudio fragment copy", () => {],},availableVariables: [],+ projectId: "",});expect(Array.from(data.styleSources.values())).toEqual([{ id: "token1", type: "token", name: "oldLabel" },@@ -1193,6 +1197,7 @@ describe("insert webstudio fragment copy", () => {],},availableVariables: [],+ projectId: "",});expect(Array.from(data.styleSourceSelections.values())).toEqual([{@@ -1258,6 +1263,7 @@ describe("insert webstudio fragment copy", () => {],},availableVariables: [],+ projectId: "",});expect(Array.from(data.styleSourceSelections.values())).toEqual([{ instanceId: "fragment", values: ["localId", "tokenId"] },diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.tsindex 42cdd1b0b8..bfc5540723 100644--- a/apps/builder/app/shared/instance-utils.ts+++ b/apps/builder/app/shared/instance-utils.ts@@ -41,11 +41,11 @@ import {$registeredComponentMetas,$dataSources,$assets,- $project,$breakpoints,$pages,$resources,$registeredTemplates,+ $project,} from "./nano-states";import {type DroppableTarget,@@ -77,6 +77,7 @@ import {findClosestNonTextualContainer,isRichTextTree,} from "./content-model";+import type { Project } from "@webstudio-is/project";/*** structuredClone can be invoked on draft and throw error@@ -259,8 +260,17 @@ export const insertInstanceChildrenMutable = (export const insertWebstudioFragmentAt = (fragment: WebstudioFragment,- insertable: Insertable-) => {+ insertable?: Insertable+): boolean => {+ // cannot insert empty fragment+ if (fragment.children.length === 0) {+ return false;+ }+ const project = $project.get();+ insertable = findClosestInsertable(fragment, insertable) ?? insertable;+ if (project === undefined || insertable === undefined) {+ return false;+ }let newInstanceSelector: undefined | InstanceSelector;updateWebstudioData((data) => {const instancePath = getInstancePath(@@ -277,6 +287,7 @@ export const insertWebstudioFragmentAt = (...data,startingInstanceId: instancePath[0].instance.id,}),+ projectId: project.id,});const children: Instance["children"] = fragment.children.map((child) => {if (child.type === "id") {@@ -314,6 +325,7 @@ export const insertWebstudioFragmentAt = (if (newInstanceSelector) {selectInstance(newInstanceSelector);}+ return true;};export const getComponentTemplateData = (@@ -349,6 +361,10 @@ export const reparentInstanceMutable = (sourceInstanceSelector: InstanceSelector,dropTarget: DroppableTarget) => {+ const project = $project.get();+ if (project === undefined) {+ return;+ }const [rootInstanceId] = sourceInstanceSelector;// delect is target is one of own descendants// prevent reparenting to avoid infinite loop@@ -420,6 +436,7 @@ export const reparentInstanceMutable = (...data,startingInstanceId: dropTarget.parentSelector[0],}),+ projectId: project.id,});const [newParentId] = dropTarget.parentSelector;const newRootInstanceId =@@ -810,10 +827,12 @@ export const insertWebstudioFragmentCopy = ({data,fragment,availableVariables,+ projectId,}: {data: Omit<WebstudioData, "pages">;fragment: WebstudioFragment;availableVariables: DataSource[];+ projectId: Project["id"];}) => {const newInstanceIds = new Map<Instance["id"], Instance["id"]>();const newDataSourceIds = new Map<DataSource["id"], DataSource["id"]>();@@ -821,10 +840,6 @@ export const insertWebstudioFragmentCopy = ({newInstanceIds,newDataSourceIds,};- const projectId = $project.get()?.id;- if (projectId === undefined) {- return newDataIds;- }const fragmentInstances: Instances = new Map();const portalContentRootIds = new Set<Instance["id"]>();diff --git a/apps/builder/app/shared/page-utils.ts b/apps/builder/app/shared/page-utils.tsindex 142f88be58..6493fa6989 100644--- a/apps/builder/app/shared/page-utils.ts+++ b/apps/builder/app/shared/page-utils.ts@@ -22,6 +22,7 @@ import {restoreExpressionVariables,unsetExpressionVariables,} from "./data-variables";+import { $project } from "./nano-states";const deduplicateName = (pages: Pages,@@ -97,8 +98,9 @@ export const insertPageCopyMutable = ({source: { data: WebstudioData; pageId: Page["id"] };target: { data: WebstudioData; folderId: Folder["id"] };}) => {+ const project = $project.get();const page = findPageByIdOrPath(source.pageId, source.data.pages);- if (page === undefined) {+ if (project === undefined || page === undefined) {return;}// copy paste project :root@@ -109,6 +111,7 @@ export const insertPageCopyMutable = ({...target.data,startingInstanceId: ROOT_INSTANCE_ID,}),+ projectId: project.id,});const unsetVariables = new Set<DataSource["id"]>();const unsetNameById = new Map<DataSource["id"], DataSource["name"]>();@@ -132,6 +135,7 @@ export const insertPageCopyMutable = ({unsetVariables,}),availableVariables,+ projectId: project.id,});// unwrap page draftconst newPage = structuredClone(unwrap(page));diff --git a/apps/builder/package.json b/apps/builder/package.jsonindex 44a94f9d64..c4ab53d2c1 100644--- a/apps/builder/package.json+++ b/apps/builder/package.json@@ -100,6 +100,7 @@"nanoevents": "^9.1.0","nanoid": "^5.1.5","nanostores": "^0.11.3",+ "parse5": "7.3.0","picocolors": "^1.1.1","pretty-bytes": "^6.1.1","react": "18.3.0-canary-14898b6a9-20240318",diff --git a/packages/cli/package.json b/packages/cli/package.jsonindex 3d637cccf0..1a02b4ed05 100644--- a/packages/cli/package.json+++ b/packages/cli/package.json@@ -40,7 +40,7 @@"env-paths": "^3.0.0","nanoid": "^5.1.5","p-limit": "^6.2.0",- "parse5": "7.2.1",+ "parse5": "7.3.0","picocolors": "^1.1.1","reserved-identifiers": "^1.0.0","tinyexec": "^0.3.2",diff --git a/packages/html-data/bin/elements.ts b/packages/html-data/bin/elements.tsindex 1784adca4c..525380c702 100644--- a/packages/html-data/bin/elements.ts+++ b/packages/html-data/bin/elements.ts@@ -74,10 +74,6 @@ for (const [tag, element] of Object.entries(elementsByTag)) {if (element.categories.includes("metadata")) {continue;}- // @todo remove when element insert can adapt to parent- if (element.categories.includes("none")) {- continue;- }tags.push(tag);}const tagsContent = `export const tags: string[] = ${JSON.stringify(tags, null, 2)};diff --git a/packages/html-data/package.json b/packages/html-data/package.jsonindex 54bfef5234..aa2d6c957c 100644--- a/packages/html-data/package.json+++ b/packages/html-data/package.json@@ -17,7 +17,7 @@"@webstudio-is/sdk": "workspace:*","@webstudio-is/tsconfig": "workspace:*","aria-query": "^5.3.2",- "parse5": "7.2.1"+ "parse5": "7.3.0"},"exports": {"webstudio": "./src/index.ts"diff --git a/packages/icons/icons/html-element.svg b/packages/icons/icons/html-element.svgdeleted file mode 100644index 9ffecde745..0000000000--- a/packages/icons/icons/html-element.svg+++ /dev/null@@ -1,20 +0,0 @@-<svg- width="16"- height="16"- viewBox="0 0 16 16"- fill="none"- xmlns="http://www.w3.org/2000/svg"->- <path- d="M13.5556 2H3.44444C2.6467 2 2 2.6467 2 3.44444V13.5556C2 14.3533 2.6467 15 3.44444 15H13.5556C14.3533 15 15 14.3533 15 13.5556V3.44444C15 2.6467 14.3533 2 13.5556 2Z"- stroke="currentColor"- stroke-linecap="round"- stroke-linejoin="round"- />- <path- d="M10.5 10L12 8.5L10.5 7M6 7L4.5 8.5L6 10M9.1875 5.5L7.3125 11.5"- stroke="currentColor"- stroke-linecap="round"- stroke-linejoin="round"- />-</svg>diff --git a/packages/icons/src/__generated__/components.tsx b/packages/icons/src/__generated__/components.tsxindex 8513918605..b66ae41f52 100644--- a/packages/icons/src/__generated__/components.tsx+++ b/packages/icons/src/__generated__/components.tsx@@ -2738,36 +2738,6 @@ export const HomeIcon: IconComponent = forwardRef();HomeIcon.displayName = "HomeIcon";-export const HtmlElementIcon: IconComponent = forwardRef(- ({ fill = "none", size = 16, ...props }, forwardedRef) => {- return (- <svg- xmlns="http://www.w3.org/2000/svg"- viewBox="0 0 16 16"- width={size}- height={size}- fill={fill}- {...props}- ref={forwardedRef}- >- <path- stroke="currentColor"- strokeLinecap="round"- strokeLinejoin="round"- d="M13.556 2H3.444C2.647 2 2 2.647 2 3.444v10.112C2 14.353 2.647 15 3.444 15h10.112c.797 0 1.444-.647 1.444-1.444V3.444C15 2.647 14.353 2 13.556 2Z"- />- <path- stroke="currentColor"- strokeLinecap="round"- strokeLinejoin="round"- d="M10.5 10 12 8.5 10.5 7M6 7 4.5 8.5 6 10m3.188-4.5-1.876 6"- />- </svg>- );- }-);-HtmlElementIcon.displayName = "HtmlElementIcon";-export const ImageIcon: IconComponent = forwardRef(({ fill = "none", size = 16, ...props }, forwardedRef) => {return (diff --git a/packages/icons/src/__generated__/svg.ts b/packages/icons/src/__generated__/svg.tsindex 506b399bb5..f2a9393528 100644--- a/packages/icons/src/__generated__/svg.ts+++ b/packages/icons/src/__generated__/svg.ts@@ -204,8 +204,6 @@ export const HelpIcon = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" vieexport const HomeIcon = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 16 16" width="100%" height="100%" style="display: block;"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" d="M10 14V8.667A.667.667 0 0 0 9.333 8H6.667A.667.667 0 0 0 6 8.667V14"/><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" d="M2 6.667a1.333 1.333 0 0 1 .473-1.019l4.666-4a1.333 1.333 0 0 1 1.722 0l4.666 4A1.332 1.332 0 0 1 14 6.667v6A1.334 1.334 0 0 1 12.667 14H3.333A1.334 1.334 0 0 1 2 12.667v-6Z"/></svg>`;-export const HtmlElementIcon = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 16 16" width="100%" height="100%" style="display: block;"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" d="M13.556 2H3.444C2.647 2 2 2.647 2 3.444v10.112C2 14.353 2.647 15 3.444 15h10.112c.797 0 1.444-.647 1.444-1.444V3.444C15 2.647 14.353 2 13.556 2Z"/><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" d="M10.5 10 12 8.5 10.5 7M6 7 4.5 8.5 6 10m3.188-4.5-1.876 6"/></svg>`;-export const ImageIcon = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 16 16" width="100%" height="100%" style="display: block;"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" d="M12.667 2H3.333C2.597 2 2 2.597 2 3.333v9.334C2 13.403 2.597 14 3.333 14h9.334c.736 0 1.333-.597 1.333-1.333V3.333C14 2.597 13.403 2 12.667 2Z"/><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" d="M6 7.333a1.333 1.333 0 1 0 0-2.666 1.333 1.333 0 0 0 0 2.666ZM14 10l-2.057-2.057a1.333 1.333 0 0 0-1.886 0L4 14"/></svg>`;export const InfoCircleIcon = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 16 16" width="100%" height="100%" style="display: block;"><circle cx="8" cy="8" r="6.5" stroke="currentColor"/><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" d="M8 11.516V7.853"/><rect width="1.4" height="1.4" x="7.3" y="4.484" fill="currentColor" rx=".7"/></svg>`;diff --git a/packages/sdk/src/__generated__/tags.ts b/packages/sdk/src/__generated__/tags.tsindex 9b7e3e4e78..eee4408144 100644--- a/packages/sdk/src/__generated__/tags.ts+++ b/packages/sdk/src/__generated__/tags.ts@@ -10,22 +10,29 @@ export const tags: string[] = ["bdi","bdo","blockquote",+ "body","br","button","canvas",+ "caption","cite","code",+ "col",+ "colgroup","data","datalist",+ "dd","del","details","dfn","dialog","div","dl",+ "dt","em","embed","fieldset",+ "figcaption","figure","footer","form",@@ -35,9 +42,11 @@ export const tags: string[] = ["h4","h5","h6",+ "head","header","hgroup","hr",+ "html","i","iframe","img",@@ -45,6 +54,8 @@ export const tags: string[] = ["ins","kbd","label",+ "legend",+ "li","main","map","mark",@@ -53,12 +64,16 @@ export const tags: string[] = ["nav","object","ol",+ "optgroup",+ "option","output","p","picture","pre","progress","q",+ "rp",+ "rt","ruby","s","samp",@@ -67,14 +82,22 @@ export const tags: string[] = ["select","slot","small",+ "source","span","strong","sub",+ "summary","sup","table",+ "tbody",+ "td","textarea",+ "tfoot","th",+ "thead","time",+ "tr",+ "track","u","ul","var",diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.tsindex 418b60d841..6ea6e89aab 100644--- a/packages/sdk/src/index.ts+++ b/packages/sdk/src/index.ts@@ -22,6 +22,7 @@ export * from "./resources-generator";export * from "./page-meta-generator";export * from "./url-pattern";export * from "./css";+export * from "./__generated__/tags";export type {AnimationAction,diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 1f77675f8a..69996902fa 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -364,6 +364,9 @@ importers:nanostores:specifier: ^0.11.3version: 0.11.3+ parse5:+ specifier: 7.3.0+ version: 7.3.0picocolors:specifier: ^1.1.1version: 1.1.1@@ -1136,8 +1139,8 @@ importers:specifier: ^6.2.0version: 6.2.0parse5:- specifier: 7.2.1- version: 7.2.1+ specifier: 7.3.0+ version: 7.3.0picocolors:specifier: ^1.1.1version: 1.1.1@@ -1623,8 +1626,8 @@ importers:specifier: ^5.3.2version: 5.3.2parse5:- specifier: 7.2.1- version: 7.2.1+ specifier: 7.3.0+ version: 7.3.0packages/http-client:dependencies:@@ -6476,6 +6479,10 @@ packages:resolution: {integrity: sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==}engines: {node: '>=0.12'}+ resolution: {integrity: sha512-aKstq2TDOndCn4diEyp9Uq/Flu2i1GlLkc6XIDQSDMuaFE3OPW5OphLCyQ5SpSJZTb4reN+kTcYru5yIfXoRPw==}+ engines: {node: '>=0.12'}+resolution: {integrity: sha512-dtJUTepzMW3Lm/NPxRf3wP4642UWhjL2sQxc+ym2YMj1m/H2zDNQOlezafzkHwn6sMstjHTwG6iQQsctDW/b1A==}engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}@@ -8092,8 +8099,8 @@ packages:resolution: {integrity: sha512-kBeTUtcj+SkyfaW4+KBe0HtsloBJ/mKTPoxpVdA57GZiPerREsUWJOhVj9anXweFiJkm5y8FG1sxFZkZ0SN6wg==}- resolution: {integrity: sha512-BuBYQYlv1ckiPdQi/ohiivi9Sagc9JG+Ozs0r7b/0iK3sKmrb0b9FdWdBbOdx6hBCM/F9Ir82ofnBhtZOjCRPQ==}+ resolution: {integrity: sha512-IInvU7fabl34qmi9gY8XOVxhYyMyuH2xUNpb2q8/Y+7552KlejkRvqvD19nMoUW/uQGGbqNpA6Tufu5FL5BZgw==}resolution: {integrity: sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==}@@ -14131,6 +14138,8 @@ snapshots:+ [email protected]: {}+@@ -15173,7 +15182,7 @@ snapshots:https-proxy-agent: 5.0.1is-potential-custom-element-name: 1.0.1nwsapi: 2.2.7- parse5: 7.2.1+ parse5: 7.3.0saxes: 6.0.0symbol-tree: 3.2.4tough-cookie: 4.1.4@@ -16380,9 +16389,9 @@ snapshots:dependencies:- entities: 4.5.0+ entities: 6.0.0