Skip to content

Commit

Permalink
bug #36906 [DomCrawler] Catch expected ValueError (derrabus)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.4 branch.

Discussion
----------

[DomCrawler] Catch expected ValueError

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | #36872
| License       | MIT
| Doc PR        | N/A

`mb_convert_encoding()` raises a `ValueError` on php 8 if an unknown character set is passed. This causes trouble with the whole test suite because the temporary error handler is not deregistered in that case.

Since the previously raised warning was actively ignored by that error handler, I'm now ignoring the `ValueError` as well. Also, I've wrapped the `restore_error_handler()` call into a `finally` statement to make the whole construct a bit more robust.

Commits
-------

b1db137 [DomCrawler] Catch expected ValueError.
  • Loading branch information
nicolas-grekas committed May 23, 2020
2 parents e220e7c + b1db137 commit 475a715
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Symfony/Component/DomCrawler/Crawler.php
Expand Up @@ -193,10 +193,11 @@ public function addHtmlContent($content, $charset = 'UTF-8')
// Convert charset to HTML-entities to work around bugs in DOMDocument::loadHTML()
$content = mb_convert_encoding($content, 'HTML-ENTITIES', $charset);
} catch (\Exception $e) {
} catch (\ValueError $e) {
} finally {
restore_error_handler();
}

restore_error_handler();

if ('' !== trim($content)) {
@$dom->loadHTML($content);
}
Expand Down

0 comments on commit 475a715

Please sign in to comment.