Skip to content

Regression in 2.2.25: :has() sibling selector throws on root elements #168

Description

@nrps9909

Version

nwsapi@2.2.25 (regression from 2.2.24)

Reproduction

With jsdom@26.1.0, which accepts nwsapi through ^2.2.16:

const { JSDOM } = require('jsdom');

const dom = new JSDOM(
  '<div class="subject"><span class="target"></span></div><div class="sibling"></div>',
);
const { document } = dom.window;

console.log(
  document.querySelector('.target').matches('.subject:has(~ .sibling) .target'),
);
console.log(document.documentElement.matches(':has(~ p)'));

Expected

The expressions return true and false, respectively. That is the behavior with nwsapi@2.2.24.

Actual

Both paths can throw in nwsapi@2.2.25:

TypeError: Cannot read properties of null (reading 'children')

The same applies to adjacent-sibling relational selectors (:has(+ ...)) when matching reaches a root element.

This appears to be a regression of #145. The 2.2.25 selector compiler emits sibling checks without first guarding e.parentElement, whereas 2.2.24 included that guard.

Downstream evidence

Ant Design PRs #59150 and #59151 independently began failing the same six Tooltip tests after 2.2.25 was published. On the exact #59151 head, the three affected Tooltip test files fail 6/55 with 2.2.25 and pass 55/55 (5 snapshots) when only nwsapi is changed to 2.2.24. A local build with the parent guard restored also passes 55/55.

I have a small fix and browser regression test ready and will open a PR linked to this issue.

AI assistance disclosure: Codex was used to trace the dependency update, compare releases, minimize the reproduction, and draft the report. I reproduced and verified all results locally.

Pinned by dperini

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions