From 5f6fe56261a5972cbf04e10052e6f28b6469e60c Mon Sep 17 00:00:00 2001 From: Kyo Nagashima Date: Wed, 31 May 2017 12:06:14 +0900 Subject: [PATCH 1/2] Support nested queries A nested media queries should be popped up to root and pack. This change resolves #50 in most case. --- index.js | 18 +++++++++++++++++- .../nested-queries.css} | 16 +++++++++------- .../nested-queries.css} | 12 +++++++++--- 3 files changed, 35 insertions(+), 11 deletions(-) rename test/{fixtures/issue50.css => expected/nested-queries.css} (61%) rename test/{expected/issue50.css => fixtures/nested-queries.css} (65%) diff --git a/index.js b/index.js index 2737ecc..e2d57fd 100644 --- a/index.js +++ b/index.js @@ -160,10 +160,26 @@ module.exports = postcss.plugin(pkg.name, (opts) => { } css.walkAtRules("media", (atRule) => { - if (atRule.parent.type !== "root") { + if (atRule.parent.parent && atRule.parent.parent.type !== "root") { return; } + if (atRule.parent.type !== "root") { + const newAtRule = postcss.atRule({ + name: atRule.parent.name, + params: atRule.parent.params + }); + + atRule.each((rule) => { + newAtRule.append(rule); + }); + atRule.remove(); + atRule = postcss.atRule({ + name: atRule.name, + params: atRule.params + }).append(newAtRule); + } + const queryList = atRule.params; const past = queries[queryList]; diff --git a/test/fixtures/issue50.css b/test/expected/nested-queries.css similarity index 61% rename from test/fixtures/issue50.css rename to test/expected/nested-queries.css index 1553bc1..27bd8cf 100644 --- a/test/fixtures/issue50.css +++ b/test/expected/nested-queries.css @@ -2,20 +2,22 @@ z-index: 1; } -@media (min-width: 1024px) { +@supports (display: auto) { .foo { - z-index: 4; + z-index: 2; } } -@supports (display: auto) { +@media (min-width: 1024px) { .foo { - z-index: 2; + z-index: 3; } - - @media (min-width: 1024px) { + @supports (display: auto) { .foo { - z-index: 3; + z-index: 4; } } + .bar { + z-index: 5; + } } diff --git a/test/expected/issue50.css b/test/fixtures/nested-queries.css similarity index 65% rename from test/expected/issue50.css rename to test/fixtures/nested-queries.css index 5d76f9b..4805e8f 100644 --- a/test/expected/issue50.css +++ b/test/fixtures/nested-queries.css @@ -2,6 +2,12 @@ z-index: 1; } +@media (min-width: 1024px) { + .foo { + z-index: 3; + } +} + @supports (display: auto) { .foo { z-index: 2; @@ -9,13 +15,13 @@ @media (min-width: 1024px) { .foo { - z-index: 3; + z-index: 4; } } } @media (min-width: 1024px) { - .foo { - z-index: 4; + .bar { + z-index: 5; } } From 1fc4ffd551c6c00b33ca745bdea89b7c71880eca Mon Sep 17 00:00:00 2001 From: Kyo Nagashima Date: Wed, 31 May 2017 23:43:04 +0900 Subject: [PATCH 2/2] Test 3 level nesting Nesting 3 or more level should be kept as it is. --- test/expected/more-nested-queries.css | 28 ++++++++++++++++++++++++ test/fixtures/more-nested-queries.css | 31 +++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 test/expected/more-nested-queries.css create mode 100644 test/fixtures/more-nested-queries.css diff --git a/test/expected/more-nested-queries.css b/test/expected/more-nested-queries.css new file mode 100644 index 0000000..5f312ad --- /dev/null +++ b/test/expected/more-nested-queries.css @@ -0,0 +1,28 @@ +.foo { + z-index: 1; +} + +.bar { + z-index: 2; +} + +@media (min-width: 1px) { + .foo { + z-index: 3; + } + + @supports (display: auto) { + .foo { + z-index: 4; + } + + @media (min-width: 2px) { + .foo { + z-index: 5; + } + } + } + .bar { + z-index: 6; + } +} diff --git a/test/fixtures/more-nested-queries.css b/test/fixtures/more-nested-queries.css new file mode 100644 index 0000000..3c48949 --- /dev/null +++ b/test/fixtures/more-nested-queries.css @@ -0,0 +1,31 @@ +.foo { + z-index: 1; +} + +@media (min-width: 1px) { + .foo { + z-index: 3; + } + + @supports (display: auto) { + .foo { + z-index: 4; + } + + @media (min-width: 2px) { + .foo { + z-index: 5; + } + } + } +} + +.bar { + z-index: 2; +} + +@media (min-width: 1px) { + .bar { + z-index: 6; + } +}