Skip to content

DomCrawler parents only on first node of selection #18723

Closed
@flip111

Description

@flip111

The DomCrawler seems to take the first node and traverse up the tree until the root node. But i think that the selection can have different parents, when i saw the function description i would expect it gets the parent of each node in the selection. So with 5 nodes selected i would get 5 parents, of which some could be the same parent (an array of 5 references of which some references to the same object). My suggestion would be to introduce another method with the functionality just described. About naming the methods i'm not sure yet.'

/**
* Returns the parents nodes of the current selection.
*
* @return Crawler A Crawler instance with the parents nodes of the current selection
*
* @throws \InvalidArgumentException When current node is empty
*/
public function parents()
{
if (!$this->nodes) {
throw new \InvalidArgumentException('The current node list is empty.');
}
$node = $this->getNode(0);
$nodes = array();
while ($node = $node->parentNode) {
if (XML_ELEMENT_NODE === $node->nodeType) {
$nodes[] = $node;
}
}
return $this->createSubCrawler($nodes);
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions