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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
<?php
/**
* @private
*/
class Less_Tree_Mixin_Call extends Less_Tree {
public $selector;
public $arguments;
public $index;
public $currentFileInfo;
public $important;
public function __construct( $elements, $args, $index, $currentFileInfo, $important = false ) {
$this->selector = new Less_Tree_Selector( $elements );
$this->arguments = $args;
$this->index = $index;
$this->currentFileInfo = $currentFileInfo;
$this->important = $important;
}
/**
* @see less-2.5.3.js#MixinCall.prototype.eval
*/
public function compile( $env ) {
$rules = [];
$match = false;
$isOneFound = false;
$candidates = [];
$conditionResult = [];
$this->selector = $this->selector->compile( $env );
$args = [];
foreach ( $this->arguments as $a ) {
$argValue = $a['value']->compile( $env );
if ( !empty( $a['expand'] ) && is_array( $argValue->value ) ) {
foreach ( $argValue->value as $value ) {
$args[] = [ 'name' => null, 'value' => $value ];
}
} else {
$args[] = [ 'name' => $a['name'], 'value' => $argValue ];
}
}
$defNone = 0;
$defTrue = 1;
$defFalse = 2;
foreach ( $env->frames as $frame ) {
$noArgumentsFilter = static function ( $rule ) use ( $env ) {
return $rule->matchArgs( [], $env );
};
$mixins = $frame->find( $this->selector, null, $noArgumentsFilter );
if ( !$mixins ) {
continue;
}
$isOneFound = true;
// To make `default()` function independent of definition order we have two "subpasses" here.
// At first we evaluate each guard *twice* (with `default() == true` and `default() == false`),
// and build candidate list with corresponding flags. Then, when we know all possible matches,
// we make a final decision.
$mixins_len = count( $mixins );
for ( $m = 0; $m < $mixins_len; $m++ ) {
$mixin = $mixins[$m]["rule"];
$mixinPath = $mixins[$m]["path"];
if ( $this->IsRecursive( $env, $mixin ) ) {
continue;
}
if ( $mixin->matchArgs( $args, $env ) ) {
// less-2.5.3.js#MixinCall calcDefGroup()
$group = -1;
for ( $f = 0; $f < 2; $f++ ) {
$conditionResult[$f] = true;
Less_Tree_DefaultFunc::value( $f );
for ( $p = 0; $p < count( $mixinPath ) && $conditionResult[$f]; $p++ ) {
$namespace = $mixinPath[$p] ?? null;
if ( isset( $namespace ) && method_exists( $namespace, "matchCondition" ) ) {
$conditionResult[$f] = $conditionResult[$f] && $namespace->matchCondition( null, $env );
}
}
if ( method_exists( $mixin, "matchCondition" ) ) {
$conditionResult[$f] = $conditionResult[$f] && $mixin->matchCondition( $args, $env );
}
}
// PhanTypeInvalidDimOffset -- False positive
'@phan-var array{0:bool,1:bool} $conditionResult';
if ( $conditionResult[0] || $conditionResult[1] ) {
if ( $conditionResult[0] != $conditionResult[1] ) {
$group = $conditionResult[1] ?
$defTrue : $defFalse;
} else {
$group = $defNone;
}
}
$candidate = [ 'mixin' => $mixin, 'group' => $group ];
if ( $candidate["group"] !== -1 ) {
$candidates[] = $candidate;
}
$match = true;
}
}
Less_Tree_DefaultFunc::reset();
$count = [ 0, 0, 0 ];
for ( $m = 0; $m < count( $candidates ); $m++ ) {
$count[ $candidates[$m]['group'] ]++;
}
if ( $count[$defNone] > 0 ) {
$defaultResult = $defFalse;
} else {
$defaultResult = $defTrue;
if ( ( $count[$defTrue] + $count[$defFalse] ) > 1 ) {
throw new Exception( 'Ambiguous use of `default()` found when matching for `' . $this->format( $args ) . '`' );
}
}
$candidates_length = count( $candidates );
for ( $m = 0; $m < $candidates_length; $m++ ) {
$candidate = $candidates[$m]['group'];
if ( ( $candidate === $defNone ) || ( $candidate === $defaultResult ) ) {
try {
$mixin = $candidates[$m]['mixin'];
if ( !( $mixin instanceof Less_Tree_Mixin_Definition ) ) {
$originalRuleset = $mixin instanceof Less_Tree_Ruleset ? $mixin->originalRuleset : $mixin;
$mixin = new Less_Tree_Mixin_Definition( '', [], $mixin->rules, null, false );
$mixin->originalRuleset = $originalRuleset;
}
$rules = array_merge( $rules, $mixin->evalCall( $env, $args, $this->important )->rules );
} catch ( Exception $e ) {
throw new Less_Exception_Compiler( $e->getMessage(), null, null, $this->currentFileInfo );
}
}
}
if ( $match ) {
if ( !$this->currentFileInfo || !isset( $this->currentFileInfo['reference'] ) || !$this->currentFileInfo['reference'] ) {
Less_Tree::ReferencedArray( $rules );
}
return $rules;
}
}
if ( $isOneFound ) {
$selectorName = $this->selector->toCSS();
throw new Less_Exception_Compiler( 'No matching definition was found for ' . $selectorName . ' with args `' . $this->Format( $args ) . '`', null, $this->index, $this->currentFileInfo );
} else {
throw new Less_Exception_Compiler( trim( $this->selector->toCSS() ) . " is undefined in " . $this->currentFileInfo['filename'], null, $this->index );
}
}
/**
* Format the args for use in exception messages
*/
private function Format( $args ) {
$message = [];
if ( $args ) {
foreach ( $args as $a ) {
$argValue = '';
if ( $a['name'] ) {
$argValue .= $a['name'] . ':';
}
if ( is_object( $a['value'] ) ) {
$argValue .= $a['value']->toCSS();
} else {
$argValue .= '???';
}
$message[] = $argValue;
}
}
return implode( ', ', $message );
}
/**
* Are we in a recursive mixin call?
*
* @return bool
*/
private function IsRecursive( $env, $mixin ) {
foreach ( $env->frames as $recur_frame ) {
if ( !( $mixin instanceof Less_Tree_Mixin_Definition ) ) {
if ( $mixin === $recur_frame ) {
return true;
}
if ( isset( $recur_frame->originalRuleset ) && $mixin->ruleset_id === $recur_frame->originalRuleset ) {
return true;
}
}
}
return false;
}
}
|