Skip to content

Commit bd81a6f

Browse files
small code improvement to map css variables correctly (for nested variables with CSS functions) and added few more tests.
1 parent dff2e3a commit bd81a6f

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

lib/resolve-value.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ var resolveValue = function(decl, map, /*optional*/ignorePseudoScope, /*internal
3232

3333
var variablesUsedInValueMap = {};
3434
// Use `replace` as a loop to go over all occurrences with the `g` flag
35-
resultantValue.replace(new RegExp(RE_VAR_FUNC.source, 'g'), function(match, variableName, fallback) {
36-
variablesUsedInValueMap[variableName] = true;
37-
});
35+
var resultantValueCopy = resultantValue;
36+
while (resultantValueCopy.match(new RegExp(RE_VAR_FUNC.source, 'g'))) {
37+
resultantValueCopy = resultantValueCopy.replace(new RegExp(RE_VAR_FUNC.source, 'g'), function(match, variableName, fallback) {
38+
variablesUsedInValueMap[variableName] = true;
39+
});
40+
}
41+
3842
var variablesUsedInValue = Object.keys(variablesUsedInValueMap);
3943

4044
//console.log(debugIndent, (_debugIsInternal ? '' : 'Try resolving'), generateScopeList(decl.parent, true), `ignorePseudoScope=${ignorePseudoScope}`, '------------------------');

test/fixtures/nested-inside-other-func.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
:root {
22
--some-width: 150px;
3+
--some-other-width: 50px;
34
}
45

56
.box-foo {
@@ -12,4 +13,16 @@
1213

1314
.box-foo {
1415
width: calc(58.3333333333% - var(--missing, 100px));
16+
}
17+
18+
.box-foo {
19+
width: calc(58.3333333333% - var(--missing, var(--some-width, 100px)));
20+
}
21+
22+
.box-foo {
23+
width: calc(58.3333333333% - var(--missing));
24+
}
25+
26+
.box-foo {
27+
width: calc(var(--some-width) - var(--some-other-width));
1528
}

test/fixtures/nested-inside-other-func.expected.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,16 @@
88

99
.box-foo {
1010
width: calc(58.3333333333% - 100px);
11+
}
12+
13+
.box-foo {
14+
width: calc(58.3333333333% - 150px);
15+
}
16+
17+
.box-foo {
18+
width: undefined;
19+
}
20+
21+
.box-foo {
22+
width: calc(150px - 50px);
1123
}

0 commit comments

Comments
 (0)