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

オープン 初心者向け
#1,945 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@bardrop がすでに取り組んでいます。

2026年11月16日 から。

  • #1947 @bardrop による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
82/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
typescript

調査の方向性

issueでリンクされている compileNth の最適化箇所を src/pseudo-selectors/filters.ts で読み、その高速パスを通常の兄弟要素カウントのパスと比較します。issueにある切り離された要素の再現例を使って、4つの :nth-* のケースを確認します。各セレクターの (1) と (n) が切り離された要素に一致すれば完了です。issueではテストファイルが指定されていないため、カバレッジを追加する前に、関連する既存テストを探します。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
TypeScript
スター
636
フォーク
85
平均マージ
1分
マージ済み PR(30日)
13

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

fb55/css-select のほかの issue

fb55/css-select の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。