Skip to content

Commit

Permalink
Remove CSS optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
jkphl committed Apr 14, 2018
1 parent 165038f commit ea3f041
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 74 deletions.
59 changes: 2 additions & 57 deletions src/Respimgcss/Infrastructure/CssRulesSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,67 +94,12 @@ public function toCss(string $selector): string

$cssDocument = new Document();

// Run through and export all rules
foreach ($this->exportCssRules($selector) as $rule) {
$cssDocument->append($rule);
}

return $cssDocument->render();
}

/**
* Export all registered CSS rules
*
* @param string $selector CSS selector
*
* @return Renderable[] Exported CSS rules
*/
protected function exportCssRules(string $selector): array
{
$rulesByImageCandidate = [];

/** @var CssRuleInterface $rule */
foreach ($this->rules as $rule) {
$ruleImageCandidate = $rule->getImageCandidate()->getFile();
if (!array_key_exists($ruleImageCandidate, $rulesByImageCandidate)) {
$rulesByImageCandidate[$ruleImageCandidate] = [];
}
$rulesByImageCandidate[$ruleImageCandidate][] = $this->exportCssRule($rule, $selector);
$cssDocument->append($this->exportCssRule($rule, $selector));
}

// Return aggregated rules
return array_map([$this, 'optimizeCssRulesByImageCandidate'], $rulesByImageCandidate);
}

/**
* Optimize a list of CSS rules by aggregating for @media blocks
*
* @param Renderable[] $rulesByImageCandidate CSS rules
*
* @return Renderable Optimized CSS rules
*/
protected function optimizeCssRulesByImageCandidate(array $rulesByImageCandidate): Renderable
{
// If there's only one ruleset: Return
if (count($rulesByImageCandidate) === 1) {
return $rulesByImageCandidate[0];
}

// Collect all @media conditions
$atRuleConditions = array_map(
function (Renderable $renderable) {
return ($renderable instanceof AtRuleBlockList) ? $renderable->atRuleArgs() : 'screen';
},
$rulesByImageCandidate
);

$renderable = array_pop($rulesByImageCandidate);
$atRuleBlockList = new AtRuleBlockList('media', implode(',', $atRuleConditions));
($renderable instanceof AtRuleBlockList) ?
$atRuleBlockList->setContents($renderable->getContents()) :
$atRuleBlockList->append($renderable);

return $atRuleBlockList;
return $cssDocument->render();
}

/**
Expand Down
17 changes: 0 additions & 17 deletions src/Respimgcss/Infrastructure/ViewportFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,6 @@ public function __construct(StringHelperInterface $stringHelper, AbsoluteLengthI
{
parent::__construct($stringHelper);
$this->viewport = $viewport;
// $this->name = rand();
// echo 'init '.$this->name;
// try {
// throw new \Exception;
// } catch (\Exception $e) {
// echo $e->getMessage().PHP_EOL.$e->getTraceAsString().PHP_EOL;
// }
// echo PHP_EOL.PHP_EOL;
// print_r($this->viewport);
}

/**
Expand All @@ -91,14 +82,6 @@ public function __construct(StringHelperInterface $stringHelper, AbsoluteLengthI
*/
public function execute(array $arguments)
{
// echo 'exec '.$this->name;
// try {
// throw new \Exception;
// } catch (\Exception $e) {
// echo $e->getMessage().PHP_EOL.$e->getTraceAsString().PHP_EOL;
// }
// print_r($this->viewport);
// echo PHP_EOL.PHP_EOL;
return $this->viewport->getValue();
}
}

0 comments on commit ea3f041

Please sign in to comment.