Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ jobs:
- run: cargo fmt --all -- --check
working-directory: ./bindings/c

- run: cargo fmt --all -- --check
working-directory: ./bindings/java

- run: cargo fmt --all -- --check
working-directory: ./bindings/javascript

Expand Down Expand Up @@ -148,6 +151,10 @@ jobs:
run: cargo clippy -- -D warnings
working-directory: ./bindings/python

- name: Java
run: cargo clippy -- -D warnings
working-directory: ./bindings/java

- name: JavaScript
run: cargo clippy -- -D warnings
working-directory: ./bindings/javascript
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## [Unreleased]

### Added

- `minify_css` option [#12](https://github.com/Stranger6667/css-inline/issues/12)

### Changed

- Bump MSRV to `1.80`

## [0.17.0] - 2025-07-26

### Added
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ fn main() -> css_inline::Result<()> {
- `keep_style_tags`. Specifies whether to keep "style" tags after inlining. Default: `false`
- `keep_link_tags`. Specifies whether to keep "link" tags after inlining. Default: `false`
- `keep_at_rules`. Specifies whether to keep "at-rules" (starting with `@`) after inlining. Default: `false`
- `minify_css`. Specifies whether to remove trailing semicolons and spaces between properties and values. Default: `false`
- `base_url`. The base URL used to resolve relative URLs. If you'd like to load stylesheets from your filesystem, use the `file://` scheme. Default: `None`
- `load_remote_stylesheets`. Specifies whether remote stylesheets should be loaded. Default: `true`
- `cache`. Specifies cache for external stylesheets. Default: `None`
Expand Down Expand Up @@ -186,6 +187,19 @@ Such tags will be kept in the resulting HTML even if the `keep_style_tags` optio
</body>
```

If you set the the `minify_css` option to `true`, the inlined styles will be minified by removing trailing semicolons
and spaces between properties and values.

```html
<head>
<!-- With minify_css=true, the <h1> will have `style="color:blue;font-weight:bold"` -->
<style>h1 { color: blue; font-weight: bold; }</style>
</head>
<body>
<h1>Big Text</h1>
</body>
```

If you'd like to load stylesheets from your filesystem, use the `file://` scheme:

```rust
Expand Down
4 changes: 4 additions & 0 deletions bindings/c/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Added

- `minify_css` option [#12](https://github.com/Stranger6667/css-inline/issues/12)

## [0.17.0] - 2025-07-26

### Added
Expand Down
14 changes: 14 additions & 0 deletions bindings/c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ Possible configurations:
- `keep_style_tags`. Specifies whether to keep "style" tags after inlining. Default: `false`
- `keep_link_tags`. Specifies whether to keep "link" tags after inlining. Default: `false`
- `keep_at_rules`. Specifies whether to keep "at-rules" (starting with `@`) after inlining. Default: `false`
- `minify_css`. Specifies whether to remove trailing semicolons and spaces between properties and values.
- `base_url`. The base URL used to resolve relative URLs. If you'd like to load stylesheets from your filesystem, use the `file://` scheme. Default: `NULL`
- `load_remote_stylesheets`. Specifies whether remote stylesheets should be loaded. Default: `true`
- `cache`. Specifies caching options for external stylesheets. Default: `NULL`
Expand Down Expand Up @@ -215,6 +216,19 @@ Such tags will be kept in the resulting HTML even if the `keep_style_tags` optio
</body>
```

If you set the the `minify_css` option to `true`, the inlined styles will be minified by removing trailing semicolons
and spaces between properties and values.

```html
<head>
<!-- With minify_css=true, the <h1> will have `style="color:blue;font-weight:bold"` -->
<style>h1 { color: blue; font-weight: bold; }</style>
</head>
<body>
<h1>Big Text</h1>
</body>
```

You can also cache external stylesheets to avoid excessive network requests:

```c
Expand Down
4 changes: 4 additions & 0 deletions bindings/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ pub struct CssInlinerOptions {
/// Pre-allocate capacity for HTML nodes during parsing.
/// It can improve performance when you have an estimate of the number of nodes in your HTML document.
pub preallocate_node_capacity: size_t,
/// Remove trailing semicolons and spaces between properties and values.
pub minify_css: bool,
}

macro_rules! inliner {
Expand Down Expand Up @@ -186,6 +188,7 @@ pub extern "C" fn css_inliner_default_options() -> CssInlinerOptions {
keep_style_tags: false,
keep_link_tags: false,
keep_at_rules: false,
minify_css: false,
base_url: ptr::null(),
load_remote_stylesheets: true,
cache: std::ptr::null(),
Expand Down Expand Up @@ -229,6 +232,7 @@ impl TryFrom<&CssInlinerOptions> for InlineOptions<'_> {
keep_style_tags: value.keep_style_tags,
keep_link_tags: value.keep_link_tags,
keep_at_rules: value.keep_at_rules,
minify_css: value.minify_css,
base_url: match base_url {
Some(url) => Some(Url::parse(url).map_err(|_| InlineOptionsError::InvalidUrl)?),
None => None,
Expand Down
4 changes: 4 additions & 0 deletions bindings/java/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Added

- `minify_css` option [#12](https://github.com/Stranger6667/css-inline/issues/12)

## [0.17.0] - 2025-07-26

### Added
Expand Down
16 changes: 15 additions & 1 deletion bindings/java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ public class ConfigExample {
- **`setInlineStyleTags(boolean)`** - Inline CSS from `<style>` tags (default: `true`)
- **`setKeepStyleTags(boolean)`** - Keep `<style>` tags after inlining (default: `false`)
- **`setKeepLinkTags(boolean)`** - Keep `<link>` tags after inlining (default: `false`)
- **`setKeepAtRules(boolean`** - Keep `at-rules` (starting with `@`) after inlining (default: `false`)
- **`setKeepAtRules(boolean)`** - Keep `at-rules` (starting with `@`) after inlining (default: `false`)
- **`setMinifyCss(boolean)`** - Remove trailing semicolons and spaces between properties and values (default: `false`)
- **`setBaseUrl(String)`** - Base URL for resolving relative URLs (default: `null`)
- **`setLoadRemoteStylesheets(boolean)`** - Load external stylesheets (default: `true`)
- **`setExtraCss(String)`** - Additional CSS to inline (default: `null`)
Expand Down Expand Up @@ -244,6 +245,19 @@ Such tags will be kept in the resulting HTML even if the `keep_style_tags` optio
</body>
```

If you set the the `minify_css` option to `true`, the inlined styles will be minified by removing trailing semicolons
and spaces between properties and values.

```html
<head>
<!-- With minify_css=true, the <h1> will have `style="color:blue;font-weight:bold"` -->
<style>h1 { color: blue; font-weight: bold; }</style>
</head>
<body>
<h1>Big Text</h1>
</body>
```

## Performance

`css-inline` is powered by efficient tooling from Mozilla's Servo project to provide high-performance CSS inlining for Java applications.
Expand Down
1 change: 1 addition & 0 deletions bindings/java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ repositories {

dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

jmh 'org.openjdk.jmh:jmh-core:1.37'
jmh 'org.openjdk.jmh:jmh-generator-annprocess:1.37'
Expand Down
26 changes: 21 additions & 5 deletions bindings/java/src/main/java/org/cssinline/CssInlineConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public class CssInlineConfig {
/** Keep "at-rules" after inlining. */
public final boolean keepAtRules;

/** Remove trailing semicolons and spaces between properties and values. */
public final boolean minifyCss;

/** Whether remote stylesheets should be loaded or not. */
public final boolean loadRemoteStylesheets;

Expand All @@ -30,12 +33,13 @@ public class CssInlineConfig {
public final int preallocateNodeCapacity;

private CssInlineConfig(boolean inlineStyleTags, boolean keepStyleTags, boolean keepLinkTags,
boolean keepAtRules, boolean loadRemoteStylesheets, String baseUrl, String extraCss, int cacheSize,
int preallocateNodeCapacity) {
boolean keepAtRules, boolean minifyCss, boolean loadRemoteStylesheets, String baseUrl, String extraCss,
int cacheSize, int preallocateNodeCapacity) {
this.inlineStyleTags = inlineStyleTags;
this.keepStyleTags = keepStyleTags;
this.keepLinkTags = keepLinkTags;
this.keepAtRules = keepAtRules;
this.minifyCss = minifyCss;
this.loadRemoteStylesheets = loadRemoteStylesheets;
this.baseUrl = baseUrl;
this.extraCss = extraCss;
Expand All @@ -51,6 +55,7 @@ public static class Builder {
private boolean keepStyleTags = false;
private boolean keepLinkTags = false;
private boolean keepAtRules = false;
private boolean minifyCss = false;
private boolean loadRemoteStylesheets = true;
private String baseUrl = null;
private String extraCss = null;
Expand Down Expand Up @@ -110,6 +115,17 @@ public Builder setKeepAtRules(boolean b) {
return this;
}

/**
* Remove trailing semicolons and spaces between properties and values.
*
* @param b true to remove, false to keep them
* @return this builder instance for method chaining
*/
public Builder setMinifyCss(boolean b) {
this.minifyCss = b;
return this;
}

/**
* Whether remote stylesheets should be loaded or not.
*
Expand Down Expand Up @@ -148,7 +164,7 @@ public Builder setExtraCss(String css) {
*
* @param size
* cache size, must be non-negative
* @return this builder instance for method chaining
* @return this builder instance for method chaining
* @throws IllegalArgumentException
* if size is negative
*/
Expand All @@ -166,7 +182,7 @@ public Builder setCacheSize(int size) {
*
* @param cap
* initial node capacity, must be positive
* @return this builder instance for method chaining
* @return this builder instance for method chaining
* @throws IllegalArgumentException
* if cap is zero or negative
*/
Expand All @@ -184,7 +200,7 @@ public Builder setPreallocateNodeCapacity(int cap) {
* @return a new immutable configuration instance
*/
public CssInlineConfig build() {
return new CssInlineConfig(inlineStyleTags, keepStyleTags, keepLinkTags, keepAtRules, loadRemoteStylesheets, baseUrl,
return new CssInlineConfig(inlineStyleTags, keepStyleTags, keepLinkTags, keepAtRules, minifyCss, loadRemoteStylesheets, baseUrl,
extraCss, cacheSize, preallocateNodeCapacity);
}
}
Expand Down
4 changes: 3 additions & 1 deletion bindings/java/src/main/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ trait JNIExt {

impl<'a> JNIExt for JNIEnv<'a> {
fn get_rust_string(&mut self, obj: &JString) -> String {
self.get_string(&obj)
self.get_string(obj)
.expect("Failed to get Java String")
.into()
}
Expand Down Expand Up @@ -75,6 +75,7 @@ fn build_inliner(
let keep_style_tags = env.get_bool_field(&cfg, "keepStyleTags")?;
let keep_link_tags = env.get_bool_field(&cfg, "keepLinkTags")?;
let keep_at_rules = env.get_bool_field(&cfg, "keepAtRules")?;
let minify_css = env.get_bool_field(&cfg, "minifyCss")?;
let load_remote_stylesheets = env.get_bool_field(&cfg, "loadRemoteStylesheets")?;
let cache_size = env.get_int_field(&cfg, "cacheSize")?;
let preallocate_node_capacity = env.get_int_field(&cfg, "preallocateNodeCapacity")?;
Expand All @@ -86,6 +87,7 @@ fn build_inliner(
.keep_style_tags(keep_style_tags)
.keep_link_tags(keep_link_tags)
.keep_at_rules(keep_at_rules)
.minify_css(minify_css)
.load_remote_stylesheets(load_remote_stylesheets)
.extra_css(extra_css.map(Cow::Owned))
.preallocate_node_capacity(preallocate_node_capacity as usize);
Expand Down
8 changes: 8 additions & 0 deletions bindings/javascript/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## [Unreleased]

### Added

- `minify_css` option [#12](https://github.com/Stranger6667/css-inline/issues/12)

### Changed

- Bump MSRV to `1.80`

## [0.17.0] - 2025-07-26

### Added
Expand Down
2 changes: 1 addition & 1 deletion bindings/javascript/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repository = "https://github.com/Stranger6667/css-inline"
keywords = ["css", "html", "email", "stylesheet", "inlining"]
categories = ["web-programming"]
license = "MIT"
rust-version = "1.77"
rust-version = "1.80"
include = ["src/*.rs", "LICENSE", "README.md", "CHANGELOG.md"]

[lib]
Expand Down
14 changes: 14 additions & 0 deletions bindings/javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ var inlined = inlineFragment(
- `keepStyleTags`. Specifies whether to keep "style" tags after inlining. Default: `false`
- `keepLinkTags`. Specifies whether to keep "link" tags after inlining. Default: `false`
- `keepAtRules`. Specifies whether to keep "at-rules" (starting with `@`) after inlining. Default: `false`
- `minifyCss`. Specifies whether to remove trailing semicolons and spaces between properties and values. Default: `false`
- `baseUrl`. The base URL used to resolve relative URLs. If you'd like to load stylesheets from your filesystem, use the `file://` scheme. Default: `null`
- `loadRemoteStylesheets`. Specifies whether remote stylesheets should be loaded. Default: `true`
- `cache`. Specifies caching options for external stylesheets (for example, `{size: 5}`). Default: `null`
Expand Down Expand Up @@ -177,6 +178,19 @@ Such tags will be kept in the resulting HTML even if the `keep_style_tags` optio
</body>
```

If you set the the `minify_css` option to `true`, the inlined styles will be minified by removing trailing semicolons
and spaces between properties and values.

```html
<head>
<!-- With minify_css=true, the <h1> will have `style="color:blue;font-weight:bold"` -->
<style>h1 { color: blue; font-weight: bold; }</style>
</head>
<body>
<h1>Big Text</h1>
</body>
```

You can also cache external stylesheets to avoid excessive network requests:

```typescript
Expand Down
2 changes: 2 additions & 0 deletions bindings/javascript/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export interface Options {
keepLinkTags?: boolean
/** Keep "at-rules" after inlining. */
keepAtRules?: boolean
/** Remove trailing semicolons and spaces between properties and values. */
minifyCss?: boolean
/** Used for loading external stylesheets via relative URLs. */
baseUrl?: string
/** Whether remote stylesheets should be loaded or not. */
Expand Down
4 changes: 4 additions & 0 deletions bindings/javascript/js-binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export interface Options {
keepStyleTags?: boolean
/** Keep "link" tags after inlining. */
keepLinkTags?: boolean
/** Keep "at-rules" after inlining. */
keepAtRules?: boolean
/** Remove trailing semicolons and spaces between properties and values. */
minifyCss?: boolean
/** Used for loading external stylesheets via relative URLs. */
baseUrl?: string
/** Whether remote stylesheets should be loaded or not. */
Expand Down
1 change: 1 addition & 0 deletions bindings/javascript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const INLINE: &'static str = r#"export interface InlineOptions {
keepStyleTags?: boolean,
keepLinkTags?: boolean,
keepAtRules?: boolean,
minifyCss?: boolean,
baseUrl?: string,
loadRemoteStylesheets?: boolean,
extraCss?: string,
Expand Down
3 changes: 3 additions & 0 deletions bindings/javascript/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub struct Options {
pub keep_link_tags: Option<bool>,
/// Keep "at-rules" after inlining.
pub keep_at_rules: Option<bool>,
/// Remove trailing semicolons and spaces between properties and values.
pub minify_css: Option<bool>,
/// Used for loading external stylesheets via relative URLs.
pub base_url: Option<String>,
/// Whether remote stylesheets should be loaded or not.
Expand All @@ -65,6 +67,7 @@ impl TryFrom<Options> for css_inline::InlineOptions<'_> {
keep_style_tags: value.keep_style_tags.unwrap_or(false),
keep_link_tags: value.keep_link_tags.unwrap_or(false),
keep_at_rules: value.keep_at_rules.unwrap_or(false),
minify_css: value.minify_css.unwrap_or(false),
base_url: parse_url(value.base_url)?,
load_remote_stylesheets: value.load_remote_stylesheets.unwrap_or(true),
extra_css: value.extra_css.map(Cow::Owned),
Expand Down
1 change: 1 addition & 0 deletions bindings/javascript/wasm/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface InlineOptions {
keepStyleTags?: boolean;
keepLinkTags?: boolean;
keepAtRules?: boolean;
minifyCss?: boolean;
baseUrl?: string;
loadRemoteStylesheets?: boolean;
extraCss?: string;
Expand Down
Loading
Loading