bug #9323 [DomCrawler]fix #9321 Crawler::addHtmlContent add gbk encoding support (bronze1man)

This PR was submitted for the 2.2 branch but it was merged into the 2.3 branch instead (closes #9323).

Discussion
----------

[DomCrawler]fix #9321 Crawler::addHtmlContent add gbk encoding support

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |  #9321
| License       | MIT
| Doc PR        | n/a

This is solution 1 in https://github.com/symfony/symfony/issues/9321#issuecomment-26508649

Commits
-------

30af288 fix some cs
9f20b24 use restore_error_handler instead of set_error_handler($previous)
53cb6ad [DomCrawler]fix #9321 Crawler::addHtmlContent add gbk encoding support
This commit is contained in:
Fabien Potencier 2013-12-29 19:30:30 +01:00
commit e0126d9d39
2 changed files with 24 additions and 2 deletions

View File

@ -147,8 +147,18 @@ class Crawler extends \SplObjectStorage
$dom = new \DOMDocument('1.0', $charset);
$dom->validateOnParse = true;
if (function_exists('mb_convert_encoding') && in_array(strtolower($charset), array_map('strtolower', mb_list_encodings()))) {
$content = mb_convert_encoding($content, 'HTML-ENTITIES', $charset);
if (function_exists('mb_convert_encoding')) {
$hasError = false;
set_error_handler(function () use (&$hasError) {
$hasError = true;
});
$tmpContent = @mb_convert_encoding($content, 'HTML-ENTITIES', $charset);
restore_error_handler();
if (!$hasError) {
$content = $tmpContent;
}
}
@$dom->loadHTML($content);

View File

@ -112,6 +112,18 @@ class CrawlerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('Žťčýů', $crawler->filterXPath('//p')->text());
}
/**
* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
*/
public function testAddHtmlContentCharsetGbk()
{
$crawler = new Crawler();
//gbk encode of <html><p>中文</p></html>
$crawler->addHtmlContent(base64_decode('PGh0bWw+PHA+1tDOxDwvcD48L2h0bWw+'), 'gbk');
$this->assertEquals('中文', $crawler->filterXPath('//p')->text());
}
/**
* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
*/