Skip to content

Commit 60d16ca

Browse files
shonieMadLittleMods
authored andcommitted
casting js-defined variables to string
PR MadLittleMods#84 remarks
1 parent 9e687d8 commit 60d16ca

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

lib/resolve-value.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ var cloneSpliceParentOntoNodeWhen = require('./clone-splice-parent-onto-node-whe
1212
// See: http://dev.w3.org/csswg/css-variables/#funcdef-var
1313
var RE_VAR_FUNC = (/var\((--[^,\s]+?)(?:\s*,\s*(.+))?\)/);
1414

15+
function toString(value) {
16+
return String(value);
17+
}
18+
1519
// Pass in a value string to parse/resolve and a map of available values
1620
// and we can figure out the final value
1721
//
@@ -23,7 +27,7 @@ var RE_VAR_FUNC = (/var\((--[^,\s]+?)(?:\s*,\s*(.+))?\)/);
2327
var resolveValue = function(decl, map, /*optional*/ignorePseudoScope, /*internal debugging*/_debugIsInternal) {
2428
var debugIndent = _debugIsInternal ? '\t' : '';
2529

26-
var resultantValue = decl.value;
30+
var resultantValue = toString(decl.value);
2731
var warnings = [];
2832

2933
var variablesUsedInValueMap = {};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.box {
2+
top: var(--number-value);
3+
width: var(--zero-value);
4+
height: var(--null-value);
5+
overflow: var(--undefined-value);
6+
font-size: var(--object-value-passed-by-mistake);
7+
visibility: var(--true-value);
8+
opacity: var(--false-value);
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.box {
2+
top: 50;
3+
width: 0;
4+
height: null;
5+
overflow: undefined;
6+
font-size: [object Object];
7+
visibility: true;
8+
opacity: false;
9+
}

test/test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ var MOCK_JS_VARIABLES = {
2222
'js-defined-no-prefix': '#ff0000'
2323
};
2424

25+
var NON_STRING_VARIABLES = {
26+
'number-value': 50,
27+
'zero-value': 0,
28+
'null-value': null,
29+
'undefined-value': undefined,
30+
'object-value-passed-by-mistake': {},
31+
'true-value': true,
32+
'false-value': false,
33+
};
34+
2535
var testPlugin = function(filePath, expectedFilePath, options) {
2636
options = options || {};
2737
return Promise.props({
@@ -177,6 +187,13 @@ describe('postcss-css-variables', function() {
177187
preserveInjectedVariables: false,
178188
}
179189
);
190+
test(
191+
'should cast non-string values to string',
192+
'js-defined-non-string-values-casted-to-string',
193+
{
194+
variables: NON_STRING_VARIABLES
195+
}
196+
);
180197
});
181198

182199
describe('with `options.preserve`', function() {
@@ -249,5 +266,4 @@ describe('postcss-css-variables', function() {
249266
'remove-nested-empty-rules-after-variable-collection'
250267
);
251268
});
252-
253269
});

0 commit comments

Comments
 (0)