Debian Codegraph @

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

raw
1 <?php
2 /**
3 * @private
4 * @see less-3.13.1.js#Declaration.prototype
5 * @todo check for feature parity with 3.13.1
6 */
7 class Less_Tree_Declaration extends Less_Tree implements Less_Tree_HasValueProperty {
8
9 /** @var string|array<Less_Tree_Keyword|Less_Tree_Variable> */
10 public $name;
11 /** @var Less_Tree[]|Less_Tree_Anonymous */
12 public $value;
13 /** @var string */
14 public $important;
15 /** @var null|false|string */
16 public $merge;
17 /** @var int|null */
18 public $index;
19 /** @var bool */
20 public $inline;
21 /** @var bool */
22 public $variable;
23 /** @var array|null */
24 public $currentFileInfo;
25
26 /**
27 * In the upstream `parsed` is stored in `Node`, but Less_Tree_Declaration is the only place
28 * that make use of it.
29 * @see less-3.13.1.js#Node.parsed
30 * @var bool
31 */
32 public $parsed = false;
33
34 /**
35 * @param string|array<Less_Tree_Keyword|Less_Tree_Variable> $name
36 * @param Less_Tree|string|null $value
37 * @param null|false|string $important
38 * @param null|false|string $merge
39 * @param int|null $index
40 * @param array|null $currentFileInfo
41 * @param bool $inline
42 * @param bool|null $variable
43 */
44 public function __construct(
45 $name,
46 $value = null,
47 $important = null,
48 $merge = null,
49 $index = null,
50 $currentFileInfo = null,
51 $inline = false,
52 $variable = null
53 ) {
54 $this->name = $name;
55 $this->value = ( $value instanceof Less_Tree )
56 ? $value
57 : new Less_Tree_Value( [ $value ? new Less_Tree_Anonymous( $value ) : null ] );
58 $this->important = $important ? ' ' . trim( $important ) : '';
59 $this->merge = $merge;
60 $this->index = $index;
61 $this->currentFileInfo = $currentFileInfo;
62 $this->inline = $inline;
63 $this->variable = $variable ?? ( is_string( $name ) && $name[0] === '@' );
64 }
65
66 public function accept( $visitor ) {
67 $this->value = $visitor->visitObj( $this->value );
68 }
69
70 /**
71 * @see less-2.5.3.js#Rule.prototype.genCSS
72 */
73 public function genCSS( $output ) {
74 $output->add( $this->name . ( Less_Parser::$options['compress'] ? ':' : ': ' ), $this->currentFileInfo, $this->index );
75 try {
76 $this->value->genCSS( $output );
77
78 } catch ( Less_Exception_Parser $e ) {
79 $e->index = $this->index;
80 $e->currentFile = $this->currentFileInfo;
81 throw $e;
82 }
83 $output->add(
84 $this->important . ( ( $this->inline || ( Less_Environment::$lastRule && Less_Parser::$options['compress'] ) ) ? "" : ";" ),
85 $this->currentFileInfo,
86 $this->index
87 );
88 }
89
90 /**
91 * @see less-2.5.3.js#Rule.prototype.eval
92 * @param Less_Environment $env
93 * @return self
94 */
95 public function compile( $env ) {
96 $variable = $this->variable;
97 $name = $this->name;
98 if ( is_array( $name ) ) {
99 // expand 'primitive' name directly to get
100 // things faster (~10% for benchmark.less):
101 if ( count( $name ) === 1 && $name[0] instanceof Less_Tree_Keyword ) {
102 $name = $name[0]->value;
103 } else {
104 $name = $this->CompileName( $env, $name );
105 }
106 $variable = false; // never treat expanded interpolation as new variable name
107 }
108
109 $mathBypass = false;
110 $prevMath = $env->math;
111 if ( $name === "font" && $env->math === Less_Environment::MATH_ALWAYS ) {
112 $mathBypass = true;
113 $env->math = Less_Environment::MATH_PARENS_DIVISION;
114 }
115
116 try {
117 $env->importantScope[] = [];
118 $evaldValue = $this->value->compile( $env );
119
120 if ( !$this->variable && $evaldValue instanceof Less_Tree_DetachedRuleset ) {
121 throw new Less_Exception_Compiler( "Rulesets cannot be evaluated on a property.", null, $this->index, $this->currentFileInfo );
122 }
123
124 $important = $this->important;
125 $importantResult = array_pop( $env->importantScope );
126
127 if ( !$important && isset( $importantResult['important'] ) && $importantResult['important'] ) {
128 $important = $importantResult['important'];
129 }
130
131 $return = new Less_Tree_Declaration(
132 $name,
133 $evaldValue,
134 $important,
135 $this->merge,
136 $this->index,
137 $this->currentFileInfo,
138 $this->inline,
139 $variable,
140 );
141
142 } catch ( Less_Exception_Parser $e ) {
143 if ( !is_numeric( $e->index ) ) {
144 $e->index = $this->index;
145 $e->currentFile = $this->currentFileInfo;
146 $e->genMessage();
147 }
148 throw $e;
149 }
150
151 if ( $mathBypass ) {
152 $env->math = $prevMath;
153 }
154
155 return $return;
156 }
157
158 public function CompileName( $env, $name ) {
159 $output = new Less_Output();
160 foreach ( $name as $n ) {
161 $n->compile( $env )->genCSS( $output );
162 }
163 return $output->toString();
164 }
165
166 public function makeImportant() {
167 return new self( $this->name, $this->value, '!important', $this->merge, $this->index, $this->currentFileInfo, $this->inline );
168 }
169
170 public function mark( $value ) {
171 if ( !is_array( $this->value ) ) {
172
173 if ( method_exists( $value, 'markReferenced' ) ) {
174 $value->markReferenced();
175 }
176 } else {
177 foreach ( $this->value as $v ) {
178 $this->mark( $v );
179 }
180 }
181 }
182
183 public function markReferenced() {
184 if ( $this->value ) {
185 $this->mark( $this->value );
186 }
187 }
188
189 }