@@ -34,12 +34,34 @@ var resolveValue = function(decl, map, /*optional*/ignorePseudoScope, /*internal
3434 var resultantValue = toString ( decl . value ) ;
3535 var warnings = [ ] ;
3636
37-
3837 // Match all variables first so we can later on if there are circular dependencies
3938 var variablesUsedInValue = [ ] ;
40- while ( ( matchingVarDecl = RE_VAR_FUNC_G . exec ( resultantValue ) ) ) {
41- variablesUsedInValue . push ( matchingVarDecl [ 1 ] ) ;
39+ // Create a temporary variable, storing resultantValue variable value
40+ var resultantValueTemp = resultantValue ;
41+ // Use balanced lib to find var() declarations and store variable names
42+ while ( ( matchingVarDecl = balanced ( 'var(' , ')' , resultantValueTemp ) ) ) {
43+ // Split at the comma to find variable name and fallback value
44+ // There may be other commas in the values so this isn't necessarily just 2 pieces
45+ var variableFallbackSplitPieces = matchingVarDecl . body . split ( ',' ) ;
46+
47+ // Get variable name and fallback, filtering empty items
48+ var variableName = variableFallbackSplitPieces [ 0 ] . trim ( ) ;
49+
50+ // Push found variable to variables used array
51+ variablesUsedInValue . push ( variableName ) ;
52+
53+ // Replace variable name (first occurence only) from result, to avoid circular loop
54+ resultantValueTemp = ( matchingVarDecl . pre || '' ) + matchingVarDecl . body . replace ( variableName , '' ) + ( matchingVarDecl . post || '' ) ;
4255 }
56+ // clear temporary variable
57+ resultantValueTemp = undefined ;
58+
59+ // Old strategy to find used variable names in declaration value, using RegExp
60+ // TODO: remove unused block
61+ // while ((matchingVarDecl = RE_VAR_FUNC_G.exec(resultantValue))) {
62+ // variablesUsedInValue.push(matchingVarDecl[1]);
63+ // }
64+
4365 // Remove duplicates from array
4466 variablesUsedInValue = variablesUsedInValue . filter ( filterDistinct ) ;
4567
0 commit comments