Debian Codegraph @

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

raw
1 <?php
2 /**
3 * @private
4 * @see less-3.13.1.js#Expression.prototype
5 */
6 class Less_Tree_Expression extends Less_Tree implements Less_Tree_HasValueProperty {
7
8 /** @var Less_Tree[] */
9 public $value = [];
10 /** @var bool */
11 public $noSpacing;
12 /** @var true|null */
13 public $parens = null;
14
15 public function __construct( $value, $noSpacing = false ) {
16 $this->value = $value;
17 $this->noSpacing = $noSpacing;
18 }
19
20 public function accept( $visitor ) {
21 $this->value = $visitor->visitArray( $this->value );
22 }
23
24 public function compile( $env ) {
25 $mathOn = $env->isMathOn();
26 // NOTE: We don't support STRICT_LEGACY (Less.js 3.13)
27 $inParenthesis = $this->parens && ( true || !$this->parensInOp );
28 $doubleParen = false;
29 if ( $inParenthesis ) {
30 $env->inParenthesis();
31 }
32
33 if ( $this->value ) {
34
35 if ( count( $this->value ) > 1 ) {
36 $ret = [];
37 foreach ( $this->value as $e ) {
38 $ret[] = $e->compile( $env );
39 }
40 $returnValue = new self( $ret, $this->noSpacing );
41
42 } else {
43 // Implied `if ( count() === 1 )`
44 if ( ( $this->value[0] instanceof self ) && $this->value[0]->parens && !$this->value[0]->parensInOp && !$env->inCalc ) {
45 $doubleParen = true;
46 }
47 $returnValue = $this->value[0]->compile( $env );
48 }
49 } else {
50 $returnValue = $this;
51 }
52
53 if ( $inParenthesis ) {
54 $env->outOfParenthesis();
55 }
56
57 if ( $this->parens && $this->parensInOp && !$mathOn && !$doubleParen &&
58 ( !( $returnValue instanceof Less_Tree_Dimension ) )
59 ) {
60 $returnValue = new Less_Tree_Paren( $returnValue );
61 }
62 return $returnValue;
63 }
64
65 /**
66 * @see Less_Tree::genCSS
67 */
68 public function genCSS( $output ) {
69 $val_len = count( $this->value );
70 for ( $i = 0; $i < $val_len; $i++ ) {
71 $this->value[$i]->genCSS( $output );
72 if ( !$this->noSpacing && ( $i + 1 < $val_len ) ) {
73 // NOTE: Comma handling backported from Less.js 4.2.1 (T386077)
74 if ( !( $this->value[$i + 1] instanceof Less_Tree_Anonymous )
75 || ( $this->value[$i + 1] instanceof Less_Tree_Anonymous && $this->value[$i + 1]->value !== ',' )
76 ) {
77 $output->add( ' ' );
78 }
79 }
80 }
81 }
82
83 public function throwAwayComments() {
84 if ( is_array( $this->value ) ) {
85 $new_value = [];
86 foreach ( $this->value as $v ) {
87 if ( $v instanceof Less_Tree_Comment ) {
88 continue;
89 }
90 $new_value[] = $v;
91 }
92 $this->value = $new_value;
93 }
94 }
95
96 public function markReferenced() {
97 if ( is_array( $this->value ) ) {
98 foreach ( $this->value as $v ) {
99 if ( method_exists( $v, 'markReferenced' ) ) {
100 $v->markReferenced();
101 }
102 }
103 }
104 }
105
106 /**
107 * Should be used only in Less_Tree_Call::functionCaller()
108 * to retrieve expression without comments
109 * @internal
110 */
111 public function mapToFunctionCallArgument() {
112 if ( is_array( $this->value ) ) {
113 $subNodes = [];
114 foreach ( $this->value as $subNode ) {
115 if ( !( $subNode instanceof Less_Tree_Comment ) ) {
116 $subNodes[] = $subNode;
117 }
118 }
119 return count( $subNodes ) === 1
120 ? $subNodes[0]
121 : new Less_Tree_Expression( $subNodes );
122 }
123 return $this;
124 }
125 }