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

Function parseLiteral

packages/css-data/src/parse-css-value.ts:544–711  ·  view source on GitHub ↗
(
  node: undefined | null | CssNode,
  keywords?: readonly string[]
)

Source from the content-addressed store, hash-verified

542};
543
544const parseLiteral = (
545 node: undefined | null | CssNode,
546 keywords?: readonly string[]
547):
548 | undefined
549 | UnitValue
550 | KeywordValue
551 | ImageValue
552 | RgbValue
553 | ColorValue
554 | VarValue
555 | FunctionValue => {
556 if (node?.type === "Number") {
557 return {
558 type: "unit",
559 unit: "number",
560 value: Number(node.value),
561 };
562 }
563 if (node?.type === "Dimension" && availableUnits.has(node.unit)) {
564 return {
565 type: "unit",
566 unit: node.unit as Unit,
567 value: Number(node.value),
568 };
569 }
570 if (node?.type === "Percentage") {
571 return {
572 type: "unit",
573 unit: "%",
574 value: Number(node.value),
575 };
576 }
577 if (node?.type === "Identifier") {
578 const name = node.name.toLowerCase();
579 if (
580 keywords?.map((keyword) => keyword.toLowerCase()).includes(name) ||
581 parseColor(name)
582 ) {
583 return {
584 type: "keyword",
585 value: name,
586 };
587 }
588 }
589 if (node?.type === "Url") {
590 return {
591 type: "image",
592 value: {
593 type: "url",
594 url: node.value,
595 },
596 };
597 }
598 if (node?.type === "Hash") {
599 const hexString = `#${node.value}`;
600 const color = parseColor(hexString);
601 if (color) {

Callers 2

parseShadowFunction · 0.85
parseCssValueFunction · 0.85

Calls 6

generateFunction · 0.85
parseColorWithVarAlphaFunction · 0.85
parseCssVarFunction · 0.85
parseShadowFunction · 0.85
hasMethod · 0.80
parseColorFunction · 0.70

Tested by

no test coverage detected