Skip to content

[DomCrawler] Added Crawler::matches(), ::closest(), ::outerHtml() #33144

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
Aug 22, 2019
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
3 changes: 3 additions & 0 deletions src/Symfony/Component/DomCrawler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ CHANGELOG
-----

* Added `Form::getName()` method.
* Added `Crawler::matches()` method.
* Added `Crawler::closest()` method.
* Added `Crawler::outerHtml()` method.

4.3.0
-----
Expand Down
55 changes: 55 additions & 0 deletions src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,45 @@ public function siblings()
return $this->createSubCrawler($this->sibling($this->getNode(0)->parentNode->firstChild));
}

public function matches(string $selector): bool
{
if (!$this->nodes) {
return false;
}

$converter = $this->createCssSelectorConverter();
$xpath = $converter->toXPath($selector, 'self::');

return 0 !== $this->filterRelativeXPath($xpath)->count();
}

/**
* Return first parents (heading toward the document root) of the Element that matches the provided selector.
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element/closest#Polyfill
*
* @throws \InvalidArgumentException When current node is empty
*/
public function closest(string $selector): ?self
{
if (!$this->nodes) {
throw new \InvalidArgumentException('The current node list is empty.');
}

$domNode = $this->getNode(0);

while (XML_ELEMENT_NODE === $domNode->nodeType) {
$node = $this->createSubCrawler($domNode);
if ($node->matches($selector)) {
return $node;
}

$domNode = $node->getNode(0)->parentNode;
}

return null;
}

/**
* Returns the next siblings nodes of the current selection.
*
Expand Down Expand Up @@ -609,6 +648,22 @@ public function html(/* $default = null */)
return $html;
}

public function outerHtml(): string
{
if (!\count($this)) {
throw new \InvalidArgumentException('The current node list is empty.');
}

$node = $this->getNode(0);
$owner = $node->ownerDocument;

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

return $owner->saveHTML($node);
}

/**
* Evaluates an XPath expression.
*
Expand Down
99 changes: 99 additions & 0 deletions src/Symfony/Component/DomCrawler/Tests/AbstractCrawlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,105 @@ public function testSiblings()
}
}

public function provideMatchTests()
{
yield ['#foo', true, '#foo'];
yield ['#foo', true, '.foo'];
yield ['#foo', true, '.other'];
yield ['#foo', false, '.bar'];

yield ['#bar', true, '#bar'];
yield ['#bar', true, '.bar'];
yield ['#bar', true, '.other'];
yield ['#bar', false, '.foo'];
}

/** @dataProvider provideMatchTests */
public function testMatch(string $mainNodeSelector, bool $expected, string $selector)
{
$html = <<<'HTML'
<html lang="en">
<body>
<div id="foo" class="foo other">
<div>
<div id="bar" class="bar other"></div>
</div>
</div>
</body>
</html>
HTML;

$crawler = $this->createCrawler($this->getDoctype().$html);
$node = $crawler->filter($mainNodeSelector);
$this->assertSame($expected, $node->matches($selector));
}

public function testClosest()
{
$html = <<<'HTML'
<html lang="en">
<body>
<div class="lorem2 ok">
<div>
<div class="lorem3 ko"></div>
</div>
<div class="lorem1 ok">
<div id="foo" class="newFoo ok">
<div class="lorem1 ko"></div>
</div>
</div>
</div>
<div class="lorem2 ko">
</div>
</body>
</html>
HTML;

$crawler = $this->createCrawler($this->getDoctype().$html);
$foo = $crawler->filter('#foo');

$newFoo = $foo->closest('#foo');
$this->assertInstanceOf(Crawler::class, $newFoo);
$this->assertSame('newFoo ok', $newFoo->attr('class'));

$lorem1 = $foo->closest('.lorem1');
$this->assertInstanceOf(Crawler::class, $lorem1);
$this->assertSame('lorem1 ok', $lorem1->attr('class'));

$lorem2 = $foo->closest('.lorem2');
$this->assertInstanceOf(Crawler::class, $lorem2);
$this->assertSame('lorem2 ok', $lorem2->attr('class'));

$lorem3 = $foo->closest('.lorem3');
$this->assertNull($lorem3);

$notFound = $foo->closest('.not-found');
$this->assertNull($notFound);
}

public function testOuterHtml()
{
$html = <<<'HTML'
<html lang="en">
<body>
<div class="foo">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</body>
</html>
HTML;

$crawler = $this->createCrawler($this->getDoctype().$html);
$bar = $crawler->filter('ul');
$output = $bar->outerHtml();
$output = str_replace([' ', "\n"], '', $output);
$expected = '<ul><li>1</li><li>2</li><li>3</li></ul>';
$this->assertSame($expected, $output);
}

public function testNextAll()
{
$crawler = $this->createTestCrawler()->filterXPath('//li')->eq(1);
Expand Down