Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty class when all optional classes are omitted #424

Open
SenojLuap opened this issue Feb 26, 2024 · 3 comments
Open

Empty class when all optional classes are omitted #424

SenojLuap opened this issue Feb 26, 2024 · 3 comments

Comments

@SenojLuap
Copy link
Contributor

This may be a 'working as intended' situation, but I thought I'd raise the issue in case it's not:

Currently, if you're applying a class to an element optionally, but the condition is falsey, maud still generates a 'class' attribute, but with an empty class list.

Ex.

use maud::{html, Render};
fn main() {
    let demo_output = html! {
      p .someClass[false] {
        "Lorem Ipsum"
      }
    }
    .render();
    println!("Generated html: {}", demo_output.0);
}

Output: Generated html: <p class="">Lorem Ipsum</p>

Note that a 'class' attribute is generated, even though it isn't needed.

Definitely not a huge deal, but it would be nice to keep the HTML trim where possible.

@SenojLuap
Copy link
Contributor Author

Also: I'm not opposed to putting in PR for this. I just wasn't sure whether this was an intentional 'feature' or not.

@soundslocke
Copy link

I came across this situation and didn't want the empty class="" either. I worked around it by turning the class attribute into an "optional attribute." It's now class=[class] where class is a Some/None value I assigned above/outside the html! {}.

@lambda-fairy
Copy link
Owner

Yeah, the current behavior isn't ideal but it's hard to do a clean fix. Consider when there's more than one class – now you need to check that all classes are omitted before removing the attribute. Not to mention, some of the classes might be non-optional too.

I remember there's a related issue about extra spaces, and a complete solution should fix that as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants