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