@@ -14,6 +14,45 @@ function toString(value) {
14
14
return String ( value ) ;
15
15
}
16
16
17
+ // Check for balanced `var(` and `)` pairs inside `value`, and return the 3 fragments:
18
+ // `body` (inside), `pre` (before), `post` (after) of the found wrapper
19
+ function balancedVar ( value ) {
20
+ var match = balanced ( '(' , ')' , value )
21
+ if ( match ) {
22
+ // Check if it was prepended with var
23
+ if ( / (?: ^ | \s ) v a r $ / . test ( match . pre ) ) {
24
+ // Remove the var from the end of pre
25
+ return {
26
+ pre : match . pre . slice ( 0 , - 3 ) ,
27
+ body : match . body ,
28
+ post : match . post
29
+ }
30
+ } else {
31
+ // Check inside body
32
+ var bodyMatch = balancedVar ( match . body )
33
+ if ( bodyMatch ) {
34
+ // Reconstruct pre and post
35
+ return {
36
+ pre : match . pre + '(' + bodyMatch . pre ,
37
+ body : bodyMatch . body ,
38
+ post : bodyMatch . post + ')' + match . post
39
+ }
40
+ } else {
41
+ // Check inside post
42
+ var postMatch = balancedVar ( match . post )
43
+ if ( postMatch ) {
44
+ // Reconstruct pre
45
+ return {
46
+ pre : match . pre + '(' + match . body + ')' + postMatch . pre ,
47
+ body : postMatch . body ,
48
+ post : postMatch . post
49
+ }
50
+ }
51
+ }
52
+ }
53
+ }
54
+ }
55
+
17
56
// Pass in a value string to parse/resolve and a map of available values
18
57
// and we can figure out the final value
19
58
//
@@ -34,7 +73,7 @@ var resolveValue = function(decl, map, /*optional*/ignorePseudoScope, /*internal
34
73
// Create a temporary variable, storing resultantValue variable value
35
74
var remainingVariableValue = resultantValue ;
36
75
// Use balanced lib to find var() declarations and store variable names
37
- while ( ( matchingVarDecl = balanced ( 'var(' , ')' , remainingVariableValue ) ) ) {
76
+ while ( ( matchingVarDecl = balancedVar ( remainingVariableValue ) ) ) {
38
77
// Split at the comma to find variable name and fallback value
39
78
// There may be other commas in the values so this isn't necessarily just 2 pieces
40
79
var variableFallbackSplitPieces = matchingVarDecl . body . split ( ',' ) ;
@@ -61,7 +100,7 @@ var resolveValue = function(decl, map, /*optional*/ignorePseudoScope, /*internal
61
100
// var() = var( <custom-property-name> [, <any-value> ]? )
62
101
// matches `name[, fallback]`, captures "name" and "fallback"
63
102
// See: http://dev.w3.org/csswg/css-variables/#funcdef-var
64
- while ( ( matchingVarDecl = balanced ( 'var(' , ')' , resultantValue ) ) ) {
103
+ while ( ( matchingVarDecl = balancedVar ( resultantValue ) ) ) {
65
104
var matchingVarDeclMapItem = undefined ;
66
105
67
106
// Split at the comma to find variable name and fallback value
0 commit comments