Skip to content

Commit

Permalink
Misc code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
robocoder committed May 2, 2019
1 parent cca055c commit 71720fa
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 35 deletions.
1 change: 1 addition & 0 deletions src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Block
* @var array
*/
public $children;

/**
* @var \Leafo\ScssPhp\Block
*/
Expand Down
67 changes: 55 additions & 12 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ protected function matchExtends($selector, &$out, $from = 0, $initial = true)

for ($l = count($tempReplacement) - 1; $l >= 0; $l--) {
$slice = [];

foreach ($tempReplacement[$l] as $chunk) {
if (!in_array($chunk, $slice)) {
$slice[] = $chunk;
Expand Down Expand Up @@ -610,7 +611,6 @@ protected function matchExtendsSingle($rawSingle, &$outOrigin)
return $found;
}


/**
* Extract a relationship from the fragment.
*
Expand All @@ -620,6 +620,7 @@ protected function matchExtendsSingle($rawSingle, &$outOrigin)
* the rest.
*
* @param array $fragment The selector fragment maybe ending with a direction relationship combinator.
*
* @return array The selector without the relationship fragment if any, the relationship fragment.
*/
protected function extractRelationshipFromFragment(array $fragment)
Expand Down Expand Up @@ -804,6 +805,7 @@ protected function compileAtRoot(Block $block)
}

$selfParent = $block->selfParent;

if (! $block->selfParent->selectors && isset($block->parent) && $block->parent && isset($block->parent->selectors) && $block->parent->selectors) {
$selfParent = $block->parent;
}
Expand All @@ -825,8 +827,10 @@ protected function compileAtRoot(Block $block)

/**
* Filter at-root scope depending of with/without option
* @param $scope
* @param $without
*
* @param \Leafo\ScssPhp\Formatter\OutputBlock $scope
* @param mixed $without
*
* @return mixed
*/
protected function filterScopeWithout($scope, $without)
Expand All @@ -846,14 +850,17 @@ protected function filterScopeWithout($scope, $without)
if (! $scope) {
break;
}

if (! $this->isWithout($without, $scope)) {
$s = clone $scope;
$s->children = [];
$s->lines = [];
$s->parent = null;

if ($s->type !== Type::T_MEDIA && $s->type !== Type::T_DIRECTIVE) {
$s->selectors = [];
}

$filteredScopes[] = $s;
}

Expand All @@ -863,15 +870,18 @@ protected function filterScopeWithout($scope, $without)
$scope = null;
}
}

if (!count($filteredScopes)) {
return $this->rootBlock;
}

$newScope = array_shift($filteredScopes);
$newScope->parent = $this->rootBlock;

$this->rootBlock->children[] = $newScope;

$p = &$newScope;

while (count($filteredScopes)) {
$s = array_shift($filteredScopes);
$s->parent = $p;
Expand All @@ -885,15 +895,18 @@ protected function filterScopeWithout($scope, $without)
/**
* found missing selector from a at-root compilation in the previous scope
* (if at-root is just enclosing a property, the selector is in the parent tree)
* @param $scope
* @param $previousScope
*
* @param \Leafo\ScssPhp\Formatter\OutputBlock $scope
* @param \Leafo\ScssPhp\Formatter\OutputBlock $previousScope
*
* @return mixed
*/
protected function completeScope($scope, $previousScope)
{
if (! $scope->type && (! $scope->selectors || ! count($scope->selectors)) && count($scope->lines)) {
$scope->selectors = $this->findScopeSelectors($previousScope, $scope->depth);
}

if ($scope->children) {
foreach ($scope->children as $k => $c) {
$scope->children[$k] = $this->completeScope($c, $previousScope);
Expand All @@ -905,22 +918,26 @@ protected function completeScope($scope, $previousScope)

/**
* Find a selector by the depth node in the scope
* @param $scope
* @param $depth
*
* @param \Leafo\ScssPhp\Formatter\OutputBlock $scope
* @param integer $depth
*
* @return array
*/
protected function findScopeSelectors($scope, $depth)
{
if ($scope->depth === $depth && $scope->selectors) {
return $scope->selectors;
}

if ($scope->children) {
foreach (array_reverse($scope->children) as $c) {
if ($s = $this->findScopeSelectors($c, $depth)) {
return $s;
}
}
}

return [];
}

Expand Down Expand Up @@ -1016,11 +1033,13 @@ protected function isWithout($without, $block)
if (isset($block->name) && $block->name === 'supports') {
return ($without & static::WITH_SUPPORTS) ? true : false;
}

if (isset($block->selectors) && strpos(serialize($block->selectors), '@supports') !== false) {
return ($without & static::WITH_SUPPORTS) ? true : false;
}
}
}

if ((($without & static::WITH_RULE) && isset($block->selectors))) {
return true;
}
Expand Down Expand Up @@ -1147,13 +1166,17 @@ protected function compileBlock(Block $block)

if (count($block->children)) {
$out->selectors = $this->multiplySelectors($env, $block->selfParent);

// propagate selfParent to the children where they still can be useful
$selfParentSelectors = null;

if (isset($block->selfParent->selectors)) {
$selfParentSelectors = $block->selfParent->selectors;
$block->selfParent->selectors = $out->selectors;
}

$this->compileChildrenNoReturn($block->children, $out, $block->selfParent);

// and revert for the following childs of the same block
if ($selfParentSelectors) {
$block->selfParent->selectors = $selfParentSelectors;
Expand All @@ -1174,6 +1197,7 @@ protected function compileComment($block)
{
$out = $this->makeOutputBlock(Type::T_COMMENT);
$out->lines[] = $block[1];

$this->scope->children[] = $out;
}

Expand Down Expand Up @@ -1406,6 +1430,7 @@ protected function compileChildren($stms, OutputBlock $out)
return $ret;
}
}

return null;
}

Expand All @@ -1420,14 +1445,12 @@ protected function compileChildren($stms, OutputBlock $out)
*/
protected function compileChildrenNoReturn($stms, OutputBlock $out, $selfParent = null)
{

foreach ($stms as $stm) {
if ($selfParent && isset($stm[1]) && is_object($stm[1]) && get_class($stm[1]) == 'Leafo\ScssPhp\Block') {
$stm[1]->selfParent = $selfParent;
$ret = $this->compileChild($stm, $out);
$stm[1]->selfParent = null;
}
elseif ($selfParent && $stm[0] === TYPE::T_INCLUDE) {
} elseif ($selfParent && $stm[0] === TYPE::T_INCLUDE) {
$stm['selfParent'] = $selfParent;
$ret = $this->compileChild($stm, $out);
unset($stm['selfParent']);
Expand Down Expand Up @@ -1681,6 +1704,7 @@ protected function compileChild($child, OutputBlock $out)
} elseif (! empty($out->sourceLine) and ! empty($out->sourceName)) {
$this->sourceLine = $out->sourceLine;
$this->sourceIndex = array_search($out->sourceName, $this->sourceNames);

if ($this->sourceIndex === false) {
$this->sourceIndex = null;
}
Expand Down Expand Up @@ -1761,6 +1785,7 @@ protected function compileChild($child, OutputBlock $out)
}

$fontValue=&$value;

if ($value[0] === Type::T_LIST && $value[1]==',') {
// this is the case if more than one font is given: example: "font: 400 1em/1.3 arial,helvetica"
// we need to handle the first list element
Expand Down Expand Up @@ -1992,14 +2017,16 @@ protected function compileChild($child, OutputBlock $out)
// Find the parent selectors in the env to be able to know what '&' refers to in the mixin
// and assign this fake parent to childs
$selfParent = null;

if (isset($child['selfParent']) && isset($child['selfParent']->selectors)) {
$selfParent = $child['selfParent'];
}
else {
} else {
$parentSelectors = $this->multiplySelectors($this->env);

if ($parentSelectors) {
$parent = new Block();
$parent->selectors = $parentSelectors;

foreach ($mixin->children as $k => $child) {
if (isset($child[1]) && is_object($child[1]) && get_class($child[1]) == 'Leafo\ScssPhp\Block') {
$mixin->children[$k][1]->parent = $parent;
Expand Down Expand Up @@ -2344,6 +2371,7 @@ protected function reduce($value, $inExp = false)
case Type::T_SELF:
$selfSelector = $this->multiplySelectors($this->env);
$selfSelector = $this->collapseSelectors($selfSelector);

return [Type::T_STRING, '', [$selfSelector]];

default:
Expand Down Expand Up @@ -2532,6 +2560,7 @@ protected function opAdd($left, $right)

return $strRight;
}

return null;
}

Expand Down Expand Up @@ -3049,6 +3078,7 @@ protected function multiplySelectors(Environment $env, $selfParent = null)
$parentSelectors = [[]];

$selfParentSelectors = null;

if (!is_null($selfParent) and $selfParent->selectors) {
$selfParentSelectors = $this->evalSelectors($selfParent->selectors);
}
Expand All @@ -3064,12 +3094,15 @@ protected function multiplySelectors(Environment $env, $selfParent = null)
foreach ($parentSelectors as $parent) {
if ($selfParentSelectors) {
$previous = null;

foreach ($selfParentSelectors as $selfParent) {
// if no '&' in the selector, each call will give same result, only add once
$s = $this->joinSelectors($parent, $selector, $selfParent);

if ($s !== $previous) {
$selectors[serialize($s)] = $s;
}

$previous = $s;
}
} else {
Expand All @@ -3083,6 +3116,7 @@ protected function multiplySelectors(Environment $env, $selfParent = null)
}

$selectors = array_values($selectors);

return $selectors;
}

Expand All @@ -3092,6 +3126,7 @@ protected function multiplySelectors(Environment $env, $selfParent = null)
* @param array $parent
* @param array $child
* @param array $selfParentSelectors
* @return array
*/
protected function joinSelectors($parent, $child, $selfParentSelectors = null)
Expand All @@ -3105,9 +3140,11 @@ protected function joinSelectors($parent, $child, $selfParentSelectors = null)
foreach ($part as $p) {
if ($p === static::$selfSelector) {
$setSelf = true;

if (is_null($selfParentSelectors)) {
$selfParentSelectors = $parent;
}

foreach ($selfParentSelectors as $i => $parentPart) {
if ($i > 0) {
$out[] = $newPart;
Expand Down Expand Up @@ -3305,6 +3342,7 @@ protected function setExisting($name, $value, Environment $env, $valueUnreduced
}

$env->store[$name] = $value;

if ($valueUnreduced) {
$env->storeUnreduced[$name] = $valueUnreduced;
}
Expand All @@ -3321,6 +3359,7 @@ protected function setExisting($name, $value, Environment $env, $valueUnreduced
protected function setRaw($name, $value, Environment $env, $valueUnreduced = null)
{
$env->store[$name] = $value;

if ($valueUnreduced) {
$env->storeUnreduced[$name] = $valueUnreduced;
}
Expand Down Expand Up @@ -3355,6 +3394,7 @@ public function get($name, $shouldThrow = true, Environment $env = null, $unredu
if ($unreduced && isset($env->storeUnreduced[$normalizedName])) {
return $env->storeUnreduced[$normalizedName];
}

return $env->store[$normalizedName];
}

Expand Down Expand Up @@ -3712,6 +3752,7 @@ public function setEncoding($encoding)
public function setIgnoreErrors($ignoreErrors)
{
$this->ignoreErrors = $ignoreErrors;

return $this;
}

Expand All @@ -3737,9 +3778,11 @@ public function throwError($msg)
$line = $this->sourceLine;
$loc = isset($this->sourceNames[$this->sourceIndex]) ? $this->sourceNames[$this->sourceIndex] . " on line $line" : "line: $line";
$msg = "$msg: $loc";

if ($this->callStack) {
$msg .= "\nCall Stack:\n";
$ncall = 0;

foreach (array_reverse($this->callStack) as $call) {
$msg .= "#" . $ncall++ . " " . $call['n'] . " ";
$msg .= (isset($this->sourceNames[$call[Parser::SOURCE_INDEX]]) ? $this->sourceNames[$call[Parser::SOURCE_INDEX]] : '(unknown file)');
Expand Down
Loading

0 comments on commit 71720fa

Please sign in to comment.