Skip to content

[DomCrawler] closest() and outerHtml() #18609

@rafis

Description

@rafis

Can you please add closest() to DomCrawler?

http://james.padolsey.com/jquery/#v=2.1.3&fn=jQuery.fn.closest

This is pseudo-code, not tested:

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

        $node = $this->getNode(0);
        while ($node = $node->parentNode) {
            if (XML_ELEMENT_NODE === $node->nodeType) {
               $parent = $this->createSubCrawler($node)->filter($selector);
               if ($parent->count()) {
                  return $parent;
               }
            }
        }

        return null;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    DomCrawlerFeatureHelp wantedIssues and PRs which are looking for volunteers to complete them.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions