qrcode.chapril.org-libreqr/vendor/wikimedia/less.php/lib/Less/Tree/Ruleset.php

731 lines
21 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_Ruleset extends Less_Tree {
protected $lookups;
public $_variables;
public $_rulesets;
public $strictImports;
public $selectors;
public $rules;
public $root;
public $allowImports;
public $paths;
public $firstRoot;
public $type = 'Ruleset';
public $multiMedia;
public $allExtends;
public $ruleset_id;
public $originalRuleset;
public $first_oelements;
public function SetRulesetIndex() {
$this->ruleset_id = Less_Parser::$next_id++;
$this->originalRuleset = $this->ruleset_id;
if ( $this->selectors ) {
foreach ( $this->selectors as $sel ) {
if ( $sel->_oelements ) {
$this->first_oelements[$sel->_oelements[0]] = true;
}
}
}
}
2023-07-07 22:33:10 +02:00
/**
* @param null|Less_Tree_Selector[] $selectors
* @param Less_Tree[] $rules
* @param null|bool $strictImports
*/
2021-02-15 21:29:38 +01:00
public function __construct( $selectors, $rules, $strictImports = null ) {
$this->selectors = $selectors;
$this->rules = $rules;
2023-07-07 22:33:10 +02:00
$this->lookups = [];
2021-02-15 21:29:38 +01:00
$this->strictImports = $strictImports;
$this->SetRulesetIndex();
}
public function accept( $visitor ) {
2023-07-07 22:33:10 +02:00
if ( $this->paths !== null ) {
2021-02-15 21:29:38 +01:00
$paths_len = count( $this->paths );
2023-07-07 22:33:10 +02:00
for ( $i = 0; $i < $paths_len; $i++ ) {
2021-02-15 21:29:38 +01:00
$this->paths[$i] = $visitor->visitArray( $this->paths[$i] );
}
} elseif ( $this->selectors ) {
$this->selectors = $visitor->visitArray( $this->selectors );
}
if ( $this->rules ) {
$this->rules = $visitor->visitArray( $this->rules );
}
}
2023-07-07 22:33:10 +02:00
/**
* @param Less_Environment $env
* @return Less_Tree_Ruleset
* @see less-2.5.3.js#Ruleset.prototype.eval
*/
2021-02-15 21:29:38 +01:00
public function compile( $env ) {
$ruleset = $this->PrepareRuleset( $env );
// Store the frames around mixin definitions,
// so they can be evaluated like closures when the time comes.
$rsRuleCnt = count( $ruleset->rules );
for ( $i = 0; $i < $rsRuleCnt; $i++ ) {
2023-07-07 22:33:10 +02:00
// These checks are the equivalent of the rule.evalFirst property in less.js
2021-02-15 21:29:38 +01:00
if ( $ruleset->rules[$i] instanceof Less_Tree_Mixin_Definition || $ruleset->rules[$i] instanceof Less_Tree_DetachedRuleset ) {
$ruleset->rules[$i] = $ruleset->rules[$i]->compile( $env );
}
}
2023-07-07 22:33:10 +02:00
$mediaBlockCount = count( $env->mediaBlocks );
2021-02-15 21:29:38 +01:00
// Evaluate mixin calls.
$this->EvalMixinCalls( $ruleset, $env, $rsRuleCnt );
// Evaluate everything else
for ( $i = 0; $i < $rsRuleCnt; $i++ ) {
if ( !( $ruleset->rules[$i] instanceof Less_Tree_Mixin_Definition || $ruleset->rules[$i] instanceof Less_Tree_DetachedRuleset ) ) {
$ruleset->rules[$i] = $ruleset->rules[$i]->compile( $env );
}
}
// Evaluate everything else
for ( $i = 0; $i < $rsRuleCnt; $i++ ) {
$rule = $ruleset->rules[$i];
// for rulesets, check if it is a css guard and can be removed
if ( $rule instanceof Less_Tree_Ruleset && $rule->selectors && count( $rule->selectors ) === 1 ) {
// check if it can be folded in (e.g. & where)
if ( $rule->selectors[0]->isJustParentSelector() ) {
array_splice( $ruleset->rules, $i--, 1 );
$rsRuleCnt--;
for ( $j = 0; $j < count( $rule->rules ); $j++ ) {
$subRule = $rule->rules[$j];
if ( !( $subRule instanceof Less_Tree_Rule ) || !$subRule->variable ) {
2023-07-07 22:33:10 +02:00
array_splice( $ruleset->rules, ++$i, 0, [ $subRule ] );
2021-02-15 21:29:38 +01:00
$rsRuleCnt++;
}
}
}
}
}
// Pop the stack
$env->shiftFrame();
if ( $mediaBlockCount ) {
$len = count( $env->mediaBlocks );
for ( $i = $mediaBlockCount; $i < $len; $i++ ) {
$env->mediaBlocks[$i]->bubbleSelectors( $ruleset->selectors );
}
}
return $ruleset;
}
/**
* Compile Less_Tree_Mixin_Call objects
*
* @param Less_Tree_Ruleset $ruleset
2023-07-07 22:33:10 +02:00
* @param int $rsRuleCnt
2021-02-15 21:29:38 +01:00
*/
private function EvalMixinCalls( $ruleset, $env, &$rsRuleCnt ) {
for ( $i = 0; $i < $rsRuleCnt; $i++ ) {
$rule = $ruleset->rules[$i];
if ( $rule instanceof Less_Tree_Mixin_Call ) {
$rule = $rule->compile( $env );
2023-07-07 22:33:10 +02:00
$temp = [];
2021-02-15 21:29:38 +01:00
foreach ( $rule as $r ) {
if ( ( $r instanceof Less_Tree_Rule ) && $r->variable ) {
// do not pollute the scope if the variable is
// already there. consider returning false here
// but we need a way to "return" variable from mixins
if ( !$ruleset->variable( $r->name ) ) {
$temp[] = $r;
}
} else {
$temp[] = $r;
}
}
$temp_count = count( $temp ) - 1;
array_splice( $ruleset->rules, $i, 1, $temp );
$rsRuleCnt += $temp_count;
$i += $temp_count;
$ruleset->resetCache();
} elseif ( $rule instanceof Less_Tree_RulesetCall ) {
$rule = $rule->compile( $env );
2023-07-07 22:33:10 +02:00
$rules = [];
2021-02-15 21:29:38 +01:00
foreach ( $rule->rules as $r ) {
if ( ( $r instanceof Less_Tree_Rule ) && $r->variable ) {
continue;
}
$rules[] = $r;
}
array_splice( $ruleset->rules, $i, 1, $rules );
$temp_count = count( $rules );
$rsRuleCnt += $temp_count - 1;
$i += $temp_count - 1;
$ruleset->resetCache();
}
}
}
/**
* Compile the selectors and create a new ruleset object for the compile() method
*
2023-07-07 22:33:10 +02:00
* @param Less_Environment $env
* @return Less_Tree_Ruleset
2021-02-15 21:29:38 +01:00
*/
private function PrepareRuleset( $env ) {
2023-07-07 22:33:10 +02:00
// NOTE: Preserve distinction between null and empty array when compiling
// $this->selectors to $selectors
$thisSelectors = $this->selectors;
$selectors = null;
2021-02-15 21:29:38 +01:00
$hasOnePassingSelector = false;
2023-07-07 22:33:10 +02:00
if ( $thisSelectors ) {
2021-02-15 21:29:38 +01:00
Less_Tree_DefaultFunc::error( "it is currently only allowed in parametric mixin guards," );
2023-07-07 22:33:10 +02:00
$selectors = [];
foreach ( $thisSelectors as $s ) {
2021-02-15 21:29:38 +01:00
$selector = $s->compile( $env );
$selectors[] = $selector;
if ( $selector->evaldCondition ) {
$hasOnePassingSelector = true;
}
}
Less_Tree_DefaultFunc::reset();
} else {
$hasOnePassingSelector = true;
}
if ( $this->rules && $hasOnePassingSelector ) {
2023-07-07 22:33:10 +02:00
// Copy the array (no need for slice in PHP)
2021-02-15 21:29:38 +01:00
$rules = $this->rules;
} else {
2023-07-07 22:33:10 +02:00
$rules = [];
2021-02-15 21:29:38 +01:00
}
$ruleset = new Less_Tree_Ruleset( $selectors, $rules, $this->strictImports );
$ruleset->originalRuleset = $this->ruleset_id;
$ruleset->root = $this->root;
$ruleset->firstRoot = $this->firstRoot;
$ruleset->allowImports = $this->allowImports;
// push the current ruleset to the frames stack
$env->unshiftFrame( $ruleset );
// Evaluate imports
if ( $ruleset->root || $ruleset->allowImports || !$ruleset->strictImports ) {
$ruleset->evalImports( $env );
}
return $ruleset;
}
function evalImports( $env ) {
$rules_len = count( $this->rules );
for ( $i = 0; $i < $rules_len; $i++ ) {
$rule = $this->rules[$i];
if ( $rule instanceof Less_Tree_Import ) {
$rules = $rule->compile( $env );
if ( is_array( $rules ) ) {
array_splice( $this->rules, $i, 1, $rules );
$temp_count = count( $rules ) - 1;
$i += $temp_count;
$rules_len += $temp_count;
} else {
2023-07-07 22:33:10 +02:00
array_splice( $this->rules, $i, 1, [ $rules ] );
2021-02-15 21:29:38 +01:00
}
$this->resetCache();
}
}
}
function makeImportant() {
2023-07-07 22:33:10 +02:00
$important_rules = [];
2021-02-15 21:29:38 +01:00
foreach ( $this->rules as $rule ) {
if ( $rule instanceof Less_Tree_Rule || $rule instanceof Less_Tree_Ruleset || $rule instanceof Less_Tree_NameValue ) {
$important_rules[] = $rule->makeImportant();
} else {
$important_rules[] = $rule;
}
}
return new Less_Tree_Ruleset( $this->selectors, $important_rules, $this->strictImports );
}
2023-07-07 22:33:10 +02:00
public function matchArgs( $args, $env = null ) {
2021-02-15 21:29:38 +01:00
return !$args;
}
// lets you call a css selector with a guard
public function matchCondition( $args, $env ) {
$lastSelector = end( $this->selectors );
if ( !$lastSelector->evaldCondition ) {
return false;
}
if ( $lastSelector->condition && !$lastSelector->condition->compile( $env->copyEvalEnv( $env->frames ) ) ) {
return false;
}
return true;
}
function resetCache() {
$this->_rulesets = null;
$this->_variables = null;
2023-07-07 22:33:10 +02:00
$this->lookups = [];
2021-02-15 21:29:38 +01:00
}
public function variables() {
2023-07-07 22:33:10 +02:00
$this->_variables = [];
2021-02-15 21:29:38 +01:00
foreach ( $this->rules as $r ) {
if ( $r instanceof Less_Tree_Rule && $r->variable === true ) {
$this->_variables[$r->name] = $r;
}
}
}
2023-07-07 22:33:10 +02:00
/**
* @param string $name
* @return Less_Tree_Rule|null
*/
2021-02-15 21:29:38 +01:00
public function variable( $name ) {
2023-07-07 22:33:10 +02:00
if ( $this->_variables === null ) {
2021-02-15 21:29:38 +01:00
$this->variables();
}
2023-07-07 22:33:10 +02:00
return $this->_variables[$name] ?? null;
2021-02-15 21:29:38 +01:00
}
public function find( $selector, $self = null ) {
$key = implode( ' ', $selector->_oelements );
if ( !isset( $this->lookups[$key] ) ) {
if ( !$self ) {
$self = $this->ruleset_id;
}
2023-07-07 22:33:10 +02:00
$this->lookups[$key] = [];
2021-02-15 21:29:38 +01:00
$first_oelement = $selector->_oelements[0];
foreach ( $this->rules as $rule ) {
if ( $rule instanceof Less_Tree_Ruleset && $rule->ruleset_id != $self ) {
if ( isset( $rule->first_oelements[$first_oelement] ) ) {
foreach ( $rule->selectors as $ruleSelector ) {
$match = $selector->match( $ruleSelector );
if ( $match ) {
if ( $selector->elements_len > $match ) {
$this->lookups[$key] = array_merge( $this->lookups[$key], $rule->find( new Less_Tree_Selector( array_slice( $selector->elements, $match ) ), $self ) );
} else {
$this->lookups[$key][] = $rule;
}
break;
}
}
}
}
}
}
return $this->lookups[$key];
}
/**
* @see Less_Tree::genCSS
*/
public function genCSS( $output ) {
if ( !$this->root ) {
Less_Environment::$tabLevel++;
}
$tabRuleStr = $tabSetStr = '';
if ( !Less_Parser::$options['compress'] ) {
if ( Less_Environment::$tabLevel ) {
2023-07-07 22:33:10 +02:00
$tabRuleStr = "\n" . str_repeat( Less_Parser::$options['indentation'], Less_Environment::$tabLevel );
$tabSetStr = "\n" . str_repeat( Less_Parser::$options['indentation'], Less_Environment::$tabLevel - 1 );
2021-02-15 21:29:38 +01:00
} else {
$tabSetStr = $tabRuleStr = "\n";
}
}
2023-07-07 22:33:10 +02:00
$ruleNodes = [];
$rulesetNodes = [];
2021-02-15 21:29:38 +01:00
foreach ( $this->rules as $rule ) {
$class = get_class( $rule );
2023-07-07 22:33:10 +02:00
if (
( $class === 'Less_Tree_Media' ) ||
( $class === 'Less_Tree_Directive' ) ||
( $this->root && $class === 'Less_Tree_Comment' ) ||
( $rule instanceof Less_Tree_Ruleset && $rule->rules )
) {
2021-02-15 21:29:38 +01:00
$rulesetNodes[] = $rule;
} else {
$ruleNodes[] = $rule;
}
}
// If this is the root node, we don't render
// a selector, or {}.
if ( !$this->root ) {
$paths_len = count( $this->paths );
for ( $i = 0; $i < $paths_len; $i++ ) {
$path = $this->paths[$i];
$firstSelector = true;
foreach ( $path as $p ) {
$p->genCSS( $output, $firstSelector );
$firstSelector = false;
}
if ( $i + 1 < $paths_len ) {
$output->add( ',' . $tabSetStr );
}
}
$output->add( ( Less_Parser::$options['compress'] ? '{' : " {" ) . $tabRuleStr );
}
// Compile rules and rulesets
$ruleNodes_len = count( $ruleNodes );
$rulesetNodes_len = count( $rulesetNodes );
for ( $i = 0; $i < $ruleNodes_len; $i++ ) {
$rule = $ruleNodes[$i];
// @page{ directive ends up with root elements inside it, a mix of rules and rulesets
// In this instance we do not know whether it is the last property
if ( $i + 1 === $ruleNodes_len && ( !$this->root || $rulesetNodes_len === 0 || $this->firstRoot ) ) {
Less_Environment::$lastRule = true;
}
$rule->genCSS( $output );
if ( !Less_Environment::$lastRule ) {
$output->add( $tabRuleStr );
} else {
Less_Environment::$lastRule = false;
}
}
if ( !$this->root ) {
$output->add( $tabSetStr . '}' );
Less_Environment::$tabLevel--;
}
$firstRuleset = true;
$space = ( $this->root ? $tabRuleStr : $tabSetStr );
for ( $i = 0; $i < $rulesetNodes_len; $i++ ) {
if ( $ruleNodes_len && $firstRuleset ) {
$output->add( $space );
} elseif ( !$firstRuleset ) {
$output->add( $space );
}
$firstRuleset = false;
$rulesetNodes[$i]->genCSS( $output );
}
if ( !Less_Parser::$options['compress'] && $this->firstRoot ) {
$output->add( "\n" );
}
}
function markReferenced() {
if ( !$this->selectors ) {
return;
}
foreach ( $this->selectors as $selector ) {
$selector->markReferenced();
}
}
2023-07-07 22:33:10 +02:00
/**
* @param Less_Tree_Selector[][] $context
* @param Less_Tree_Selector[]|null $selectors
* @return Less_Tree_Selector[][]
*/
2021-02-15 21:29:38 +01:00
public function joinSelectors( $context, $selectors ) {
2023-07-07 22:33:10 +02:00
$paths = [];
if ( $selectors !== null ) {
2021-02-15 21:29:38 +01:00
foreach ( $selectors as $selector ) {
$this->joinSelector( $paths, $context, $selector );
}
}
return $paths;
}
2023-07-07 22:33:10 +02:00
public function joinSelector( array &$paths, array $context, Less_Tree_Selector $selector ) {
$newPaths = [];
$hadParentSelector = $this->replaceParentSelector( $newPaths, $context, $selector );
2021-02-15 21:29:38 +01:00
2023-07-07 22:33:10 +02:00
if ( !$hadParentSelector ) {
2021-02-15 21:29:38 +01:00
if ( $context ) {
2023-07-07 22:33:10 +02:00
$newPaths = [];
foreach ( $context as $path ) {
$newPaths[] = array_merge( $path, [ $selector ] );
2021-02-15 21:29:38 +01:00
}
} else {
2023-07-07 22:33:10 +02:00
$newPaths = [ [ $selector ] ];
2021-02-15 21:29:38 +01:00
}
}
2023-07-07 22:33:10 +02:00
foreach ( $newPaths as $newPath ) {
$paths[] = $newPath;
}
}
/**
* Replace all parent selectors inside $inSelector with $context.
*
* @param array &$paths Resulting selectors are appended to $paths.
* @param mixed $context
* @param Less_Tree_Selector $inSelector Inner selector from Less_Tree_Paren
* @return bool True if $inSelector contained at least one parent selector
*/
private function replaceParentSelector( array &$paths, $context, Less_Tree_Selector $inSelector ) {
$hadParentSelector = false;
2021-02-15 21:29:38 +01:00
// The paths are [[Selector]]
// The first list is a list of comma separated selectors
// The inner list is a list of inheritance separated selectors
// e.g.
// .a, .b {
// .c {
// }
// }
// == [[.a] [.c]] [[.b] [.c]]
//
// the elements from the current selector so far
2023-07-07 22:33:10 +02:00
$currentElements = [];
2021-02-15 21:29:38 +01:00
// the current list of new selectors to add to the path.
// We will build it up. We initiate it with one empty selector as we "multiply" the new selectors
// by the parents
2023-07-07 22:33:10 +02:00
$newSelectors = [
[]
];
2021-02-15 21:29:38 +01:00
2023-07-07 22:33:10 +02:00
foreach ( $inSelector->elements as $el ) {
// non-parent reference elements just get added
2021-02-15 21:29:38 +01:00
if ( $el->value !== '&' ) {
2023-07-07 22:33:10 +02:00
$nestedSelector = $this->findNestedSelector( $el );
if ( $nestedSelector !== null ) {
$this->mergeElementsOnToSelectors( $currentElements, $newSelectors );
$nestedPaths = [];
$replacedNewSelectors = [];
$replaced = $this->replaceParentSelector( $nestedPaths, $context, $nestedSelector );
$hadParentSelector = $hadParentSelector || $replaced;
// $nestedPaths is populated by replaceParentSelector()
// $nestedPaths should have exactly one TODO, replaceParentSelector does not multiply selectors
foreach ( $nestedPaths as $nestedPath ) {
$replacementSelector = $this->createSelector( $nestedPath, $el );
// join selector path from $newSelectors with every selector path in $addPaths array.
// $el contains the element that is being replaced by $addPaths
//
// @see less-2.5.3.js#Ruleset-addAllReplacementsIntoPath
$addPaths = [ $replacementSelector ];
foreach ( $newSelectors as $newSelector ) {
$replacedNewSelectors[] = $this->addReplacementIntoPath( $newSelector, $addPaths, $el, $inSelector );
}
}
$newSelectors = $replacedNewSelectors;
$currentElements = [];
} else {
$currentElements[] = $el;
}
2021-02-15 21:29:38 +01:00
} else {
2023-07-07 22:33:10 +02:00
$hadParentSelector = true;
2021-02-15 21:29:38 +01:00
// the new list of selectors to add
2023-07-07 22:33:10 +02:00
$selectorsMultiplied = [];
2021-02-15 21:29:38 +01:00
// merge the current list of non parent selector elements
// on to the current list of selectors to add
2023-07-07 22:33:10 +02:00
$this->mergeElementsOnToSelectors( $currentElements, $newSelectors );
2021-02-15 21:29:38 +01:00
foreach ( $newSelectors as $sel ) {
// if we don't have any parent paths, the & might be in a mixin so that it can be used
// whether there are parents or not
if ( !$context ) {
// the combinator used on el should now be applied to the next element instead so that
// it is not lost
if ( $sel ) {
$sel[0]->elements[] = new Less_Tree_Element( $el->combinator, '', $el->index, $el->currentFileInfo );
}
$selectorsMultiplied[] = $sel;
} else {
// and the parent selectors
foreach ( $context as $parentSel ) {
// We need to put the current selectors
// then join the last selector's elements on to the parents selectors
2023-07-07 22:33:10 +02:00
$newSelectorPath = $this->addReplacementIntoPath( $sel, $parentSel, $el, $inSelector );
2021-02-15 21:29:38 +01:00
// add that to our new set of selectors
$selectorsMultiplied[] = $newSelectorPath;
}
}
}
// our new selectors has been multiplied, so reset the state
$newSelectors = $selectorsMultiplied;
2023-07-07 22:33:10 +02:00
$currentElements = [];
2021-02-15 21:29:38 +01:00
}
}
// if we have any elements left over (e.g. .a& .b == .b)
// add them on to all the current selectors
2023-07-07 22:33:10 +02:00
$this->mergeElementsOnToSelectors( $currentElements, $newSelectors );
foreach ( $newSelectors as &$sel ) {
$length = count( $sel );
if ( $length ) {
$paths[] = $sel;
$lastSelector = $sel[$length - 1];
$sel[$length - 1] = $lastSelector->createDerived( $lastSelector->elements, $inSelector->extendList );
}
2021-02-15 21:29:38 +01:00
}
2023-07-07 22:33:10 +02:00
return $hadParentSelector;
}
/**
* @param array $elementsToPak
* @param Less_Tree_Element $originalElement
* @return Less_Tree_Selector
*/
private function createSelector( array $elementsToPak, $originalElement ) {
if ( !$elementsToPak ) {
// This is an invalid call. Kept to match less.js. Appears unreachable.
// @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal
$containedElement = new Less_Tree_Paren( null );
} else {
$insideParent = [];
foreach ( $elementsToPak as $elToPak ) {
$insideParent[] = new Less_Tree_Element( null, $elToPak, $originalElement->index, $originalElement->currentFileInfo );
2021-02-15 21:29:38 +01:00
}
2023-07-07 22:33:10 +02:00
$containedElement = new Less_Tree_Paren( new Less_Tree_Selector( $insideParent ) );
2021-02-15 21:29:38 +01:00
}
2023-07-07 22:33:10 +02:00
$element = new Less_Tree_Element( null, $containedElement, $originalElement->index, $originalElement->currentFileInfo );
return new Less_Tree_Selector( [ $element ] );
}
/**
* @param Less_Tree_Element $element
* @return Less_Tree_Selector|null
*/
private function findNestedSelector( $element ) {
$maybeParen = $element->value;
if ( !( $maybeParen instanceof Less_Tree_Paren ) ) {
return null;
}
$maybeSelector = $maybeParen->value;
if ( !( $maybeSelector instanceof Less_Tree_Selector ) ) {
return null;
}
return $maybeSelector;
}
/**
* joins selector path from $beginningPath with selector path in $addPath.
*
* $replacedElement contains the element that is being replaced by $addPath
*
* @param Less_Tree_Selector[] $beginningPath
* @param Less_Tree_Selector[] $addPath
* @param Less_Tree_Element $replacedElement
* @param Less_Tree_Selector $originalSelector
* @return Less_Tree_Selector[] Concatenated path
* @see less-2.5.3.js#Ruleset-addReplacementIntoPath
*/
private function addReplacementIntoPath( array $beginningPath, array $addPath, $replacedElement, $originalSelector ) {
// our new selector path
$newSelectorPath = [];
// construct the joined selector - if `&` is the first thing this will be empty,
// if not newJoinedSelector will be the last set of elements in the selector
if ( $beginningPath ) {
// NOTE: less.js uses Array slice() to copy. In PHP, arrays are naturally copied by value.
$newSelectorPath = $beginningPath;
$lastSelector = array_pop( $newSelectorPath );
$newJoinedSelector = $originalSelector->createDerived( $lastSelector->elements );
} else {
$newJoinedSelector = $originalSelector->createDerived( [] );
}
if ( $addPath ) {
// if the & does not have a combinator that is "" or " " then
// and there is a combinator on the parent, then grab that.
// this also allows `+ a { & .b { .a & { ...`
$combinator = $replacedElement->combinator;
$parentEl = $addPath[0]->elements[0];
if ( $replacedElement->combinatorIsEmptyOrWhitespace && !$parentEl->combinatorIsEmptyOrWhitespace ) {
$combinator = $parentEl->combinator;
}
// join the elements so far with the first part of the parent
$newJoinedSelector->elements[] = new Less_Tree_Element( $combinator, $parentEl->value, $replacedElement->index, $replacedElement->currentFileInfo );
$newJoinedSelector->elements = array_merge(
$newJoinedSelector->elements,
array_slice( $addPath[0]->elements, 1 )
);
}
// now add the joined selector - but only if it is not empty
if ( $newJoinedSelector->elements ) {
$newSelectorPath[] = $newJoinedSelector;
}
// put together the parent selectors after the join (e.g. the rest of the parent)
if ( count( $addPath ) > 1 ) {
$newSelectorPath = array_merge( $newSelectorPath, array_slice( $addPath, 1 ) );
}
return $newSelectorPath;
2021-02-15 21:29:38 +01:00
}
function mergeElementsOnToSelectors( $elements, &$selectors ) {
2023-07-07 22:33:10 +02:00
if ( !$elements ) {
return;
}
2021-02-15 21:29:38 +01:00
if ( !$selectors ) {
2023-07-07 22:33:10 +02:00
$selectors[] = [ new Less_Tree_Selector( $elements ) ];
2021-02-15 21:29:38 +01:00
return;
}
foreach ( $selectors as &$sel ) {
// if the previous thing in sel is a parent this needs to join on to it
if ( $sel ) {
$last = count( $sel ) - 1;
$sel[$last] = $sel[$last]->createDerived( array_merge( $sel[$last]->elements, $elements ) );
} else {
$sel[] = new Less_Tree_Selector( $elements );
}
}
}
}