:is(), :where(), :not() and aliases inside :has() are read as relative to the :has() element

Đang mở
#1,930 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@mayankdhingra02 đang làm issue này rồi.

Từ ngày 2/11/2026.

  • #1934 của @mayankdhingra02 — đang mở

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
55/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
typescript
Lĩnh vực
tooling

Hướng nghiên cứu

Start by tracing the named entry points: subselects.has, is, compileNth, copyOptions and absolutize; reproduce the examples against css-select 7.0.0 and compare the relevant behavior with a browser. Done means the reported selectors, including aliases and of selectors, match the browser’s results without breaking the existing :is(> .a) behavior.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

A selector inside :is(), :where() or :not() is matched against the whole document. When a :has() argument has a combinator in it, css-select reads those selectors as relative to the :has() element instead, so the .b in :not(.b) means :scope .b. Nothing after a + or a ~ is inside that element, so there :not() passes everything and :is() matches nothing.

import { compile } from 'css-select';
import { parseDocument } from 'htmlparser2';
import { findOne } from 'domutils';

const ask = (selector, html) => {
  const doc = parseDocument(html);
  const t = findOne((e) => e.attribs.id === 't', doc.children, true);
  return compile(selector, { xmlMode: false })(t);
};

const p = '<div class="p" id="t"></div>';

// each line prints the opposite of Chrome 152's answer
console.log(ask('.p:has(+ .a:not(.b))', `${p}<div class="a b"></div>`)); // true
console.log(ask('.p:has(+ :is(.a))', `${p}<div class="a"></div>`)); // false
console.log(ask('.p:has(~ :where(.a, .b))', `${p}<i></i><div class="b"></div>`)); // false

// aliases are :is() under another name, and `of` compiles a selector too
console.log(ask('.p:has(+ input:checked)', `${p}<input type="checkbox" checked>`)); // false
console.log(ask('.p:has(+ :nth-child(2 of .a))', `<i class="a"></i>${p}<i class="a"></i>`)); // false

// and a selector in a function may read above the :has() element
console.log(
  ask(
    '.p:has(> .a:is(.x .a))',
    '<section class="x"><div class="p" id="t"><b class="a"></b></div></section>',
  ),
); // false

:has(> .a:not(.b)) only looks right, because a child is a descendant, so :scope .b happens to find it. An argument with no combinator in it at all is unaffected, since has passes no context there. In the wild this is Apple's product pages, which style a section by :has(+ .section.theme-dark:not(.background-alt)).

Cause. subselects.has compiles its argument with a [PLACEHOLDER_ELEMENT] context and sets relativeSelector: true. is, not and compileNth hand the context on with compileToken(token, copyOptions(options), context), copyOptions keeps relativeSelector, and absolutize then prepends :scope to each entry of the inner list. Aliases go through subselects.is, so they inherit it.

What was tried. On a copy of 7.0.0, compiling those three with { ...copyOptions(options), relativeSelector: false } makes every example above answer as Chrome does, and keeps the context, so :scope still resolves. It does make :is(> .a) throw, where it was accepted before and a browser treats it as an entry that matches nothing.

I compared css-select with Chrome 152 on 700 generated :has() selectors over 12 generated documents, 8,400 answers in all. 7.0.0 gets 6,555 right. With this change and the ones in #1928 and #1929, it gets all 8,400.

Run on css-select 7.0.0. master at 2b69f6c has the same lines, which I checked by reading it, not by running it. For anyone who needs it before a fix, https://github.com/sidorares/react-x11-components/pull/1132 works around this by compiling each such function on its own and handing it in through pseudos.

Ngôn ngữ chính
TypeScript
Star
636
Fork
85
Merge trung bình
1 phút
Pull request đã merge (30 ngày)
13

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của fb55/css-select

Tất cả issue của fb55/css-select

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.