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: Report the constraining HTML ancestor (#6083)

Oleg Isonen8e4ee80badca
diff --git a/packages/project-build/src/runtime/components.test.ts b/packages/project-build/src/runtime/components.test.ts
index 817f3d8920..c42a0583d2 100644
--- a/packages/project-build/src/runtime/components.test.ts
+++ b/packages/project-build/src/runtime/components.test.ts
@@ -604,7 +604,7 @@ test("rejects fragment trees that violate the HTML content model", async () => {
{ parentInstanceId: parent.id, fragment },
{ createId: createIdFactory() }
)
- ).toThrow("Placing <a> element inside a <span> violates HTML spec.");
+ ).toThrow("Placing <a> element inside a <a> violates HTML spec.");
});
test("rejects fragments that violate the destination HTML content model", async () => {
@@ -630,7 +630,7 @@ test("rejects fragments that violate the destination HTML content model", async
{ parentInstanceId: parent.id, fragment },
{ createId: createIdFactory() }
)
- ).toThrow("Placing <button> element inside a <span> violates HTML spec.");
+ ).toThrow("Placing <button> element inside a <a> violates HTML spec.");
});
test("allows legacy invalid fragments when warnings are explicitly enabled", async () => {
diff --git a/packages/project-build/src/runtime/content-model.test.tsx b/packages/project-build/src/runtime/content-model.test.tsx
index 7d9ac78c16..f83430bbaa 100644
--- a/packages/project-build/src/runtime/content-model.test.tsx
+++ b/packages/project-build/src/runtime/content-model.test.tsx
@@ -280,6 +280,29 @@ test("transparent category should not pass through invalid parent", () => {
).toBeFalsy();
});
+test("reports the ancestor that introduces a transparent constraint", () => {
+ const messages: string[] = [];
+ expect(
+ isTreeSatisfyingContentModel({
+ ...renderData(
+ <ws.element ws:tag="body" ws:id="bodyId">
+ <ws.element ws:tag="ul" ws:id="listId">
+ <$.Slot>
+ <ws.element ws:tag="div" />
+ </$.Slot>
+ </ws.element>
+ </ws.element>
+ ),
+ metas: defaultMetas,
+ instanceSelector: ["bodyId"],
+ onError: (message) => messages.push(message),
+ })
+ ).toBeFalsy();
+ expect(messages).toEqual([
+ "Placing <div> element inside a <ul> violates HTML spec.",
+ ]);
+});
+
test("transparent category should pass through category when check deep in the tree", () => {
expect(
isTreeSatisfyingContentModel({
diff --git a/packages/project-build/src/runtime/content-model.ts b/packages/project-build/src/runtime/content-model.ts
index 7f9d64432e..e33a367b3d 100644
--- a/packages/project-build/src/runtime/content-model.ts
+++ b/packages/project-build/src/runtime/content-model.ts
@@ -197,6 +197,53 @@ const computeAllowedCategories = ({
return allowedCategories;
};
+const findHtmlConstraintInstance = ({
+ instances,
+ props,
+ metas,
+ instanceSelector,
+ htmlTagsByInstanceId,
+ tag,
+ component,
+}: {
+ instances: Instances;
+ props: Props;
+ metas: Metas;
+ instanceSelector: InstanceSelector;
+ htmlTagsByInstanceId?: HtmlTagsByInstanceId;
+ tag: undefined | string;
+ component: Instance["component"];
+}) => {
+ let allowedCategories: undefined | string[];
+ let wasSatisfying = true;
+ let constraintInstance: undefined | Instance;
+
+ for (const instanceId of instanceSelector.slice(1).reverse()) {
+ const ancestor = instances.get(instanceId);
+ if (ancestor === undefined) {
+ continue;
+ }
+ const ancestorTag = getTag({
+ instance: ancestor,
+ metas,
+ props,
+ htmlTagsByInstanceId,
+ });
+ allowedCategories = getElementChildren(ancestorTag, allowedCategories);
+ const isSatisfying = isTagSatisfyingContentModel({
+ tag,
+ component,
+ allowedCategories,
+ });
+ if (wasSatisfying && isSatisfying === false) {
+ constraintInstance = ancestor;
+ }
+ wasSatisfying = isSatisfying;
+ }
+
+ return constraintInstance;
+};
+
const defaultComponentContentModel: ContentModel = {
category: "instance",
children: ["rich-text", "instance"],
@@ -412,19 +459,27 @@ export const isTreeSatisfyingContentModel = ({
allowedCategories,
});
if (isTagSatisfying === false) {
- const parentInstance = instances.get(parentInstanceId);
- let parentTag: undefined | string;
- if (parentInstance) {
- parentTag = getTag({
- instance: parentInstance,
+ const constraintInstance = findHtmlConstraintInstance({
+ instances,
+ props,
+ metas,
+ instanceSelector,
+ htmlTagsByInstanceId,
+ tag,
+ component: instance.component,
+ });
+ let constraintTag: undefined | string;
+ if (constraintInstance) {
+ constraintTag = getTag({
+ instance: constraintInstance,
metas,
props,
htmlTagsByInstanceId,
});
}
- if (parentTag) {
+ if (constraintTag) {
onError?.(
- `Placing <${tag}> element inside a <${parentTag}> violates HTML spec.`,
+ `Placing <${tag}> element inside a <${constraintTag}> violates HTML spec.`,
instanceSelector
);
} else {
diff --git a/packages/project-build/src/runtime/pre-publish-audit.test.tsx b/packages/project-build/src/runtime/pre-publish-audit.test.tsx
index d61e72a2b9..6bdbec4c8e 100644
--- a/packages/project-build/src/runtime/pre-publish-audit.test.tsx
+++ b/packages/project-build/src/runtime/pre-publish-audit.test.tsx
@@ -128,19 +128,47 @@ test("allows valid legacy CodeText children", () => {
expect(runAudit({ instances, props })).toEqual([]);
});
-test("allows valid Video children", () => {
+test("allows Video inside a Div with valid children", () => {
const { instances, props } = renderData(
<$.Body ws:id="body">
- <$.Video>
- <ws.element ws:tag="source" />
- <ws.element ws:tag="track" />
- </$.Video>
+ <$.Box>
+ <$.Video>
+ <ws.element ws:tag="source" />
+ <ws.element ws:tag="track" />
+ </$.Video>
+ </$.Box>
</$.Body>
);
expect(runAudit({ instances, props })).toEqual([]);
});
+test("reports the Link constraint for Video inside a Div", () => {
+ const { instances, props } = renderData(
+ <$.Body ws:id="body">
+ <$.Link>
+ <$.Box>
+ <$.Video ws:id="video" controls />
+ </$.Box>
+ </$.Link>
+ </$.Body>
+ );
+
+ expect(runAudit({ instances, props })).toEqual([
+ {
+ ruleId: "html-content-model",
+ severity: "warning",
+ message: "Placing <video> element inside a <a> violates HTML spec.",
+ location: {
+ pageId: "marketedge",
+ pageName: "MarketEdge",
+ pagePath: "/marketedge",
+ instanceId: "video",
+ },
+ },
+ ]);
+});
+
test("warns about unknown element tags without throwing", () => {
const { instances, props } = renderData(
<$.Body ws:id="body">