Skip to content

Fixes #37 #48

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 6 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
25 changes: 13 additions & 12 deletions lib/resolve-decl.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,19 @@ function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/logResol
if(shouldPreserve === true && decl.value !== valueResults.value) {
decl.cloneAfter();
}


// Set 'undefined' value as a string to avoid making other plugins down the line unhappy
// See #22
if (valueResults.value === undefined) {
valueResults.value = 'undefined';
}


// Set the new value after we are done dealing with at-rule stuff
decl.value = valueResults.value;
}


// Set 'undefined' value as a string to avoid making other plugins down the line unhappy
// See #22
if (valueResults.value === undefined) {
valueResults.value = 'undefined';
}


// Set the new value after we are done dealing with at-rule stuff
decl.value = valueResults.value;
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebase this onto the current master and squash things down. This is already in the codebase




Expand Down
33 changes: 31 additions & 2 deletions lib/resolve-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,30 @@ var gatherVariableDependencies = require('./gather-variable-dependencies');
var findNodeAncestorWithSelector = require('./find-node-ancestor-with-selector');
var cloneSpliceParentOntoNodeWhen = require('./clone-splice-parent-onto-node-when');


if (typeof Object.assign != 'function') {
Object.assign = function(target, varArgs) { // .length of function is 2
'use strict';
if (target == null) { // TypeError if undefined or null
throw new TypeError('Cannot convert undefined or null to object');
}

var to = Object(target);

for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];

if (nextSource != null) { // Skip over if undefined or null
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
};
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have extend for this type of thing, see

var opts = extend({}, defaults, options);


// var() = var( <custom-property-name> [, <any-value> ]? )
// matches `name[, fallback]`, captures "name" and "fallback"
Expand Down Expand Up @@ -61,8 +84,14 @@ var resolveValue = function(decl, map, /*optional*/ignorePseudoScope, /*internal
}
});

var fallbackValue;
if(fallback) {
var cloneDecl = Object.assign({},decl,{value:fallback});
fallbackValue = resolveValue(cloneDecl, map, false,true).value;
Copy link
Owner

@MadLittleMods MadLittleMods Mar 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's space this out (sorry no eslint)

var cloneDecl = Object.assign({}, decl, { value: fallback });
fallbackValue = resolveValue(cloneDecl, 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));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's space it out 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;
}
}
5 changes: 5 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ describe('postcss-css-variables', function() {
describe('missing variable declarations', function() {
test('should work with missing variables', 'missing-variable-usage');
test('should use fallback value if provided with missing variables', 'missing-variable-should-fallback');
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 use string values for `undefined` values, see #22', function() {
return fs.readFileAsync('./test/fixtures/missing-variable-usage.css', 'utf8')
.then(function(buffer) {
Expand All @@ -204,6 +208,7 @@ describe('postcss-css-variables', function() {
});
});
});

});

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