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

Skip to content

Commit 471acb7

Browse files
longwavenicolas-grekas
authored andcommitted
[DomCrawler] Handle malformed tags in HTML5 parser
1 parent 506b333 commit 471acb7

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

src/Symfony/Component/DomCrawler/Crawler.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,11 @@ private function copyFromHtml5ToDom(\Dom\Node $source, \DOMDocument $target): vo
12721272
continue;
12731273
}
12741274

1275-
$element = $target->createElement($source->tagName);
1275+
try {
1276+
$element = $target->createElement($source->tagName);
1277+
} catch (\DOMException) {
1278+
continue;
1279+
}
12761280

12771281
foreach ($source->attributes as $attr) {
12781282
try {

src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,13 @@ public static function html5Provider(): iterable
13801380
yield 'All together' => [$BOM.' <!--c-->'.$html];
13811381
}
13821382

1383+
public function testHtml5MalformedContent()
1384+
{
1385+
$crawler = $this->createCrawler();
1386+
$crawler->addHtmlContent('<script&>');
1387+
self::assertEquals('<head></head><body></body>', $crawler->html());
1388+
}
1389+
13831390
public function testAlpineJs()
13841391
{
13851392
$crawler = $this->createCrawler();

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ public static function invalidHtml5Provider(): iterable
7272
yield 'Text between comments' => ['<!--c--> test <!--cc-->'.$html];
7373
}
7474

75+
public function testHtml5MalformedContent()
76+
{
77+
$crawler = $this->createCrawler();
78+
$crawler->addHtmlContent('<script&>');
79+
self::assertEquals('<head><script></script></head>', $crawler->html());
80+
}
81+
7582
protected function createCrawler($node = null, ?string $uri = null, ?string $baseHref = null)
7683
{
7784
return new Crawler($node, $uri, $baseHref, true);

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,11 @@ public function testAddHtml5()
9595
public function testHtml5ParserParseContentStartingWithValidHeading(string $content)
9696
{
9797
}
98+
99+
public function testHtml5MalformedContent()
100+
{
101+
$crawler = $this->createCrawler();
102+
$crawler->addHtmlContent('<script&>');
103+
self::assertEquals('<head><script></script></head>', $crawler->html());
104+
}
98105
}

0 commit comments

Comments
 (0)