| 1 |
<?php |
| 2 |
/** |
| 3 |
* @private |
| 4 |
*/ |
| 5 |
class Less_Visitor_joinSelector extends Less_Visitor { |
| 6 |
|
| 7 |
/** @var Less_Tree_Selector[][][] */ |
| 8 |
public $contexts = [ [] ]; |
| 9 |
|
| 10 |
/** |
| 11 |
* @param Less_Tree_Ruleset $root |
| 12 |
*/ |
| 13 |
public function run( $root ) { |
| 14 |
return $this->visitObj( $root ); |
| 15 |
} |
| 16 |
|
| 17 |
public function visitDeclaration( $declNode, &$visitDeeper ) { |
| 18 |
$visitDeeper = false; |
| 19 |
} |
| 20 |
|
| 21 |
public function visitMixinDefinition( $mixinDefinitionNode, &$visitDeeper ) { |
| 22 |
$visitDeeper = false; |
| 23 |
} |
| 24 |
|
| 25 |
public function visitRuleset( $rulesetNode ) { |
| 26 |
$context = end( $this->contexts ); |
| 27 |
$paths = []; |
| 28 |
|
| 29 |
if ( !$rulesetNode->root ) { |
| 30 |
$selectors = $rulesetNode->selectors; |
| 31 |
if ( $selectors !== null ) { |
| 32 |
$filtered = []; |
| 33 |
foreach ( $selectors as $selector ) { |
| 34 |
if ( $selector->getIsOutput() ) { |
| 35 |
$filtered[] = $selector; |
| 36 |
} |
| 37 |
} |
| 38 |
$selectors = $rulesetNode->selectors = $filtered ?: null; |
| 39 |
if ( $selectors ) { |
| 40 |
$paths = $rulesetNode->joinSelectors( $context, $selectors ); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
if ( $selectors === null ) { |
| 45 |
$rulesetNode->rules = null; |
| 46 |
} |
| 47 |
|
| 48 |
$rulesetNode->paths = $paths; |
| 49 |
} |
| 50 |
|
| 51 |
// NOTE: Assigned here instead of at the start like less.js, |
| 52 |
// because PHP arrays aren't by-ref |
| 53 |
$this->contexts[] = $paths; |
| 54 |
} |
| 55 |
|
| 56 |
public function visitRulesetOut() { |
| 57 |
array_pop( $this->contexts ); |
| 58 |
} |
| 59 |
|
| 60 |
public function visitMedia( $mediaNode ) { |
| 61 |
$context = end( $this->contexts ); |
| 62 |
|
| 63 |
if ( count( $context ) === 0 || ( $context[0] instanceof Less_Tree_Ruleset && $context[0]->multiMedia ) ) { |
| 64 |
$mediaNode->rules[0]->root = true; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
public function visitAtRule( $atRuleNode ) { |
| 69 |
$context = end( $this->contexts ); |
| 70 |
|
| 71 |
if ( $atRuleNode->rules && count( $atRuleNode->rules ) > 0 ) { |
| 72 |
$atRuleNode->rules[0]->root = $atRuleNode->isRooted || count( $context ) === 0; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
} |