@@ -14,6 +14,10 @@ function toString(value) {
14
14
return String ( value ) ;
15
15
}
16
16
17
+ function filterDistinct ( value , index , self ) {
18
+ return self . indexOf ( value ) === index ;
19
+ }
20
+
17
21
// Pass in a value string to parse/resolve and a map of available values
18
22
// and we can figure out the final value
19
23
//
@@ -25,15 +29,19 @@ function toString(value) {
25
29
var resolveValue = function ( decl , map , /*optional*/ ignorePseudoScope , /*internal debugging*/ _debugIsInternal ) {
26
30
var debugIndent = _debugIsInternal ? '\t' : '' ;
27
31
32
+ var matchingVarDecl = undefined ;
33
+ var RE_VAR_FUNC_G = new RegExp ( RE_VAR_FUNC . source , 'g' ) ;
28
34
var resultantValue = toString ( decl . value ) ;
29
35
var warnings = [ ] ;
36
+
30
37
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 ) ;
37
45
38
46
//console.log(debugIndent, (_debugIsInternal ? '' : 'Try resolving'), generateScopeList(decl.parent, true), `ignorePseudoScope=${ignorePseudoScope}`, '------------------------');
39
47
@@ -43,14 +51,16 @@ var resolveValue = function(decl, map, /*optional*/ignorePseudoScope, /*internal
43
51
// var() = var( <custom-property-name> [, <any-value> ]? )
44
52
// matches `name[, fallback]`, captures "name" and "fallback"
45
53
// 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 ) ) ) {
48
55
var matchingVarDeclMapItem = undefined ;
49
56
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
+
52
61
// 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 ;
54
64
55
65
( map [ variableName ] || [ ] ) . forEach ( function ( varDeclMapItem ) {
56
66
// Make sure the variable declaration came from the right spot
@@ -103,7 +113,7 @@ var resolveValue = function(decl, map, /*optional*/ignorePseudoScope, /*internal
103
113
}
104
114
105
115
// replace original declaration
106
- resultantValue = ` ${ match . pre || '' } ${ replaceValue } ${ match . post || '' } ` ;
116
+ resultantValue = ( matchingVarDecl . pre || '' ) + replaceValue + ( matchingVarDecl . post || '' )
107
117
}
108
118
109
119
return {
0 commit comments