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
|
<?php
/**
* @private
*/
class Less_Tree_Rule extends Less_Tree implements Less_Tree_HasValueProperty {
public $name;
/** @var Less_Tree */
public $value;
/** @var string */
public $important;
public $merge;
public $index;
public $inline;
public $variable;
public $currentFileInfo;
/**
* @param string|array<Less_Tree_Keyword|Less_Tree_Variable> $name
* @param mixed $value
* @param null|false|string $important
* @param null|false|string $merge
* @param int|null $index
* @param array|null $currentFileInfo
* @param bool $inline
*/
public function __construct( $name, $value = null, $important = null, $merge = null, $index = null, $currentFileInfo = null, $inline = false ) {
$this->name = $name;
$this->value = ( $value instanceof Less_Tree )
? $value
: new Less_Tree_Value( [ $value ] );
$this->important = $important ? ' ' . trim( $important ) : '';
$this->merge = $merge;
$this->index = $index;
$this->currentFileInfo = $currentFileInfo;
$this->inline = $inline;
$this->variable = ( is_string( $name ) && $name[0] === '@' );
}
public function accept( $visitor ) {
$this->value = $visitor->visitObj( $this->value );
}
/**
* @see less-2.5.3.js#Rule.prototype.genCSS
*/
public function genCSS( $output ) {
$output->add( $this->name . ( Less_Parser::$options['compress'] ? ':' : ': ' ), $this->currentFileInfo, $this->index );
try {
$this->value->genCSS( $output );
} catch ( Less_Exception_Parser $e ) {
$e->index = $this->index;
$e->currentFile = $this->currentFileInfo;
throw $e;
}
$output->add( $this->important . ( ( $this->inline || ( Less_Environment::$lastRule && Less_Parser::$options['compress'] ) ) ? "" : ";" ), $this->currentFileInfo, $this->index );
}
/**
* @see less-2.5.3.js#Rule.prototype.eval
* @param Less_Environment $env
* @return self
*/
public function compile( $env ) {
$name = $this->name;
if ( is_array( $name ) ) {
// expand 'primitive' name directly to get
// things faster (~10% for benchmark.less):
if ( count( $name ) === 1 && $name[0] instanceof Less_Tree_Keyword ) {
$name = $name[0]->value;
} else {
$name = $this->CompileName( $env, $name );
}
}
$strictMathBypass = false;
if ( $name === "font" && !$env->strictMath ) {
$strictMathBypass = true;
$env->strictMath = true;
}
try {
$evaldValue = $this->value->compile( $env );
if ( !$this->variable && $evaldValue instanceof Less_Tree_DetachedRuleset ) {
throw new Less_Exception_Compiler( "Rulesets cannot be evaluated on a property.", null, $this->index, $this->currentFileInfo );
}
if ( Less_Environment::$mixin_stack ) {
$return = new self( $name, $evaldValue, $this->important, $this->merge, $this->index, $this->currentFileInfo, $this->inline );
} else {
$this->name = $name;
$this->value = $evaldValue;
$return = $this;
}
} catch ( Less_Exception_Parser $e ) {
if ( !is_numeric( $e->index ) ) {
$e->index = $this->index;
$e->currentFile = $this->currentFileInfo;
$e->genMessage();
}
throw $e;
}
if ( $strictMathBypass ) {
$env->strictMath = false;
}
return $return;
}
public function CompileName( $env, $name ) {
$output = new Less_Output();
foreach ( $name as $n ) {
$n->compile( $env )->genCSS( $output );
}
return $output->toString();
}
public function makeImportant() {
return new self( $this->name, $this->value, '!important', $this->merge, $this->index, $this->currentFileInfo, $this->inline );
}
public function mark( $value ) {
if ( !is_array( $this->value ) ) {
if ( method_exists( $value, 'markReferenced' ) ) {
// @phan-suppress-next-line PhanUndeclaredMethod
$value->markReferenced();
}
} else {
foreach ( $this->value as $v ) {
$this->mark( $v );
}
}
}
public function markReferenced() {
if ( $this->value ) {
$this->mark( $this->value );
}
}
}
|