Debian Codegraph @

vendor/wikimedia/less.php/lib/Less/Tree/Call.php

raw
1 <?php
2 /**
3 * @private
4 * @see less-3.13.1.js#Call.prototype
5 */
6 class Less_Tree_Call extends Less_Tree {
7 /** @var string */
8 public $name;
9 /** @var Less_Tree[] */
10 public $args;
11 /** @var bool */
12 public $calc;
13 /** @var int */
14 public $index;
15 /** @var array|null */
16 public $currentFileInfo;
17
18 public function __construct( $name, $args, $index, $currentFileInfo = null ) {
19 $this->name = $name;
20 $this->args = $args;
21 $this->calc = $name === 'calc';
22 $this->index = $index;
23 $this->currentFileInfo = $currentFileInfo;
24 }
25
26 public function accept( $visitor ) {
27 $this->args = $visitor->visitArray( $this->args );
28 }
29
30 /**
31 * @see less-3.13.1.js#functionCaller.prototype.call
32 */
33 private function functionCaller( $function, array $arguments, $env, $evalArgs ) {
34 // This code is terrible and should be replaced as per this issue...
35 // https://github.com/less/less.js/issues/2477
36 $filtered = [];
37
38 if ( $evalArgs !== false ) {
39 foreach ( $arguments as $a ) {
40 $filtered[] = $a->compile( $env );
41 }
42 $arguments = $filtered;
43 $filtered = [];
44 }
45
46 foreach ( $arguments as $argument ) {
47 if ( $argument instanceof Less_Tree_Comment ) {
48 continue;
49 }
50 $filtered[] = $argument;
51 }
52 foreach ( $filtered as $index => $argument ) {
53 if ( $argument instanceof Less_Tree_Expression ) {
54 $filtered[$index] = $argument->mapToFunctionCallArgument();
55 }
56 }
57
58 return $function( ...$filtered );
59 }
60
61 /**
62 * @param Less_Environment $env
63 * @return void
64 */
65 private function exitCalc( $env, $currentMathContext ) {
66 if ( $this->calc || $env->inCalc ) {
67 $env->exitCalc();
68 }
69 $env->mathOn = $currentMathContext;
70 }
71
72 //
73 // When evaluating a function call,
74 // we either find the function in Less_Functions,
75 // in which case we call it, passing the evaluated arguments,
76 // or we simply print it out as it literal CSS.
77 //
78 // The reason why we compile the arguments, is in the case one
79 // of them is a LESS variable that only PHP knows the value of,
80 // like: `saturate(@mycolor)`.
81 // The function should receive the value, not the variable.
82 // TODO less.js#3.13.1 provide better parity with upstream.
83 public function compile( $env ) {
84 /**
85 * Turn off math for calc(), and switch back on for evaluating nested functions
86 */
87 $currentMathContext = $env->mathOn;
88 $env->mathOn = !$this->calc;
89 $evalArgs = null;
90
91 if ( $this->calc || $env->inCalc ) {
92 $env->enterCalc();
93 }
94 $nameLC = strtolower( $this->name );
95
96 $args = [];
97
98 switch ( $nameLC ) {
99 case '%':
100 $nameLC = '_percent';
101 break;
102 case 'get-unit':
103 $nameLC = 'getunit';
104 break;
105 case 'data-uri':
106 $nameLC = 'datauri';
107 break;
108 case 'svg-gradient':
109 $nameLC = 'svggradient';
110 break;
111 case 'image-size':
112 $nameLC = 'imagesize';
113 break;
114 case 'image-width':
115 $nameLC = 'imagewidth';
116 break;
117 case 'image-height':
118 $nameLC = 'imageheight';
119 break;
120 case 'if':
121 $evalArgs = false;
122 break;
123 }
124
125 $result = null;
126 if ( $nameLC === 'default' ) {
127 $result = Less_Tree_DefaultFunc::compile();
128 } else {
129 $func = null;
130 $functions = new Less_Functions( $env, $this->currentFileInfo );
131 $funcBuiltin = [ $functions, $nameLC ];
132 // Avoid method_exists() as that considers private utility functions too
133 if ( is_callable( $funcBuiltin ) ) {
134 $func = $funcBuiltin;
135 } elseif ( isset( $env->functions[$nameLC] ) && is_callable( $env->functions[$nameLC] ) ) {
136 $func = $env->functions[$nameLC];
137 }
138 // If the function name isn't known to LESS, output it unchanged as CSS.
139 if ( $func ) {
140 try {
141 $result = $this->functionCaller( $func, $this->args, $env, $evalArgs );
142 $this->exitCalc( $env, $currentMathContext );
143
144 } catch ( Exception $e ) {
145 // Preserve original trace, especially from custom functions.
146 // https://github.com/wikimedia/less.php/issues/38
147
148 // Check if 'error evaluating function' is the start of the error message
149 // less.js does this by checking if line and column already set
150 if ( str_starts_with( $e->getMessage(), 'error evaluating function' ) ) {
151 throw $e;
152 }
153
154 throw new Less_Exception_Compiler(
155 'error evaluating function `' . $this->name . '`' . ( $e->getMessage() ? ': ' . $e->getMessage() : '' ),
156 $e,
157 $this->index,
158 $this->currentFileInfo
159 );
160 }
161 }
162 }
163
164 if ( $result !== null ) {
165 return $result;
166 }
167 foreach ( $this->args as $a ) {
168 $args[] = $a->compile( $env );
169 }
170 $this->exitCalc( $env, $currentMathContext );
171
172 return new self( $this->name, $args, $this->index, $this->currentFileInfo );
173 }
174
175 /**
176 * @see Less_Tree::genCSS
177 */
178 public function genCSS( $output ) {
179 $output->add( $this->name . '(', $this->currentFileInfo, $this->index );
180 $args_len = count( $this->args );
181 for ( $i = 0; $i < $args_len; $i++ ) {
182 $this->args[$i]->genCSS( $output );
183 if ( $i + 1 < $args_len ) {
184 $output->add( ', ' );
185 }
186 }
187
188 $output->add( ')' );
189 }
190
191 }