summaryrefslogtreecommitdiffstats
path: root/vendor/wikimedia/less.php/lib/Less/Tree/Call.php
blob: 28a3a192eee0b8b3a9b733e8418461241c7fff21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
 * @private
 */
class Less_Tree_Call extends Less_Tree {
	public $value;

	public $name;
	public $args;
	public $index;
	public $currentFileInfo;
	public $type = 'Call';

	public function __construct( $name, $args, $index, $currentFileInfo = null ) {
		$this->name = $name;
		$this->args = $args;
		$this->index = $index;
		$this->currentFileInfo = $currentFileInfo;
	}

	public function accept( $visitor ) {
		$this->args = $visitor->visitArray( $this->args );
	}

	//
	// When evaluating a function call,
	// we either find the function in `tree.functions` [1],
	// in which case we call it, passing the  evaluated arguments,
	// or we simply print it out as it appeared originally [2].
	//
	// The *functions.js* file contains the built-in functions.
	//
	// The reason why we evaluate the arguments, is in the case where
	// we try to pass a variable to a function, like: `saturate(@color)`.
	// The function should receive the value, not the variable.
	//
	public function compile( $env = null ) {
		$args = [];
		foreach ( $this->args as $a ) {
			$args[] = $a->compile( $env );
		}

		$nameLC = strtolower( $this->name );
		switch ( $nameLC ) {
			case '%':
			$nameLC = '_percent';
				break;

			case 'get-unit':
			$nameLC = 'getunit';
				break;

			case 'data-uri':
			$nameLC = 'datauri';
				break;

			case 'svg-gradient':
			$nameLC = 'svggradient';
				break;
		}

		$result = null;
		if ( $nameLC === 'default' ) {
			$result = Less_Tree_DefaultFunc::compile();
		} else {
			$func = null;
			if ( method_exists( 'Less_Functions', $nameLC ) ) {
				$functions = new Less_Functions( $env, $this->currentFileInfo );
				$func = [ $functions, $nameLC ];
			} elseif ( isset( $env->functions[$nameLC] ) && is_callable( $env->functions[$nameLC] ) ) {
				$func = $env->functions[$nameLC];
			}
			// If the function name isn't known to LESS, output it unchanged as CSS.
			if ( $func ) {
				try {
					$result = call_user_func_array( $func, $args );
				} catch ( Exception $e ) {
					// Preserve original trace, especially from custom functions.
					// https://github.com/wikimedia/less.php/issues/38
					throw new Less_Exception_Compiler(
						'error evaluating function `' . $this->name . '` ' . $e->getMessage()
							. ' index: ' . $this->index,
						$e
					);
				}
			}
		}

		if ( $result !== null ) {
			return $result;
		}

		return new Less_Tree_Call( $this->name, $args, $this->index, $this->currentFileInfo );
	}

	/**
	 * @see Less_Tree::genCSS
	 */
	public function genCSS( $output ) {
		$output->add( $this->name . '(', $this->currentFileInfo, $this->index );
		$args_len = count( $this->args );
		for ( $i = 0; $i < $args_len; $i++ ) {
			$this->args[$i]->genCSS( $output );
			if ( $i + 1 < $args_len ) {
				$output->add( ', ' );
			}
		}

		$output->add( ')' );
	}

	// public function toCSS(){
	//    return $this->compile()->toCSS();
	//}

}