Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.41% covered (success)
90.41%
66 / 73
58.33% covered (warning)
58.33%
7 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
Less_Tree_Media
90.41% covered (success)
90.41%
66 / 73
58.33% covered (warning)
58.33%
7 / 12
30.79
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 accept
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 genCSS
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 compile
80.00% covered (warning)
80.00%
12 / 15
0.00% covered (danger)
0.00%
0 / 1
4.13
 variable
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 find
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 emptySelectors
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 markReferenced
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 compileTop
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 compileNested
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
8
 permute
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
7.02
 bubbleSelectors
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
1<?php
2/**
3 * @private
4 */
5class Less_Tree_Media extends Less_Tree {
6
7    public $features;
8    public $rules;
9    public $index;
10    public $currentFileInfo;
11    public $isReferenced;
12
13    public function __construct( $value = [], $features = [], $index = null, $currentFileInfo = null ) {
14        $this->index = $index;
15        $this->currentFileInfo = $currentFileInfo;
16
17        $selectors = $this->emptySelectors();
18
19        $this->features = new Less_Tree_Value( $features );
20
21        $this->rules = [ new Less_Tree_Ruleset( $selectors, $value ) ];
22        $this->rules[0]->allowImports = true;
23    }
24
25    public function accept( $visitor ) {
26        $this->features = $visitor->visitObj( $this->features );
27        $this->rules = $visitor->visitArray( $this->rules );
28    }
29
30    /**
31     * @see Less_Tree::genCSS
32     */
33    public function genCSS( $output ) {
34        $output->add( '@media ', $this->currentFileInfo, $this->index );
35        $this->features->genCSS( $output );
36        Less_Tree::outputRuleset( $output, $this->rules );
37    }
38
39    /**
40     * @param Less_Environment $env
41     * @return self|Less_Tree_Ruleset
42     * @see less-2.5.3.js#Media.prototype.eval
43     */
44    public function compile( $env ) {
45        $media = new self( [], [], $this->index, $this->currentFileInfo );
46
47        $mathBypass = false;
48        if ( !$env->mathOn ) {
49            $mathBypass = true;
50            $env->mathOn = true;
51        }
52
53        $media->features = $this->features->compile( $env );
54
55        if ( $mathBypass ) {
56            $env->mathOn = false;
57        }
58
59        $env->mediaPath[] = $media;
60        $env->mediaBlocks[] = $media;
61
62        array_unshift( $env->frames, $this->rules[0] );
63        $media->rules = [ $this->rules[0]->compile( $env ) ];
64        array_shift( $env->frames );
65
66        array_pop( $env->mediaPath );
67
68        return !$env->mediaPath ? $media->compileTop( $env ) : $media->compileNested( $env );
69    }
70
71    public function variable( $name ) {
72        return $this->rules[0]->variable( $name );
73    }
74
75    public function find( $selector ) {
76        return $this->rules[0]->find( $selector, $this );
77    }
78
79    public function emptySelectors() {
80        $el = new Less_Tree_Element( '', '&', $this->index, $this->currentFileInfo );
81        $sels = [ new Less_Tree_Selector( [ $el ], [], null, $this->index, $this->currentFileInfo ) ];
82        $sels[0]->mediaEmpty = true;
83        return $sels;
84    }
85
86    public function markReferenced() {
87        $this->rules[0]->markReferenced();
88        $this->isReferenced = true;
89        Less_Tree::ReferencedArray( $this->rules[0]->rules );
90    }
91
92    // evaltop
93    public function compileTop( $env ) {
94        $result = $this;
95
96        if ( count( $env->mediaBlocks ) > 1 ) {
97            $selectors = $this->emptySelectors();
98            $result = new Less_Tree_Ruleset( $selectors, $env->mediaBlocks );
99            $result->multiMedia = true;
100        }
101
102        $env->mediaBlocks = [];
103        $env->mediaPath = [];
104
105        return $result;
106    }
107
108    /**
109     * @param Less_Environment $env
110     * @return Less_Tree_Ruleset
111     */
112    public function compileNested( $env ) {
113        $path = array_merge( $env->mediaPath, [ $this ] );
114        '@phan-var self[] $path';
115
116        // Extract the media-query conditions separated with `,` (OR).
117        foreach ( $path as $key => $p ) {
118            $value = $p->features instanceof Less_Tree_Value ? $p->features->value : $p->features;
119            $path[$key] = is_array( $value ) ? $value : [ $value ];
120        }
121        '@phan-var array<array<Less_Tree>> $path';
122
123        // Trace all permutations to generate the resulting media-query.
124        //
125        // (a, b and c) with nested (d, e) ->
126        //    a and d
127        //    a and e
128        //    b and c and d
129        //    b and c and e
130
131        $permuted = $this->permute( $path );
132        '@phan-var (Less_Tree|string)[][] $permuted';
133        $expressions = [];
134        foreach ( $permuted as $path ) {
135
136            for ( $i = 0, $len = count( $path ); $i < $len; $i++ ) {
137                $path[$i] = Less_Parser::is_method( $path[$i], 'toCSS' ) ? $path[$i] : new Less_Tree_Anonymous( $path[$i] );
138            }
139
140            for ( $i = count( $path ) - 1; $i > 0; $i-- ) {
141                array_splice( $path, $i, 0, [ new Less_Tree_Anonymous( 'and' ) ] );
142            }
143
144            $expressions[] = new Less_Tree_Expression( $path );
145        }
146        $this->features = new Less_Tree_Value( $expressions );
147
148        // Fake a tree-node that doesn't output anything.
149        return new Less_Tree_Ruleset( [], [] );
150    }
151
152    public function permute( $arr ) {
153        if ( !$arr ) {
154            return [];
155        }
156
157        if ( count( $arr ) == 1 ) {
158            return $arr[0];
159        }
160
161        $result = [];
162        $rest = $this->permute( array_slice( $arr, 1 ) );
163        foreach ( $rest as $r ) {
164            foreach ( $arr[0] as $a ) {
165                $result[] = array_merge(
166                    is_array( $a ) ? $a : [ $a ],
167                    is_array( $r ) ? $r : [ $r ]
168                );
169            }
170        }
171
172        return $result;
173    }
174
175    public function bubbleSelectors( $selectors ) {
176        if ( !$selectors ) {
177            return;
178        }
179
180        $this->rules = [ new Less_Tree_Ruleset( $selectors, [ $this->rules[0] ] ) ];
181    }
182
183}