A descendant combinator after a complex :is() or :not() keeps answers it should not

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

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

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

2026年11月8日 から。

  • #1940 @Aschenbath による — オープン

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
48/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
javascript, typescript
領域
web-dev

調査の方向性

Start by reading compileToken, getQuality, and isCachedDescendant (named cachedDescendant in 7.0.0); the issue does not name source or test files. Run the two supplied reproductions with caching enabled and disabled, then trace why complex :is() or :not() selectors arm this cache. Done means the first example matches o m and the second reflects the updated document.

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

説明

isCachedDescendant (cachedDescendant in 7.0.0) keeps one answer per element for as long as the compiled function lives. It is armed by hasExpensiveSubselector, which compileToken sets for any rule getQuality rates 0. That is meant for :has() and :contains(), but :is() or :not() with a combinator inside rates 0 as well, because the combinator's -1 is clamped to 0.

The answer the cache keeps can depend on :scope, which :has() changes from one element to the next:

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

const doc = parseDocument(
  '<div class="c" id="o"><div class="b c" id="m"><span class="b c" id="t">' +
    '<input class="b" disabled></span></div></div>',
);
const elements = findAll((e) => !!e.attribs.id, doc.children);
const ids = (options) =>
  elements
    .filter(compile(':has(:is(.c .b) :disabled)', options))
    .map((e) => e.attribs.id)
    .join(' ');

console.log(ids({ xmlMode: false }));
// o m t, where Chrome 152 matches o m: nothing strictly inside #t is a
// `.c .b` with a :disabled in it
console.log(ids({ xmlMode: false, cacheResults: false }));
// o m

The same cache reaches a selector with no :has() or :contains() in it, which is less of a bug, since the README tells you to disable cacheResults if the document changes between queries, but it is easy to miss that :is() is among what it applies to:

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

const doc = parseDocument(
  '<div id="o"><div class="a"><span class="y" id="t"></span></div></div>',
);
const byId = (id) => findOne((e) => e.attribs.id === id, doc.children, true);
const matches = compile(':is(.x .a) .y', { xmlMode: false });

console.log(matches(byId('t'))); // false
byId('o').attribs.class = 'x';
console.log(matches(byId('t'))); // false, and true with cacheResults: false

What was tried. Turning the cached path off alone fixes both examples, so it is that cache and not the one in cacheParentResults. On a copy of 7.0.0, setting hasExpensiveSubselector only for has, contains and icontains rather than for every rule of quality 0 fixes both too, and leaves the cache for the selectors it was written for.

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

主要言語
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 を短くまとめたダイジェスト。