| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
Codecov Report✅ All modified and coverable lines are covered by tests. @@ Coverage Diff @@
## main #921 +/- ##
==========================================
+ Coverage 97.26% 97.28% +0.01%
==========================================
Files 119 119
Lines 3478 3493 +15
Branches 1048 1051 +3
==========================================
+ Hits 3383 3398 +15
Misses 87 87
Partials 8 8 ☔ View full report in Codecov by Harness.
|
Sorry, something went wrong.
|
@media () => @media() -> need test on IE |
Sorry, something went wrong.
|
ok changed the PR description,
yes invalid, I removed it from the PR description, I couldn't find any example in the spec, but postcss should throw parsing error right if this is an invalid syntax ?
I think the current regex will only convert multiple spaces to a single one. I think it is safe, it won't do any changes to the newlines, neither to tabs |
Sorry, something went wrong.
If it is invalid syntax - yes, but I am not usre we need read spec |
Sorry, something went wrong.
I am afraid about var() function, it can be part of @supports |
Sorry, something went wrong.
|
We can use postcss-value-parser for this, because syntax is same |
Sorry, something went wrong.
the spec says, @supports <supports-condition> {
<stylesheet>
}
so I suppose there can be nested at-rules, but no atules in <supports-condition>
var does supports spaces inside of the (..) so even removing them should not be unsafe. I will add some test to show that.
does it supports parsing at rules ? yea it does, though I will check it if it is better to use this or the regex. |
Sorry, something went wrong.
|
@evilebottnawi I think there is some issue if we go with AST. @media screen and ( max-width: 500px) {
}this is the tree ValueParser {
nodes: [
{ type: 'word', sourceIndex: 0, value: 'screen' },
{ type: 'space', sourceIndex: 6, value: ' ' },
{ type: 'word', sourceIndex: 7, value: 'and' },
{ type: 'space', sourceIndex: 10, value: ' ' },
{
type: 'function',
sourceIndex: 11,
value: '',
before: ' ',
after: '',
nodes: [
{ type: 'word', sourceIndex: 13, value: 'max-width' },
{ type: 'div', sourceIndex: 22, value: ':', before: '', after: '' },
{ type: 'word', sourceIndex: 23, value: '500px' }
]
}
]
}
it would be unsafe to remove all the space. we can remove the whitespace inside of the function i.e inside of the parens (...) but not it would be kind of hacky for and ( |
Sorry, something went wrong.
There was a problem hiding this comment.
Can you test this compression on IE8/IE10/IE11, need to investigate
Sorry, something went wrong.
|
I am afraid @media () => @media() is not valid |
Sorry, something went wrong.
I have added test for ie11. should we still support ie10, 8? |
Sorry, something went wrong.
opz, might have forgot to add and I changed it 👍 |
Sorry, something went wrong.
|
I mean we should test it manually #921 (comment) 😄 |
Sorry, something went wrong.
|
I dont have ie in my machine. I think we can add a check using browserlist + caniuse-lite. It would be a breaking change then. |
Sorry, something went wrong.
|
@anikethsaha I think it is time to learn virtual box 😄 I'm not sure if this does not work, maybe everything is fine, just need test |
Sorry, something went wrong.
|
I have some bad experience with Virtual box earlier, I do have it installed and my config for that is messed up. I will try to look for some online VM to test them. |
Sorry, something went wrong.
At least for @media queries, the spaces appear to be necessary for Chrome and Chromium-based browsers (e.g. Edge, etc...) to parse and apply them correctly. For example, the current cssnano (7.1.0) transforms this: @media screen and (prefers-contrast:more) and (-ms-high-contrast:active) and (prefers-color-scheme: light),
screen and (prefers-contrast:more) and (-ms-high-contrast:black-on-white) {
/* some rules */
}To this without spaces between ) + and, and between , + screen: @media screen and (prefers-contrast:more)and (-ms-high-contrast:active)and (prefers-color-scheme: light),screen and (prefers-contrast:more)and (-ms-high-contrast:black-on-white), {
/* some rules */
}When using Chrome's "Rendering" -> "Emulate CSS media feature prefers-contrast", the media queries don't match after cssnano processing. Turning off the normalizeWhitespace and minifyParams optimizations fixes the issue, and the @media queries match properly again: module.exports = {
plugins: [
// ... possibly other plugins ...
...process.env.NODE_ENV === 'production'
? [cssNano({preset: [
"default",
{
"normalizeWhitespace": false,
"minifyParams": false
}]})
]
: []
]
} |
Sorry, something went wrong.
|
@trinitronx Sounds like a bug in chrome... |
Sorry, something went wrong.
🤔 Hmm... It's not clear whether it is a bug or not... In the W3C recommendation spec for "Media Queries Level 3", there is "Example 21" under "Malformed media query" section:
It makes no mention of the preceding space before an "and" whether it's required or not. So that's inconclusive. Then, in the W3C spec for "Media Queries Level 4" they have a more formal Backus-Naur definition of the media query syntax, which does mention how a comma-separated <media-query-list> is parsed, as well as defining the <media-and> = and <media-in-parens> operator. There is also a similar section about the space after the and and before an opening paren (: Note Whitespace is required between a not, and, or or keyword and the following ( character, because without it that would instead parse as a . This is not made explicitly invalid because it’s already covered by the above grammar. It’s fine to have whitespace between a ) and a following keyword, however. Reference: W3C Recommendation: Media Queries Level 4 - § 3. Syntax That still makes no mention of omitting the whitespace between a closing paren ) and a boolean operator and, or, or not. Yet, since the BNF definition for <media-and> has spaces on both sides of the and ("<media-and> = and <media-in-parens>")... perhaps the Chromium developers interpreted this as being required, or added it into the parsing in such a way as to effectively make it required? Just speculation: It might be parsing something as a not all or false, or unknown... It's unclear which, because there is no useful output in the developer console saying what is going on. We also have -ms-high-contrast which is vendor-specific to older versions of Microsoft Edge (now deprecated in favor of forced-colors). So that portion in Chrome should evaluate to not all, triggering the grammar mismatch logic or perhaps the <general-enclosed> future grammar logic branch. It could also be discarding the media query list portion up to the next comma , given the following:
Note
Reference: W3C Recommendation: Media Queries Level 4 - § 3.2 Error Handling Since the broken example has ),screen and ... it could be that the ,screen portion is breaking it because a typical @media screen requires the space in front of screen <media-type> (as in: <media-type> = <ident>) If it's a Chromium bug, then it's broken out in the wild and places limitations on what minification transforms are "safe" effectively due to the bug. If it's not a bug, then it's not "safe" to do the transform in any case. Whatever the case, current Chrome versions (tested on 137.0.7151.119 and above) seem to choke on the minified @media queries produced by cssnano's current 'default' preset. So, in the practical sense, those particular transforms shouldn't be considered "safe". |
Sorry, something went wrong.
|
Ok, so I have found something that works while digging more into the ,screen spacing issue for Chrome. It turns out that it works if I prefix the nested source CSS @media query rules with only (e.g. only screen and (...) ). For example, these nested @media queries: @media (prefers-contrast: more) {
@media (forced-colors: none) and (prefers-color-scheme: dark),
only screen and (-ms-high-contrast: active) and (prefers-color-scheme: dark),
only screen and (-ms-high-contrast: white-on-black) {
/* ... some rules ... */
}
}Transforms into: /*
Note that the PostCSS plugin prefers-color-scheme-query, is run here before CSSNano.
The corresponding JS polyfill has been added to the page also.
The previous example had it turned off and it still did not parse correctly in Google Chrome.
*/
@media (prefers-contrast:more) and (forced-colors:none) and (color: 48842621),only screen and (prefers-contrast:more) and (-ms-high-contrast:active) and (color:48842621),only screen and (prefers-contrast:more) and (-ms-high-contrast:white-on-black) {
/* ... some rules ... */
}
@media (prefers-contrast:more) and (forced-colors:none) and (prefers-color-scheme: dark),only screen and (prefers-contrast:more) and (-ms-high-contrast:active) and (prefers-color-scheme:dark),only screen and (prefers-contrast:more) and (-ms-high-contrast:white-on-black) {
/* ... same rules as above block duplicated ... */
/* ... for browsers that support prefers-color-scheme ... */
}Note that now we have (...),only screen and (...) instead of just (...),screen and (...). That seems to parse OK in Chrome, and the styles are applied when prefers-contrast emulation is set to "more" and prefers-color-scheme is set to "dark". Therefore, it seems that before we had a syntax construct ,screen and (-ms-high-contrast:active) where the Microsoft Edge-specific vendor media query would probably evaluate to an unknown value, or perhaps as ,screen and not all,. Yet when prefixed with ,screen and it seemed to break the entire query, even for previous ,-separated queries that should have been processed as if a logical OR were used. Yet, now with the prefixed ,only screen and , it probably evaluates to a ,true and (unknown). The W3C "Media Queries Level 4" spec says this:
Note This means that, for example, when a media query is used in a @media rule, if it resolves to “unknown” it’s treated as “false” and fails to match. Media Queries use a three-value logic where terms can be “true”, “false”, or “unknown”. Specifically, it uses the Kleene 3-valued logic. In this logic, “unknown” means “either true or false, but we’re not sure which yet”. This logic was adopted because <general-enclosed> needs to be assigned a truth value. In standard boolean logic, the only reasonable value is “false”, but this means that not unknown(function) is true, which can be confusing and unwanted. Kleene’s 3-valued logic ensures that unknown things will prevent a media query from matching, unless their value is irrelevant to the final result. Reference: W3C Recommendation: Media Queries Level 4 - § 3.1. Evaluating Media Queries So, if we were dealing with an unknown here... then the transform of ,screen and (unknown) may have been parsed incorrectly or differently than ,only screen and (unknown). It would seem as if the only prefix is not optional when omitting the space. |
Sorry, something went wrong.
Note: This implements Chrome syntax findings from my testing exploration Reference: - cssnano/cssnano#921 (comment)
|
@trinitronx You should submit a bug report to Chrome. |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
ref #428
Some of the transforms