Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 53cb6ad

Browse filesBrowse files
committed
[DomCrawler]fix #9321 Crawler::addHtmlContent add gbk encoding support
1 parent 0080399 commit 53cb6ad
Copy full SHA for 53cb6ad

File tree

Expand file treeCollapse file tree

2 files changed

+24
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+24
-2
lines changed

‎src/Symfony/Component/DomCrawler/Crawler.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DomCrawler/Crawler.php
+12-2Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,18 @@ public function addHtmlContent($content, $charset = 'UTF-8')
143143
$dom = new \DOMDocument('1.0', $charset);
144144
$dom->validateOnParse = true;
145145

146-
if (function_exists('mb_convert_encoding') && in_array(strtolower($charset), array_map('strtolower', mb_list_encodings()))) {
147-
$content = mb_convert_encoding($content, 'HTML-ENTITIES', $charset);
146+
if (function_exists('mb_convert_encoding')) {
147+
$has_error = false;
148+
$previous = set_error_handler(function()use(&$has_error){
149+
$has_error = true;
150+
});
151+
$tmpContent = @mb_convert_encoding($content, 'HTML-ENTITIES', $charset);
152+
153+
set_error_handler($previous);
154+
155+
if (!$has_error) {
156+
$content = $tmpContent;
157+
}
148158
}
149159

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

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ public function testAddHtmlContentUnsupportedCharset()
103103
$this->assertEquals('Žťčýů', $crawler->filterXPath('//p')->text());
104104
}
105105

106+
/**
107+
* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
108+
*/
109+
public function testAddHtmlContentCharsetGbk()
110+
{
111+
$crawler = new Crawler();
112+
//gbk encode of <html><p>中文</p></html>
113+
$crawler->addHtmlContent(base64_decode('PGh0bWw+PHA+1tDOxDwvcD48L2h0bWw+'), 'gbk');
114+
115+
$this->assertEquals('中文', $crawler->filterXPath('//p')->text());
116+
}
117+
106118
/**
107119
* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
108120
*/

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.