diff --git a/Classes/Emogrifier.php b/Classes/Emogrifier.php index 9cff5100..e99a23e9 100644 --- a/Classes/Emogrifier.php +++ b/Classes/Emogrifier.php @@ -1,4 +1,5 @@ \\s*/' => '/', + '/\\s*>\\s*/' => '/', // adjacent sibling - '/\\s+\\+\\s+/' => '/following-sibling::*[1]/self::', + '/\\s+\\+\\s+/' => '/following-sibling::*[1]/self::', // descendant - '/\\s+(?=.*[^\\]]{1}$)/' => '//', + '/\\s+(?=.*[^\\]]{1}$)/' => '//', // :first-child - '/([^\\/]+):first-child/i' => '*[1]/self::\\1', + '/([^\\/]+):first-child/i' => '*[1]/self::\\1', // :last-child - '/([^\\/]+):last-child/i' => '*[last()]/self::\\1', + '/([^\\/]+):last-child/i' => '*[last()]/self::\\1', // attribute only - '/^\\[(\\w+|\\w+\\=[\'"]?\\w+[\'"]?)\\]/' => '*[@\\1]', + '/^\\[(\\w+|\\w+\\=[\'"]?\\w+[\'"]?)\\]/' => '*[@\\1]', // attribute - '/(\\w)\\[(\\w+)\\]/' => '\\1[@\\2]', + '/(\\w)\\[(\\w+)\\]/' => '\\1[@\\2]', // exact attribute '/(\\w)\\[(\\w+)\\=[\'"]?([\\w\\s]+)[\'"]?\\]/' => '\\1[@\\2="\\3"]', // element attribute~= @@ -222,7 +223,7 @@ class Emogrifier * The constructor. * * @param string $html the HTML to emogrify, must be UTF-8-encoded - * @param string $css the CSS to merge, must be UTF-8-encoded + * @param string $css the CSS to merge, must be UTF-8-encoded */ public function __construct($html = '', $css = '') { @@ -242,8 +243,6 @@ public function __destruct() * Sets the HTML to emogrify. * * @param string $html the HTML to emogrify, must be UTF-8-encoded - * - * @return void */ public function setHtml($html) { @@ -254,8 +253,6 @@ public function setHtml($html) * Sets the CSS to merge with the HTML. * * @param string $css the CSS to merge, must be UTF-8-encoded - * - * @return void */ public function setCss($css) { @@ -317,8 +314,6 @@ public function emogrifyBodyContent() * This method places the CSS inline. * * @param \DOMDocument $xmlDocument - * - * @return void */ protected function process(\DOMDocument $xmlDocument) { @@ -351,6 +346,7 @@ protected function process(\DOMDocument $xmlDocument) $allCss .= $this->getCssFromAllStyleNodes($xPath); } + $cssImports = $this->extractImportsFromCss($allCss); $cssParts = $this->splitCssAndMediaQuery($allCss); $excludedNodes = $this->getNodesToExclude($xPath); $cssRules = $this->parseCssRules($cssParts['css']); @@ -394,6 +390,7 @@ protected function process(\DOMDocument $xmlDocument) $this->removeInvisibleNodes($xPath); } + $this->copyImportsToStyleNode($xmlDocument, $xPath, $cssImports); $this->copyCssWithMediaToStyleNode($xmlDocument, $xPath, $cssParts['media']); } @@ -405,8 +402,6 @@ protected function process(\DOMDocument $xmlDocument) * * @param string[] $styles the new CSS styles taken from the global styles to be applied to this node * @param \DOMNode $node node to apply styles to - * - * @return void */ private function mapCssToHtmlAttributes(array $styles, \DOMNode $node) { @@ -422,11 +417,9 @@ private function mapCssToHtmlAttributes(array $styles, \DOMNode $node) * * This method maps a CSS rule to HTML attributes and adds those to the node. * - * @param string $property the name of the CSS property to map - * @param string $value the value of the style rule to map - * @param \DOMNode $node node to apply styles to - * - * @return void + * @param string $property the name of the CSS property to map + * @param string $value the value of the style rule to map + * @param \DOMNode $node node to apply styles to */ private function mapCssToHtmlAttribute($property, $value, \DOMNode $node) { @@ -438,9 +431,9 @@ private function mapCssToHtmlAttribute($property, $value, \DOMNode $node) /** * Looks up the CSS property in the mapping table and maps it if it matches the conditions. * - * @param string $property the name of the CSS property to map - * @param string $value the value of the style rule to map - * @param \DOMNode $node node to apply styles to + * @param string $property the name of the CSS property to map + * @param string $value the value of the style rule to map + * @param \DOMNode $node node to apply styles to * * @return bool true if the property cab be mapped using the simple mapping table */ @@ -465,11 +458,9 @@ private function mapSimpleCssProperty($property, $value, \DOMNode $node) /** * Maps CSS properties that need special transformation to an HTML attribute. * - * @param string $property the name of the CSS property to map - * @param string $value the value of the style rule to map - * @param \DOMNode $node node to apply styles to - * - * @return void + * @param string $property the name of the CSS property to map + * @param string $value the value of the style rule to map + * @param \DOMNode $node node to apply styles to */ private function mapComplexCssProperty($property, $value, \DOMNode $node) { @@ -515,7 +506,7 @@ private function mapComplexCssProperty($property, $value, \DOMNode $node) } /** - * Parses a shorthand CSS value and splits it into individual values + * Parses a shorthand CSS value and splits it into individual values. * * @param string $value a string of CSS value with 1, 2, 3 or 4 sizes * For example: padding: 0 auto; @@ -543,10 +534,10 @@ private function parseCssShorthandValue($value) * @param string $css a string of raw CSS code * * @return string[][] an array of string sub-arrays with the keys - * "selector" (the CSS selector(s), e.g., "*" or "h1"), - * "declarationsBLock" (the semicolon-separated CSS declarations for that selector(s), - * e.g., "color: red; height: 4px;"), - * and "line" (the line number e.g. 42) + * "selector" (the CSS selector(s), e.g., "*" or "h1"), + * "declarationsBLock" (the semicolon-separated CSS declarations for that selector(s), + * e.g., "color: red; height: 4px;"), + * and "line" (the line number e.g. 42) */ private function parseCssRules($css) { @@ -568,8 +559,8 @@ private function parseCssRules($css) // don't process pseudo-elements and behavioral (dynamic) pseudo-classes; // only allow structural pseudo-classes $hasPseudoElement = strpos($selector, '::') !== false; - $hasAnyPseudoClass = (bool)preg_match('/:[a-zA-Z]/', $selector); - $hasSupportedPseudoClass = (bool)preg_match('/:\\S+\\-(child|type\\()/i', $selector); + $hasAnyPseudoClass = (bool) preg_match('/:[a-zA-Z]/', $selector); + $hasSupportedPseudoClass = (bool) preg_match('/:\\S+\\-(child|type\\()/i', $selector); if ($hasPseudoElement || ($hasAnyPseudoClass && !$hasSupportedPseudoClass)) { continue; } @@ -593,8 +584,6 @@ private function parseCssRules($css) /** * Disables the parsing of inline styles. - * - * @return void */ public function disableInlineStyleAttributesParsing() { @@ -603,8 +592,6 @@ public function disableInlineStyleAttributesParsing() /** * Disables the parsing of