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

Aperta
#1,931 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
55/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
typescript
Ambito
frontend

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
TypeScript
Stelle
636
Fork
85
Merge medio
1m
PR unite (30g)
13

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di fb55/css-select

Tutte le issue di fb55/css-select

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.