Fix CSS plugin incorrectly grouping global rules inside @media blocks - #73
Merged
Dan Marshall (danmarshall) merged 3 commits intoSep 8, 2025
Conversation
Co-authored-by: danmarshall <11507384+danmarshall@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] The css plugin has a bug: this css
"css": [
"body { font-family: sans-serif; margin: 0; padding: 20px; background: #f8fafc; max-width: 1200px; margin: 0 auto; }",
"#header, #habits, #table, #monthly, #heatmap { background: white; paddin...
Fix CSS plugin incorrectly grouping global rules inside @media blocks
Sep 8, 2025
Dan Marshall (danmarshall)
approved these changes
Sep 8, 2025
Dan Marshall (danmarshall)
marked this pull request as ready for review
September 8, 2025 15:16
Dan Marshall (danmarshall)
deleted the
copilot/fix-9b059d06-ec1c-4842-9aa2-7a0563199321
branch
September 8, 2025 15:16
Collaborator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The CSS plugin had a bug where CSS rules appearing after
@mediarules were incorrectly parsed as being inside the@mediablock instead of remaining in global scope. This caused CSS like:To be incorrectly rendered as:
Instead of the correct:
Root Cause
The
categorizeCss()function used a singlecurrentAtRuleSignaturevariable to track parsing context. When encountering an@mediarule with a block, this variable was set to the@mediacontext but never reset back to global context. This caused all subsequent rules to be incorrectly associated with the@mediacontext regardless of their actual location in the CSS structure.Solution
Replaced broken context tracking: Removed the problematic
currentAtRuleSignatureapproach and implemented proper AST-based context detection.Added
findAtRuleContext()function: This function walks the CSS AST to determine the correct parent context for each rule by checking if the rule node is actually inside an at-rule's block.Implemented two-pass processing:
Fixed TypeScript ES5 compatibility: Updated code to work with older TypeScript targets by replacing
Object.values(),.includes(), and problematic function declarations.Impact
The fix ensures that CSS arrays are parsed correctly, with rules appearing in their proper contexts based on the actual CSS structure rather than processing order.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.