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 4 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
49 changes: 45 additions & 4 deletions Classes/Emogrifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,8 @@ protected function process(\DOMDocument $xmlDocument)
if ($this->isStyleBlocksParsingEnabled) {
$allCss .= $this->getCssFromAllStyleNodes($xPath);
}


$cssImports = $this->extractImportsFromCss($allCss);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please read over all changes and fix the indentation? Thanks!

Copy link
Author

Choose a reason for hiding this comment

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

believe me I m trying to, the git editor not saving it correct

On 9 Sep 2016, at 16:44, Oliver Klee notifications@github.com wrote:

indentation

$cssParts = $this->splitCssAndMediaQuery($allCss);
$excludedNodes = $this->getNodesToExclude($xPath);
$cssRules = $this->parseCssRules($cssParts['css']);
Expand Down Expand Up @@ -394,6 +395,7 @@ protected function process(\DOMDocument $xmlDocument)
$this->removeInvisibleNodes($xPath);
}

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

Expand Down Expand Up @@ -930,6 +932,24 @@ private function copyCssWithMediaToStyleNode(\DOMDocument $xmlDocument, \DOMXPat

$this->addStyleElementToDocument($xmlDocument, implode($mediaQueriesRelevantForDocument));
}

/**
* Applies $css to $xmlDocument, limited to the @import that actually apply to the document.
*
* @param \DOMDocument $xmlDocument the document to match against
* @param \DOMXPath $xPath
* @param string $css a string of CSS
*
* @return void
*/
private function copyImportsToStyleNode(\DOMDocument $xmlDocument, \DOMXPath $xPath, $css)
{
if ($css === '') {
return;
}

$this->addStyleElementToDocument($xmlDocument, $css);
}

/**
* Extracts the media queries from $css while skipping empty media queries.
Expand All @@ -954,6 +974,27 @@ private function extractMediaQueriesFromCss($css)

return $parsedQueries;
}

/**
* Extracts the imports from $css
*
* @param string $css
*
* @return string, css @imports
*/
private function extractImportsFromCss($css)
{
$imports = '';

preg_match_all('/^\\s*@import\\s[^;]+;/misU', $css, $importsMatches, PREG_PATTERN_ORDER);
if( !empty($importsMatches[0]) ){
$importsArray = $importsMatches[0];
$imports = "\n" . implode("\n", array_map('trim', $importsArray)) . "\n";
}

return $imports;

}

/**
* Checks whether there is at least one matching element for $cssSelector.
Expand Down Expand Up @@ -1054,8 +1095,8 @@ private function getOrCreateHeadElement(\DOMDocument $document)
*
* "css" => "h1 { color:red; }"
* "media" => "@media { h1 {}}"
*
* @param string $css
*
* @param string $css
*
* @return string[]
*/
Expand All @@ -1076,7 +1117,7 @@ function ($matches) use (&$media) {
},
$cssWithoutComments
);

// filter the CSS
$search = [
'import directives' => '/^\\s*@import\\s[^;]+;/misU',
Expand Down