| 1 |
<?php |
| 2 |
/** |
| 3 |
* @private |
| 4 |
* @see less-3.13.1.js#Operation.prototype |
| 5 |
*/ |
| 6 |
class Less_Tree_Operation extends Less_Tree { |
| 7 |
|
| 8 |
/** @var string */ |
| 9 |
public $op; |
| 10 |
/** @var Less_Tree[] */ |
| 11 |
public $operands; |
| 12 |
/** @var bool */ |
| 13 |
public $isSpaced; |
| 14 |
|
| 15 |
/** |
| 16 |
* @param string $op |
| 17 |
*/ |
| 18 |
public function __construct( $op, $operands, $isSpaced = false ) { |
| 19 |
$this->op = trim( $op ); |
| 20 |
$this->operands = $operands; |
| 21 |
$this->isSpaced = $isSpaced; |
| 22 |
} |
| 23 |
|
| 24 |
public function accept( $visitor ) { |
| 25 |
$this->operands = $visitor->visitArray( $this->operands ); |
| 26 |
} |
| 27 |
|
| 28 |
public function compile( $env ) { |
| 29 |
$a = $this->operands[0]->compile( $env ); |
| 30 |
$b = $this->operands[1]->compile( $env ); |
| 31 |
|
| 32 |
// Skip operation if argument was not compiled down to a non-operable value. |
| 33 |
// For example, if one argument is a Less_Tree_Call like 'var(--foo)' then we |
| 34 |
// preserve it as literal for native CSS. |
| 35 |
// https://phabricator.wikimedia.org/T331688 |
| 36 |
if ( $env->isMathOn( $this->op ) ) { |
| 37 |
$op = $this->op === './' ? '/' : $this->op; |
| 38 |
|
| 39 |
if ( $a instanceof Less_Tree_Dimension && $b instanceof Less_Tree_Color ) { |
| 40 |
$a = $a->toColor(); |
| 41 |
} elseif ( $b instanceof Less_Tree_Dimension && $a instanceof Less_Tree_Color ) { |
| 42 |
$b = $b->toColor(); |
| 43 |
} |
| 44 |
|
| 45 |
if ( !( $a instanceof Less_Tree_Dimension || $a instanceof Less_Tree_Color ) |
| 46 |
|| !( $b instanceof Less_Tree_Dimension || $b instanceof Less_Tree_Color ) |
| 47 |
) { |
| 48 |
if ( $a instanceof Less_Tree_Operation && $a->op === '/' && $env->math === Less_Environment::MATH_PARENS_DIVISION |
| 49 |
) { |
| 50 |
return new self( $this->op, [ $a, $b ], $this->isSpaced ); |
| 51 |
} |
| 52 |
throw new Less_Exception_Compiler( "Operation on an invalid type" ); |
| 53 |
} |
| 54 |
|
| 55 |
return $a->operate( $op, $b ); |
| 56 |
} else { |
| 57 |
return new self( $this->op, [ $a, $b ], $this->isSpaced ); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* @see Less_Tree::genCSS |
| 63 |
*/ |
| 64 |
public function genCSS( $output ) { |
| 65 |
$this->operands[0]->genCSS( $output ); |
| 66 |
if ( $this->isSpaced ) { |
| 67 |
$output->add( " " ); |
| 68 |
} |
| 69 |
$output->add( $this->op ); |
| 70 |
if ( $this->isSpaced ) { |
| 71 |
$output->add( ' ' ); |
| 72 |
} |
| 73 |
$this->operands[1]->genCSS( $output ); |
| 74 |
} |
| 75 |
|
| 76 |
} |