Stranger6667 / css-inline

High-performance library for inlining CSS into HTML 'style' attributes
https://css-inline.org/
MIT License
232 stars 29 forks source link

Option to keep raw HTML tags structure #335

Closed Eliot00 closed 4 months ago

Eliot00 commented 5 months ago

my code:

use std::borrow::Cow;

const HTML: &str = r#"<main>
<h1>Hello</h1>
<section>
<p>who am i</p>
</section>
</main>
"#;

const CSS: &str = r#"
p {
    color: red;
}

h1 {
    color: blud;
}
"#;

fn main() -> css_inline::Result<()> {
    let inliner = css_inline::CSSInliner::options()
        .inline_style_tags(false)
        .extra_css(Some(Cow::from(CSS)))
        .build();
    let inlined = inliner.inline(HTML)?;
    println!("result: {}", inlined);

    Ok(())
}

current output:

<html><head></head><body><main>
<h1 style="color: blud;">Hello</h1>
<section>
<p style="color: red;">who am i</p>
</section>
</main>
</body></html>

expect output:

<main>
<h1 style="color: blud;">Hello</h1>
<section>
<p style="color: red;">who am i</p>
</section>
</main>
Stranger6667 commented 5 months ago

I think it comes from the html5ever parser that adds html, head & body elements to the tree if they are missing, then they get serialized later on. What we can do on the css-inline side is to provide a way to configure the serialization step, so it can skip tags in a more flexible manner, or just give a config option for skipping html / head / body

jlarmstrongiv commented 4 months ago

That would be great! I’m inlining individual components, rather than the entire email all at once

Stranger6667 commented 4 months ago

This feature will be released in 0.14 via the inline_fragment function (bindings have the same name for this function in their public API)