:is(), :where(), :not() and aliases inside :has() are read as relative to the :has() element
Maintainer thường phản hồi trong vòng 1 ngày
Đá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
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của fb55/css-select
-
:nth-* selectors incorrectly reject detached elements when the formula matches every indexCó thể đã có người làm @bardrop đã nhận 3 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
fb55/css-select#1945 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 55/100
fb55/css-select#1931 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
A descendant combinator after a complex :is() or :not() keeps answers it should notCó thể đã có người làm @Aschenbath đã nhận 12 ngày trước. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
fb55/css-select#1929 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
:has(.a .b) matches with the :has() element itself as .aCó thể đã có người làm @manav4u đã nhận 14 ngày trước. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 72/100
fb55/css-select#1928 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Nesting selector supportĐang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 42/100
fb55/css-select#1838 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của fb55/css-select
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
vega/vega-webgpu#40 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
openwatersio/slackwater#550 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bug size:small
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
fb55/htmlparser2#2579 ·
Maintainer thường phản hồi trong vòng 1 ngày