From da4f68688575a146d31743cc29680cb75fd8518c Mon Sep 17 00:00:00 2001 From: Dmitry Dygalo Date: Mon, 16 Nov 2026 18:00:02 +0100 Subject: [PATCH] feat: Add keep_classes option --- CHANGELOG.md | 1 + README.md | 1 + crates/css-inline-c/CHANGELOG.md | 1 + crates/css-inline-c/README.md | 1 + crates/css-inline-c/src/lib.rs | 4 ++ crates/css-inline-c/tests/main.c | 10 +++++ crates/css-inline-java/CHANGELOG.md | 1 + crates/css-inline-java/README.md | 1 + .../java/org/cssinline/CssInlineConfig.java | 19 +++++++- crates/css-inline-java/src/main/rust/lib.rs | 4 +- .../java/org/cssinline/CssInlineTest.java | 11 +++++ crates/css-inline-js/CHANGELOG.md | 1 + crates/css-inline-js/README.md | 1 + crates/css-inline-js/__test__/index.spec.ts | 10 +++++ crates/css-inline-js/__test__/wasm.spec.ts | 10 +++++ crates/css-inline-js/index.d.ts | 2 + crates/css-inline-js/js-binding.d.ts | 2 + crates/css-inline-js/src/lib.rs | 1 + crates/css-inline-js/src/options.rs | 3 ++ crates/css-inline-js/wasm/README.md | 2 +- crates/css-inline-js/wasm/index.d.ts | 1 + crates/css-inline-php/CHANGELOG.md | 1 + crates/css-inline-php/README.md | 1 + crates/css-inline-php/src/lib.rs | 3 ++ crates/css-inline-php/stubs/css_inline.php | 2 + crates/css-inline-php/tests/CssInlineTest.php | 15 +++++++ crates/css-inline-python/CHANGELOG.md | 1 + crates/css-inline-python/README.md | 1 + crates/css-inline-python/css_inline.pyi | 5 +++ crates/css-inline-python/src/lib.rs | 43 ++++++++++++------- .../tests-py/test_inlining.py | 13 ++++++ crates/css-inline-ruby/CHANGELOG.md | 1 + crates/css-inline-ruby/README.md | 1 + .../css-inline-ruby/spec/css_inline_spec.rb | 7 +++ crates/css-inline-ruby/src/lib.rs | 4 ++ crates/css-inline/src/html/document.rs | 3 ++ crates/css-inline/src/html/serializer.rs | 24 ++++++++++- crates/css-inline/src/lib.rs | 14 ++++++ crates/css-inline/src/main.rs | 10 +++++ crates/css-inline/tests/test_cli.rs | 10 +++++ crates/css-inline/tests/test_inlining.rs | 25 ++++++++++- 41 files changed, 248 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dbc58065..0b1259d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Added - `media_type` option to inline rules from `@media` blocks that apply to the given media type. [#410](https://github.com/Stranger6667/css-inline/issues/410) +- `keep_classes` option to remove `class` attributes from the output. [#13](https://github.com/Stranger6667/css-inline/issues/13) ### Changed diff --git a/README.md b/README.md index ddca3703..ba544d8e 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,7 @@ fn main() -> css_inline::Result<()> { - `apply_height_attributes`. Add `height` HTML attributes from CSS `height` properties on `table`, `td`, `th` and `img`. Default: `false` - `resolve_css_variables`. Replace `var()` references with the values of the custom properties they refer to and drop the `--*` declarations. Default: `false` - `media_type`. Inline rules from `@media` blocks whose query lists this media type or `all`, e.g. `@media print` or `@media screen, print` for `print`. Queries with media features, such as `print and (min-width: 600px)`, never apply. An inlined block is not kept as an at-rule. Ignored when `keep_at_rules` is enabled and `inline_style_tags` is disabled. Default: `None` +- `keep_classes`. Keep `class` attributes in the output. When disabled, class selectors in kept `

Test

") == 0); } +static void test_keep_classes(void) { + char html[] = "

Test

"; + CssInlinerOptions options = css_inliner_default_options(); + options.keep_classes = false; + char output[MAX_SIZE]; + assert(css_inline_to(&options, html, output, sizeof(output)) == CSS_RESULT_OK); + assert(strcmp(output, "

Test

") == 0); +} + static void test_resolve_css_variables(void) { char html[] = "

Test

"; CssInlinerOptions options = css_inliner_default_options(); @@ -184,6 +193,7 @@ int main(void) { test_inline_fragment(); test_inline_fragment_keep_at_rules(); test_remove_inlined_selectors(); + test_keep_classes(); test_resolve_css_variables(); test_media_type(); return 0; diff --git a/crates/css-inline-java/CHANGELOG.md b/crates/css-inline-java/CHANGELOG.md index d093cec2..cc0caa1d 100644 --- a/crates/css-inline-java/CHANGELOG.md +++ b/crates/css-inline-java/CHANGELOG.md @@ -5,6 +5,7 @@ ### Added - `setMediaType` option to inline rules from `@media` blocks that apply to the given media type. [#410](https://github.com/Stranger6667/css-inline/issues/410) +- `keepClasses` option to remove `class` attributes from the output. [#13](https://github.com/Stranger6667/css-inline/issues/13) ### Changed diff --git a/crates/css-inline-java/README.md b/crates/css-inline-java/README.md index cba32353..1e96fde8 100644 --- a/crates/css-inline-java/README.md +++ b/crates/css-inline-java/README.md @@ -181,6 +181,7 @@ public class ConfigExample { - **`setApplyHeightAttributes(boolean)`** - Add `height` HTML attributes from CSS `height` properties on supported elements (`table`, `td`, `th`, `img`) (default: `false`) - **`setResolveCssVariables(boolean)`** - Replace `var()` references with the values of the custom properties they refer to and drop the `--*` declarations (default: `false`) - **`setMediaType(String)`** - Inline rules from `@media` blocks whose query lists this media type or `all`, e.g. `@media print` or `@media screen, print` for `print`. Queries with media features, such as `print and (min-width: 600px)`, never apply. An inlined block is not kept as an at-rule; ignored with `setKeepAtRules(true)` and `setInlineStyleTags(false)` (default: `null`) +- **`setKeepClasses(boolean)`** - Keep `class` attributes in the output; when disabled, class selectors in kept `

Test

", out); } + @Test + void removeClasses() { + String html = "

Test

"; + + CssInlineConfig cfg = new CssInlineConfig.Builder().setKeepClasses(false).build(); + + String out = CssInline.inline(html, cfg); + + assertEquals("

Test

", out); + } + @Test void keepsCssVariablesByDefault() { String html = "

Test

"; diff --git a/crates/css-inline-js/CHANGELOG.md b/crates/css-inline-js/CHANGELOG.md index 49adca3d..d56e6b4c 100644 --- a/crates/css-inline-js/CHANGELOG.md +++ b/crates/css-inline-js/CHANGELOG.md @@ -5,6 +5,7 @@ ### Added - `mediaType` option to inline rules from `@media` blocks that apply to the given media type. [#410](https://github.com/Stranger6667/css-inline/issues/410) +- `keepClasses` option to remove `class` attributes from the output. [#13](https://github.com/Stranger6667/css-inline/issues/13) ### Changed diff --git a/crates/css-inline-js/README.md b/crates/css-inline-js/README.md index fb9fd6c5..93585e98 100644 --- a/crates/css-inline-js/README.md +++ b/crates/css-inline-js/README.md @@ -94,6 +94,7 @@ Pass options as the second argument to `inline` or the third to `inlineFragment` - `applyHeightAttributes`. Add `height` HTML attributes from CSS `height` properties on `table`, `td`, `th` and `img`. Default: `false` - `resolveCssVariables`. Replace `var()` references with the values of the custom properties they refer to and drop the `--*` declarations. Default: `false` - `mediaType`. Inline rules from `@media` blocks whose query lists this media type or `all`, e.g. `@media print` or `@media screen, print` for `print`. Queries with media features, such as `print and (min-width: 600px)`, never apply. An inlined block is not kept as an at-rule. Ignored when `keepAtRules` is enabled and `inlineStyleTags` is disabled. Default: `null` +- `keepClasses`. Keep `class` attributes in the output. When disabled, class selectors in kept `

Test

', + { keepClasses: false }, + ), + '

Test

', + ); +}); + const cssVariablesHtml = "

Test

"; diff --git a/crates/css-inline-js/__test__/wasm.spec.ts b/crates/css-inline-js/__test__/wasm.spec.ts index f3c07016..44b0592c 100644 --- a/crates/css-inline-js/__test__/wasm.spec.ts +++ b/crates/css-inline-js/__test__/wasm.spec.ts @@ -133,6 +133,16 @@ h1 { ); }); +test("remove classes", (t) => { + t.is( + inline( + '

Test

', + { keepClasses: false }, + ), + '

Test

', + ); +}); + const cssVariablesHtml = "

Test

"; diff --git a/crates/css-inline-js/index.d.ts b/crates/css-inline-js/index.d.ts index 8d8fb659..ab7ac2c9 100644 --- a/crates/css-inline-js/index.d.ts +++ b/crates/css-inline-js/index.d.ts @@ -51,6 +51,8 @@ export interface Options { resolveCssVariables?: boolean /** Inline rules from `@media` blocks that apply to this media type, e.g. `print`. Queries with media features never apply. */ mediaType?: string + /** Keep `class` attributes in the output. */ + keepClasses?: boolean } export interface StylesheetCache { diff --git a/crates/css-inline-js/js-binding.d.ts b/crates/css-inline-js/js-binding.d.ts index cfd89f57..8b2a53f2 100644 --- a/crates/css-inline-js/js-binding.d.ts +++ b/crates/css-inline-js/js-binding.d.ts @@ -70,6 +70,8 @@ export interface Options { * Queries with media features, such as `(min-width: 600px)`, never apply. */ mediaType?: string + /** Keep `class` attributes in the output. */ + keepClasses?: boolean } export interface StylesheetCache { diff --git a/crates/css-inline-js/src/lib.rs b/crates/css-inline-js/src/lib.rs index 5e515547..d1f73f4c 100644 --- a/crates/css-inline-js/src/lib.rs +++ b/crates/css-inline-js/src/lib.rs @@ -139,6 +139,7 @@ const INLINE: &'static str = r#"export interface InlineOptions { applyHeightAttributes?: boolean, resolveCssVariables?: boolean, mediaType?: string, + keepClasses?: boolean, } export function inline(html: string, options?: InlineOptions): string; diff --git a/crates/css-inline-js/src/options.rs b/crates/css-inline-js/src/options.rs index 4ec320f6..b598413a 100644 --- a/crates/css-inline-js/src/options.rs +++ b/crates/css-inline-js/src/options.rs @@ -77,6 +77,8 @@ pub struct Options { /// /// Queries with media features, such as `(min-width: 600px)`, never apply. pub media_type: Option, + /// Keep `class` attributes in the output. + pub keep_classes: Option, } impl TryFrom for css_inline::InlineOptions<'_> { @@ -168,6 +170,7 @@ impl TryFrom for css_inline::InlineOptions<'_> { apply_height_attributes: value.apply_height_attributes.unwrap_or(false), resolve_css_variables: value.resolve_css_variables.unwrap_or(false), media_type: value.media_type, + keep_classes: value.keep_classes.unwrap_or(true), }) } } diff --git a/crates/css-inline-js/wasm/README.md b/crates/css-inline-js/wasm/README.md index 9c66758f..2f365cd8 100644 --- a/crates/css-inline-js/wasm/README.md +++ b/crates/css-inline-js/wasm/README.md @@ -63,7 +63,7 @@ From a CDN, the script defines a global `cssInline`. Pin the version in both URL - `version()` Options match the [Node.js package](https://github.com/Stranger6667/css-inline/tree/master/crates/css-inline-js#configuration) except `cache`, which WASM lacks: -`inlineStyleTags`, `keepStyleTags`, `keepLinkTags`, `keepAtRules`, `minifyCss`, `baseUrl`, `loadRemoteStylesheets`, `extraCss`, `preallocateNodeCapacity`, `removeInlinedSelectors`, `applyWidthAttributes`, `applyHeightAttributes`, `resolveCssVariables`, `mediaType`. +`inlineStyleTags`, `keepStyleTags`, `keepLinkTags`, `keepAtRules`, `minifyCss`, `baseUrl`, `loadRemoteStylesheets`, `extraCss`, `preallocateNodeCapacity`, `removeInlinedSelectors`, `applyWidthAttributes`, `applyHeightAttributes`, `resolveCssVariables`, `mediaType`, `keepClasses`. ## Playground diff --git a/crates/css-inline-js/wasm/index.d.ts b/crates/css-inline-js/wasm/index.d.ts index 6bff45d7..518fd8be 100644 --- a/crates/css-inline-js/wasm/index.d.ts +++ b/crates/css-inline-js/wasm/index.d.ts @@ -17,6 +17,7 @@ export interface InlineOptions { applyHeightAttributes?: boolean; resolveCssVariables?: boolean; mediaType?: string; + keepClasses?: boolean; } export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module; /** diff --git a/crates/css-inline-php/CHANGELOG.md b/crates/css-inline-php/CHANGELOG.md index aaec6448..00adc15c 100644 --- a/crates/css-inline-php/CHANGELOG.md +++ b/crates/css-inline-php/CHANGELOG.md @@ -5,6 +5,7 @@ ### Added - `mediaType` option to inline rules from `@media` blocks that apply to the given media type. [#410](https://github.com/Stranger6667/css-inline/issues/410) +- `keepClasses` option to remove `class` attributes from the output. [#13](https://github.com/Stranger6667/css-inline/issues/13) ### Changed diff --git a/crates/css-inline-php/README.md b/crates/css-inline-php/README.md index d7b708c7..5f109829 100644 --- a/crates/css-inline-php/README.md +++ b/crates/css-inline-php/README.md @@ -205,6 +205,7 @@ $inliner->inline($html); - `applyHeightAttributes`. Add `height` HTML attributes from CSS `height` properties on `table`, `td`, `th` and `img`. Default: `false` - `resolveCssVariables`. Replace `var()` references with the values of the custom properties they refer to and drop the `--*` declarations. Default: `false` - `mediaType`. Inline rules from `@media` blocks whose query lists this media type or `all`, e.g. `@media print` or `@media screen, print` for `print`. Queries with media features, such as `print and (min-width: 600px)`, never apply. An inlined block is not kept as an at-rule. Ignored when `keepAtRules` is enabled and `inlineStyleTags` is disabled. Default: `null` +- `keepClasses`. Keep `class` attributes in the output. When disabled, class selectors in kept `

Test

'; + $result = $inliner->inline($html); + + $this->assertEquals( + '

Test

', + $result + ); + } + public function testRemoveInlinedSelectors(): void { $inliner = new CssInliner( diff --git a/crates/css-inline-python/CHANGELOG.md b/crates/css-inline-python/CHANGELOG.md index ea984a25..87b6fc34 100644 --- a/crates/css-inline-python/CHANGELOG.md +++ b/crates/css-inline-python/CHANGELOG.md @@ -5,6 +5,7 @@ ### Added - `media_type` option to inline rules from `@media` blocks that apply to the given media type. [#410](https://github.com/Stranger6667/css-inline/issues/410) +- `keep_classes` option to remove `class` attributes from the output. [#13](https://github.com/Stranger6667/css-inline/issues/13) ### Changed diff --git a/crates/css-inline-python/README.md b/crates/css-inline-python/README.md index 7a90ab36..34042dd6 100644 --- a/crates/css-inline-python/README.md +++ b/crates/css-inline-python/README.md @@ -167,6 +167,7 @@ inliner.inline("...") - `apply_height_attributes`. Add `height` HTML attributes from CSS `height` properties on supported elements (`table`, `td`, `th`, `img`). Default: `False` - `resolve_css_variables`. Replace `var()` references with the values of the custom properties they refer to and drop the `--*` declarations. Default: `False` - `media_type`. Inline rules from `@media` blocks whose query lists this media type or `all`, e.g. `@media print` or `@media screen, print` for `print`. Queries with media features, such as `print and (min-width: 600px)`, never apply. An inlined block is not kept as an at-rule. Ignored when `keep_at_rules` is enabled and `inline_style_tags` is disabled. Default: `None` +- `keep_classes`. Keep `class` attributes in the output. When disabled, class selectors in kept `

Test

' + result = css_inline.inline(html, keep_classes=False) + assert result == '

Test

' + + +def test_remove_classes_inliner(): + inliner = css_inline.CSSInliner(keep_classes=False) + html = '

Test

' + result = inliner.inline(html) + assert result == '

Test

' + + def test_resolve_css_variables_default(): result = css_inline.inline(CSS_VARIABLES_HTML) assert result == '

Test

' diff --git a/crates/css-inline-ruby/CHANGELOG.md b/crates/css-inline-ruby/CHANGELOG.md index c0387dfb..966d4429 100644 --- a/crates/css-inline-ruby/CHANGELOG.md +++ b/crates/css-inline-ruby/CHANGELOG.md @@ -5,6 +5,7 @@ ### Added - `media_type` option to inline rules from `@media` blocks that apply to the given media type. [#410](https://github.com/Stranger6667/css-inline/issues/410) +- `keep_classes` option to remove `class` attributes from the output. [#13](https://github.com/Stranger6667/css-inline/issues/13) ### Changed diff --git a/crates/css-inline-ruby/README.md b/crates/css-inline-ruby/README.md index 96cfd7fd..1e7df16f 100644 --- a/crates/css-inline-ruby/README.md +++ b/crates/css-inline-ruby/README.md @@ -162,6 +162,7 @@ The module functions accept the same keyword arguments, for example `CSSInline.i - `apply_height_attributes`. Add `height` HTML attributes from CSS `height` properties on `table`, `td`, `th` and `img`. Default: `false` - `resolve_css_variables`. Replace `var()` references with the values of the custom properties they refer to and drop the `--*` declarations. Default: `false` - `media_type`. Inline rules from `@media` blocks whose query lists this media type or `all`, e.g. `@media print` or `@media screen, print` for `print`. Queries with media features, such as `print and (min-width: 600px)`, never apply. An inlined block is not kept as an at-rule. Ignored when `keep_at_rules` is enabled and `inline_style_tags` is disabled. Default: `nil` +- `keep_classes`. Keep `class` attributes in the output. When disabled, class selectors in kept `

Test

') end + it 'Removes classes' do + html = '

Test

' + inliner = CSSInline::CSSInliner.new(keep_classes: false) + result = inliner.inline(html) + expect(result).to eq('

Test

') + end + it 'Keeps CSS variables by default' do html = '

Test

' result = CSSInline::inline(html) diff --git a/crates/css-inline-ruby/src/lib.rs b/crates/css-inline-ruby/src/lib.rs index 25881328..e7bac0d8 100644 --- a/crates/css-inline-ruby/src/lib.rs +++ b/crates/css-inline-ruby/src/lib.rs @@ -78,6 +78,8 @@ struct Options { resolve_css_variables: Option, /// Inline rules from `@media` blocks that apply to this media type. media_type: Option, + /// Keep `class` attributes in the output. + keep_classes: Option, } impl TryConvert for Options { @@ -106,6 +108,7 @@ impl TryConvert for Options { resolve_css_variables: h .aref::<_, Option>(ruby.to_symbol("resolve_css_variables"))?, media_type: h.aref::<_, Option>(ruby.to_symbol("media_type"))?, + keep_classes: h.aref::<_, Option>(ruby.to_symbol("keep_classes"))?, }) } } @@ -133,6 +136,7 @@ fn parse_options( apply_height_attributes: kwargs.apply_height_attributes.unwrap_or(false), resolve_css_variables: kwargs.resolve_css_variables.unwrap_or(false), media_type: kwargs.media_type, + keep_classes: kwargs.keep_classes.unwrap_or(true), }) } diff --git a/crates/css-inline/src/html/document.rs b/crates/css-inline/src/html/document.rs index cb6f448f..e363193e 100644 --- a/crates/css-inline/src/html/document.rs +++ b/crates/css-inline/src/html/document.rs @@ -596,6 +596,7 @@ impl Document { mode: InliningMode, apply_width_attributes: bool, apply_height_attributes: bool, + keep_classes: bool, variables: CssVariables, ) -> Result<(), InlineError> { serialize_to( @@ -609,6 +610,7 @@ impl Document { mode, apply_width_attributes, apply_height_attributes, + keep_classes, variables, ) } @@ -769,6 +771,7 @@ mod tests { InliningMode::Document, false, false, + true, CssVariables::Keep, ) .expect("Failed to serialize"); diff --git a/crates/css-inline/src/html/serializer.rs b/crates/css-inline/src/html/serializer.rs index 2d191a78..0bd2d7e8 100644 --- a/crates/css-inline/src/html/serializer.rs +++ b/crates/css-inline/src/html/serializer.rs @@ -162,6 +162,7 @@ pub(crate) fn serialize_to( mode: InliningMode, apply_width_attributes: bool, apply_height_attributes: bool, + keep_classes: bool, variables: CssVariables, ) -> Result<(), InlineError> { let sink = Sink::new( @@ -174,6 +175,7 @@ pub(crate) fn serialize_to( mode, apply_width_attributes, apply_height_attributes, + keep_classes, ); match variables { CssVariables::Keep => { @@ -203,6 +205,7 @@ struct Sink<'a> { inlining_mode: InliningMode, apply_width_attributes: bool, apply_height_attributes: bool, + keep_classes: bool, } impl<'a> Sink<'a> { @@ -217,6 +220,7 @@ impl<'a> Sink<'a> { inlining_mode: InliningMode, apply_width_attributes: bool, apply_height_attributes: bool, + keep_classes: bool, ) -> Sink<'a> { Sink { document, @@ -228,6 +232,7 @@ impl<'a> Sink<'a> { inlining_mode, apply_width_attributes, apply_height_attributes, + keep_classes, } } #[inline] @@ -242,6 +247,7 @@ impl<'a> Sink<'a> { self.inlining_mode, self.apply_width_attributes, self.apply_height_attributes, + self.keep_classes, ) } #[inline] @@ -299,6 +305,7 @@ impl<'a> Sink<'a> { self.minify_css, self.apply_width_attributes, self.apply_height_attributes, + self.keep_classes, )?; if element.name.local == local_name!("head") { @@ -430,7 +437,11 @@ impl<'a, W: Write, V: VariableScope> HtmlSerializer<'a, W, V> { Ok(()) } - #[allow(clippy::too_many_lines)] + #[allow( + clippy::too_many_lines, + clippy::too_many_arguments, + clippy::fn_params_excessive_bools + )] fn start_elem( &mut self, name: &QualName, @@ -439,6 +450,7 @@ impl<'a, W: Write, V: VariableScope> HtmlSerializer<'a, W, V> { minify_css: bool, apply_width_attributes: bool, apply_height_attributes: bool, + keep_classes: bool, ) -> Result<(), InlineError> { let html_name = match name.ns { ns!(html) => Some(name.local.clone()), @@ -469,7 +481,7 @@ impl<'a, W: Write, V: VariableScope> HtmlSerializer<'a, W, V> { self.writer.write_all(b"<")?; self.writer.write_all(name.local.as_bytes())?; - if let Some(class) = &attrs.class { + if let Some(class) = attrs.class.as_ref().filter(|_| keep_classes) { self.writer.write_all(b" class=\"")?; self.write_attributes(&class.value)?; self.writer.write_all(b"\"")?; @@ -923,6 +935,7 @@ mod tests { InliningMode::Document, false, false, + true, CssVariables::Keep, ) .expect("Should not fail"); @@ -1005,6 +1018,7 @@ mod tests { InliningMode::Document, false, false, + true, CssVariables::Keep, ) .expect("Should not fail"); @@ -1029,6 +1043,7 @@ mod tests { InliningMode::Document, false, false, + true, CssVariables::Keep, ) .expect("Should not fail"); @@ -1053,6 +1068,7 @@ mod tests { InliningMode::Document, false, false, + true, CssVariables::Keep, ) .expect("Should not fail"); @@ -1077,6 +1093,7 @@ mod tests { InliningMode::Document, false, false, + true, CssVariables::Keep, ) .expect("Should not fail"); @@ -1104,6 +1121,7 @@ mod tests { InliningMode::Document, false, false, + true, CssVariables::Keep, ) .expect("Should not fail"); @@ -1128,6 +1146,7 @@ mod tests { InliningMode::Document, false, false, + true, CssVariables::Keep, ) .expect("Should not fail"); @@ -1154,6 +1173,7 @@ mod tests { InliningMode::Document, false, false, + true, CssVariables::Keep, ) .expect("Should not fail"); diff --git a/crates/css-inline/src/lib.rs b/crates/css-inline/src/lib.rs index 11b1c9dd..cb110193 100644 --- a/crates/css-inline/src/lib.rs +++ b/crates/css-inline/src/lib.rs @@ -106,6 +106,10 @@ pub struct InlineOptions<'a> { /// Queries with media features, such as `(min-width: 600px)`, never apply. /// Ignored when `keep_at_rules` is enabled and `inline_style_tags` is disabled. pub media_type: Option, + /// Keep `class` attributes in the output. + /// + /// When disabled, class selectors in kept `

Test

"); } + #[test] + fn keep_classes() { + css_inline() + .write_stdin(r#"

Test

"#) + .arg("--keep-classes=false") + .assert() + .success() + .stdout("

Test

"); + } + #[test] fn unknown_short_flag() { css_inline() diff --git a/crates/css-inline/tests/test_inlining.rs b/crates/css-inline/tests/test_inlining.rs index fa1e26e4..0afc0960 100644 --- a/crates/css-inline/tests/test_inlining.rs +++ b/crates/css-inline/tests/test_inlining.rs @@ -1410,7 +1410,7 @@ fn test_disable_cache() { .cache(None) .build(); let debug = format!("{inliner:?}"); - assert_eq!(debug, "CSSInliner { options: InlineOptions { inline_style_tags: true, keep_style_tags: false, keep_link_tags: false, base_url: None, load_remote_stylesheets: true, cache: None, extra_css: None, preallocate_node_capacity: 32, remove_inlined_selectors: false, apply_width_attributes: false, apply_height_attributes: false, resolve_css_variables: false, media_type: None, .. } }"); + assert_eq!(debug, "CSSInliner { options: InlineOptions { inline_style_tags: true, keep_style_tags: false, keep_link_tags: false, base_url: None, load_remote_stylesheets: true, cache: None, extra_css: None, preallocate_node_capacity: 32, remove_inlined_selectors: false, apply_width_attributes: false, apply_height_attributes: false, resolve_css_variables: false, media_type: None, keep_classes: true, .. } }"); } #[test] @@ -1820,6 +1820,29 @@ fn remove_inlined_selectors_class_attribute_substring(padding: &str) { assert!(result.contains(r#"
Test
"#)); } +#[test] +fn keep_classes_document() { + let inliner = CSSInliner::options().keep_classes(false).build(); + let html = r#"

Test

Text

"#; + let result = inliner.inline(html).unwrap(); + assert_eq!( + result, + r#"

Test

Text

"# + ); +} + +#[test] +fn keep_classes_fragment() { + let inliner = CSSInliner::options().keep_classes(false).build(); + let result = inliner + .inline_fragment( + r#"

Test

"#, + ".m .a { color: blue; }", + ) + .unwrap(); + assert_eq!(result, r#"

Test

"#); +} + #[test] fn remove_inlined_selectors_basic() { let inliner = CSSInliner::options().remove_inlined_selectors(true).build();