:scope inside :has() is taken for the :has() element

Đang mở
#1,931 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

Chưa có ai nhận issue nà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
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
typescript
Lĩnh vực
frontend

Hướng nghiên cứu

Start by reading subselects.has, absolutize, and the scope filter to understand how :has() handles its anchor and the query scope. Run the WPT test css/selectors/has-argument-with-explicit-scope.html and compare its results with the examples in the issue; done means all 13 subtests match the expected behavior.

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

Mô tả

Inside :has(), :scope is still the scope of the query, and the root where the query has none. css-select reads it as something else: the :has() element where the argument has a combinator in it, and the root where it has none, whatever the query's scope.

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

const doc = parseDocument(
  '<div id="m"><div class="a" id="s"><i class="a" id="i"><b class="b"></b></i></div></div>',
);
const [m, s, i] = ['m', 's', 'i'].map((id) =>
  findOne((e) => e.attribs.id === id, doc.children, true),
);

// does `element` match `selector`, in a query whose scope is `scope`
const ask = (selector, scope, element) =>
  compile(selector, { xmlMode: false, relativeSelector: false }, scope)(element);

// each line prints the opposite of Chrome 152's answer
console.log(ask(':has(:scope .b)', i, i)); // true
console.log(ask(':has(> :scope)', i, s)); // false
console.log(ask(':has(.a :scope)', i, m)); // false
console.log(ask(':has(:scope)', i, s)); // false

// with no scope, as in a style sheet, `:scope` is the root, which nothing holds
console.log(compile('.a:has(:scope .b)', { xmlMode: false })(s)); // true

WPT has this as css/selectors/has-argument-with-explicit-scope.html.

Cause. subselects.has is handed the query's context as _context and does not use it. With a combinator in the argument it compiles with a [PLACEHOLDER_ELEMENT] context of its own, which it points at each element it is asked of, context[0] = element, and the scope filter reads that. Without one it compiles with no context, which scope reads as :root. An entry that names :scope is also left unanchored, because absolutize skips it by hasScopePseudo, so :has(.a :scope) looks under the element for the element itself and never finds it.

What was tried. Nothing in css-select. :has() needs two things here that it has one context for, the element it anchors at and the query's scope, so a fix probably has to anchor with something other than :scope. For anyone who needs it before a fix, https://github.com/sidorares/react-x11-components/pull/1150 compiles each :scope in a :has() on its own with the query's context and hands it in through pseudos. With that and a workaround for #1928, 80 generated selectors over 6 generated documents, each asked from about a dozen scopes and from the document, answer as Chrome 152 does in all 480 lines of answers, where 192 did without it, and the WPT test above goes from 10 of 13 subtests to 13.

Run on css-select 7.0.0. master at 2b69f6c has the same lines, which I checked by reading it, not by running it.

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.