Skip to content

Commit 94eb0aa

Browse files
Code refactor, to make lib more robust and compatible with older node versions
1 parent ba6e178 commit 94eb0aa

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

lib/resolve-value.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ function toString(value) {
1414
return String(value);
1515
}
1616

17+
function filterDistinct(value, index, self) {
18+
return self.indexOf(value) === index;
19+
}
20+
1721
// Pass in a value string to parse/resolve and a map of available values
1822
// and we can figure out the final value
1923
//
@@ -25,15 +29,19 @@ function toString(value) {
2529
var resolveValue = function(decl, map, /*optional*/ignorePseudoScope, /*internal debugging*/_debugIsInternal) {
2630
var debugIndent = _debugIsInternal ? '\t' : '';
2731

32+
var matchingVarDecl = undefined;
33+
var RE_VAR_FUNC_G = new RegExp(RE_VAR_FUNC.source, 'g');
2834
var resultantValue = toString(decl.value);
2935
var warnings = [];
36+
3037

31-
var variablesUsedInValueMap = {};
32-
// Use `replace` as a loop to go over all occurrences with the `g` flag
33-
resultantValue.replace(new RegExp(RE_VAR_FUNC.source, 'g'), function(match, variableName, fallback) {
34-
variablesUsedInValueMap[variableName] = true;
35-
});
36-
var variablesUsedInValue = Object.keys(variablesUsedInValueMap);
38+
// match all variables first, to gather all available variables in decl.value
39+
var variablesUsedInValue = [];
40+
while ((matchingVarDecl = RE_VAR_FUNC_G.exec(resultantValue))) {
41+
variablesUsedInValue.push(matchingVarDecl[1]);
42+
}
43+
// remove duplicates from array
44+
variablesUsedInValue = variablesUsedInValue.filter(filterDistinct);
3745

3846
//console.log(debugIndent, (_debugIsInternal ? '' : 'Try resolving'), generateScopeList(decl.parent, true), `ignorePseudoScope=${ignorePseudoScope}`, '------------------------');
3947

@@ -43,14 +51,16 @@ var resolveValue = function(decl, map, /*optional*/ignorePseudoScope, /*internal
4351
// var() = var( <custom-property-name> [, <any-value> ]? )
4452
// matches `name[, fallback]`, captures "name" and "fallback"
4553
// See: http://dev.w3.org/csswg/css-variables/#funcdef-var
46-
var match;
47-
while (match = balanced('var(', ')', resultantValue)) {
54+
while ((matchingVarDecl = balanced('var(', ')', resultantValue))) {
4855
var matchingVarDeclMapItem = undefined;
4956

50-
// split comma to find variable name and fallback value
51-
match.body = match.body.split(',');
57+
// Split at the comma to find variable name and fallback value
58+
// There may be other commas in the values so this isn't necessary just 2 pieces
59+
var variableFallbackSplitPieces = matchingVarDecl.body.split(',');
60+
5261
// get variable name and fallback, filtering empty items
53-
var [ variableName, fallback ] = [ match.body.shift(), match.body.join(',') ].map(item => item.trim()).filter(item => !!item);
62+
var variableName = variableFallbackSplitPieces[0].trim();
63+
var fallback = variableFallbackSplitPieces.length > 1 ? variableFallbackSplitPieces.slice(1).join(',').trim() : undefined;
5464

5565
(map[variableName] || []).forEach(function(varDeclMapItem) {
5666
// Make sure the variable declaration came from the right spot
@@ -103,7 +113,7 @@ var resolveValue = function(decl, map, /*optional*/ignorePseudoScope, /*internal
103113
}
104114

105115
// replace original declaration
106-
resultantValue = `${match.pre || ''}${replaceValue}${match.post || ''}`;
116+
resultantValue = (matchingVarDecl.pre || '') + replaceValue + (matchingVarDecl.post || '')
107117
}
108118

109119
return {

0 commit comments

Comments
 (0)