Skip to content

Commit 1a5289f

Browse files
committed
Resolve fallback with all test cases
1 parent 4265ae7 commit 1a5289f

10 files changed

+75
-11
lines changed

lib/resolve-value.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,29 @@ var gatherVariableDependencies = require('./gather-variable-dependencies');
55
var findNodeAncestorWithSelector = require('./find-node-ancestor-with-selector');
66
var cloneSpliceParentOntoNodeWhen = require('./clone-splice-parent-onto-node-when');
77

8+
/*
9+
Object.assign polyfill
10+
https://gist.github.com/WebReflection/10404826
11+
*/
12+
if (!('assign' in Object)) {
13+
Object.assign = function(has){
14+
'use strict';
15+
return assign;
16+
function assign(target, source) {
17+
for (var i = 1; i < arguments.length; i++) {
18+
copy(target, arguments[i]);
19+
}
20+
return target;
21+
}
22+
function copy(target, source) {
23+
for (var key in source) {
24+
if (has.call(source, key)) {
25+
target[key] = source[key];
26+
}
27+
}
28+
}
29+
}({}.hasOwnProperty);
30+
}
831

932

1033
// var() = var( <custom-property-name> [, <any-value> ]? )
@@ -61,8 +84,14 @@ var resolveValue = function(decl, map, /*optional*/ignorePseudoScope, /*internal
6184
}
6285
});
6386

87+
var fallbackValue;
88+
if(fallback) {
89+
var cloneDecl = Object.assign({},decl,{value:fallback});
90+
fallbackValue = resolveValue(cloneDecl, map, false,true).value;
91+
}
92+
6493
// Default to the calculatedInPlaceValue which might be a previous fallback, then try this declarations fallback
65-
var replaceValue = (matchingVarDeclMapItem || {}).calculatedInPlaceValue || fallback;
94+
var replaceValue = (matchingVarDeclMapItem || {}).calculatedInPlaceValue || fallbackValue;
6695
// Otherwise if the dependency health is good(no circular or self references), dive deeper and resolve
6796
if(matchingVarDeclMapItem !== undefined && !gatherVariableDependencies(variablesUsedInValue, map).hasCircularOrSelfReference) {
6897
// Splice the declaration parent onto the matching entry
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:root {
2+
--foo1: 150px;
3+
}
4+
5+
.box-bar {
6+
width: var(--missing,calc(var(--foo1) + 100px));
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.box-bar {
2+
width: calc(150px + 100px);
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
:root {
2+
--foo1: 100px;
3+
--foo2: 150px;
4+
}
5+
6+
.box-bar {
7+
width: var(--missing, var(--missing, var(--foo1)));
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.box-bar {
2+
width: 100px;
3+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
:root {
2+
--foo-default: 100px;
3+
--foo-width: 150px;
4+
}
5+
6+
.box-foo {
7+
width: var(--missing);
8+
}
9+
10+
.box-bar {
11+
width: var(--missing, var(--foo-default));
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.box-foo {
2+
width: undefined;
3+
}
4+
5+
.box-bar {
6+
width: 100px;
7+
}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
:root {
22
--foo-width: 150px;
33
}
4-
5-
.box-foo {
6-
width: var(--missing);
7-
}
8-
94
.box-bar {
105
width: var(--missing, 30px);
11-
}
6+
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
.box-foo {
2-
width: undefined;
3-
}
41

52
.box-bar {
63
width: 30px;
7-
}
4+
}

test/test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ describe('postcss-css-variables', function() {
180180
describe('missing variable declarations', function() {
181181
test('should work with missing variables', 'missing-variable-usage');
182182
test('should use fallback value if provided with missing variables', 'missing-variable-should-fallback');
183+
test('should use fallback variable if provided with missing variables', 'missing-variable-should-fallback-var');
184+
test('should use fallback variable if provided with missing variables calc', 'missing-variable-should-fallback-calc');
185+
test('should use fallback variable if provided with missing variables nested', 'missing-variable-should-fallback-nested');
183186
});
184187

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

0 commit comments

Comments
 (0)