Rule.php 3.1 KB

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