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
feat: Keep original case of custom attributes (#3777)
Oleg Isonenb9e60e9f7c4b
diff --git a/apps/builder/app/builder/features/settings-panel/controls/boolean.tsx b/apps/builder/app/builder/features/settings-panel/controls/boolean.tsxindex 0c91d65bf6..36c10ea017 100644--- a/apps/builder/app/builder/features/settings-panel/controls/boolean.tsx+++ b/apps/builder/app/builder/features/settings-panel/controls/boolean.tsx@@ -11,8 +11,8 @@ import {$selectedInstanceScope,updateExpressionValue,useBindingState,+ humanizeAttribute,} from "../shared";-import { humanizeString } from "~/shared/string-utils";export const BooleanControl = ({meta,@@ -23,7 +23,7 @@ export const BooleanControl = ({onChange,onDelete,}: ControlProps<"boolean">) => {- const label = humanizeString(meta.label || propName);+ const label = humanizeAttribute(meta.label || propName);const { scope, aliases } = useStore($selectedInstanceScope);const expression =prop?.type === "expression" ? prop.value : JSON.stringify(computedValue);diff --git a/apps/builder/app/builder/features/settings-panel/controls/check.tsx b/apps/builder/app/builder/features/settings-panel/controls/check.tsxindex 133ee81b18..4f0afd571c 100644--- a/apps/builder/app/builder/features/settings-panel/controls/check.tsx+++ b/apps/builder/app/builder/features/settings-panel/controls/check.tsx@@ -1,6 +1,5 @@import { useStore } from "@nanostores/react";import { Checkbox, CheckboxAndLabel, useId } from "@webstudio-is/design-system";-import { humanizeString } from "~/shared/string-utils";import {BindingControl,BindingPopover,@@ -12,6 +11,7 @@ import {$selectedInstanceScope,updateExpressionValue,useBindingState,+ humanizeAttribute,} from "../shared";const add = (array: string[], item: string) => {@@ -45,7 +45,7 @@ export const CheckControl = ({const options = Array.from(new Set([...meta.options, ...value]));const id = useId();- const label = humanizeString(meta.label || propName);+ const label = humanizeAttribute(meta.label || propName);const { scope, aliases } = useStore($selectedInstanceScope);const expression =prop?.type === "expression" ? prop.value : JSON.stringify(computedValue);@@ -85,7 +85,7 @@ export const CheckControl = ({}}id={`${id}:${option}`}/>- <Label htmlFor={`${id}:${option}`}>{humanizeString(option)}</Label>+ <Label htmlFor={`${id}:${option}`}>{option}</Label></CheckboxAndLabel>))}<BindingPopoverdiff --git a/apps/builder/app/builder/features/settings-panel/controls/code.tsx b/apps/builder/app/builder/features/settings-panel/controls/code.tsxindex a2556d8e3d..df22bf2102 100644--- a/apps/builder/app/builder/features/settings-panel/controls/code.tsx+++ b/apps/builder/app/builder/features/settings-panel/controls/code.tsx@@ -12,6 +12,7 @@ import {updateExpressionValue,$selectedInstanceScope,useBindingState,+ humanizeAttribute,} from "../shared";import { useState } from "react";import {@@ -24,7 +25,6 @@ import {theme,} from "@webstudio-is/design-system";import { InfoCircleIcon } from "@webstudio-is/icons";-import { humanizeString } from "~/shared/string-utils";const ErrorInfo = ({error,@@ -120,7 +120,7 @@ export const CodeControl = ({onChange({ type: "string", value });}});- const label = humanizeString(metaOverride.label || propName);+ const label = humanizeAttribute(metaOverride.label || propName);const { scope, aliases } = useStore($selectedInstanceScope);const expression =diff --git a/apps/builder/app/builder/features/settings-panel/controls/file.tsx b/apps/builder/app/builder/features/settings-panel/controls/file.tsxindex 2c2acf5843..8f8b29baa8 100644--- a/apps/builder/app/builder/features/settings-panel/controls/file.tsx+++ b/apps/builder/app/builder/features/settings-panel/controls/file.tsx@@ -13,9 +13,9 @@ import {updateExpressionValue,$selectedInstanceScope,useBindingState,+ humanizeAttribute,} from "../shared";import { SelectAsset } from "./select-asset";-import { humanizeString } from "~/shared/string-utils";const UrlInput = ({id,@@ -72,7 +72,7 @@ export const FileControl = ({});- const label = humanizeString(meta.label || propName);+ const label = humanizeAttribute(meta.label || propName);const { scope, aliases } = useStore($selectedInstanceScope);const expression =prop?.type === "expression" ? prop.value : JSON.stringify(computedValue);diff --git a/apps/builder/app/builder/features/settings-panel/controls/json.tsx b/apps/builder/app/builder/features/settings-panel/controls/json.tsxindex b79a32f6ce..b4d5428e36 100644--- a/apps/builder/app/builder/features/settings-panel/controls/json.tsx+++ b/apps/builder/app/builder/features/settings-panel/controls/json.tsx@@ -9,6 +9,7 @@ import {updateExpressionValue,$selectedInstanceScope,useBindingState,+ humanizeAttribute,} from "../shared";import {ExpressionEditor,@@ -18,7 +19,6 @@ import {BindingControl,BindingPopover,} from "~/builder/shared/binding-popover";-import { humanizeString } from "~/shared/string-utils";export const JsonControl = ({meta,@@ -50,7 +50,7 @@ export const JsonControl = ({// empty block}});- const label = humanizeString(meta.label || propName);+ const label = humanizeAttribute(meta.label || propName);const { scope, aliases } = useStore($selectedInstanceScope);const expression = prop?.type === "expression" ? prop.value : valueString;diff --git a/apps/builder/app/builder/features/settings-panel/controls/number.tsx b/apps/builder/app/builder/features/settings-panel/controls/number.tsxindex e161b58437..c3f7b782e4 100644--- a/apps/builder/app/builder/features/settings-panel/controls/number.tsx+++ b/apps/builder/app/builder/features/settings-panel/controls/number.tsx@@ -13,8 +13,8 @@ import {updateExpressionValue,$selectedInstanceScope,useBindingState,+ humanizeAttribute,} from "../shared";-import { humanizeString } from "~/shared/string-utils";export const NumberControl = ({meta,@@ -45,7 +45,7 @@ export const NumberControl = ({});- const label = humanizeString(meta.label || propName);+ const label = humanizeAttribute(meta.label || propName);const { scope, aliases } = useStore($selectedInstanceScope);const expression =prop?.type === "expression" ? prop.value : JSON.stringify(computedValue);diff --git a/apps/builder/app/builder/features/settings-panel/controls/radio.tsx b/apps/builder/app/builder/features/settings-panel/controls/radio.tsxindex 3e10bd81a2..c9b57a592c 100644--- a/apps/builder/app/builder/features/settings-panel/controls/radio.tsx+++ b/apps/builder/app/builder/features/settings-panel/controls/radio.tsx@@ -1,7 +1,6 @@import { useId } from "react";import { useStore } from "@nanostores/react";import { RadioGroup, Radio, RadioAndLabel } from "@webstudio-is/design-system";-import { humanizeString } from "~/shared/string-utils";import {BindingControl,BindingPopover,@@ -13,6 +12,7 @@ import {$selectedInstanceScope,updateExpressionValue,useBindingState,+ humanizeAttribute,} from "../shared";export const RadioControl = ({@@ -32,7 +32,7 @@ export const RadioControl = ({: [value, ...meta.options];const id = useId();- const label = humanizeString(meta.label || propName);+ const label = humanizeAttribute(meta.label || propName);const { scope, aliases } = useStore($selectedInstanceScope);const expression =prop?.type === "expression" ? prop.value : JSON.stringify(computedValue);@@ -70,7 +70,7 @@ export const RadioControl = ({{options.map((value) => (<RadioAndLabel key={value}><Radio value={value} id={`${id}:${value}`} />- <Label htmlFor={`${id}:${value}`}>{humanizeString(value)}</Label>+ <Label htmlFor={`${id}:${value}`}>{value}</Label></RadioAndLabel>))}</RadioGroup>diff --git a/apps/builder/app/builder/features/settings-panel/controls/select.tsx b/apps/builder/app/builder/features/settings-panel/controls/select.tsxindex d9d5bdf11b..d26291d1db 100644--- a/apps/builder/app/builder/features/settings-panel/controls/select.tsx+++ b/apps/builder/app/builder/features/settings-panel/controls/select.tsx@@ -1,7 +1,6 @@import { useId } from "react";import { useStore } from "@nanostores/react";import { Select } from "@webstudio-is/design-system";-import { humanizeString } from "~/shared/string-utils";import {BindingControl,BindingPopover,@@ -13,6 +12,7 @@ import {$selectedInstanceScope,updateExpressionValue,useBindingState,+ humanizeAttribute,} from "../shared";export const SelectControl = ({@@ -33,7 +33,7 @@ export const SelectControl = ({? meta.options: [value, ...meta.options];- const label = humanizeString(meta.label || propName);+ const label = humanizeAttribute(meta.label || propName);const { scope, aliases } = useStore($selectedInstanceScope);const expression =prop?.type === "expression" ? prop.value : JSON.stringify(computedValue);diff --git a/apps/builder/app/builder/features/settings-panel/controls/text-content.tsx b/apps/builder/app/builder/features/settings-panel/controls/text-content.tsxindex 5f50962313..be7b99bf67 100644--- a/apps/builder/app/builder/features/settings-panel/controls/text-content.tsx+++ b/apps/builder/app/builder/features/settings-panel/controls/text-content.tsx@@ -17,8 +17,8 @@ import {Label,updateExpressionValue,useBindingState,+ humanizeAttribute,} from "../shared";-import { humanizeString } from "~/shared/string-utils";const useInstance = (instanceId: Instance["id"]) => {const $store = useMemo(() => {@@ -58,7 +58,7 @@ export const TextContent = ({}});const id = useId();- const label = humanizeString(meta.label || propName);+ const label = humanizeAttribute(meta.label || propName);const { scope, aliases } = useStore($selectedInstanceScope);let expression: undefined | string;diff --git a/apps/builder/app/builder/features/settings-panel/controls/text.tsx b/apps/builder/app/builder/features/settings-panel/controls/text.tsxindex 5dab0eca20..b5410c6496 100644--- a/apps/builder/app/builder/features/settings-panel/controls/text.tsx+++ b/apps/builder/app/builder/features/settings-panel/controls/text.tsx@@ -12,9 +12,9 @@ import {updateExpressionValue,$selectedInstanceScope,useBindingState,+ humanizeAttribute,} from "../shared";import { useEffect, useRef } from "react";-import { humanizeString } from "~/shared/string-utils";export const TextControl = ({meta,@@ -34,7 +34,7 @@ export const TextControl = ({}});const id = useId();- const label = humanizeString(meta.label || propName);+ const label = humanizeAttribute(meta.label || propName);const textAreaRef = useRef<HTMLTextAreaElement>(null);const { scope, aliases } = useStore($selectedInstanceScope);const expression =diff --git a/apps/builder/app/builder/features/settings-panel/controls/url.tsx b/apps/builder/app/builder/features/settings-panel/controls/url.tsxindex b92753a038..927f5d66b3 100644--- a/apps/builder/app/builder/features/settings-panel/controls/url.tsx+++ b/apps/builder/app/builder/features/settings-panel/controls/url.tsx@@ -39,10 +39,10 @@ import {updateExpressionValue,$selectedInstanceScope,useBindingState,+ humanizeAttribute,} from "../shared";import { SelectAsset } from "./select-asset";import { createRootFolder } from "@webstudio-is/project-build";-import { humanizeString } from "~/shared/string-utils";type UrlControlProps = ControlProps<"url">;@@ -476,7 +476,7 @@ export const UrlControl = ({const BaseControl = modes[mode].control;- const label = humanizeString(meta.label || propName);+ const label = humanizeAttribute(meta.label || propName);const { scope, aliases } = useStore($selectedInstanceScope);const expression =prop?.type === "expression" ? prop.value : JSON.stringify(computedValue);diff --git a/apps/builder/app/builder/features/settings-panel/props-section/props-section.tsx b/apps/builder/app/builder/features/settings-panel/props-section/props-section.tsxindex f38bf1b85d..3860c80123 100644--- a/apps/builder/app/builder/features/settings-panel/props-section/props-section.tsx+++ b/apps/builder/app/builder/features/settings-panel/props-section/props-section.tsx@@ -2,7 +2,13 @@ import { useState } from "react";import { useStore } from "@nanostores/react";import { matchSorter } from "match-sorter";import type { Instance } from "@webstudio-is/sdk";-import { theme, Combobox, Separator, Flex } from "@webstudio-is/design-system";+import {+ theme,+ Combobox,+ Separator,+ Flex,+ Box,+} from "@webstudio-is/design-system";import { descendantComponent } from "@webstudio-is/react-sdk";import {$propValuesByInstanceSelector,@@ -12,22 +18,24 @@ import {} from "~/shared/nano-states";import { CollapsibleSectionWithAddButton } from "~/builder/shared/collapsible-section";import { renderControl } from "../controls/combined";-import {- usePropsLogic,- type NameAndLabel,- type PropAndMeta,-} from "./use-props-logic";+import { usePropsLogic, type PropAndMeta } from "./use-props-logic";import { Row } from "../shared";import { serverSyncStore } from "~/shared/sync";+import { isAttributeNameSafe } from "~/shared/dom-utils";-const itemToString = (item: NameAndLabel | null) =>- item?.label || item?.name || "";+type Item = {+ name: string;+ label?: string;+ description?: string;+};++const itemToString = (item: Item | null) => item?.label || item?.name || "";const matchOrSuggestToCreate = (search: string,- items: Array<NameAndLabel>,- itemToString: (item: NameAndLabel) => string-): Array<NameAndLabel> => {+ items: Array<Item>,+ itemToString: (item: Item) => string+): Array<Item> => {const matched = matchSorter(items, search, {keys: [itemToString],});@@ -39,7 +47,7 @@ const matchOrSuggestToCreate = () {matched.unshift({name: search.trim(),- label: `Create "${search.trim()}"`,+ label: `Create attribute: "${search.trim()}"`,});}return matched;@@ -82,32 +90,39 @@ const renderProperty = (},});-const forbiddenProperties = new Set(["style"]);+const forbiddenProperties = new Set(["style", "class", "className"]);-const AddProperty = ({+const AddPropertyOrAttribute = ({availableProps,onPropSelected,}: {- availableProps: NameAndLabel[];+ availableProps: Item[];onPropSelected: (propName: string) => void;}) => {const [value, setValue] = useState("");+ const [isValid, setIsValid] = useState(true);return (<Flexcss={{ height: theme.spacing[13] }}direction="column"justify="center">- <Combobox<NameAndLabel>+ <Combobox<Item>+ defaultHighlightedIndex={0}autoFocus- placeholder="Find or create a property"- // @todo add descriptions+ color={isValid ? undefined : "error"}+ placeholder="Select or create"items={availableProps}itemToString={itemToString}onItemSelect={(item) => {- if (forbiddenProperties.has(item.name)) {+ if (+ forbiddenProperties.has(item.name) ||+ isAttributeNameSafe(value) === false+ ) {+ setIsValid(false);return;}+ setIsValid(true);onPropSelected(item.name);}}match={matchOrSuggestToCreate}@@ -115,6 +130,13 @@ const AddProperty = ({onInputChange={(value) => {setValue(value ?? "");}}+ getDescription={(item) => {+ return (+ <Box css={{ width: theme.spacing[28] }}>+ {item?.description ?? "No description available"}+ </Box>+ );+ }}/></Flex>);@@ -145,13 +167,13 @@ export const PropsSection = (props: PropsSectionProps) => {<Separator /><CollapsibleSectionWithAddButton- label="Properties"+ label="Properties & Attributes"onAdd={() => setAddingProp(true)}hasItems={hasItems}><Flex gap="1" direction="column">{addingProp && (- <AddProperty+ <AddPropertyOrAttributeavailableProps={logic.availableProps}onPropSelected={(propName) => {setAddingProp(false);diff --git a/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts b/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.tsindex a48cbea079..9c70d4d32a 100644--- a/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts+++ b/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts@@ -19,7 +19,6 @@ export type PropAndMeta = {propName: string;meta: PropMeta;};-export type NameAndLabel = { name: string; label?: string };// The value we set prop to when it's added//@@ -317,7 +316,7 @@ export const usePropsLogic = ({/** List of remaining props still available to add */availableProps: Array.from(unprocessedKnown.entries(),- ([name, { label }]) => ({ name, label })+ ([name, { label, description }]) => ({ name, label, description })),};};diff --git a/apps/builder/app/builder/features/settings-panel/shared.tsx b/apps/builder/app/builder/features/settings-panel/shared.tsxindex 22e7c98fe1..f033f1392a 100644--- a/apps/builder/app/builder/features/settings-panel/shared.tsx+++ b/apps/builder/app/builder/features/settings-panel/shared.tsx@@ -37,6 +37,7 @@ import {$variableValuesByInstanceSelector,} from "~/shared/nano-states";import type { BindingVariant } from "~/builder/shared/binding-popover";+import { humanizeString } from "~/shared/string-utils";export type PropValue =| { type: "number"; value: number }@@ -385,3 +386,13 @@ export const useBindingState = (expression: undefined | string) => {}, [expression]);return useStore($bindingState);};++export const humanizeAttribute = (string: string) => {+ if (string.includes("-")) {+ return string;+ }+ if (string === "className") {+ return "Class";+ }+ return humanizeString(string);+};diff --git a/apps/builder/app/shared/dom-utils.ts b/apps/builder/app/shared/dom-utils.tsindex 940f3b2170..40e90b2844 100644--- a/apps/builder/app/shared/dom-utils.ts+++ b/apps/builder/app/shared/dom-utils.ts@@ -143,3 +143,39 @@ export const getAllElementsBoundingBox = (elements: Element[]): DOMRect => {width: right - left,});};++/**+ * Copyright (c) Meta Platforms, Inc. and affiliates.+ *+ * This source code is licensed under the MIT license found in the+ * LICENSE file in the root directory of this source tree.+ *+ * https://github.com/facebook/react/blob/main/packages/react-dom-bindings/src/shared/isAttributeNameSafe.js+ */+const attributeNameStartChar =+ ":A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD";+const attributeNameChar =+ attributeNameStartChar + "\\-.0-9\\u00B7\\u0300-\\u036F\\u203F-\\u2040";++// eslint-disable-next-line no-misleading-character-class+const validAttributeNameRegex = new RegExp(+ "^[" + attributeNameStartChar + "][" + attributeNameChar + "]*$"+);++const illegalAttributeNameCache = new Map<string, boolean>();+const validatedAttributeNameCache = new Map<string, boolean>();++export const isAttributeNameSafe = (attributeName: string) => {+ if (validatedAttributeNameCache.has(attributeName)) {+ return true;+ }+ if (illegalAttributeNameCache.has(attributeName)) {+ return false;+ }+ if (validAttributeNameRegex.test(attributeName)) {+ validatedAttributeNameCache.set(attributeName, true);+ return true;+ }+ illegalAttributeNameCache.set(attributeName, true);+ return false;+};diff --git a/apps/builder/app/shared/string-utils.ts b/apps/builder/app/shared/string-utils.tsindex 8d5375f4ed..778d148dc2 100644--- a/apps/builder/app/shared/string-utils.ts+++ b/apps/builder/app/shared/string-utils.ts@@ -8,7 +8,10 @@ const cache: Map<string, string> = new Map([]);export const humanizeString = (string: string): string => {- const result = cache.get(string) ?? titleCase(noCase(string));- cache.set(string, result);+ let result = cache.get(string);+ if (result === undefined) {+ result = titleCase(noCase(string));+ cache.set(string, result);+ }return result;};diff --git a/packages/design-system/src/components/combobox.tsx b/packages/design-system/src/components/combobox.tsxindex 5e39e1159b..c3f69c00c5 100644--- a/packages/design-system/src/components/combobox.tsx+++ b/packages/design-system/src/components/combobox.tsx@@ -35,8 +35,10 @@ import {MenuCheckedIcon,} from "./menu";import { ScrollArea } from "./scroll-area";-import { Flex, InputField, NestedInputButton } from "..";import { Box } from "./box";+import { Flex } from "./flex";+import { NestedInputButton } from "./nested-input-button";+import { InputField } from "./input-field";export const ComboboxListbox = styled("ul",@@ -142,7 +144,7 @@ export const ComboboxItemDescription = ({order: "var(--ws-combobox-description-order)",}}>- {descriptions.map((descr, index) => (+ {descriptions.map((description, index) => (<Boxcss={{gridColumn: "1",@@ -151,7 +153,7 @@ export const ComboboxItemDescription = ({}}key={index}>- {descr}+ {description}</Box>))}<Box@@ -445,11 +447,18 @@ export const useCombobox = <Item,>({};};+type ComboboxProps<Item> = UseComboboxProps<Item> &+ Omit<ComponentProps<"input">, "value"> & {+ color?: ComponentProps<typeof InputField>["color"];+ };+export const Combobox = <Item,>({autoFocus,getDescription,+ placeholder,+ color,...props-}: UseComboboxProps<Item> & Omit<ComponentProps<"input">, "value">) => {+}: ComboboxProps<Item>) => {const combobox = useCombobox<Item>(props);const descriptionItem =@@ -466,7 +475,9 @@ export const Combobox = <Item,>({<ComboboxAnchor><InputField{...combobox.getInputProps()}+ placeholder={placeholder}autoFocus={autoFocus}+ color={color}suffix={<NestedInputButton {...combobox.getToggleButtonProps()} />}/></ComboboxAnchor>