[Bug]: `html:has(> body)` and `:scope > body` on the `<html>` element match nothing

Abierto
#43,987 3 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
76/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
playwright, typescript

Línea de trabajo

Start in packages/injected/src/selectorEvaluator.ts, especially _matchesSimple and _expandContextForScopeMatching, which the issue identifies as the failing path. Compare the behavior for the root element with the included reproduction and native querySelectorAll. Done means the three reported locators match one element, with regression coverage for root :scope matching.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Version

1.65.0 (also 1.66.0-alpha-1793916328000)

Steps to reproduce
  1. Clone https://github.com/trackoor/playwright-css-has-root-repro
  2. npm install && npx playwright install chromium
  3. npm test

The test:

import { test, expect } from '@playwright/test';

test(':has(> ...) and :scope > ... on the <html> element', async ({ page }) => {
  await page.setContent('<body class="dark"><div><p>x</p></div></body>');

  // native querySelectorAll finds 1 for each of these
  expect(await page.evaluate(() => document.querySelectorAll('html:has(> body)').length)).toBe(1);

  await expect(page.locator('html:has(> body)')).toHaveCount(1);
  await expect(page.locator(':root:has(> body)')).toHaveCount(1);
  await expect(page.locator('html').locator(':scope > body')).toHaveCount(1);
});
Expected behavior

All three locators find the one element, like document.querySelectorAll does for the same selectors.

Actual behavior

All three return 0 elements:

Error: expect(locator).toHaveCount(expected) failed
Locator:  locator('html:has(> body)')
Expected: 1
Received: 0

html:has(body) and div:has(> p) work, so only :scope matching on the root element is affected.

Additional context

In packages/injected/src/selectorEvaluator.ts, _matchesSimple refuses element === context.scope and relies on _expandContextForScopeMatching to widen the scope to the parent. For <html>, parentElementOrShadowHost() returns null, so the context is returned unchanged and <html> can never match as :scope. Same code on main at 07c7069.

Environment
System:
  OS: Linux (x64)
Binaries:
  Node: 26.13.0
npmPackages:
  @playwright/test: 1.65.0

Credit: https://ucsbplse.github.io/

Lenguaje dominante
TypeScript
Estrellas
99.1k
Forks
6.8k
Merge medio
17 h 6 min
PR fusionados (30 d)
306

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de microsoft/playwright

Todos los issues de microsoft/playwright

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.