-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When the new PHP 8.4 DOM API is used, and HTML declared as UTF-8 contains non UTF-8 compatible characters, it does not replace them with a � character, but instead removes it. This behaviour is consistent with the data returned by Symfony DomCrawler.
- Loading branch information
Showing
5 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace tests\_Integration\Http; | ||
|
||
use Crwlr\Crawler\HttpCrawler; | ||
use Crwlr\Crawler\Loader\LoaderInterface; | ||
use Crwlr\Crawler\Steps\Html; | ||
use Crwlr\Crawler\Steps\Loading\Http; | ||
use Crwlr\Crawler\UserAgents\UserAgent; | ||
use Crwlr\Crawler\UserAgents\UserAgentInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
use function tests\helper_generatorToArray; | ||
use function tests\helper_getFastLoader; | ||
|
||
class CharsetExampleCrawler extends HttpCrawler | ||
{ | ||
public function loader(UserAgentInterface $userAgent, LoggerInterface $logger): LoaderInterface | ||
{ | ||
return helper_getFastLoader($userAgent, $logger); | ||
} | ||
|
||
protected function userAgent(): UserAgentInterface | ||
{ | ||
return new UserAgent('SomeUserAgent'); | ||
} | ||
} | ||
|
||
it('removes (and not replaces with broken ? replacement char) non utf-8 characters from extracted data', function () { | ||
$crawler = new CharsetExampleCrawler(); | ||
|
||
$crawler | ||
->input('http://localhost:8000/non-utf-8-charset') | ||
->addStep(Http::get()) | ||
->addStep(Html::root()->extract(['foo' => '.element'])); | ||
|
||
$results = helper_generatorToArray($crawler->run()); | ||
|
||
expect($results)->toHaveCount(1) | ||
->and($results[0]->toArray())->toBe(['foo' => '0 l/m']); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!Doctype html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | ||
<title>Non UTF-8 charset page</title> | ||
</head> | ||
<body> | ||
<div class="element"> | ||
<?php | ||
$string = ''; | ||
|
||
// 178 is square (² in ISO-8859-1) but broken in UTF-8 | ||
foreach ([48, 32, 108, 47, 109, 178] as $ord) { | ||
$string .= chr($ord); | ||
} | ||
|
||
echo $string; | ||
?> | ||
</div> | ||
</body> | ||
</html> |