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/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/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/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; + } +} 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; } }