[Bug] Declarations directly inside nested @media are dropped from darkreader--sync, overriding the page's values

Aperta
#16,097 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

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

Direzione di ricerca

Start from the code that copies style rules into the darkreader--sync sheet and check how it walks CSSNestedDeclarations inside nested @media blocks, which it currently skips. Reproduce with the self-contained HTML in the report, open it in Dynamic mode, and compare the nested and flat margin values. Done means the nested @media declaration is copied, so the nested bar also reads 72px at a 640px+ viewport.

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

Descrizione

Prerequisites
  • I searched for any existing report about this bug to avoid opening a duplicate.
  • I can reproduce this bug in a new, unmodified web browser profile with Dark Reader installed as the only extension.
  • I understand I need to use the Broken Website Report template if this bug I am reporting occurs on a single website.
Bug Description

In Dynamic mode, Dark Reader copies a style rule into its darkreader--sync sheet (inserted after the original sheet) when the rule contains something it processes, e.g. var(). Declarations placed directly inside a nested @media (CSSOM CSSNestedDeclarations, Chrome 130+) are not copied. Because the copy comes after the original, its top-level declarations override the page's media-specific values. This changes the page layout, not just colors.

:root { --unit: 16px; }

.nested {
  --w: 80vw;
  --h: calc(1.5 * var(--unit));

  @media (min-width: 40rem) {
    --w: calc(4.5 * var(--unit));
  }
}

Generated darkreader--sync:

.nested { --w: 80vw; --h: calc(1.5 * var(--unit)); }

The @media block is gone, so --w resolves to 80vw at any viewport width. The same rules written flat (@media (min-width: 40rem) { .nested { ... } }) are copied correctly and work.

Related: #14188 (nested CSS not being darkened). This report is about a different symptom: nested declarations are dropped from the copy, which then overrides the page's own values.

Website Address

Self-contained test case (save as an .html file and open it):

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Dark Reader: nested @media declarations</title>
<style>
  :root { --unit: 16px; }

  .nested {
    --w: 80vw;
    --h: calc(1.5 * var(--unit));

    @media (min-width: 40rem) {
      --w: calc(4.5 * var(--unit));
    }
  }

  .flat {
    --w: 80vw;
    --h: calc(1.5 * var(--unit));
  }
  @media (min-width: 40rem) {
    .flat { --w: calc(4.5 * var(--unit)); }
  }

  i { display: block; height: var(--h); margin-inline-start: var(--w); background: #2563eb; }
</style>
</head>
<body>
  <p>Viewport ≥ 640px: both bars should start 72px from the left.</p>
  <p>nested</p>
  <div class="nested"><i id="nested"></i></div>
  <p>flat</p>
  <div class="flat"><i id="flat"></i></div>
  <pre id="out"></pre>
  <script>
    setTimeout(() => {
      const m = (id) => getComputedStyle(document.getElementById(id)).marginInlineStart;
      document.getElementById('out').textContent = `innerWidth: ${innerWidth}\nnested: ${m('nested')}\nflat: ${m('flat')}`;
    }, 1000);
  </script>
</body>
</html>
Steps To Reproduce
  1. Save the test case above as an .html file and serve it (or open it with file access allowed for Dark Reader).
  2. Use a viewport at least 640px wide.
  3. Enable Dark Reader in Dynamic mode for the page.
Expected Behavior

Both bars start 72px from the left, as they do with Dark Reader disabled. The page prints nested: 72px, flat: 72px.

Actual Behavior

The nested bar starts at 80vw. The page prints nested: 1491.2px (viewport 1864px) and flat: 72px.

Screenshots

No response

Operating System

macOS 27.0.1

Web Browser name and version

Chrome 155

Dark Reader version

4.9.133

Additional Context

Found via Drupal core's Navigation module, whose CSS has body { --admin-toolbar-sidebar-width: 80vw; @media (min-width: 64rem) { --admin-toolbar-sidebar-width: calc(4.5 * var(--admin-toolbar-rem)); } }. Drupal's admin theme is dark, so Dark Reader removes its styles about 200ms after load. Before that, JavaScript that measures layout on DOMContentLoaded reads the wrong value (80vw) and keeps it, so the admin page content ends up shifted 80vw to the right.

Lingua principale
TypeScript
Stelle
22.6k
Fork
2.8k
Merge medio
1g 17h
PR unite (30g)
82

Preparare l'ambiente

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 darkreader/darkreader

Tutte le issue di darkreader/darkreader

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.