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
4 changes: 4 additions & 0 deletions src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -1112,9 +1112,13 @@ private function parseXhtml(string $htmlContent, string $charset = 'UTF-8'): \DO
$htmlContent = $document->saveXml();
$charset = $document->inputEncoding;

$internalErrors = libxml_use_internal_errors(true);

$dom = new \DOMDocument('1.0', $charset);
$dom->loadXML($htmlContent);

libxml_use_internal_errors($internalErrors);

// Register id attributes as ID attributes for getElementById to work
foreach ((new \DOMXPath($dom))->query('//*[@id]') as $element) {
if ($element instanceof \DOMElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\Error\Notice;
use PHPUnit\Framework\TestCase;
Expand All @@ -22,7 +23,8 @@
use Symfony\Component\DomCrawler\Image;
use Symfony\Component\DomCrawler\Link;

class CrawlerTestCase extends TestCase
#[RequiresPhp('>=8.4')]
class CrawlerTest extends TestCase
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this really be a test on its own rather than a testcase extended by bother various tests ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, that's how PHP 8.5 is tested (should be)

{
public static function getDoctype(): string
{
Expand Down Expand Up @@ -1378,6 +1380,21 @@ public static function html5Provider(): iterable
yield 'All together' => [$BOM.' <!--c-->'.$html];
}

public function testAlpineJs()
{
$crawler = $this->createCrawler();
$crawler->addHtmlContent(file_get_contents(__DIR__.'/Fixtures/alpine-js.html'));

if (\PHP_VERSION_ID < 80400) {
$this->assertCount(1, $crawler->filterXPath('//button'));
$this->assertCount(3, $crawler->filterXPath('//div'));
} else {
// Alpine JS is well known for not having valid HTML tags...
$this->assertCount(0, $crawler->filterXPath('//button'));
$this->assertCount(0, $crawler->filterXPath('//div'));
}
}

protected function createTestCrawler($uri = null)
{
$html = $this->getDoctype().'
Expand Down
44 changes: 44 additions & 0 deletions src/Symfony/Component/DomCrawler/Tests/Fixtures/alpine-js.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Alpine.js Test Page</title>
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
</head>
<body>
<div x-data="{ open: false, message: 'Hello Alpine!' }">
<!-- Alpine.js shorthand @ for x-on: -->
<button @click="open = !open" class="btn">
Toggle
</button>

<!-- Alpine.js shorthand : for x-bind: -->
<div x-show="open" :class="open ? 'visible' : 'hidden'">
<p x-text="message"></p>

<!-- More Alpine.js directives -->
<input
type="text"
x-model="message"
@input="console.log('Input changed')"
@keydown.enter="console.log('Enter pressed')"
@click.away="open = false"
:disabled="!open"
:placeholder="message"
>

<!-- Template with x-for -->
<template x-for="(item, index) in [1, 2, 3]" :key="index">
<div
@click="console.log(item)"
:aria-selected="index === 0"
:style="`color: red`"
>
Item: <span x-text="item"></span>
</div>
</template>
</div>
</div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Symfony\Component\DomCrawler\Crawler;

#[RequiresPhp('<8.4')]
class LegacyHtml5ParserCrawlerTest extends CrawlerTestCase
class LegacyHtml5ParserCrawlerTest extends CrawlerTest
{
public function testHtml()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use PHPUnit\Framework\Attributes\RequiresPhp;

#[RequiresPhp('<8.4')]
class LegacyParserCrawlerTest extends CrawlerTestCase
class LegacyParserCrawlerTest extends CrawlerTest
{
public static function getDoctype(): string
{
Expand Down
Loading