technosophos / querypath

QueryPath is a PHP library for manipulating XML and HTML. It is designed to work not only with local files, but also with web services and database resources.
http://querypath.org
Other
823 stars 115 forks source link

Multiple finds throw exception when selector has an ID #145

Closed nick-andren closed 9 years ago

nick-andren commented 9 years ago

Hello,

First of all, thank you for QueryPath! It is really an awesome library.

I have noticed an issue where an exception is thrown when find() is executed multiple times on invalid elements. Here's an example:

$dom = htmlqp($content, NULL, array(
    'convert_to_encoding'   => 'UTF-8'
));

// This element does not exist
$nonexistent = $dom->find('.nonexistentElement');
$myElement = $nonexistent->find('input');

This behaves as I would expect, where $myElement equals a QueryPath\DOMQuery object that returns a count() of 0.

It doesn't have to be 'input', but can be a class, attribute, or any other type of selector, except for an ID, from what I've seen. Given a similar example:

$dom = htmlqp($content, NULL, array(
    'convert_to_encoding'   => 'UTF-8'
));

// This element does not exist
$nonexistent = $dom->find('.nonexistentElement');
$myElement = $nonexistent->find('input#exampleId');

This throws the following error:

Catchable fatal error: Argument 1 passed to DOMXPath::__construct() must be an instance of DOMDocument, null given, called in /web/vendor/querypath/querypath/src/QueryPath/CSS/DOMTraverser.php on line 406 and defined in /web/vendor/querypath/querypath/src/QueryPath/CSS/DOMTraverser.php on line 440

I would expect it to return an empty element like the other invalid selectors

I have tried this using QueryPath versions 3.0.3 and dev-master. Both behave the same way.

technosophos commented 9 years ago

I see. Since $nonexistent doesn't have any elements, it's having trouble traversing the DOM tree for the next find. That does seem like something we should catch.

technosophos commented 9 years ago

Found the issue. It's with an XPath optimization I did on IDs and classes. Working on a fix.

technosophos commented 9 years ago

I think this is fixed now.