Skip to content

Resolve var usage in fallbacks #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/resolve-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,15 @@ var resolveValue = function(decl, map, /*optional*/ignorePseudoScope, /*internal
}
});

// Resolve values in fallback
var fallbackValue = fallback;
if(fallback) {
var fallbackDecl = decl.clone({ parent: decl.parent, value: fallback });
fallbackValue = resolveValue(fallbackDecl, map, false, true).value;
}

// Default to the calculatedInPlaceValue which might be a previous fallback, then try this declarations fallback
var replaceValue = (matchingVarDeclMapItem || {}).calculatedInPlaceValue || fallback;
var replaceValue = (matchingVarDeclMapItem || {}).calculatedInPlaceValue || fallbackValue;
// Otherwise if the dependency health is good(no circular or self references), dive deeper and resolve
if(matchingVarDeclMapItem !== undefined && !gatherVariableDependencies(variablesUsedInValue, map).hasCircularOrSelfReference) {
// Splice the declaration parent onto the matching entry
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/missing-variable-should-fallback-calc.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
:root {
--foo1: 150px;
}

.box-bar {
width: var(--missing,calc(var(--foo1) + 100px));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.box-bar {
width: calc(150px + 100px);
}
8 changes: 8 additions & 0 deletions test/fixtures/missing-variable-should-fallback-nested.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
:root {
--foo1: 100px;
--foo2: 150px;
}

.box-bar {
width: var(--missing, var(--missing, var(--foo1)));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.box-bar {
width: 100px;
}
12 changes: 12 additions & 0 deletions test/fixtures/missing-variable-should-fallback-var.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
:root {
--foo-default: 100px;
--foo-width: 150px;
}

.box-foo {
width: var(--missing);
}

.box-bar {
width: var(--missing, var(--foo-default));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.box-foo {
width: undefined;
}

.box-bar {
width: 100px;
}
7 changes: 1 addition & 6 deletions test/fixtures/missing-variable-should-fallback.css
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
:root {
--foo-width: 150px;
}

.box-foo {
width: var(--missing);
}

.box-bar {
width: var(--missing, 30px);
}
}
5 changes: 1 addition & 4 deletions test/fixtures/missing-variable-should-fallback.expected.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
.box-foo {
width: undefined;
}

.box-bar {
width: 30px;
}
}
3 changes: 3 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ describe('postcss-css-variables', function() {
});
});
});
test('should use fallback variable if provided with missing variables', 'missing-variable-should-fallback-var');
test('should use fallback variable if provided with missing variables calc', 'missing-variable-should-fallback-calc');
test('should use fallback variable if provided with missing variables nested', 'missing-variable-should-fallback-nested');
});

it('should not parse malformed var() declarations', function() {
Expand Down