Skip to content

Commit f6639fa

Browse files
committed
[#95] fix: recursion infinite-loop
1 parent fc07367 commit f6639fa

File tree

7 files changed

+94
-5
lines changed

7 files changed

+94
-5
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"cssvar.files": ["testing.css"],
3+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
:root {
2+
--foo: #f00;
3+
--bar: var(--foo);
4+
}
5+
6+
.test {
7+
background: var(--foo);
8+
}
9+
10+
.test--two {
11+
--foo: var(--bar);
12+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
:root {
2+
--foo: #f00;
3+
--joo: 4px;
4+
/* Following does not get normalised in post-processing, because of regex */
5+
--too: 2px var(--joo);
6+
--bar: var(--too);
7+
}
8+
9+
.test {
10+
/* This needs to be fixed, and will be fixed once stop using object mapping in */
11+
/* done to single value in v3.0 */
12+
background: var(--foo);
13+
}
14+
15+
.test--two {
16+
--foo: var(--bar);
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* Folllowing is valid css */
2+
:root {
3+
--bar: var(--foo);
4+
}
5+
6+
.test {
7+
background: var(--foo);
8+
}
9+
10+
.test--two {
11+
--foo: var(--bar);
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* Folllowing is valid css */
2+
:root {
3+
--bar: var(--foo);
4+
}
5+
6+
.test {
7+
background: var(--foo);
8+
}
9+
10+
.test--two {
11+
--foo: var(--bar);
12+
}
13+
14+
.test--two.define {
15+
--foo: lightgreen;
16+
}

examples/recursion/testing.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
:root {
2+
--foo: #f00;
3+
--joo: 4px;
4+
/* Following does not get normalised in post-processing, because of regex */
5+
--too: 2px var(--joo);
6+
--bar: var(--too);
7+
}
8+
9+
.test {
10+
background: var(--foo);
11+
}
12+
13+
.test--two {
14+
--foo: var(--bar);
15+
}

src/utils.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
SupportedLanguageIds,
1515
VAR_KEYWORD_REVERSE,
1616
CACHE,
17+
PATTERN_ALL_VARIABLE_USAGES,
1718
} from "./constants";
1819
import { CSSVarDeclarations } from "./main";
1920
import { serializeColor } from "./color-parser";
@@ -100,6 +101,7 @@ export const populateValue = async (
100101
// Get Color for each, and modify the cssVar Record.
101102
const vars = getCSSDeclarationArray(cssVars);
102103

104+
// TODO(phoenisx) Remove this state.
103105
const cssVarsMapToSelf = vars.reduce((defs, cssVar) => {
104106
defs[cssVar.property] = cssVar;
105107
return defs;
@@ -110,7 +112,7 @@ export const populateValue = async (
110112
for await (const cssVar of vars) {
111113
const isVariable = getVariableType(cssVar.value);
112114
if (typeof isVariable === "string") {
113-
const value = getValue(getVariableName(cssVar.value), cssVarsMapToSelf);
115+
const value = getValue(getVariableName(cssVar.value), vars);
114116
cssVar.value = value || cssVar.value;
115117
}
116118

@@ -124,16 +126,28 @@ export const populateValue = async (
124126
};
125127

126128
function getValue(
127-
name: string,
128-
cssVarsMapToSelf: Record<string, CSSVarDeclarations | null>
129+
prop: string, // Points to CSS variable name (a.k.a Propname in a CSS declarations)
130+
cssVars: CSSVarDeclarations[]
129131
): string | null {
130-
const currentCssVar = cssVarsMapToSelf[name];
132+
const currentCssVar = cssVars.reduce<CSSVarDeclarations | null>(
133+
(currentCssVar, cssVar) => {
134+
if (
135+
!currentCssVar &&
136+
cssVar.property === prop &&
137+
!cssVar.value.match(PATTERN_ALL_VARIABLE_USAGES)
138+
) {
139+
currentCssVar = cssVar;
140+
}
141+
return currentCssVar;
142+
},
143+
null
144+
);
131145
if (currentCssVar) {
132146
const variableType = getVariableType(currentCssVar.value);
133147
if (variableType === null) {
134148
return currentCssVar.value;
135149
} else {
136-
return getValue(getVariableName(currentCssVar.value), cssVarsMapToSelf);
150+
return getValue(getVariableName(currentCssVar.value), cssVars);
137151
}
138152
}
139153

0 commit comments

Comments
 (0)