8000 [DomCrawler] hasChildren() function · Issue #8048 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DomCrawler] hasChildren() function #8048

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

Closed
ghost opened this issue May 15, 2013 · 3 comments
Closed

[DomCrawler] hasChildren() function #8048

ghost opened this issue May 15, 2013 · 3 comments

Comments

@ghost
Copy link
ghost commented May 15, 2013

It's currently not possible to know if an element has children without getting notices when the input is an empty div:

$elem->children()->count();

(even when just doing $elem->children())

Notice: Trying to get property of non-object in /Users/wesley/Sites/xyz/goutte.phar/vendor/symfony/dom-crawler/Symfony/Component/DomCrawler/Crawler.php on line 738

Notice: Trying to get property of non-object in /Users/wesley/Sites/xyz/goutte.phar/vendor/symfony/dom-crawler/Symfony/Component/DomCrawler/Crawler.php on line 741

This is due to the call to the sibling function with:

$this->getNode(0)->firstChild

An empty div has no firstChild so in the sibling function this generates the notice above.

One could fix this by adding a function such as:

public function hasChildren()
{
    if (!count($this) || !$this->getNode(0)->firstChild || (new static($this->sibling($this->getNode(0)->firstChild), $this->uri))->count() == 0) {
        return false;
    } else {
        return true;
    }
}
@jakzal
Copy link
Contributor
jakzal commented May 15, 2013

Could you please provide a code example which would raise a notice?

The following example works just fine even though h1 element doesn't have children:

use Symfony\Component\DomCrawler\Crawler;

$crawler = new Crawler('<html><body><h1>Hello</h1><p>World!</p></body></html>');
$crawler = $crawler->filter('h1');
var_dump($crawler->children()->count() === 0);

@lazyhammer
Copy link
Contributor

@jakzal, it doesn't have children of type DOMElement, but it has the D 8000 OMText 'Hello', so ->sibling() receives the node and thus triggers no error. Empty that h1 and you'll see the notice.

@jakzal
Copy link
Contributor
jakzal commented May 16, 2013

@lazyhammer 👍

fabpot added a commit that referenced this issue May 16, 2013
This PR was merged into the 2.1 branch.

Discussion
----------

[DomCrawler] Fix Crawler::children() to not trigger a notice for childless node

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #8048
| License       | MIT
| Doc PR        | n/a

Commits
-------

91b8490 Fix Crawler::children() to not trigger a notice for childless node
@fabpot fabpot closed this as completed May 16, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants
0