Skip to content
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

feat: remove extra space before media dn suppots at-rules #640

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
145 changes: 135 additions & 10 deletions packages/postcss-minify-params/src/__tests__/index.js
Expand Up @@ -29,14 +29,91 @@ test(
'should normalise @media queries (3)',
processCSS,
'@media (min-height: 680px),(min-height: 680px){h1{color:red}}',
'@media (min-height:680px){h1{color:red}}'
'@media(min-height:680px){h1{color:red}}'
);

test.skip(
'should normalise @media queries (3) (lowercase and uppercase)',
processCSS,
'@media (min-height: 680px),(MIN-HEIGHT: 680PX){h1{color:red}}',
'@media (min-height:680px){h1{color:red}}'
'@media(min-height:680px){h1{color:red}}'
);

test(
'should normalise @media queries (4)',
processCSS,
'@media (min-width:1px){:root{background:lime}}',
'@media(min-width:1px){:root{background:lime}}'
);

test(
'should normalise @media queries (5)',
processCSS,
'@media ( color ){}',
'@media(color){}'
);

test(
'should normalise @media queries (6)',
processCSS,
'@media (min-width: 30em) and (orientation: landscape){}',
'@media(min-width:30em) and (orientation:landscape){}'
);

test(
'should normalise @media queries (7)',
processCSS,
'@media (min-height: 680px), screen and (orientation: portrait){}',
'@media(min-height:680px),screen and (orientation:portrait){}'
);

test(
'should normalise @media queries (8)',
processCSS,
'@media ( not( hover ) ){}',
'@media(not(hover)){}'
);

test(
'should normalise @supports',
processCSS,
'@supports (display: grid){div{display:block}}',
'@supports(display:grid){div{display:block}}'
);

test(
'should normalise @supports (2)',
processCSS,
'@SUPPORTS (display: grid){div{display:block}}',
'@SUPPORTS(display:grid){div{display:block}}'
);

test(
'should normalise nested at rules',
processCSS,
'@media (min-width: 900px){@media (min-width: 900px){div{display:block}}}',
'@media(min-width:900px){@media(min-width:900px){div{display:block}}}'
);

test(
'should normalise nested at rules (1)',
processCSS,
'@supports (display: flex){@media (min-width: 900px){div{display:block}}}',
'@supports(display:flex){@media(min-width:900px){div{display:block}}}'
);

test(
'should normalise spaces after comma',
processCSS,
'@media print, screen{}',
'@media print,screen{}'
);

test(
'should sort params',
processCSS,
'@media screen, print{}',
'@media print,screen{}'
);

test(
Expand Down Expand Up @@ -67,14 +144,14 @@ test(
'should normalise "all and" in @media queries',
processCSS,
'@media all and (min-width:500px){h1{color:blue}}',
'@media (min-width:500px){h1{color:blue}}'
'@media(min-width:500px){h1{color:blue}}'
);

test(
'should normalise "all and" in @media queries (uppercase)',
processCSS,
'@media ALL AND (min-width:500px){h1{color:blue}}',
'@media (min-width:500px){h1{color:blue}}'
'@media(min-width:500px){h1{color:blue}}'
);

test(
Expand Down Expand Up @@ -102,45 +179,93 @@ test(
'@font-face{font-family:test;src:local(test)}'
);

test(
'should not change charset params',
passthroughCSS,
'@charset "utf-8";'
);

test(
'should not change import params',
passthroughCSS,
'@import url("fineprint.css") print;'
);

test(
'should not change namespace params',
passthroughCSS,
'@namespace url(http://www.w3.org/1999/xhtml);'
);

test(
'should not change document params',
passthroughCSS,
'@document url("https://www.example.com/");'
);

test(
'should not change keyframes params',
passthroughCSS,
'@keyframes sLiDeIn{}'
);

test(
'should not change counter-style params',
passthroughCSS,
'@counter-style tHuMbS'
);

test(
'should not change unknown params',
passthroughCSS,
'@unknown iDeNt keyword (foo:bar) {}'
);

test(
'should not change unknown params (1)',
passthroughCSS,
'@unknown iDeNt keyword (foo:bar);'
);

test(
'should reduce min-aspect-ratio',
processCSS,
'@media (min-aspect-ratio: 32/18){h1{color:blue}}',
'@media (min-aspect-ratio:16/9){h1{color:blue}}'
'@media(min-aspect-ratio:16/9){h1{color:blue}}'
);

test(
'should reduce min-aspect-ratio (uppercase)',
processCSS,
'@media (MIN-ASPECT-RATIO: 32/18){h1{color:blue}}',
'@media (MIN-ASPECT-RATIO:16/9){h1{color:blue}}'
'@media(MIN-ASPECT-RATIO:16/9){h1{color:blue}}'
);

test(
'should reduce max-aspect-ratio',
processCSS,
'@media (max-aspect-ratio: 48000000/32000000){h1{color:blue}}',
'@media (max-aspect-ratio:3/2){h1{color:blue}}'
'@media(max-aspect-ratio:3/2){h1{color:blue}}'
);

test(
'should multiply aspect ratio',
processCSS,
'@media (max-aspect-ratio: 1.5/1){h1{color:blue}}',
'@media (max-aspect-ratio:3/2){h1{color:blue}}'
'@media(max-aspect-ratio:3/2){h1{color:blue}}'
);

test(
'should multiply aspect ratio (2)',
processCSS,
'@media (max-aspect-ratio: .5 / 1){h1{color:blue}}',
'@media (max-aspect-ratio:1/2){h1{color:blue}}'
'@media(max-aspect-ratio:1/2){h1{color:blue}}'
);

test(
'should not throw on empty parentheses',
passthroughCSS,
'@media (){h1{color:blue}}'
'@media(){h1{color:blue}}'
);

test(
Expand Down
7 changes: 6 additions & 1 deletion packages/postcss-minify-params/src/index.js
Expand Up @@ -38,6 +38,7 @@ function transform (legacy, rule) {
return;
}

const ruleName = rule.name.toLowerCase();
const params = valueParser(rule.params);

params.walk((node, index) => {
Expand All @@ -61,7 +62,7 @@ function transform (legacy, rule) {
const prevWord = params.nodes[index - 2];
if (
node.value.toLowerCase() === 'all' &&
rule.name.toLowerCase() === 'media' &&
ruleName === 'media' &&
!prevWord
) {
const nextWord = params.nodes[index + 2];
Expand All @@ -86,6 +87,10 @@ function transform (legacy, rule) {
if (!rule.params.length) {
rule.raws.afterName = '';
}

if ((ruleName === "media" || ruleName === "supports") && rule.params.startsWith('(')) {
rule.raws.afterName = '';
}
}

function hasAllBug (browser) {
Expand Down
4 changes: 4 additions & 0 deletions packages/postcss-minify-params/yarn.lock
Expand Up @@ -334,6 +334,10 @@ cross-spawn@^6.0.5:
shebang-command "^1.2.0"
which "^1.2.9"

cssnano-util-get-arguments@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/cssnano-util-get-arguments/-/cssnano-util-get-arguments-4.0.0.tgz#ed3a08299f21d75741b20f3b81f194ed49cc150f"

debug@^2.1.2, debug@^2.6.8, debug@^2.6.9:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
Expand Down