futursolo / stylist-rs

A CSS-in-Rust styling solution for WebAssembly Applications
https://crates.io/crates/stylist
MIT License
370 stars 22 forks source link

Can not add css comments at the end #36

Closed Madoshakalaka closed 2 years ago

Madoshakalaka commented 2 years ago

This compiles:

    let backdrop = css!(
        r#"
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  width: 100%; /* Full width */
  height: 100%;
        "#);

This doesn't:

    let backdrop = css!(
        r#"
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  width: 100%; /* Full width */
  height: 100%; /* Full height */
        "#);

compiler output:

error: Failed to Parse CSS, due to:
0: at line 5, in Many0:
  height: 100%; /* Full height */
                ^

1: at line 1, in Trimmed:

^

2: at line 1, in StyleSheet:

^

  --> src\main.rs:12:9
   |
12 | /         r#"
13 | |   position: fixed; /* Stay in place */
14 | |   z-index: 1; /* Sit on top */
15 | |   width: 100%; /* Full width */
16 | |   height: 100%; /* Full height */
17 | |         "#);
   | |__________^
WorldSEnder commented 2 years ago

Thanks for the report.

Preliminary analysis makes me think the parser see this something like this:

  position: fixed; /* Stay in place */
                   └─────────────────┐
┌────────────────────────────────────┘
│ z-index: 1; /* Sit on top */
└───────────┘ └──────────────┐
┌────────────────────────────┘
│ width: 100%; /* Full width */
└────────────┘ └──────────────┐
┌─────────────────────────────┘
│ height: 100%; /* Full height */
└─────────────┘ ^^^^^^^^^^^^^^^^^

I.e. the comments are recognized as part of the following attribute name, and leaves the rest dangling.

https://github.com/futursolo/stylist-rs/blob/master/packages/stylist-core/src/parser.rs#L140

I think the best course of action would be to completely disregard comments anywhere, compared to selectively as done now. I'm not 100% sure how this can be done in nom, but will dig into it.

WorldSEnder commented 2 years ago

Tentative bug fix lives @ https://github.com/WorldSEnder/stylist/commit/6833f1c7017bd44719fc7399c2a65225ef6c2c8c I do want to slim the changes down a bit before creating the PR.