Skip to content

\Dom\HTMLDocument querySelectorAll selecting only the first when using ~ and :has #18877

Description

@momala454

Description

The following code:

<?php

$text = <<<TEXT
<html>
<head>
</head>
<body>
    <div><div></div></div>
    <span></span>
    <span></span>
    <span></span>
</body>
</html>
TEXT;

$dom = \Dom\HTMLDocument::createFromString($text, options: LIBXML_NOERROR);
var_dump($dom->querySelectorAll('div:has(div) ~ *'));

Resulted in this output:

object(Dom\NodeList)#3 (1) {
  ["length"]=>
  int(1)
}

But I expected this output instead:

object(Dom\NodeList)#3 (1) {
  ["length"]=>
  int(3)
}

PHP Version

PHP 8.4.8 (cli) (built: Jun 11 2025 00:01:02) (NTS)
Copyright (c) The PHP Group
Built by https://github.com/docker-library/php
Zend Engine v4.4.8, Copyright (c) Zend Technologies

Operating System

No response

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions