Skip to content

Commit

Permalink
[+]: fix css-inline with "@charset" | thx @mdio
Browse files Browse the repository at this point in the history
  • Loading branch information
voku committed Mar 9, 2016
1 parent d4a5f82 commit 28ae2a9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/CssToInlineStyles.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ class CssToInlineStyles
*/
private static $cssMediaQueriesRegEx = '#@media\\s+(?:only\\s)?(?:[\\s{\\(]|screen|all)\\s?[^{]+{.*}\\s*}\\s*#misU';
/**
* regular expression: css charset
*
* @var string
*/
private static $cssCharsetRegEx = '/@charset [\'"][^\'"]+[\'"];/i';


/**
* regular expression: conditional inline style tags
*
Expand Down Expand Up @@ -103,6 +111,13 @@ class CssToInlineStyles
*/
private $excludeMediaQueries = true;

/**
* Exclude media queries from "$this->css" and keep media queries for inline-styles blocks
*
* @var bool
*/
private $excludeCssCharset = true;

/**
* Creates an instance, you could set the HTML and CSS here, or load it
* later.
Expand Down Expand Up @@ -375,6 +390,11 @@ private function doCleanup($css)
// remove spaces
$css = preg_replace('/\s\s+/', ' ', $css);

// remove css charset
if (true === $this->excludeCssCharset) {
$css = $this->stripeCharsetInCss($css);
}

// remove css media queries
if (true === $this->excludeMediaQueries) {
$css = $this->stripeMediaQueries($css);
Expand All @@ -398,6 +418,18 @@ private function stripeMediaQueries($css)
return (string)preg_replace(self::$cssMediaQueriesRegEx, '', $css);
}

/**
* remove charset from the string
*
* @param $css
*
* @return string
*/
private function stripeCharsetInCss($css)
{
return (string)preg_replace(self::$cssCharsetRegEx, '', $css);
}

/**
* Process the CSS-properties
*
Expand Down Expand Up @@ -836,6 +868,16 @@ public function setExcludeMediaQueries($on = true)
$this->excludeMediaQueries = (bool)$on;
}

/**
* Set exclude charset
*
* @param bool $on
*/
public function setExcludeCssCharset($on = true)
{
$this->excludeCssCharset = (bool)$on;
}

/**
* Set exclude conditional inline-style blocks e.g.: <!--[if gte mso 9]><style>.foo { bar } </style><![endif]-->
*
Expand Down
1 change: 1 addition & 0 deletions tests/CssToInlineStylesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ public function testKeepMediaQuery()
$cssToInlineStyles->setUseInlineStylesBlock(true);
$cssToInlineStyles->setStripOriginalStyleTags(true);
$cssToInlineStyles->setExcludeMediaQueries(true);
$cssToInlineStyles->setExcludeCssCharset(true);
$cssToInlineStyles->setHTML($html);
$cssToInlineStyles->setCSS($css);
$actual = $cssToInlineStyles->convert();
Expand Down
2 changes: 2 additions & 0 deletions tests/test2Css.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@charset "UTF-8";

@media only screen and (max-width: 599px) {
table.body {
position: relative;
Expand Down

0 comments on commit 28ae2a9

Please sign in to comment.