-
Notifications
You must be signed in to change notification settings - Fork 154
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
Changes from 3 commits
8f2e217
6736f56
3d1ee6f
872b901
a69ee19
5f6dae8
d5bd8ed
e2d8b69
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -225,6 +225,13 @@ class Emogrifier | |
], | ||
]; | ||
|
||
/** | ||
* If true, allow debug Exception to be thrown. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's make this more specific: … Emogrifier will throw Exceptions when it encounters an error instead of silently ignoring them. |
||
* | ||
* @var bool | ||
*/ | ||
private $debug = false; | ||
|
||
/** | ||
* The constructor. | ||
* | ||
|
@@ -1529,18 +1536,32 @@ private function getNodesToExclude(\DOMXPath $xPath) | |
public function handleXpathError($type, $message, $file, $line, array $context) | ||
{ | ||
if ($type === E_WARNING && isset($context['cssRule']['selector'])) { | ||
throw new \InvalidArgumentException( | ||
sprintf( | ||
'%s in selector >> %s << in %s on line %s', | ||
$message, | ||
$context['cssRule']['selector'], | ||
$file, | ||
$line | ||
) | ||
); | ||
if ($this->debug) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need the else here as throwing an exception will exit the method anyway. |
||
throw new \InvalidArgumentException( | ||
sprintf( | ||
'%s in selector >> %s << in %s on line %s', | ||
$message, | ||
$context['cssRule']['selector'], | ||
$file, | ||
$line | ||
) | ||
); | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
// the normal error handling continues when handler return false | ||
return false; | ||
} | ||
|
||
/** | ||
* Set debug mode. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please always use the third person for the first sentence of a method description: [This Method] "Sets the debug mode." |
||
* | ||
* @param bool $debug set to true to enable debug mode | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also add the return void annotation. |
||
public function setDebug($debug) | ||
{ | ||
$this->debug = $debug; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -956,9 +956,26 @@ public function emogrifyForInvalidCssSelectorThrowsException() | |
'<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 emogrifyIgnoreInvalidCssSelectors() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also add another test that tests for the behavior without debug mode. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To test with debug mode enabled I kept the test from pull request #361 "Handling invalid xPath expression warnings" and added the following line to test debug mode : The test emogrifyIgnoreInvalidCssSelectors it the test with debug mode disabled. |
||
{ | ||
$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. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you also please link the PR (i.e., use the same format as for the other changelog entries)?