9 class Less_Tree_Media
extends Less_Tree
{
14 public $currentFileInfo;
16 public $type = 'Media';
18 public function __construct($value = array(), $features = array(), $index = null, $currentFileInfo = null ){
20 $this->index
= $index;
21 $this->currentFileInfo
= $currentFileInfo;
23 $selectors = $this->emptySelectors();
25 $this->features
= new Less_Tree_Value($features);
27 $this->rules
= array(new Less_Tree_Ruleset($selectors, $value));
28 $this->rules
[0]->allowImports
= true;
31 public function accept( $visitor ){
32 $this->features
= $visitor->visitObj($this->features
);
33 $this->rules
= $visitor->visitArray($this->rules
);
37 * @see Less_Tree::genCSS
39 public function genCSS( $output ){
41 $output->add( '@media ', $this->currentFileInfo
, $this->index
);
42 $this->features
->genCSS( $output );
43 Less_Tree
::outputRuleset( $output, $this->rules
);
47 public function compile($env) {
49 $media = new Less_Tree_Media(array(), array(), $this->index
, $this->currentFileInfo
);
51 $strictMathBypass = false;
52 if( Less_Parser
::$options['strictMath'] === false) {
53 $strictMathBypass = true;
54 Less_Parser
::$options['strictMath'] = true;
57 $media->features
= $this->features
->compile($env);
59 if( $strictMathBypass ){
60 Less_Parser
::$options['strictMath'] = false;
63 $env->mediaPath
[] = $media;
64 $env->mediaBlocks
[] = $media;
66 array_unshift($env->frames
, $this->rules
[0]);
67 $media->rules
= array($this->rules
[0]->compile($env));
68 array_shift($env->frames
);
70 array_pop($env->mediaPath
);
72 return !$env->mediaPath ?
$media->compileTop($env) : $media->compileNested($env);
75 public function variable($name) {
76 return $this->rules
[0]->variable($name);
79 public function find($selector) {
80 return $this->rules
[0]->find($selector, $this);
83 public function emptySelectors(){
84 $el = new Less_Tree_Element('','&', $this->index
, $this->currentFileInfo
);
85 $sels = array( new Less_Tree_Selector(array($el), array(), null, $this->index
, $this->currentFileInfo
) );
86 $sels[0]->mediaEmpty
= true;
90 public function markReferenced(){
91 $this->rules
[0]->markReferenced();
92 $this->isReferenced
= true;
93 Less_Tree
::ReferencedArray($this->rules
[0]->rules
);
97 public function compileTop($env) {
100 if (count($env->mediaBlocks
) > 1) {
101 $selectors = $this->emptySelectors();
102 $result = new Less_Tree_Ruleset($selectors, $env->mediaBlocks
);
103 $result->multiMedia
= true;
106 $env->mediaBlocks
= array();
107 $env->mediaPath
= array();
112 public function compileNested($env) {
113 $path = array_merge($env->mediaPath
, array($this));
115 // Extract the media-query conditions separated with `,` (OR).
116 foreach ($path as $key => $p) {
117 $value = $p->features
instanceof Less_Tree_Value ?
$p->features
->value
: $p->features
;
118 $path[$key] = is_array($value) ?
$value : array($value);
121 // Trace all permutations to generate the resulting media-query.
123 // (a, b and c) with nested (d, e) ->
129 $permuted = $this->permute($path);
130 $expressions = array();
131 foreach($permuted as $path){
133 for( $i=0, $len=count($path); $i < $len; $i++
){
134 $path[$i] = Less_Parser
::is_method($path[$i], 'toCSS') ?
$path[$i] : new Less_Tree_Anonymous($path[$i]);
137 for( $i = count($path) - 1; $i > 0; $i-- ){
138 array_splice($path, $i, 0, array(new Less_Tree_Anonymous('and')));
141 $expressions[] = new Less_Tree_Expression($path);
143 $this->features
= new Less_Tree_Value($expressions);
147 // Fake a tree-node that doesn't output anything.
148 return new Less_Tree_Ruleset(array(), array());
151 public function permute($arr) {
155 if (count($arr) == 1)
159 $rest = $this->permute(array_slice($arr, 1));
160 foreach ($rest as $r) {
161 foreach ($arr[0] as $a) {
162 $result[] = array_merge(
163 is_array($a) ?
$a : array($a),
164 is_array($r) ?
$r : array($r)
172 public function bubbleSelectors($selectors) {
174 if( !$selectors) return;
176 $this->rules
= array(new Less_Tree_Ruleset( $selectors, array($this->rules
[0])));