Description
Description
In jQuery, there is the .find()
method and the .children([selector])
method. Its documentation states that :
The .children() method differs from .find() in that .children() only travels a single level down the DOM tree while .find() can traverse down multiple levels to select descendant elements (grandchildren, etc.) as well.
The Dom-Crawler component only has a filter()
method (to filter the node and all its children) and a children()
method to return direct children. There is no way to easily filter (thanks to a selector) the direct children of a node.
I think it could be nice to add an optional $selector
parameter to the children()
method, to allow to filter direct children, as jQuery allows to.
Example
Considering this example:
$html = <<<'HTML'
<html>
<body>
<div id="foo">
<p class="lorem" id="p1">ipsum</p>
<p class="lorem" id="p2">era</p>
<div id="nested">
<p class="lorem" id="p3">amenos</p>
</div>
</div>
</body>
</html>
HTML;
$crawler = new Crawler($html);
$foo = $crawler->filter('#foo');
Currently, there is no way (or am I missing something?) to only return #p1
and #p2
starting from the $foo
variable.
$foo->filter('.lorem')
returns #p1
, #p2
and #p3
.
$foo->children()
returns #p1
, #p2
and #nested
.
Adding this new feature would allow $foo->children('.lorem')
to return #p1
and #p2
.
Also, since it would be an optional parameter, it would not break BC.