Skip to content

Potential bug: nesting with unknown AtRule, removes the selector #353

@RobinMalfait

Description

@RobinMalfait

Hey!

We are using the nesting feature, and if you nest an @media inside of a rule, it properly gets hoisted to the top and the rule with the selector is put inside the @media AtRule. However, if you try to do this with let's say @container then the selector is gone.

This also happens with any other AtRule it doesn't recognize.

Input:

/* Normal syntax, works */
@container (min-width: 200px) {
  .bar {
    text-decoration-line: underline
  }
}

/* Nested syntax with @container, selector is gone */
.foo {
  @container (min-width: 200px) {
    text-decoration-line: underline
  }

  @any-at-rule {
    color: red;
  }
}

/* Nested syntax with @media, selector is still there */
.baz {
  @media (min-width: 200px) {
    text-decoration-line: underline
  }
}

Current behaviour:

@container (min-width: 200px) {
  .bar {
    text-decoration-line: underline;
  }
}

@container (min-width: 200px) {
  text-decoration-line: underline
}

@any-at-rule {
  color: red;
}

@media (min-width: 200px) {
  .baz {
    text-decoration-line: underline;
  }
}

Expected behavour:

@container (min-width: 200px) {
  .bar {
    text-decoration-line: underline;
  }
}

@container (min-width: 200px) {
  .foo {
    text-decoration-line: underline
  }
}

@any-at-rule {
  .foo {
    color: red;
  }
}

@media (min-width: 200px) {
  .baz {
    text-decoration-line: underline;
  }
}

Is this a bug, or is this the expected behaviour?


Reproduction link: https://lightningcss.dev/playground/index.html#%7B%22minify%22%3Afalse%2C%22nesting%22%3Atrue%2C%22customMedia%22%3Atrue%2C%22cssModules%22%3Afalse%2C%22analyzeDependencies%22%3Afalse%2C%22targets%22%3A%7B%22chrome%22%3A6225920%7D%2C%22source%22%3A%22%2F*%20Normal%20syntax%2C%20works%20*%2F%5Cn%40container%20(min-width%3A%20200px)%20%7B%5Cn%20%20.bar%20%7B%5Cn%20%20%20%20text-decoration-line%3A%20underline%5Cn%20%20%7D%5Cn%7D%5Cn%5Cn%2F*%20Nested%20syntax%20with%20%40container%2C%20selector%20is%20gone%20*%2F%5Cn.foo%20%7B%5Cn%20%20%40container%20(min-width%3A%20200px)%20%7B%5Cn%20%20%20%20text-decoration-line%3A%20underline%5Cn%20%20%7D%5Cn%5Cn%20%20%40any-at-rule%20%7B%5Cn%20%20%20%20color%3A%20red%3B%5Cn%20%20%7D%5Cn%7D%5Cn%5Cn%2F*%20Nested%20syntax%20with%20%40media%2C%20selector%20is%20still%20there%20*%2F%5Cn.baz%20%7B%5Cn%20%20%40media%20(min-width%3A%20200px)%20%7B%5Cn%20%20%20%20text-decoration-line%3A%20underline%5Cn%20%20%7D%5Cn%7D%5Cn%22%2C%22unusedSymbols%22%3A%5B%5D%2C%22version%22%3A%22local%22%7D

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions