From 36d0dc73f4439b0b65c38c2bbf349dad1d13297b Mon Sep 17 00:00:00 2001 From: Erik Hansen Date: Sat, 9 May 2015 14:38:23 -0500 Subject: [PATCH] [FEATURE] Allow disabling of "inline style" and "style block" parsing * Added two new methods: disableInlineStylesParsing and disableStyleBlocksParsing * Added unit tests to cover the two new options * Updated documentation to cover new options * Fixes #155 --- Classes/Emogrifier.php | 124 ++++++++++++++++++++++++++-------- README.md | 16 +++++ Tests/Unit/EmogrifierTest.php | 73 ++++++++++++++++++++ 3 files changed, 186 insertions(+), 27 deletions(-) diff --git a/Classes/Emogrifier.php b/Classes/Emogrifier.php index 88afc3d2..479dfbde 100644 --- a/Classes/Emogrifier.php +++ b/Classes/Emogrifier.php @@ -101,6 +101,26 @@ class Emogrifier */ private $styleAttributesForNodes = array(); + /** + * Determines whether the "style" attributes of tags in the the HTML passed to this class should be preserved. + * If set to false, the value of the style attributes will be discarded. + * + * @var bool + */ + private $isInlineStyleAttributesParsingEnabled = true; + + /** + * Determines whether the '; + $this->subject->setHtml($html); + $this->subject->disableStyleBlocksParsing(); + + $this->assertNotContains( + '', + $this->subject->emogrify() + ); + } + + /** + * @test + */ + public function emogrifyWhenStyleBlocksParsingDisabledKeepInlineStyles() + { + $styleAttributeValue = 'text-align: center;'; + $html = $this->html5DocumentType . self::LF . + '' + . '

paragraph

'; + $expected = '

'; + $this->subject->setHtml($html); + $this->subject->disableStyleBlocksParsing(); + + $this->assertContains( + $expected, + $this->subject->emogrify() + ); + } + + /** + * @test + */ + public function emogrifyWhenDisabledNotAppliesCssFromInlineStyles() + { + $styleAttributeValue = 'color: #ccc;'; + $html = $this->html5DocumentType . self::LF . + ''; + $expected = ''; + $this->subject->setHtml($html); + $this->subject->disableInlineStyleAttributesParsing(); + + $this->assertContains( + $expected, + $this->subject->emogrify() + ); + } + + /** + * @test + */ + public function emogrifyWhenInlineStyleAttributesParsingDisabledKeepStyleBlockStyles() + { + $styleAttributeValue = 'color: #ccc;'; + $html = $this->html5DocumentType . self::LF . + '' + . '

paragraph

'; + $expected = '

'; + $this->subject->setHtml($html); + $this->subject->disableInlineStyleAttributesParsing(); + + $this->assertContains( + $expected, + $this->subject->emogrify() + ); + } + /** * @test */