8000 [DomCrawler] Give choice of used parser by victor-prdh · Pull Request #49121 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DomCrawler] Give choice of used parser #49121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 6, 2023
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
15 changes: 14 additions & 1 deletion src/Symfony/Component/BrowserKit/AbstractBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ abstract class AbstractBrowser
protected $internalResponse;
protected $response;
protected $crawler;
protected bool $useHtml5Parser = true;
protected $insulated = false;
protected $redirect;
protected $followRedirects = true;
Expand Down Expand Up @@ -207,6 +208,18 @@ public function getCrawler(): Crawler
return $this->crawler;
}

/**
* Sets whether parsing should be done using "masterminds/html5".
*
* @return $this
*/
public function useHtml5Parser(bool $useHtml5Parser): static
{
$this->useHtml5Parser = $useHtml5Parser;

return $this;
}

/**
* Returns the current BrowserKit Response instance.
*/
Expand Down Expand Up @@ -497,7 +510,7 @@ protected function createCrawlerFromContent(string $uri, string $content, string
return null;
}

$crawler = new Crawler(null, $uri);
$crawler = new Crawler(null, $uri, null, $this->useHtml5Parser);
$crawler->addContent($content, $type);

return $crawler;
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/BrowserKit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.3
---

* Add `AbstractBrowser::useHtml5Parser()`

6.1
---

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/DomCrawler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
6.3
---

* Add `$useHtml5Parser` argument to `Crawler`
* Add `CrawlerSelectorCount` test constraint
* Add argument `$normalizeWhitespace` to `Crawler::innerText()`
* Make `Crawler::innerText()` return the first non-empty text
Expand Down
15 changes: 10 additions & 5 deletions src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,17 @@ class Crawler implements \Countable, \IteratorAggregate
*/
private bool $isHtml = true;

private HTML5 $html5Parser;

private ?HTML5 $html5Parser = null;

/**
* @param \DOMNodeList|\DOMNode|\DOMNode[]|string|null $node A Node to use as the base for the crawling
*/
public function __construct(\DOMNodeList|\DOMNode|array|string $node = null, string $uri = null, string $baseHref = null)
public function __construct(\DOMNodeList|\DOMNode|array|string $node = null, string $uri = null, string $baseHref = null, bool $useHtml5Parser = true)
{
$this->uri = $uri;
$this->baseHref = $baseHref ?: $uri;
$this->html5Parser = new HTML5(['disable_html_ns' => true]);
$this->html5Parser = $useHtml5Parser ? new HTML5(['disable_html_ns' => true]) : null;
$this->cachedNamespaces = new \ArrayObject();

$this->add($node);
Expand Down Expand Up @@ -621,7 +622,7 @@ public function html(string $default = null): string
$node = $this->getNode(0);
$owner = $node->ownerDocument;

if ('<!DOCTYPE html>' === $owner->saveXML($owner->childNodes[0])) {
if ($this->html5Parser && '<!DOCTYPE html>' === $owner->saveXML($owner->childNodes[0])) {
$owner = $this->html5Parser;
}

Expand All @@ -642,7 +643,7 @@ public function outerHtml(): string
$node = $this->getNode(0);
$owner = $node->ownerDocument;

if ('<!DOCTYPE html>' === $owner->saveXML($owner->childNodes[0])) {
if ($this->html5Parser && '<!DOCTYPE html>' === $owner->saveXML($owner->childNodes[0])) {
$owner = $this->html5Parser;
}

Expand Down Expand Up @@ -1215,6 +1216,10 @@ private function parseHtmlString(string $content, string $charset): \DOMDocument

private function canParseHtml5String(string $content): bool
{
if (!$this->html5Parser) {
return false;
}

if (false === ($pos = stripos($content, '<!doctype html>'))) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ abstract class AbstractCrawlerTestCase extends TestCase
{
abstract public static function getDoctype(): string;

protected function createCrawler($node = null, string $uri = null, string $baseHref = null)
protected function createCrawler($node = null, string $uri = null, string $baseHref = null, bool $useHtml5Parser = true)
{
return new Crawler($node, $uri, $baseHref);
return new Crawler($node, $uri, $baseHref, $useHtml5Parser);
}

public function testConstructor()
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/DomCrawler/Tests/Html5ParserCrawlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ public function testHtml5ParserWithInvalidHeadedContent(string $content)
self::assertEmpty($crawler->filterXPath('//h1')->text(), '->addHtmlContent failed as expected');
}

public function testHtml5ParserNotSameAsNativeParserForSpecificHtml()
{
// Html who create a bug specific to the DOM extension (see https://github.com/symfony/symfony/issues/28596)
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ A test that relies on the presence of a bug to pass is extremely brittle. If the purpose of the test is to ensure the parser is different then that is what the assertion should be checking, even if you have to use reflection to achieve it.

$html = $this->getDoctype().'<html><body><h1><p>Foo</p></h1></body></html>';

$html5Crawler = $this->createCrawler(null, null, null, true);
$html5Crawler->add($html);

$nativeCrawler = $this->createCrawler(null, null, null, false);
$nativeCrawler->add($html);

$this->assertNotEquals($nativeCrawler->filterXPath('//h1')->text(), $html5Crawler->filterXPath('//h1')->text(), 'Native parser and Html5 parser must be different');
}

public static function validHtml5Provider(): iterable
{
$html = self::getDoctype().'<html><body><h1><p>Foo</p></h1></body></html>';
Expand Down
0