Skip to content

getChildren() raises TypeError if the node has no children #166

@jiripudil

Description

@jiripudil

Hello, thank you for a great library! I'd like to report a bug we've encountered: the getChildren() method raises a TypeError if the node doesn't have any children.

Code to reproduce:

<?php
require __DIR__ . '/vendor/autoload.php';
use PHPHtmlParser\Dom;

$dom = new Dom();
$dom->loadStr('<div>Test <img src="test.jpg"></div>', []);

$imgNode = $dom->root->find('img');
$imgNode->getChildren();

Tested on PHP 7.2.9 and paquettg/php-html-parser 2.0.0. Version 2.0.1 is not available on packagist, but from looking at the code I believe the problem persists there.

Current behaviour:

TypeError: Argument 1 passed to PHPHtmlParser\Dom\InnerNode::getChild() must be of the type integer, null given, called in vendor/paquettg/php-html-parser/src/PHPHtmlParser/Dom/InnerNode.php on line 355 in vendor/paquettg/php-html-parser/src/PHPHtmlParser/Dom/InnerNode.php on line 59

Expected behaviour:

If a node has no children, getChildren() should return an empty array, and perhaps firstChild() and lastChild() should properly throw a ChildNotFoundException.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions