mnussbaumer / cssex

An Elixir based and opinionated way to write CSS
MIT License
20 stars 0 forks source link

Nested media queries & whitespace in variable values #53

Closed mnussbaumer closed 6 months ago

mnussbaumer commented 6 months ago
.a {
  .b {
    .c {
       some-rule: some-value;
       @media (...) {
         some-rule: other-value;
       }
    }
  }
}

The rule inside media, when parsed, because that block (that starts a new parser) has no selector, when it goes to store the rules would just write the selector as the list [['.a', '.b', '.c']] which wouldn't add spaces, so the rule would be written as:

@media ..... {
    .a.b.c { ...}
}

Instead of (notice the white space between the classes selectors):

@media ..... {
    .a .b .c { ...}
}

If the @media declaration had a selector inside it, then it would work correctly because that selector starts a chain in the new parser instance, which then makes it so that it gets correctly assembled. The solution is to, before writing out the css, we check if the selector is still a list, and if so we join the elements with whitespace into a string, as they would be if there was a selector.

After parsing that line, the variable name would be some_var (notice the whitespace) and value would be: 1px1px5px10px making it impossible to use vars to store values that have whitespaces in their declarations (outside of other delimiters such as (...), "...", etc). Now it works as expected