[GH-ISSUE #2950] [Issue]: Nested conditional styles in a card (e.g. @media inside @supports) lose their outer condition #1528

Closed
opened 2026-10-21 05:38:01 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Jun 28, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2950

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

When a character card's CSS nests two at-rules — for example @media wrapping @supports — the card-CSS scoper drops the outer at-rule and keeps only the inner rules. The inner rules are still safely scoped (no security problem), but they now apply unconditionally instead of only under the intended condition. So a style the card author meant for "narrow screens only" (or "only if the browser supports grid") silently applies everywhere. It's a quiet correctness bug that's hard to debug because the CSS looks fine in the card.

How to reproduce (try it yourself)

  1. In a character card's creator-notes <style>, put:
    @media (max-width: 600px) { @supports (display: grid) { .bubble { display: none } } }
    
  2. Open a chat with that card and card CSS enabled, then inspect the injected #marinara-card-css <style>.

You'll see (verified by running the actual scopeChatCss against this input):

@supports (display: grid) {.mari-card-css .bubble { display: none }}

The @media (max-width: 600px) wrapper is gone, so .bubble is hidden at every width, not just below 600px. The reverse nesting (@supports { @media { … } }) drops the @supports the same way (also verified).
Expected: the full nesting is preserved so the rule only applies when both conditions match.

Where it is

packages/client/src/lib/card-css.ts — scopeChatCss, the rule-splitter regex ruleRe at line 525:
/([^{}]+)\{([^{}]*(?:\{[^{}]*\}[^{}]*)*)\}/g
understands only one level of brace nesting in a rule body, and the recursive at-rule branch at lines 545-549 only fires when the outer block is the one matched — which never happens for double-nested at-rules. So the regex matches the inner at-rule first and the outer prelude is left behind as inter-rule text and dropped.

What's happening

ruleRe can't match an at-rule whose body itself contains a nested {…}{…} ruleset, so for @media{@supports{…}} it skips the @media prelude and matches @supports{…} directly, discarding the @media condition.

Suggested fix

The file already has a string/comment/brace-aware scanner, findCssBlockEnd (lines 18-64). Use it (or a small generalization) to split rules by real brace depth instead of the regex, so at-rule preludes are captured and recursed into at any nesting depth. (Worth also checking the separate inline-message <style> scoper in ChatMessage.tsx for the same one-level limitation.)


AI-investigated against v2.0.6; reproduced by running the real scopeChatCss. Please confirm before fixing.

Originally created by @kolacheee on GitHub (Jun 28, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2950 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** When a character card's CSS nests two at-rules — for example `@media` wrapping `@supports` — the card-CSS scoper drops the **outer** at-rule and keeps only the inner rules. The inner rules are still safely scoped (no security problem), but they now apply unconditionally instead of only under the intended condition. So a style the card author meant for "narrow screens only" (or "only if the browser supports grid") silently applies everywhere. It's a quiet correctness bug that's hard to debug because the CSS looks fine in the card. **How to reproduce (try it yourself)** 1. In a character card's creator-notes `<style>`, put: ``` @media (max-width: 600px) { @supports (display: grid) { .bubble { display: none } } } ``` 2. Open a chat with that card and card CSS enabled, then inspect the injected `#marinara-card-css` `<style>`. **You'll see** (verified by running the actual `scopeChatCss` against this input): ``` @supports (display: grid) {.mari-card-css .bubble { display: none }} ``` The `@media (max-width: 600px)` wrapper is gone, so `.bubble` is hidden at **every** width, not just below 600px. The reverse nesting (`@supports { @media { … } }`) drops the `@supports` the same way (also verified). **Expected:** the full nesting is preserved so the rule only applies when both conditions match. **Where it is** `packages/client/src/lib/card-css.ts` — `scopeChatCss`, the rule-splitter regex `ruleRe` at line 525: `/([^{}]+)\{([^{}]*(?:\{[^{}]*\}[^{}]*)*)\}/g` understands only **one** level of brace nesting in a rule body, and the recursive at-rule branch at lines 545-549 only fires when the **outer** block is the one matched — which never happens for double-nested at-rules. So the regex matches the inner at-rule first and the outer prelude is left behind as inter-rule text and dropped. **What's happening** `ruleRe` can't match an at-rule whose body itself contains a nested `{…}{…}` ruleset, so for `@media{@supports{…}}` it skips the `@media` prelude and matches `@supports{…}` directly, discarding the `@media` condition. **Suggested fix** The file already has a string/comment/brace-aware scanner, `findCssBlockEnd` (lines 18-64). Use it (or a small generalization) to split rules by real brace depth instead of the regex, so at-rule preludes are captured and recursed into at any nesting depth. (Worth also checking the separate inline-message `<style>` scoper in `ChatMessage.tsx` for the same one-level limitation.) --- *AI-investigated against v2.0.6; reproduced by running the real `scopeChatCss`. Please confirm before fixing.*
Author
Owner

@SpicyMarinara commented on GitHub (Jun 28, 2026):

Fixed on staging: card CSS scoping now uses a brace-aware rule splitter so nested conditional at-rules preserve their outer conditions.

<!-- gh-comment-id:4827784802 --> @SpicyMarinara commented on GitHub (Jun 28, 2026): Fixed on staging: card CSS scoping now uses a brace-aware rule splitter so nested conditional at-rules preserve their outer conditions.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1528
No description provided.