:nth-* selectors incorrectly reject detached elements when the formula matches every index

Đang mở Phù hợp với người mới
#1,945 0 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

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

Từ ngày 16/11/2026.

  • #1947 của @bardrop — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
82/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
backend-api-design

Hướng nghiên cứu

Đọc src/pseudo-selectors/filters.ts tại phần tối ưu hóa compileNth được liên kết trong issue, sau đó so sánh đường chạy nhanh này với đường chạy đếm các phần tử anh em thông thường. Kiểm tra bốn trường hợp :nth-* bằng ví dụ tái hiện với phần tử đã tách khỏi cây trong issue; hoàn tất khi mỗi bộ chọn có (1) và (n) khớp với phần tử đã tách khỏi cây. Issue không nêu tên tệp kiểm thử, vì vậy hãy tìm các kiểm thử hiện có liên quan trước khi bổ sung phạm vi kiểm thử.

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

Mô tả

In [email protected], a detached element matches :nth-child(1) but fails :nth-child(n). The same inconsistency affects all four :nth-* pseudo-classes.

This reproduces with the default adapter:

import { is } from 'css-select';
import { Element } from 'domhandler';

const element = new Element('div', {});

for (const pseudo of [
  'nth-child',
  'nth-last-child',
  'nth-of-type',
  'nth-last-of-type',
]) {
  console.log(
    pseudo,
    is(element, `:${pseudo}(1)`),
    is(element, `:${pseudo}(n)`),
  );
}

Actual output:

nth-child true false
nth-last-child true false
nth-of-type true false
nth-last-of-type true false

Expected: true true for every row. A detached element is its only inclusive sibling, at index 1. Selectors Level 4 explicitly allows these pseudo-classes to match elements without parents.

The apparent cause is the compileNth optimization:

if (nthCheck === boolbase.trueFunc && !ofSelector) {
    return (element) =>
        getElementParent(element, adapter) !== null && next(element);
}

This path requires an element parent, whereas the ordinary sibling-counting path handles detached elements correctly. Returning next directly appears sufficient for this optimization.

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.