Skip to content
17 changes: 15 additions & 2 deletions css-inline/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ pub struct InlineOptions<'a> {
// Python wrapper for `CSSInliner` and `&str` in Rust & simple functions on the Python side
/// Additional CSS to inline.
pub extra_css: Option<Cow<'a, str>>,
/// Whether to break down styles into property tags
pub styles_as_props: bool,
}

impl<'a> InlineOptions<'a> {
Expand All @@ -87,6 +89,7 @@ impl<'a> InlineOptions<'a> {
base_url: None,
load_remote_stylesheets: true,
extra_css: None,
styles_as_props: false,
}
}

Expand Down Expand Up @@ -125,6 +128,13 @@ impl<'a> InlineOptions<'a> {
self
}

/// Insert styles as properties
#[must_use]
pub fn styles_as_properties(mut self, styles_as_props: bool) -> Self {
self.styles_as_props = styles_as_props;
self
}

/// Create a new `CSSInliner` instance from this options.
#[must_use]
pub const fn build(self) -> CSSInliner<'a> {
Expand All @@ -141,6 +151,7 @@ impl Default for InlineOptions<'_> {
base_url: None,
load_remote_stylesheets: true,
extra_css: None,
styles_as_props: false,
}
}
}
Expand Down Expand Up @@ -284,7 +295,7 @@ impl<'a> CSSInliner<'a> {
}
}
if let Some(extra_css) = &self.options.extra_css {
process_css(&document, extra_css, &mut styles);
process_css(&document, extra_css.as_ref(), &mut styles);
}
for (node_id, styles) in styles {
// SAFETY: All nodes are alive as long as `document` is in scope.
Expand All @@ -299,7 +310,9 @@ impl<'a> CSSInliner<'a> {
.attributes
.try_borrow_mut()
{
if let Some(existing_style) = attributes.get_mut("style") {
if self.options.styles_as_props {
// TODO
} else if let Some(existing_style) = attributes.get_mut("style") {
*existing_style = merge_styles(existing_style, &styles)?;
} else {
let mut final_styles = String::with_capacity(128);
Expand Down
6 changes: 6 additions & 0 deletions css-inline/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ OPTIONS:

--extra-css
Additional CSS to inline.

--styles-as-props
Will insert styles as properties
"#
)
.as_bytes();
Expand All @@ -58,6 +61,7 @@ struct Args {
extra_css: Option<String>,
load_remote_stylesheets: bool,
files: Vec<String>,
styles_as_props: bool,
}

fn parse_url(url: Option<String>) -> Result<Option<url::Url>, url::ParseError> {
Expand All @@ -83,6 +87,7 @@ fn main() -> Result<(), Box<dyn Error>> {
base_url: args.opt_value_from_str("--base-url")?,
extra_css: args.opt_value_from_str("--extra-css")?,
load_remote_stylesheets: args.contains("--load-remote-stylesheets"),
styles_as_props: args.opt_value_from_str("--styles-as-props")?.unwrap_or(false),
files: args.free()?,
};
let options = InlineOptions {
Expand All @@ -91,6 +96,7 @@ fn main() -> Result<(), Box<dyn Error>> {
base_url: parse_url(args.base_url)?,
load_remote_stylesheets: args.load_remote_stylesheets,
extra_css: args.extra_css.as_deref().map(Cow::Borrowed),
styles_as_props: args.styles_as_props,
};
let inliner = CSSInliner::new(options);
if args.files.is_empty() {
Expand Down