You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Invoking ->is(':text') on a QueryPath object that contains a single text node throws.
This is the sixth PHP DOM library I'm trying this week, so with each new one I start by writing a couple of suitability tests. This is the first suitability test I wrote for querypath, and should serve as repro.
Thanks for taking the time to report this. We certainly will want to fix the PHP error that's occurring when passing this pseudo-class selector to is().
You should be aware that even when we do, your test self::assertTrue($firstItem->is(':text')); will fail. This library was originally created as the PHP equivalent of jQuery. In jQuery the custom pseudo-class selector :text only matches the <input> and <input type="text"/> tags. It won't tell you if $firstItem is a text node.
@rulatir the QueryPath way would be $this->assertNotEmpty($firstItem->text());, which will return the contents of textContent for all matches in the current collection.
But if you really need low-level access to the DOM classes in a collection, this can be done via ->toArray() or ->get(). You can then check the class type or $item->nodeType (https://www.php.net/manual/en/dom.constants.php).
I've incorporated your bug report into a draft pull request so the PHP error can be fixed in the future: #50. If you have further questions about using QueryPath please open a Discussion: https://github.com/GravityPDF/querypath/discussions
Activity
jakejackson1 commentedon Mar 21, 2024
Hi @rulatir,
Thanks for taking the time to report this. We certainly will want to fix the PHP error that's occurring when passing this pseudo-class selector to
is().You should be aware that even when we do, your test
self::assertTrue($firstItem->is(':text'));will fail. This library was originally created as the PHP equivalent of jQuery. In jQuery the custom pseudo-class selector:textonly matches the<input>and<input type="text"/>tags. It won't tell you if$firstItemis a text node.Source: https://api.jquery.com/text-selector/
rulatir commentedon Mar 21, 2024
What is then the idiomatic way to determine whether the collection is holding a text node?
$q->get(0) instanceof DOMText?jakejackson1 commentedon Mar 21, 2024
@rulatir the QueryPath way would be
$this->assertNotEmpty($firstItem->text());, which will return the contents oftextContentfor all matches in the current collection.But if you really need low-level access to the DOM classes in a collection, this can be done via
->toArray()or->get(). You can then check the class type or$item->nodeType(https://www.php.net/manual/en/dom.constants.php).I've incorporated your bug report into a draft pull request so the PHP error can be fixed in the future: #50. If you have further questions about using QueryPath please open a Discussion: https://github.com/GravityPDF/querypath/discussions
Fix :text pseudo-class and selector fatals on non-element nodes
Fix :text pseudo-class and selector fatals on non-element nodes