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

Function resolveVars

packages/css-data/src/parse-css.ts:111–206  ·  view source on GitHub ↗
(
  value: string,
  customProperties: Map<string, string>,
  cssVars: Map<string, string> | undefined,
  depth = 0
)

Source from the content-addressed store, hash-verified

109 * recursion depth limit is exceeded.
110 */
111const resolveVars = (
112 value: string,
113 customProperties: Map<string, string>,
114 cssVars: Map<string, string> | undefined,
115 depth = 0
116):
117 | { resolved: string; dropped: string[]; usedCssVarsSubstitution: boolean }
118 | undefined => {
119 if (!value.includes("var(") || depth > 8) {
120 return;
121 }
122
123 let ast: csstree.CssNode;
124 try {
125 ast = csstree.parse(value, { context: "value", positions: true });
126 } catch {
127 return;
128 }
129
130 const subs: Array<{ start: number; end: number; text: string }> = [];
131 const dropped: string[] = [];
132 let usedCssVarsSubstitution = false;
133
134 csstree.walk(ast, {
135 visit: "Function",
136 enter(node) {
137 const fnNode = node as csstree.FunctionNode;
138 if (fnNode.name !== "var" || !fnNode.loc) {
139 return;
140 }
141 const varRef = parseCssVar(fnNode);
142 if (!varRef) {
143 return walkSkip;
144 }
145
146 const direct =
147 customProperties.get(`--${varRef.value}`) ??
148 cssVars?.get(`--${varRef.value}`);
149
150 if (direct !== undefined) {
151 if (!customProperties.has(`--${varRef.value}`)) {
152 usedCssVarsSubstitution = true;
153 }
154 subs.push({
155 start: fnNode.loc.start.offset,
156 end: fnNode.loc.end.offset,
157 text: direct,
158 });
159 return walkSkip;
160 }
161
162 // Try inline fallback — may itself contain var() references.
163 if (varRef.fallback?.type === "unparsed") {
164 const fbValue = varRef.fallback.value;
165 // If the fallback itself has no var(), use it directly.
166 // Otherwise recursively resolve any nested vars inside it.
167 const fallbackResult = fbValue.includes("var(")
168 ? resolveVars(fbValue, customProperties, cssVars, depth + 1)

Callers 2

enterFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected