MCPcopy Create free account
hub / github.com/webstudio-is/webstudio / parseCssValue

Function parseCssValue

packages/css-data/src/parse-css-value.ts:713–870  ·  view source on GitHub ↗
(
  property: CssProperty, // Handles only long-hand values.
  input: string,
  topLevel = true
)

Source from the content-addressed store, hash-verified

711};
712
713export const parseCssValue = (
714 property: CssProperty, // Handles only long-hand values.
715 input: string,
716 topLevel = true
717): StyleValue => {
718 const potentialKeyword = input.toLowerCase().trim();
719 if (cssWideKeywords.has(potentialKeyword)) {
720 return { type: "keyword", value: potentialKeyword };
721 }
722
723 if (property === "transition-property" && potentialKeyword === "none") {
724 if (topLevel) {
725 return { type: "keyword", value: potentialKeyword };
726 } else {
727 // none is not valid layer keyword
728 return { type: "unparsed", value: potentialKeyword };
729 }
730 }
731
732 const invalidValue = {
733 type: "invalid",
734 value: input,
735 } as const;
736
737 if (input.length === 0) {
738 // custom properties can be empty
739 // in case interpolated value need to be avoided
740 if (property.startsWith("--")) {
741 return { type: "unparsed", value: "" };
742 }
743 return invalidValue;
744 }
745
746 if (isValidDeclaration(property, input) === false) {
747 return invalidValue;
748 }
749
750 const ast = cssTryParseValue(input);
751
752 if (ast == null) {
753 warnOnce(
754 true,
755 `Can't parse css property "${property}" with value "${input}"`
756 );
757 return invalidValue;
758 }
759 const nodes = "children" in ast ? (ast.children?.toArray() ?? []) : [ast];
760
761 // support only following types in custom properties
762 if (property.startsWith("--")) {
763 if (nodes.length === 1) {
764 const parsedValue = parseLiteral(nodes[0]);
765 if (
766 parsedValue?.type === "var" ||
767 parsedValue?.type === "unit" ||
768 parsedValue?.type === "rgb" ||
769 parsedValue?.type === "color"
770 ) {

Callers 5

leaveFunction · 0.90
mapLengthPercentageOrVarFunction · 0.90
getColorFunction · 0.90

Calls 7

isValidDeclarationFunction · 0.85
cssTryParseValueFunction · 0.85
parseLiteralFunction · 0.85
splitRepeatedFunction · 0.85
generateFunction · 0.85
parseShadowFunction · 0.85
hasMethod · 0.80

Tested by

no test coverage detected