Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Crawler.php

function Crawler::closest

Return first parents (heading toward the document root) of the Element that matches the provided selector.

Throws

\InvalidArgumentException When current node is empty

See also

https://developer.mozilla.org/en-US/docs/Web/API/Element/closest#Polyfi…

File

vendor/symfony/dom-crawler/Crawler.php, line 406

Class

Crawler
Crawler eases navigation of a list of \DOMNode objects.

Namespace

Symfony\Component\DomCrawler

Code

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;
}
RSS feed
Powered by Drupal