Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Split copyUninlinableCssToStyleNode() #706

Merged
merged 3 commits into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions src/Emogrifier/CssInliner.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ class CssInliner extends AbstractHtmlProcessor
'(?:(?<![="\':\\w\\-])|::)' => 1,
];

/**
* array of data describing CSS rules which apply to the document but cannot be inlined, in the format returned by
* `parseCssRules`
*
* @var string[][]
*/
private $matchingUninlinableCssRules = [];

/**
* Emogrifier will throw Exceptions when it encounters an error instead of silently ignoring them.
*
Expand Down Expand Up @@ -210,7 +218,8 @@ public function inlineCss($css = '')

$this->removeImportantAnnotationFromAllInlineStyles();

$this->copyUninlinableCssToStyleNode($cssRules['uninlinable']);
$this->determineMatchingUninlinableCssRules($cssRules['uninlinable']);
$this->copyUninlinableCssToStyleNode();

return $this;
}
Expand Down Expand Up @@ -618,15 +627,16 @@ private function attributeValueIsImportant($attributeValue)
}

/**
* Applies $cssRules to $this->domDocument, limited to the rules that actually apply to the document.
* Determines which of `$cssRules` actually apply to `$this->domDocument`, and sets them in
* `$this->matchingUninlinableCssRules`.
*
* @param string[][] $cssRules The "uninlinable" array of CSS rules returned by `parseCssRules`
* @param string[][] $cssRules the "uninlinable" array of CSS rules returned by `parseCssRules`
*
* @return void
*/
private function copyUninlinableCssToStyleNode(array $cssRules)
private function determineMatchingUninlinableCssRules(array $cssRules)
{
$cssRulesRelevantForDocument = \array_filter(
$this->matchingUninlinableCssRules = \array_filter(
$cssRules,
function (array $cssRule) {
$selector = $cssRule['selector'];
Expand All @@ -636,14 +646,23 @@ function (array $cssRule) {
return $this->existsMatchForCssSelector($selector);
}
);
}

if ($cssRulesRelevantForDocument === []) {
/**
* Applies `$this->matchingUninlinableCssRules` to `$this->domDocument` by placing them as CSS in a `<style>`
* element.
*
* @return void
*/
private function copyUninlinableCssToStyleNode()
{
if ($this->matchingUninlinableCssRules === []) {
// avoid adding empty style element (or including unneeded class dependency)
return;
}

$cssConcatenator = new CssConcatenator();
foreach ($cssRulesRelevantForDocument as $cssRule) {
foreach ($this->matchingUninlinableCssRules as $cssRule) {
$cssConcatenator->append([$cssRule['selector']], $cssRule['declarationsBlock'], $cssRule['media']);
}

Expand Down
10 changes: 8 additions & 2 deletions tests/Unit/CssInlinerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2641,19 +2641,25 @@ public function copyUninlinableCssToStyleNodeHasNoSideEffects()
'line' => 1,
],
];
$matchingUninlinableCssRulesProperty = new \ReflectionProperty(
CssInliner::class,
'matchingUninlinableCssRules'
);
$matchingUninlinableCssRulesProperty->setAccessible(true);
$matchingUninlinableCssRulesProperty->setValue($subject, $uninlinableCssRules);

$copyUninlinableCssToStyleNode = new \ReflectionMethod(CssInliner::class, 'copyUninlinableCssToStyleNode');
$copyUninlinableCssToStyleNode->setAccessible(true);

$domDocument = $subject->getDomDocument();

$copyUninlinableCssToStyleNode->invoke($subject, $uninlinableCssRules);
$copyUninlinableCssToStyleNode->invoke($subject);
$expectedHtml = $subject->render();

$styleElement = $domDocument->getElementsByTagName('style')->item(0);
$styleElement->parentNode->removeChild($styleElement);

$copyUninlinableCssToStyleNode->invoke($subject, $uninlinableCssRules);
$copyUninlinableCssToStyleNode->invoke($subject);

self::assertSame($expectedHtml, $subject->render());
}
Expand Down