Moodle release 2.7.8
[moodle.git] / lib / lessphp / Tree / Rule.php
blob221008bf1c50cd2094a97b41efd16edca8b745fd
1 <?php
3 /**
4 * Rule
6 * @package Less
7 * @subpackage tree
8 */
9 class Less_Tree_Rule extends Less_Tree{
11 public $name;
12 public $value;
13 public $important;
14 public $merge;
15 public $index;
16 public $inline;
17 public $variable;
18 public $currentFileInfo;
19 public $type = 'Rule';
21 /**
22 * @param string $important
24 public function __construct($name, $value = null, $important = null, $merge = null, $index = null, $currentFileInfo = null, $inline = false){
25 $this->name = $name;
26 $this->value = ($value instanceof Less_Tree_Value || $value instanceof Less_Tree_Ruleset) ? $value : new Less_Tree_Value(array($value));
27 $this->important = $important ? ' ' . trim($important) : '';
28 $this->merge = $merge;
29 $this->index = $index;
30 $this->currentFileInfo = $currentFileInfo;
31 $this->inline = $inline;
32 $this->variable = ( is_string($name) && $name[0] === '@');
35 function accept($visitor) {
36 $this->value = $visitor->visitObj( $this->value );
39 /**
40 * @see Less_Tree::genCSS
42 function genCSS( $output ){
44 $output->add( $this->name . Less_Environment::$_outputMap[': '], $this->currentFileInfo, $this->index);
45 try{
46 $this->value->genCSS( $output);
48 }catch( Less_Exception_Parser $e ){
49 $e->index = $this->index;
50 $e->currentFile = $this->currentFileInfo;
51 throw $e;
53 $output->add( $this->important . (($this->inline || (Less_Environment::$lastRule && Less_Parser::$options['compress'])) ? "" : ";"), $this->currentFileInfo, $this->index);
56 public function compile ($env){
58 $name = $this->name;
59 if( is_array($name) ){
60 // expand 'primitive' name directly to get
61 // things faster (~10% for benchmark.less):
62 if( count($name) === 1 && $name[0] instanceof Less_Tree_Keyword ){
63 $name = $name[0]->value;
64 }else{
65 $name = $this->CompileName($env,$name);
69 $strictMathBypass = Less_Parser::$options['strictMath'];
70 if( $name === "font" && !Less_Parser::$options['strictMath'] ){
71 Less_Parser::$options['strictMath'] = true;
74 try {
75 $evaldValue = $this->value->compile($env);
77 if( !$this->variable && $evaldValue->type === "DetachedRuleset") {
78 throw new Less_Exception_Compiler("Rulesets cannot be evaluated on a property.", null, $this->index, $this->currentFileInfo);
81 if( Less_Environment::$mixin_stack ){
82 $return = new Less_Tree_Rule($name, $evaldValue, $this->important, $this->merge, $this->index, $this->currentFileInfo, $this->inline);
83 }else{
84 $this->name = $name;
85 $this->value = $evaldValue;
86 $return = $this;
89 }catch( Less_Exception_Parser $e ){
90 if( !is_numeric($e->index) ){
91 $e->index = $this->index;
92 $e->currentFile = $this->currentFileInfo;
94 throw $e;
97 Less_Parser::$options['strictMath'] = $strictMathBypass;
99 return $return;
103 function CompileName( $env, $name ){
104 $output = new Less_Output();
105 foreach($name as $n){
106 $n->compile($env)->genCSS($output);
108 return $output->toString();
111 function makeImportant(){
112 return new Less_Tree_Rule($this->name, $this->value, '!important', $this->merge, $this->index, $this->currentFileInfo, $this->inline);