Skip to content

Commit 266bb5f

Browse files
committed
feat: remove inlined selectors
Signed-off-by: Dmitry Dygalo <[email protected]>
1 parent c66c8b0 commit 266bb5f

File tree

35 files changed

+720
-42
lines changed

35 files changed

+720
-42
lines changed

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+
- `InlineOptions::remove_inlined_selectors` option to remove selectors that were successfully inlined from `<style>` blocks.
8+
59
### Changed
610

711
- Update `cssparser` to `0.36`.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ fn main() -> css_inline::Result<()> {
133133
- `cache`. Specifies cache for external stylesheets. Default: `None`
134134
- `extra_css`. Extra CSS to be inlined. Default: `None`
135135
- `preallocate_node_capacity`. **Advanced**. Preallocates capacity for HTML nodes during parsing. This can improve performance when you have an estimate of the number of nodes in your HTML document. Default: `32`
136+
- `remove_inlined_selectors`. Specifies whether to remove selectors that were successfully inlined from `<style>` blocks. Default: `false`
136137

137138
You can also skip CSS inlining for an HTML tag by adding the `data-css-inline="ignore"` attribute to it:
138139

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+
- `remove_inlined_selectors` option to remove selectors that were successfully inlined from `<style>` blocks.
8+
59
### Changed
610

711
- Update `cssparser` to `0.36`.

bindings/c/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ Possible configurations:
160160
- `cache`. Specifies caching options for external stylesheets. Default: `NULL`
161161
- `extra_css`. Extra CSS to be inlined. Default: `NULL`
162162
- `preallocate_node_capacity`. **Advanced**. Preallocates capacity for HTML nodes during parsing. This can improve performance when you have an estimate of the number of nodes in your HTML document. Default: `32`
163+
- `remove_inlined_selectors`. Specifies whether to remove selectors that were successfully inlined from `<style>` blocks. Default: `false`
163164
164165
You can also skip CSS inlining for an HTML tag by adding the `data-css-inline="ignore"` attribute to it:
165166

bindings/c/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ pub struct CssInlinerOptions {
9797
pub preallocate_node_capacity: size_t,
9898
/// Remove trailing semicolons and spaces between properties and values.
9999
pub minify_css: bool,
100+
/// Remove selectors that were successfully inlined from inline `<style>` blocks.
101+
pub remove_inlined_selectors: bool,
100102
}
101103

102104
macro_rules! inliner {
@@ -198,6 +200,7 @@ pub extern "C" fn css_inliner_default_options() -> CssInlinerOptions {
198200
cache: std::ptr::null(),
199201
extra_css: ptr::null(),
200202
preallocate_node_capacity: 32,
203+
remove_inlined_selectors: false,
201204
}
202205
}
203206

@@ -254,6 +257,7 @@ impl TryFrom<&CssInlinerOptions> for InlineOptions<'_> {
254257
extra_css: extra_css.map(Cow::Borrowed),
255258
preallocate_node_capacity: value.preallocate_node_capacity,
256259
resolver: Arc::new(DefaultStylesheetResolver),
260+
remove_inlined_selectors: value.remove_inlined_selectors,
257261
})
258262
}
259263
}

bindings/c/tests/main.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ static void test_inline_fragment(void) {
133133
assert(strcmp(output, SAMPLE_INLINED_FRAGMENT) == 0);
134134
}
135135

136+
static void test_remove_inlined_selectors(void) {
137+
char html[] = "<html><head><style>h1 { color: blue; } h2 { color: red; }</style></head><body><h1>Test</h1></body></html>";
138+
CssInlinerOptions options = css_inliner_default_options();
139+
options.remove_inlined_selectors = true;
140+
char output[MAX_SIZE];
141+
assert(css_inline_to(&options, html, output, sizeof(output)) == CSS_RESULT_OK);
142+
assert(strcmp(output, "<html><head><style>h2 { color: red; }</style></head><body><h1 style=\"color: blue;\">Test</h1></body></html>") == 0);
143+
}
144+
136145
int main(void) {
137146
test_default_options();
138147
test_output_size_too_small();
@@ -142,5 +151,6 @@ int main(void) {
142151
test_cache_valid();
143152
test_cache_invalid();
144153
test_inline_fragment();
154+
test_remove_inlined_selectors();
145155
return 0;
146156
}

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+
- `removeInlinedSelectors` option to remove selectors that were successfully inlined from `<style>` blocks.
8+
59
### Changed
610

711
- Update `cssparser` to `0.36`.

bindings/java/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public class ConfigExample {
153153
- **`setExtraCss(String)`** - Additional CSS to inline (default: `null`)
154154
- **`setCacheSize(int)`** - External stylesheet cache size, must be ≥ 0 (default: `0`)
155155
- **`setPreallocateNodeCapacity(int)`** - HTML node capacity, must be > 0 (default: `32`)
156+
- **`setRemoveInlinedSelectors(boolean)`** - Remove selectors that were successfully inlined from `<style>` blocks (default: `false`)
156157

157158

158159
### HTML Fragments

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ public class CssInlineConfig {
3232
/** Pre-allocate capacity for HTML nodes during parsing. */
3333
public final int preallocateNodeCapacity;
3434

35+
/** Remove selectors that were successfully inlined from inline style blocks. */
36+
public final boolean removeInlinedSelectors;
37+
3538
private CssInlineConfig(boolean inlineStyleTags, boolean keepStyleTags, boolean keepLinkTags,
3639
boolean keepAtRules, boolean minifyCss, boolean loadRemoteStylesheets, String baseUrl, String extraCss,
37-
int cacheSize, int preallocateNodeCapacity) {
40+
int cacheSize, int preallocateNodeCapacity, boolean removeInlinedSelectors) {
3841
this.inlineStyleTags = inlineStyleTags;
3942
this.keepStyleTags = keepStyleTags;
4043
this.keepLinkTags = keepLinkTags;
@@ -45,6 +48,7 @@ private CssInlineConfig(boolean inlineStyleTags, boolean keepStyleTags, boolean
4548
this.extraCss = extraCss;
4649
this.cacheSize = cacheSize;
4750
this.preallocateNodeCapacity = preallocateNodeCapacity;
51+
this.removeInlinedSelectors = removeInlinedSelectors;
4852
}
4953

5054
/**
@@ -61,6 +65,7 @@ public static class Builder {
6165
private String extraCss = null;
6266
private int cacheSize = 0;
6367
private int preallocateNodeCapacity = 32;
68+
private boolean removeInlinedSelectors = false;
6469

6570
/**
6671
* Creates a new builder with default configuration values.
@@ -194,14 +199,25 @@ public Builder setPreallocateNodeCapacity(int cap) {
194199
return this;
195200
}
196201

202+
/**
203+
* Remove selectors that were successfully inlined from inline style blocks.
204+
*
205+
* @param b true to remove inlined selectors, false to keep them
206+
* @return this builder instance for method chaining
207+
*/
208+
public Builder setRemoveInlinedSelectors(boolean b) {
209+
this.removeInlinedSelectors = b;
210+
return this;
211+
}
212+
197213
/**
198214
* Creates a new {@link CssInlineConfig} instance with the current builder settings.
199215
*
200216
* @return a new immutable configuration instance
201217
*/
202218
public CssInlineConfig build() {
203219
return new CssInlineConfig(inlineStyleTags, keepStyleTags, keepLinkTags, keepAtRules, minifyCss, loadRemoteStylesheets, baseUrl,
204-
extraCss, cacheSize, preallocateNodeCapacity);
220+
extraCss, cacheSize, preallocateNodeCapacity, removeInlinedSelectors);
205221
}
206222
}
207223
}

bindings/java/src/main/rust/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ fn build_inliner(
7979
let load_remote_stylesheets = env.get_bool_field(&cfg, "loadRemoteStylesheets")?;
8080
let cache_size = env.get_int_field(&cfg, "cacheSize")?;
8181
let preallocate_node_capacity = env.get_int_field(&cfg, "preallocateNodeCapacity")?;
82+
let remove_inlined_selectors = env.get_bool_field(&cfg, "removeInlinedSelectors")?;
8283

8384
let extra_css = env.get_string_field_opt(&cfg, "extraCss")?;
8485
let base_url = env.get_string_field_opt(&cfg, "baseUrl")?;
@@ -90,7 +91,8 @@ fn build_inliner(
9091
.minify_css(minify_css)
9192
.load_remote_stylesheets(load_remote_stylesheets)
9293
.extra_css(extra_css.map(Cow::Owned))
93-
.preallocate_node_capacity(preallocate_node_capacity as usize);
94+
.preallocate_node_capacity(preallocate_node_capacity as usize)
95+
.remove_inlined_selectors(remove_inlined_selectors);
9496

9597
if let Some(url) = base_url {
9698
match css_inline::Url::parse(&url) {

0 commit comments

Comments
 (0)