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] Ignore invalid CSS selectors #194

Merged
merged 1 commit into from
Jul 5, 2015
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
4 changes: 4 additions & 0 deletions Classes/Emogrifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ public function emogrify()
foreach ($this->caches[self::CACHE_KEY_CSS][$cssKey] as $value) {
// query the body for the xpath selector
$nodesMatchingCssSelectors = $xpath->query($this->translateCssToXpath($value['selector']));
// ignore invalid selectors
if ($nodesMatchingCssSelectors === false) {
continue;
}

/** @var \DOMElement $node */
foreach ($nodesMatchingCssSelectors as $node) {
Expand Down
27 changes: 27 additions & 0 deletions Tests/Unit/EmogrifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,33 @@ public function emogrifyRemovesStyleNodes()
);
}

/**
* @test
*/
public function emogrifyIgnoresInvalidCssSelector()
{
$html = $this->html5DocumentType . self::LF
. '<html><style type="text/css">p{color:red;} <style data-x="1">html{cursor:text;}</style></html>';
$this->subject->setHtml($html);

$hasError = false;
set_error_handler(function ($errorNumber, $errorMessage) use (&$hasError) {
if ($errorMessage === 'DOMXPath::query(): Invalid expression') {
return true;
}

$hasError = true;
return true;
});

$this->subject->emogrify();
restore_error_handler();

self::assertFalse(
$hasError
);
}

/**
* Data provider for things that should be left out when applying the CSS.
*
Expand Down