Skip to content
Merged
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
6 changes: 5 additions & 1 deletion lib/resolve-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ var cloneSpliceParentOntoNodeWhen = require('./clone-splice-parent-onto-node-whe
// See: http://dev.w3.org/csswg/css-variables/#funcdef-var
var RE_VAR_FUNC = (/var\((--[^,\s]+?)(?:\s*,\s*(.+))?\)/);

function toString(value) {
return String(value);
}

// Pass in a value string to parse/resolve and a map of available values
// and we can figure out the final value
//
Expand All @@ -23,7 +27,7 @@ var RE_VAR_FUNC = (/var\((--[^,\s]+?)(?:\s*,\s*(.+))?\)/);
var resolveValue = function(decl, map, /*optional*/ignorePseudoScope, /*internal debugging*/_debugIsInternal) {
var debugIndent = _debugIsInternal ? '\t' : '';

var resultantValue = decl.value;
var resultantValue = toString(decl.value);
var warnings = [];

var variablesUsedInValueMap = {};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.box {
top: var(--number-value);
width: var(--zero-value);
height: var(--null-value);
overflow: var(--undefined-value);
font-size: var(--object-value-passed-by-mistake);
visibility: var(--true-value);
opacity: var(--false-value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.box {
top: 50;
width: 0;
height: null;
overflow: undefined;
font-size: [object Object];
visibility: true;
opacity: false;
}
18 changes: 17 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ var MOCK_JS_VARIABLES = {
'js-defined-no-prefix': '#ff0000'
};

var NON_STRING_VARIABLES = {
'number-value': 50,
'zero-value': 0,
'null-value': null,
'undefined-value': undefined,
'object-value-passed-by-mistake': {},
'true-value': true,
'false-value': false,
};

var testPlugin = function(filePath, expectedFilePath, options) {
options = options || {};
return Promise.props({
Expand Down Expand Up @@ -177,6 +187,13 @@ describe('postcss-css-variables', function() {
preserveInjectedVariables: false,
}
);
test(
'should cast non-string values to string',
'js-defined-non-string-values-casted-to-string',
{
variables: NON_STRING_VARIABLES
}
);
});

describe('with `options.preserve`', function() {
Expand Down Expand Up @@ -249,5 +266,4 @@ describe('postcss-css-variables', function() {
'remove-nested-empty-rules-after-variable-collection'
);
});

});