@@ -2,6 +2,8 @@ import Node from './node';
2
2
import Color from './color' ;
3
3
import Dimension from './dimension' ;
4
4
import * as Constants from '../constants' ;
5
+ import Variable from './variable' ;
6
+ import Call from './call' ;
5
7
const MATH = Constants . Math ;
6
8
7
9
@@ -18,8 +20,51 @@ Operation.prototype = Object.assign(new Node(), {
18
20
this . operands = visitor . visitArray ( this . operands ) ;
19
21
} ,
20
22
23
+ find : function ( obj , fun ) {
24
+ for ( var i_2 = 0 , r = void 0 ; i_2 < obj . length ; i_2 ++ ) {
25
+ r = fun . call ( obj , obj [ i_2 ] ) ;
26
+ if ( r ) {
27
+ return r ;
28
+ }
29
+ }
30
+ return null ;
31
+ } ,
32
+
33
+ evalVariable : function ( context , operand ) {
34
+ if ( operand . name === 'var' && operand . args . length === 1 ) {
35
+ var varName = operand . args [ 0 ] . toCSS ( ) ;
36
+ var variable = this . find ( context . frames , function ( frame ) {
37
+ var v = frame . variable ( varName ) ;
38
+ if ( v ) {
39
+ if ( v . important ) {
40
+ var importantScope = context . importantScope [ context . importantScope . length - 1 ] ;
41
+ importantScope . important = v . important ;
42
+ }
43
+ // If in calc, wrap vars in a function call to cascade evaluate args first
44
+ if ( context . inCalc ) {
45
+ return ( new Call ( '_SELF' , [ v . value ] ) ) . eval ( context ) ;
46
+ }
47
+ else {
48
+ return v . value . eval ( context ) ;
49
+ }
50
+ }
51
+ } ) ;
52
+ if ( variable ) {
53
+ return variable ;
54
+ }
55
+ }
56
+ } ,
57
+
21
58
eval ( context ) {
22
- let a = this . operands [ 0 ] . eval ( context ) , b = this . operands [ 1 ] . eval ( context ) , op ;
59
+ var a = this . evalVariable ( context , this . operands [ 0 ] )
60
+ if ( ! a ) {
61
+ a = this . operands [ 0 ] . eval ( context )
62
+ }
63
+ var b = this . evalVariable ( context , this . operands [ 1 ] ) ;
64
+ if ( ! b ) {
65
+ b = this . operands [ 1 ] . eval ( context ) ;
66
+ }
67
+ var op ;
23
68
24
69
if ( context . isMathOn ( this . op ) ) {
25
70
op = this . op === './' ? '/' : this . op ;
@@ -29,6 +74,18 @@ Operation.prototype = Object.assign(new Node(), {
29
74
if ( b instanceof Dimension && a instanceof Color ) {
30
75
b = b . toColor ( ) ;
31
76
}
77
+ if ( a instanceof Dimension && b instanceof Call && b . name === 'var' ) {
78
+ if ( b . args && b . args . length === 1 ) {
79
+ b = new Variable ( b . args [ 0 ] . toCSS ( ) , 0 , { } ) ;
80
+ return a . operate ( context , op , b ) ;
81
+ }
82
+ }
83
+ if ( b instanceof Dimension && a instanceof Call && a . name === 'var' ) {
84
+ if ( a . args && a . args . length === 1 ) {
85
+ a = new Variable ( a . args [ 0 ] . toCSS ( ) , 0 , { } ) ;
86
+ return b . operate ( context , op , a ) ;
87
+ }
88
+ }
32
89
if ( ! a . operate || ! b . operate ) {
33
90
if (
34
91
( a instanceof Operation || b instanceof Operation )
0 commit comments