Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -1151,12 +1151,30 @@ protected function sibling(\DOMNode $node, string $siblingDir = 'nextSibling')

private function parseHtml5(string $htmlContent, string $charset = 'UTF-8'): \DOMDocument
{
return $this->html5Parser->parse($this->convertToHtmlEntities($htmlContent, $charset));
if (!$this->supportsEncoding($charset)) {
$htmlContent = $this->convertToHtmlEntities($htmlContent, $charset);
$charset = 'UTF-8';
}

return $this->html5Parser->parse($htmlContent, ['encoding' => $charset]);
}

private function supportsEncoding(string $encoding): bool
{
try {
return '' === @mb_convert_encoding('', $encoding, 'UTF-8');
} catch (\Throwable $e) {
return false;
}
}

private function parseXhtml(string $htmlContent, string $charset = 'UTF-8'): \DOMDocument
{
$htmlContent = $this->convertToHtmlEntities($htmlContent, $charset);
if ('UTF-8' === $charset && preg_match('//u', $htmlContent)) {
$htmlContent = '<?xml encoding="UTF-8">'.$htmlContent;
} else {
$htmlContent = $this->convertToHtmlEntities($htmlContent, $charset);
}

$internalErrors = libxml_use_internal_errors(true);
if (\LIBXML_VERSION < 20900) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ public function testAddContent()
$crawler = $this->createCrawler();
$crawler->addContent($this->getDoctype().'<html><meta http-equiv="Content-Type" content="text/html; charset=unicode" /><div class="foo"></html></html>');
$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() ignores bad charset');

$crawler = $this->createCrawler();
$crawler->addContent($this->getDoctype().'<html><script>var foo = "bär";</script></html>', 'text/html; charset=UTF-8');
$this->assertEquals('var foo = "bär";', $crawler->filterXPath('//script')->text(), '->addContent() does not interfere with script content');
}

/**
Expand Down