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

Save @import tag #334

Closed
wants to merge 10 commits into from
Closed
Changes from 3 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
17 changes: 13 additions & 4 deletions Classes/Emogrifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ protected function process(\DOMDocument $xmlDocument)
$this->removeInvisibleNodes($xPath);
}

$this->copyCssWithMediaToStyleNode($xmlDocument, $xPath, $cssParts['media']);
$this->copyCssWithMediaToStyleNode($xmlDocument, $xPath, $cssParts['media'], $cssParts['imports']);
}

/**
Expand Down Expand Up @@ -911,13 +911,14 @@ private function attributeValueIsImportant($attributeValue)
*
* @return void
*/
private function copyCssWithMediaToStyleNode(\DOMDocument $xmlDocument, \DOMXPath $xPath, $css)
private function copyCssWithMediaToStyleNode(\DOMDocument $xmlDocument, \DOMXPath $xPath, $css, $imports)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the missing param annotation.

{
if ($css === '') {
if ($css === '' && $imports === '') {
return;
}

$mediaQueriesRelevantForDocument = [];
!$imports ?: $mediaQueriesRelevantForDocument[] = "\n".$imports."\n";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use the Elvis operator for commands. (It should only be used for return values or assignments). Please use an IF instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put spaces around the concatenation dots.


foreach ($this->extractMediaQueriesFromCss($css) as $mediaQuery) {
foreach ($this->parseCssRules($mediaQuery['css']) as $selector) {
Expand Down Expand Up @@ -1077,6 +1078,14 @@ function ($matches) use (&$media) {
$cssWithoutComments
);

//Keep @imports
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is a smell that the additional code should go into a separate method.

$imports = '';
preg_match_all('/^\\s*@import\\s[^;]+;/misU', $css, $importsMatches, PREG_PATTERN_ORDER);
if( !empty($importsMatches[0]) ){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No spaces after the opening parenthesis or before the closing one, please. But there should be a space between the closing parenthesis and the opening curly brace.

$importsArray = $importsMatches[0];
$imports = implode("\n", array_map('trim', $importsArray));
}

// filter the CSS
$search = [
'import directives' => '/^\\s*@import\\s[^;]+;/misU',
Expand All @@ -1085,7 +1094,7 @@ function ($matches) use (&$media) {

$cleanedCss = preg_replace($search, '', $cssForAllowedMediaTypes);

return ['css' => $cleanedCss, 'media' => $media];
return ['css' => $cleanedCss, 'media' => $media, 'imports' => $imports];
}

/**
Expand Down