From 2c4887135677a26822cd59b9dca5f67cf363fe08 Mon Sep 17 00:00:00 2001 From: JakeQz Date: Thu, 8 Feb 2018 17:42:55 +0000 Subject: [PATCH] [BUGFIX] Allow @charset without error Filter out `@charset` at-rules from the CSS before processing, to avoid an error when presented with CSS containing this at-rule. Obviously this does not mean `@charset` is supported - its value is still ignored - but does address the immediate error in #296. Added PHPUnit test like that for `@import`, and also test that such CSS content does not prevent Emogrifier working correctly on the remainder of the CSS. --- CHANGELOG.md | 2 ++ src/Emogrifier.php | 2 +- tests/Unit/EmogrifierTest.php | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6432a7a0..247f77ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,8 @@ This project adheres to [Semantic Versioning](https://semver.org/). ### Removed ### Fixed +- Allow `@charset` in the CSS without error (note: its value is currently + ignored, [#507](https://github.com/MyIntervals/emogrifier/pull/507)) - Allow attribute selectors in descendants ([#506](https://github.com/MyIntervals/emogrifier/pull/506), [#381](https://github.com/MyIntervals/emogrifier/issues/381)) diff --git a/src/Emogrifier.php b/src/Emogrifier.php index f227ada5..538e0606 100644 --- a/src/Emogrifier.php +++ b/src/Emogrifier.php @@ -1258,7 +1258,7 @@ function ($matches) use (&$media) { // filter the CSS $search = [ - 'import directives' => '/^\\s*@import\\s[^;]+;/misU', + 'import/charset directives' => '/^\\s*@(?:import|charset)\\s[^;]+;/misU', 'remaining media enclosures' => '/^\\s*@media\\s[^{]+{(.*)}\\s*}\\s/misU', ]; diff --git a/tests/Unit/EmogrifierTest.php b/tests/Unit/EmogrifierTest.php index 30d5634a..ff5c3952 100644 --- a/tests/Unit/EmogrifierTest.php +++ b/tests/Unit/EmogrifierTest.php @@ -1054,6 +1054,7 @@ public function unneededCssThingsDataProvider() 'CSS comments with one asterisk' => ['p {color: #000;/* black */}', 'black'], 'CSS comments with two asterisks' => ['p {color: #000;/** black */}', 'black'], '@import directive' => ['@import "foo.css";', '@import'], + '@charset directive' => ['@charset "UTF-8";', '@charset'], 'style in "aural" media type rule' => ['@media aural {p {color: #000;}}', '#000'], 'style in "braille" media type rule' => ['@media braille {p {color: #000;}}', '#000'], 'style in "embossed" media type rule' => ['@media embossed {p {color: #000;}}', '#000'], @@ -1083,6 +1084,23 @@ public function emogrifyFiltersUnneededCssThings($css, $markerNotExpectedInHtml) static::assertNotContains($markerNotExpectedInHtml, $result); } + /** + * @test + * + * @param string $css + * + * @dataProvider unneededCssThingsDataProvider + */ + public function emogrifyUnaffectedByUnneededCssThings($css) + { + $this->subject->setHtml(''); + $this->subject->setCss($css . ' body { color: green; }'); + + $result = $this->subject->emogrify(); + + static::assertContains('', $result); + } + /** * Data provider for media rules. *