qrcode.chapril.org/vendor/wikimedia/less.php/lib/Less/Tree/Media.php

184 lines
4.5 KiB
PHP
Raw Normal View History

2021-02-15 21:29:38 +01:00
<?php
/**
2023-07-07 22:33:10 +02:00
* @private
2021-02-15 21:29:38 +01:00
*/
class Less_Tree_Media extends Less_Tree {
public $features;
public $rules;
public $index;
public $currentFileInfo;
public $isReferenced;
public $type = 'Media';
2023-07-07 22:33:10 +02:00
public function __construct( $value = [], $features = [], $index = null, $currentFileInfo = null ) {
2021-02-15 21:29:38 +01:00
$this->index = $index;
$this->currentFileInfo = $currentFileInfo;
$selectors = $this->emptySelectors();
$this->features = new Less_Tree_Value( $features );
2023-07-07 22:33:10 +02:00
$this->rules = [ new Less_Tree_Ruleset( $selectors, $value ) ];
2021-02-15 21:29:38 +01:00
$this->rules[0]->allowImports = true;
}
public function accept( $visitor ) {
$this->features = $visitor->visitObj( $this->features );
$this->rules = $visitor->visitArray( $this->rules );
}
/**
* @see Less_Tree::genCSS
*/
public function genCSS( $output ) {
$output->add( '@media ', $this->currentFileInfo, $this->index );
$this->features->genCSS( $output );
Less_Tree::outputRuleset( $output, $this->rules );
}
2023-07-07 22:33:10 +02:00
/**
* @param Less_Environment $env
* @return Less_Tree_Media|Less_Tree_Ruleset
* @see less-2.5.3.js#Media.prototype.eval
*/
2021-02-15 21:29:38 +01:00
public function compile( $env ) {
2023-07-07 22:33:10 +02:00
$media = new Less_Tree_Media( [], [], $this->index, $this->currentFileInfo );
2021-02-15 21:29:38 +01:00
$strictMathBypass = false;
if ( Less_Parser::$options['strictMath'] === false ) {
$strictMathBypass = true;
Less_Parser::$options['strictMath'] = true;
}
$media->features = $this->features->compile( $env );
if ( $strictMathBypass ) {
Less_Parser::$options['strictMath'] = false;
}
$env->mediaPath[] = $media;
$env->mediaBlocks[] = $media;
array_unshift( $env->frames, $this->rules[0] );
2023-07-07 22:33:10 +02:00
$media->rules = [ $this->rules[0]->compile( $env ) ];
2021-02-15 21:29:38 +01:00
array_shift( $env->frames );
array_pop( $env->mediaPath );
return !$env->mediaPath ? $media->compileTop( $env ) : $media->compileNested( $env );
}
public function variable( $name ) {
return $this->rules[0]->variable( $name );
}
public function find( $selector ) {
return $this->rules[0]->find( $selector, $this );
}
public function emptySelectors() {
$el = new Less_Tree_Element( '', '&', $this->index, $this->currentFileInfo );
2023-07-07 22:33:10 +02:00
$sels = [ new Less_Tree_Selector( [ $el ], [], null, $this->index, $this->currentFileInfo ) ];
2021-02-15 21:29:38 +01:00
$sels[0]->mediaEmpty = true;
return $sels;
}
public function markReferenced() {
$this->rules[0]->markReferenced();
$this->isReferenced = true;
Less_Tree::ReferencedArray( $this->rules[0]->rules );
}
// evaltop
public function compileTop( $env ) {
$result = $this;
if ( count( $env->mediaBlocks ) > 1 ) {
$selectors = $this->emptySelectors();
$result = new Less_Tree_Ruleset( $selectors, $env->mediaBlocks );
$result->multiMedia = true;
}
2023-07-07 22:33:10 +02:00
$env->mediaBlocks = [];
$env->mediaPath = [];
2021-02-15 21:29:38 +01:00
return $result;
}
2023-07-07 22:33:10 +02:00
/**
* @param Less_Environment $env
* @return Less_Tree_Ruleset
*/
2021-02-15 21:29:38 +01:00
public function compileNested( $env ) {
2023-07-07 22:33:10 +02:00
$path = array_merge( $env->mediaPath, [ $this ] );
'@phan-var array<Less_Tree_Media> $path';
2021-02-15 21:29:38 +01:00
// Extract the media-query conditions separated with `,` (OR).
foreach ( $path as $key => $p ) {
$value = $p->features instanceof Less_Tree_Value ? $p->features->value : $p->features;
2023-07-07 22:33:10 +02:00
$path[$key] = is_array( $value ) ? $value : [ $value ];
2021-02-15 21:29:38 +01:00
}
2023-07-07 22:33:10 +02:00
'@phan-var array<array<Less_Tree>> $path';
2021-02-15 21:29:38 +01:00
// Trace all permutations to generate the resulting media-query.
//
// (a, b and c) with nested (d, e) ->
// a and d
// a and e
// b and c and d
// b and c and e
$permuted = $this->permute( $path );
2023-07-07 22:33:10 +02:00
$expressions = [];
2021-02-15 21:29:38 +01:00
foreach ( $permuted as $path ) {
for ( $i = 0, $len = count( $path ); $i < $len; $i++ ) {
$path[$i] = Less_Parser::is_method( $path[$i], 'toCSS' ) ? $path[$i] : new Less_Tree_Anonymous( $path[$i] );
}
for ( $i = count( $path ) - 1; $i > 0; $i-- ) {
2023-07-07 22:33:10 +02:00
array_splice( $path, $i, 0, [ new Less_Tree_Anonymous( 'and' ) ] );
2021-02-15 21:29:38 +01:00
}
$expressions[] = new Less_Tree_Expression( $path );
}
$this->features = new Less_Tree_Value( $expressions );
// Fake a tree-node that doesn't output anything.
2023-07-07 22:33:10 +02:00
return new Less_Tree_Ruleset( [], [] );
2021-02-15 21:29:38 +01:00
}
public function permute( $arr ) {
2023-07-07 22:33:10 +02:00
if ( !$arr ) {
return [];
}
2021-02-15 21:29:38 +01:00
2023-07-07 22:33:10 +02:00
if ( count( $arr ) == 1 ) {
2021-02-15 21:29:38 +01:00
return $arr[0];
2023-07-07 22:33:10 +02:00
}
2021-02-15 21:29:38 +01:00
2023-07-07 22:33:10 +02:00
$result = [];
2021-02-15 21:29:38 +01:00
$rest = $this->permute( array_slice( $arr, 1 ) );
foreach ( $rest as $r ) {
foreach ( $arr[0] as $a ) {
$result[] = array_merge(
2023-07-07 22:33:10 +02:00
is_array( $a ) ? $a : [ $a ],
is_array( $r ) ? $r : [ $r ]
2021-02-15 21:29:38 +01:00
);
}
}
return $result;
}
public function bubbleSelectors( $selectors ) {
2023-07-07 22:33:10 +02:00
if ( !$selectors ) {
return;
}
2021-02-15 21:29:38 +01:00
2023-07-07 22:33:10 +02:00
$this->rules = [ new Less_Tree_Ruleset( $selectors, [ $this->rules[0] ] ) ];
2021-02-15 21:29:38 +01:00
}
}