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

Fix InvalidArgumentException #392

Merged
merged 8 commits into from
Jun 30, 2017
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## x.y.z (unreleased)

### Added
- Debug mode. Throw debug exceptions only if debug is active.
([#392](https://github.com/jjriv/emogrifier/pull/392))


### Changed
Expand Down
21 changes: 20 additions & 1 deletion Classes/Emogrifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,13 @@ class Emogrifier
],
];

/**
* Emogrifier will throw Exceptions when it encounters an error instead of silently ignoring them.
*
* @var bool
*/
private $debug = false;

/**
* The constructor.
*
Expand Down Expand Up @@ -1528,7 +1535,7 @@ private function getNodesToExclude(\DOMXPath $xPath)
*/
public function handleXpathError($type, $message, $file, $line, array $context)
{
if ($type === E_WARNING && isset($context['cssRule']['selector'])) {
if ($this->debug && $type === E_WARNING && isset($context['cssRule']['selector'])) {
throw new \InvalidArgumentException(
sprintf(
'%s in selector >> %s << in %s on line %s',
Expand All @@ -1543,4 +1550,16 @@ public function handleXpathError($type, $message, $file, $line, array $context)
// the normal error handling continues when handler return false
return false;
}

/**
* Sets the debug mode.
*
* @param bool $debug set to true to enable debug mode
*
* @return void
*/
public function setDebug($debug)
{
$this->debug = $debug;
}
}
19 changes: 18 additions & 1 deletion Tests/Unit/EmogrifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -950,15 +950,32 @@ public function emogrifyRemovesStyleNodes()
*
* @expectedException \InvalidArgumentException
*/
public function emogrifyForInvalidCssSelectorThrowsException()
public function emogrifyInDebugModeForInvalidCssSelectorThrowsException()
{
$this->subject->setHtml(
'<html><style type="text/css">p{color:red;} <style data-x="1">html{cursor:text;}</style></html>'
);

$this->subject->setDebug(true);
$this->subject->emogrify();
}

/**
* @test
*/
public function emogrifyNotInDebugModeIgnoresInvalidCssSelectors()
{
$html = '<html><style type="text/css">' .
'p{color:red;} <style data-x="1">html{cursor:text;} p{background-color:blue;}</style> ' .
'<body><p></p></body></html>';

$this->subject->setHtml($html);

$html = $this->subject->emogrify();
self::assertContains('color: red', $html);
self::assertContains('background-color: blue', $html);
}

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