-
Notifications
You must be signed in to change notification settings - Fork 3.4k
fix: keep the media type first when flattening nested @media queries #4481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| @media screen and (max-width: 500px) { | ||
| .a { | ||
| color: red; | ||
| } | ||
| } | ||
| @media only screen and (min-width: 100px) { | ||
| .b { | ||
| color: blue; | ||
| } | ||
| } | ||
| @media all and (max-width: 9px) { | ||
| .c { | ||
| color: green; | ||
| } | ||
| } | ||
| @media print and (color) and (min-width: 1px) { | ||
| .d { | ||
| color: black; | ||
| } | ||
| } | ||
| @media screen and (max-width: 500px) { | ||
| .e { | ||
| color: red; | ||
| } | ||
| } | ||
| @media (max-width: 500px) and (min-width: 100px) { | ||
| .f { | ||
| color: red; | ||
| } | ||
| } | ||
| @media tester and (max-width: 500px) { | ||
| .g { | ||
| color: red; | ||
| } | ||
| } | ||
| @media screen and (max-width: 500px) { | ||
| .h { | ||
| color: red; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,51 @@ | ||||||||
| // A media type nested inside conditions must lead the flattened query, | ||||||||
| // otherwise the result is invalid CSS (issue #3694, #3764). | ||||||||
| @media (max-width: 500px) { | ||||||||
| @media screen { | ||||||||
| .a { color: red; } | ||||||||
| } | ||||||||
| } | ||||||||
| @media (min-width: 100px) { | ||||||||
| @media only screen { | ||||||||
| .b { color: blue; } | ||||||||
| } | ||||||||
| } | ||||||||
| @media (max-width: 9px) { | ||||||||
| @media all { | ||||||||
| .c { color: green; } | ||||||||
| } | ||||||||
| } | ||||||||
| @media (min-width: 1px) { | ||||||||
| @media print and (color) { | ||||||||
| .d { color: black; } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| // Already-correct ordering is preserved. | ||||||||
|
coderabbitai[bot] marked this conversation as resolved.
|
||||||||
| @media screen { | ||||||||
| @media (max-width: 500px) { | ||||||||
| .e { color: red; } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| // Queries without a media type are left untouched. | ||||||||
| @media (max-width: 500px) { | ||||||||
| @media (min-width: 100px) { | ||||||||
| .f { color: red; } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| // Unknown media types are valid CSS non-matches, so they reorder too. | ||||||||
| @media (max-width: 500px) { | ||||||||
| @media tester { | ||||||||
| .g { color: red; } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| // An escaped-string feature condition (Anonymous) must not be mistaken for a media type. | ||||||||
| @fc: ~"(max-width: 500px)"; | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Prevent Stylelint from rejecting the valid LESS variable declaration.
Proposed local suppression+// stylelint-disable-next-line scss/at-rule-no-unknown
`@fc`: ~"(max-width: 500px)";📝 Committable suggestion
Suggested change
🧰 Tools🪛 Stylelint (17.14.0)[error] 46-46: Unexpected unknown at-rule " (scss/at-rule-no-unknown) 🤖 Prompt for AI AgentsSource: Linters/SAST tools |
||||||||
| @media @fc { | ||||||||
| @media screen { | ||||||||
| .h { color: red; } | ||||||||
| } | ||||||||
| } | ||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: less/less.js
Length of output: 275
🏁 Script executed:
Repository: less/less.js
Length of output: 12974
🏁 Script executed:
Repository: less/less.js
Length of output: 49897
🏁 Script executed:
Repository: less/less.js
Length of output: 17993
Skip media-query modifiers in string-valued fragments too.
Keyword/Anonymousfragments do not skipnot/only, so parsed inputs like@media not (color)are reclassified asAnonymous('not (color)')and sorted ahead of later types likescreen. Inspect the non-modifier token for these fragments before deciding whether a following(...)must remain a condition.Proposed fix
if (fragment.type === 'Keyword' || fragment.type === 'Anonymous') { - head = fragment.value; + const raw = fragment.value; + if (typeof raw === 'string') { + const tokens = raw.trim().split(/\s+/); + let idx = 0; + const first = tokens[idx] && tokens[idx].toLowerCase(); + if (first === 'not' || first === 'only') { idx++; } + head = tokens[idx]; + }📝 Committable suggestion
🤖 Prompt for AI Agents