mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-12-04 22:31:33 +09:00
## Description closes #4695 Also allow link in xml closes #4401 ## Steps for reproduction 1. click button 2. expect xyz ## Code Review - [ ] hi @kof, I need you to do - conceptual review (architecture, feature-correctness) - detailed review (read every line) - test it on preview ## Before requesting a review - [ ] made a self-review - [ ] added inline comments where things may be not obvious (the "why", not "what") ## Before merging - [ ] tested locally and on preview environment (preview dev login: 0000) - [ ] updated [test cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md) document - [ ] added tests - [ ] if any new env variables are added, added them to `.env` file
89 lines
2.3 KiB
TypeScript
89 lines
2.3 KiB
TypeScript
import {
|
|
ReactSdkContext,
|
|
xmlNodeTagSuffix,
|
|
} from "@webstudio-is/react-sdk/runtime";
|
|
import {
|
|
Children,
|
|
createElement,
|
|
forwardRef,
|
|
useContext,
|
|
type ElementRef,
|
|
type ReactNode,
|
|
} from "react";
|
|
|
|
export const defaultTag = "div";
|
|
|
|
// We don't want to enable all tags because Box is usually a container and we have specific components for many tags.
|
|
type Props = {
|
|
tag: string;
|
|
xmlns?: string;
|
|
children?: ReactNode;
|
|
rel?: string;
|
|
hreflang?: string;
|
|
href?: string;
|
|
"xmlns:xhtml"?: string;
|
|
};
|
|
|
|
export const XmlNode = forwardRef<ElementRef<"div">, Props>(
|
|
({ tag = "", children, ...props }, ref) => {
|
|
const { renderer } = useContext(ReactSdkContext);
|
|
|
|
const attributeEntries = Object.entries(props)
|
|
.filter(
|
|
([key]) =>
|
|
key.startsWith("data-") === false && key.startsWith("aria-") === false
|
|
)
|
|
.filter(([key]) => key !== "tabIndex")
|
|
.filter(([, value]) => typeof value !== "function");
|
|
|
|
const elementName = tag
|
|
// Must start from letter or underscore
|
|
.replace(/^[^\p{L}_]+/u, "")
|
|
// Clear all non letter, number, underscore, dot, and dash
|
|
.replaceAll(/[^\p{L}\p{N}\-._:]+/gu, "")
|
|
.trim();
|
|
|
|
if (renderer === undefined) {
|
|
const attrProps = Object.fromEntries(attributeEntries);
|
|
return createElement(
|
|
`${elementName}${xmlNodeTagSuffix}`,
|
|
attrProps,
|
|
children
|
|
);
|
|
}
|
|
|
|
const childrenArray = Children.toArray(children);
|
|
const isTextChild =
|
|
childrenArray.length > 0 &&
|
|
childrenArray.every((child) => typeof child === "string");
|
|
|
|
const attributes = attributeEntries.map(
|
|
([key, value]) => `${key}=${JSON.stringify(value)}`
|
|
);
|
|
|
|
return (
|
|
<div {...props}>
|
|
<span style={{ color: "rgb(16, 23, 233)" }}>
|
|
<{[elementName, ...attributes].join(" ")}>
|
|
</span>
|
|
{childrenArray.length > 0 && (
|
|
<div
|
|
ref={ref}
|
|
style={{
|
|
display: isTextChild ? "inline" : "block",
|
|
marginLeft: isTextChild ? 0 : "1rem",
|
|
}}
|
|
>
|
|
{children}
|
|
</div>
|
|
)}
|
|
<span style={{ color: "rgb(16, 23, 233)" }}>
|
|
</{elementName}>
|
|
</span>
|
|
</div>
|
|
);
|
|
}
|
|
);
|
|
|
|
XmlNode.displayName = "XmlNode";
|