Skip to content

siblings() raises warnings on an element with no parent #139

Open

Description

@gravitypdf-bot

Description of the bug

siblings() reads $m->parentNode->childNodes without checking for a parent, so calling it on an element that has no parent raises two warnings:

$doc  = qp('<?xml version="1.0"?><root/>')->get(0)->ownerDocument;
$lone = $doc->createElement('lone');

qp($lone)->siblings()->count();
// Warning: Attempt to read property "childNodes" on null in src/Helpers/QueryFilters.php on line 1083
// Warning: foreach() argument must be of type array|object, null given in src/Helpers/QueryFilters.php on line 1083
// 0

The result (an empty set) is right, and matches jQuery 3.7.1 ($(document.createElement('lone')).siblings().length is 0). But the warnings are wrong, and become a fatal error under any error handler that converts warnings to exceptions (PHPUnit, Laravel, Symfony, ...). PHP 7.x raises the equivalent notice and warning.

An element in a detached subtree, which does have a parent, is fine: qp($a)->siblings() returns its siblings.

Expected

siblings() on an element with no parent returns an empty set, without warnings. contents() already guards with if (empty($m->childNodes)) continue;; siblings() needs the same check on $m->parentNode.

Found while reviewing #129, which fixed the same class of problem in parentsUntil() (an infinite loop on a detached element) by routing it through one walk helper. siblings() keeps its own loop there, so it was not covered.

QueryPath version

main at ed98764, PHP 8.5.12.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions