10000 fix #9321 Crawler::addHtmlContent add gbk encoding support · symfony/symfony@acb2df0 · GitHub
[go: up one dir, main page]

Skip to content

Commit acb2df0

Browse files
bronze1manfabpot
authored andcommitted
fix #9321 Crawler::addHtmlContent add gbk encoding support
1 parent 0285bfd commit acb2df0

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/Symfony/Component/DomCrawler/Crawler.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,18 @@ public function addHtmlContent($content, $charset = 'UTF-8')
147147
$dom = new \DOMDocument('1.0', $charset);
148148
$dom->validateOnParse = true;
149149

150-
if (function_exists('mb_convert_encoding') && in_array(strtolower($charset), array_map('strtolower', mb_list_encodings()))) {
151-
$content = mb_convert_encoding($content, 'HTML-ENTITIES', $charset);
150+
if (function_exists('mb_convert_encoding')) {
151+
$has_error = false;
152+
$previous = set_error_handler(function()use(&$has_error){
153+
$has_error = true;
154+
});
155+
$tmpContent = @mb_convert_encoding($content, 'HTML-ENTITIES', $charset);
156+
157+
set_error_handler($previous);
158+
159+
if (!$has_error) {
160+
$content = $tmpContent;
161+
}
152162
}
153163

154164
@$dom->loadHTML($content);

src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ public function testAddHtmlContentUnsupportedCharset()
112112
$this->assertEquals('Žťčýů', $crawler->filterXPath('//p')->text());
113113
}
114114

115+
/**
116+
* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
117+
*/
118+
public function testAddHtmlContentCharsetGbk()
119+
{
120+
$crawler = new Crawler();
121+
//gbk encode of <html><p>中文</p></html>
122+
$crawler->addHtmlContent(base64_decode('PGh0bWw+PHA+1tDOxDwvcD48L2h0bWw+'), 'gbk');
123+
124+
$this->assertEquals('中文', $crawler->filterXPath('//p')->text());
125+
}
126+
115127
/**
116128
* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
117129
*/

0 commit comments

Comments
 (0)
0