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

[BUGFIX] Don’t create empty style attributes #702

Merged
merged 1 commit into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).
([#690](https://github.com/MyIntervals/emogrifier/pull/690))

### Fixed
- Don't create empty `style` attributes for unparsable declarations
([#259](https://github.com/MyIntervals/emogrifier/issues/259),
[#702](https://github.com/MyIntervals/emogrifier/pull/702))
- Allow `:not(:behavioural-pseudo-class)` in selectors
([#697](https://github.com/MyIntervals/emogrifier/pull/697))

Expand Down Expand Up @@ -93,7 +96,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
([#633](https://github.com/MyIntervals/emogrifier/pull/633))
- Add a `getDomDocument()` method
([#630](https://github.com/MyIntervals/emogrifier/pull/630))
- Add a Composer script for PHP CS Fixer
- Add a Composer script for PHP CS Fixer
([#607](https://github.com/MyIntervals/emogrifier/pull/607))
- Copy matching rules with dynamic pseudo-classes or pseudo-elements in
selectors to the style element
Expand Down
6 changes: 5 additions & 1 deletion src/Emogrifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -958,14 +958,18 @@ private function attributeValueIsImportant($attributeValue)
*/
private function copyInlineableCssToStyleAttribute(\DOMElement $node, array $cssRule)
{
$newStyleDeclarations = $this->parseCssDeclarationsBlock($cssRule['declarationsBlock']);
if ($newStyleDeclarations === []) {
return;
}

// if it has a style attribute, get it, process it, and append (overwrite) new stuff
if ($node->hasAttribute('style')) {
// break it up into an associative array
$oldStyleDeclarations = $this->parseCssDeclarationsBlock($node->getAttribute('style'));
} else {
$oldStyleDeclarations = [];
}
$newStyleDeclarations = $this->parseCssDeclarationsBlock($cssRule['declarationsBlock']);
$node->setAttribute(
'style',
$this->generateStyleStringFromDeclarationsArrays($oldStyleDeclarations, $newStyleDeclarations)
Expand Down
6 changes: 5 additions & 1 deletion src/Emogrifier/CssInliner.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,14 +714,18 @@ private function replaceUnmatchableNotComponent(array $matches)
*/
private function copyInlineableCssToStyleAttribute(\DOMElement $node, array $cssRule)
{
$newStyleDeclarations = $this->parseCssDeclarationsBlock($cssRule['declarationsBlock']);
if ($newStyleDeclarations === []) {
return;
}

// if it has a style attribute, get it, process it, and append (overwrite) new stuff
if ($node->hasAttribute('style')) {
// break it up into an associative array
$oldStyleDeclarations = $this->parseCssDeclarationsBlock($node->getAttribute('style'));
} else {
$oldStyleDeclarations = [];
}
$newStyleDeclarations = $this->parseCssDeclarationsBlock($cssRule['declarationsBlock']);
$node->setAttribute(
'style',
$this->generateStyleStringFromDeclarationsArrays($oldStyleDeclarations, $newStyleDeclarations)
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/CssInlinerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ public function inlineCssDropsInvalidCssDeclaration($cssDeclarationBlock)

$subject->inlineCss('html {' . $cssDeclarationBlock . '}');

self::assertContains('<html style="">', $subject->render());
self::assertContains('<html>', $subject->render());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/EmogrifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ public function emogrifyDropsInvalidCssDeclaration($cssDeclarationBlock)

$result = $this->subject->emogrify();

self::assertContains('<html style="">', $result);
self::assertContains('<html>', $result);
}

/**
Expand Down