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

Sign up
Appearance settings

Commit 0362d51

Browse files
[DomCrawler] Properly ignore errors when using the native HTML5 parser
1 parent 43d99a5 commit 0362d51

File tree

5 files changed

+68
-3
lines changed

5 files changed

+68
-3
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,9 +1112,13 @@ private function parseXhtml(string $htmlContent, string $charset = 'UTF-8'): \DO
11121112
$htmlContent = $document->saveXml();
11131113
$charset = $document->inputEncoding;
11141114

1115+
$internalErrors = libxml_use_internal_errors(true);
1116+
11151117
$dom = new \DOMDocument('1.0', $charset);
11161118
$dom->loadXML($htmlContent);
11171119

1120+
libxml_use_internal_errors($internalErrors);
1121+
11181122
// Register id attributes as ID attributes for getElementById to work
11191123
foreach ((new \DOMXPath($dom))->query('//*[@id]') as $element) {
11201124
if ($element instanceof \DOMElement) {

‎src/Symfony/Component/DomCrawler/Tests/CrawlerTestCase.php‎ renamed to ‎src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\Attributes\DataProvider;
1515
use PHPUnit\Framework\Attributes\Group;
1616
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
17+
use PHPUnit\Framework\Attributes\RequiresPhp;
1718
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
1819
use PHPUnit\Framework\Error\Notice;
1920
use PHPUnit\Framework\TestCase;
@@ -22,7 +23,8 @@
2223
use Symfony\Component\DomCrawler\Image;
2324
use Symfony\Component\DomCrawler\Link;
2425

25-
class CrawlerTestCase extends TestCase
26+
#[RequiresPhp('>=8.4')]
27+
class CrawlerTest extends TestCase
2628
{
2729
public static function getDoctype(): string
2830
{
@@ -1378,6 +1380,21 @@ public static function html5Provider(): iterable
13781380
yield 'All together' => [$BOM.' <!--c-->'.$html];
13791381
}
13801382

1383+
public function testAlpineJs()
1384+
{
1385+
$crawler = $this->createCrawler();
1386+
$crawler->addHtmlContent(file_get_contents(__DIR__.'/Fixtures/alpine-js.html'));
1387+
1388+
if (\PHP_VERSION_ID < 80400) {
1389+
$this->assertCount(1, $crawler->filterXPath('//button'));
1390+
$this->assertCount(3, $crawler->filterXPath('//div'));
1391+
} else {
1392+
// Alpine JS is well known for not having valid HTML tags...
1393+
$this->assertCount(0, $crawler->filterXPath('//button'));
1394+
$this->assertCount(0, $crawler->filterXPath('//div'));
1395+
}
1396+
}
1397+
13811398
protected function createTestCrawler($uri = null)
13821399
{
13831400
$html = $this->getDoctype().'
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Alpine.js Test Page</title>
7+
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
8+
</head>
9+
<body>
10+
<div x-data="{ open: false, message: 'Hello Alpine!' }">
11+
<!-- Alpine.js shorthand @ for x-on: -->
12+
<button @click="open = !open" class="btn">
13+
Toggle
14+
</button>
15+
16+
<!-- Alpine.js shorthand : for x-bind: -->
17+
<div x-show="open" :class="open ? 'visible' : 'hidden'">
18+
<p x-text="message"></p>
19+
20+
<!-- More Alpine.js directives -->
21+
<input
22+
type="text"
23+
x-model="message"
24+
@input="console.log('Input changed')"
25+
@keydown.enter="console.log('Enter pressed')"
26+
@click.away="open = false"
27+
:disabled="!open"
28+
:placeholder="message"
29+
>
30+
31+
<!-- Template with x-for -->
32+
<template x-for="(item, index) in [1, 2, 3]" :key="index">
33+
<div
34+
@click="console.log(item)"
35+
:aria-selected="index === 0"
36+
:style="`color: red`"
37+
>
38+
Item: <span x-text="item"></span>
39+
</div>
40+
</template>
41+
</div>
42+
</div>
43+
</body>
44+
</html>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Symfony\Component\DomCrawler\Crawler;
1717

1818
#[RequiresPhp('<8.4')]
19-
class LegacyHtml5ParserCrawlerTest extends CrawlerTestCase
19+
class LegacyHtml5ParserCrawlerTest extends CrawlerTest
2020
{
2121
public function testHtml()
2222
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use PHPUnit\Framework\Attributes\RequiresPhp;
1515

1616
#[RequiresPhp('<8.4')]
17-
class LegacyParserCrawlerTest extends CrawlerTestCase
17+
class LegacyParserCrawlerTest extends CrawlerTest
1818
{
1919
public static function getDoctype(): string
2020
{

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /