Skip to content

Commit c3ee65d

Browse files
authored
feat: Add option to minify CSS (#559)
* feat: Add option to minify CSS. * chore: Add junit-platform-launcher. * chore: Run `fmt` and `clippy` on java bindings. * chore: Update docs. * chore: Update ruby build to use local css-inline. * chore: Fix python clippy doc warnings. * feat: Add keep_at_rules option to python and javascript bindings. * chore: Bump MSRV to 1.80.
1 parent e189022 commit c3ee65d

37 files changed

+852
-647
lines changed

.github/workflows/build.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ jobs:
107107
- run: cargo fmt --all -- --check
108108
working-directory: ./bindings/c
109109

110+
- run: cargo fmt --all -- --check
111+
working-directory: ./bindings/java
112+
110113
- run: cargo fmt --all -- --check
111114
working-directory: ./bindings/javascript
112115

@@ -148,6 +151,10 @@ jobs:
148151
run: cargo clippy -- -D warnings
149152
working-directory: ./bindings/python
150153

154+
- name: Java
155+
run: cargo clippy -- -D warnings
156+
working-directory: ./bindings/java
157+
151158
- name: JavaScript
152159
run: cargo clippy -- -D warnings
153160
working-directory: ./bindings/javascript

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
## [Unreleased]
44

5+
### Added
6+
7+
- `minify_css` option [#12](https://github.com/Stranger6667/css-inline/issues/12)
8+
9+
### Changed
10+
11+
- Bump MSRV to `1.80`
12+
513
## [0.17.0] - 2025-07-26
614

715
### Added

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ fn main() -> css_inline::Result<()> {
127127
- `keep_style_tags`. Specifies whether to keep "style" tags after inlining. Default: `false`
128128
- `keep_link_tags`. Specifies whether to keep "link" tags after inlining. Default: `false`
129129
- `keep_at_rules`. Specifies whether to keep "at-rules" (starting with `@`) after inlining. Default: `false`
130+
- `minify_css`. Specifies whether to remove trailing semicolons and spaces between properties and values. Default: `false`
130131
- `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`
131132
- `load_remote_stylesheets`. Specifies whether remote stylesheets should be loaded. Default: `true`
132133
- `cache`. Specifies cache for external stylesheets. Default: `None`
@@ -186,6 +187,19 @@ Such tags will be kept in the resulting HTML even if the `keep_style_tags` optio
186187
</body>
187188
```
188189

190+
If you set the the `minify_css` option to `true`, the inlined styles will be minified by removing trailing semicolons
191+
and spaces between properties and values.
192+
193+
```html
194+
<head>
195+
<!-- With minify_css=true, the <h1> will have `style="color:blue;font-weight:bold"` -->
196+
<style>h1 { color: blue; font-weight: bold; }</style>
197+
</head>
198+
<body>
199+
<h1>Big Text</h1>
200+
</body>
201+
```
202+
189203
If you'd like to load stylesheets from your filesystem, use the `file://` scheme:
190204

191205
```rust

bindings/c/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Added
6+
7+
- `minify_css` option [#12](https://github.com/Stranger6667/css-inline/issues/12)
8+
59
## [0.17.0] - 2025-07-26
610

711
### Added

bindings/c/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ Possible configurations:
154154
- `keep_style_tags`. Specifies whether to keep "style" tags after inlining. Default: `false`
155155
- `keep_link_tags`. Specifies whether to keep "link" tags after inlining. Default: `false`
156156
- `keep_at_rules`. Specifies whether to keep "at-rules" (starting with `@`) after inlining. Default: `false`
157+
- `minify_css`. Specifies whether to remove trailing semicolons and spaces between properties and values.
157158
- `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`
158159
- `load_remote_stylesheets`. Specifies whether remote stylesheets should be loaded. Default: `true`
159160
- `cache`. Specifies caching options for external stylesheets. Default: `NULL`
@@ -215,6 +216,19 @@ Such tags will be kept in the resulting HTML even if the `keep_style_tags` optio
215216
</body>
216217
```
217218

219+
If you set the the `minify_css` option to `true`, the inlined styles will be minified by removing trailing semicolons
220+
and spaces between properties and values.
221+
222+
```html
223+
<head>
224+
<!-- With minify_css=true, the <h1> will have `style="color:blue;font-weight:bold"` -->
225+
<style>h1 { color: blue; font-weight: bold; }</style>
226+
</head>
227+
<body>
228+
<h1>Big Text</h1>
229+
</body>
230+
```
231+
218232
You can also cache external stylesheets to avoid excessive network requests:
219233

220234
```c

bindings/c/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ pub struct CssInlinerOptions {
9595
/// Pre-allocate capacity for HTML nodes during parsing.
9696
/// It can improve performance when you have an estimate of the number of nodes in your HTML document.
9797
pub preallocate_node_capacity: size_t,
98+
/// Remove trailing semicolons and spaces between properties and values.
99+
pub minify_css: bool,
98100
}
99101

100102
macro_rules! inliner {
@@ -186,6 +188,7 @@ pub extern "C" fn css_inliner_default_options() -> CssInlinerOptions {
186188
keep_style_tags: false,
187189
keep_link_tags: false,
188190
keep_at_rules: false,
191+
minify_css: false,
189192
base_url: ptr::null(),
190193
load_remote_stylesheets: true,
191194
cache: std::ptr::null(),
@@ -229,6 +232,7 @@ impl TryFrom<&CssInlinerOptions> for InlineOptions<'_> {
229232
keep_style_tags: value.keep_style_tags,
230233
keep_link_tags: value.keep_link_tags,
231234
keep_at_rules: value.keep_at_rules,
235+
minify_css: value.minify_css,
232236
base_url: match base_url {
233237
Some(url) => Some(Url::parse(url).map_err(|_| InlineOptionsError::InvalidUrl)?),
234238
None => None,

bindings/java/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Added
6+
7+
- `minify_css` option [#12](https://github.com/Stranger6667/css-inline/issues/12)
8+
59
## [0.17.0] - 2025-07-26
610

711
### Added

bindings/java/README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ public class ConfigExample {
146146
- **`setInlineStyleTags(boolean)`** - Inline CSS from `<style>` tags (default: `true`)
147147
- **`setKeepStyleTags(boolean)`** - Keep `<style>` tags after inlining (default: `false`)
148148
- **`setKeepLinkTags(boolean)`** - Keep `<link>` tags after inlining (default: `false`)
149-
- **`setKeepAtRules(boolean`** - Keep `at-rules` (starting with `@`) after inlining (default: `false`)
149+
- **`setKeepAtRules(boolean)`** - Keep `at-rules` (starting with `@`) after inlining (default: `false`)
150+
- **`setMinifyCss(boolean)`** - Remove trailing semicolons and spaces between properties and values (default: `false`)
150151
- **`setBaseUrl(String)`** - Base URL for resolving relative URLs (default: `null`)
151152
- **`setLoadRemoteStylesheets(boolean)`** - Load external stylesheets (default: `true`)
152153
- **`setExtraCss(String)`** - Additional CSS to inline (default: `null`)
@@ -244,6 +245,19 @@ Such tags will be kept in the resulting HTML even if the `keep_style_tags` optio
244245
</body>
245246
```
246247

248+
If you set the the `minify_css` option to `true`, the inlined styles will be minified by removing trailing semicolons
249+
and spaces between properties and values.
250+
251+
```html
252+
<head>
253+
<!-- With minify_css=true, the <h1> will have `style="color:blue;font-weight:bold"` -->
254+
<style>h1 { color: blue; font-weight: bold; }</style>
255+
</head>
256+
<body>
257+
<h1>Big Text</h1>
258+
</body>
259+
```
260+
247261
## Performance
248262

249263
`css-inline` is powered by efficient tooling from Mozilla's Servo project to provide high-performance CSS inlining for Java applications.

bindings/java/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ repositories {
1818

1919
dependencies {
2020
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
21+
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
2122

2223
jmh 'org.openjdk.jmh:jmh-core:1.37'
2324
jmh 'org.openjdk.jmh:jmh-generator-annprocess:1.37'

bindings/java/src/main/java/org/cssinline/CssInlineConfig.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ public class CssInlineConfig {
1414
/** Keep "at-rules" after inlining. */
1515
public final boolean keepAtRules;
1616

17+
/** Remove trailing semicolons and spaces between properties and values. */
18+
public final boolean minifyCss;
19+
1720
/** Whether remote stylesheets should be loaded or not. */
1821
public final boolean loadRemoteStylesheets;
1922

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

3235
private CssInlineConfig(boolean inlineStyleTags, boolean keepStyleTags, boolean keepLinkTags,
33-
boolean keepAtRules, boolean loadRemoteStylesheets, String baseUrl, String extraCss, int cacheSize,
34-
int preallocateNodeCapacity) {
36+
boolean keepAtRules, boolean minifyCss, boolean loadRemoteStylesheets, String baseUrl, String extraCss,
37+
int cacheSize, int preallocateNodeCapacity) {
3538
this.inlineStyleTags = inlineStyleTags;
3639
this.keepStyleTags = keepStyleTags;
3740
this.keepLinkTags = keepLinkTags;
3841
this.keepAtRules = keepAtRules;
42+
this.minifyCss = minifyCss;
3943
this.loadRemoteStylesheets = loadRemoteStylesheets;
4044
this.baseUrl = baseUrl;
4145
this.extraCss = extraCss;
@@ -51,6 +55,7 @@ public static class Builder {
5155
private boolean keepStyleTags = false;
5256
private boolean keepLinkTags = false;
5357
private boolean keepAtRules = false;
58+
private boolean minifyCss = false;
5459
private boolean loadRemoteStylesheets = true;
5560
private String baseUrl = null;
5661
private String extraCss = null;
@@ -110,6 +115,17 @@ public Builder setKeepAtRules(boolean b) {
110115
return this;
111116
}
112117

118+
/**
119+
* Remove trailing semicolons and spaces between properties and values.
120+
*
121+
* @param b true to remove, false to keep them
122+
* @return this builder instance for method chaining
123+
*/
124+
public Builder setMinifyCss(boolean b) {
125+
this.minifyCss = b;
126+
return this;
127+
}
128+
113129
/**
114130
* Whether remote stylesheets should be loaded or not.
115131
*
@@ -148,7 +164,7 @@ public Builder setExtraCss(String css) {
148164
*
149165
* @param size
150166
* cache size, must be non-negative
151-
* @return this builder instance for method chaining
167+
* @return this builder instance for method chaining
152168
* @throws IllegalArgumentException
153169
* if size is negative
154170
*/
@@ -166,7 +182,7 @@ public Builder setCacheSize(int size) {
166182
*
167183
* @param cap
168184
* initial node capacity, must be positive
169-
* @return this builder instance for method chaining
185+
* @return this builder instance for method chaining
170186
* @throws IllegalArgumentException
171187
* if cap is zero or negative
172188
*/
@@ -184,7 +200,7 @@ public Builder setPreallocateNodeCapacity(int cap) {
184200
* @return a new immutable configuration instance
185201
*/
186202
public CssInlineConfig build() {
187-
return new CssInlineConfig(inlineStyleTags, keepStyleTags, keepLinkTags, keepAtRules, loadRemoteStylesheets, baseUrl,
203+
return new CssInlineConfig(inlineStyleTags, keepStyleTags, keepLinkTags, keepAtRules, minifyCss, loadRemoteStylesheets, baseUrl,
188204
extraCss, cacheSize, preallocateNodeCapacity);
189205
}
190206
}

0 commit comments

Comments
 (0)