diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b1259d5..f1759c89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - `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) +- `remove_unused_at_rules` option to drop rules inside kept at-rules that match no element. [#724](https://github.com/Stranger6667/css-inline/issues/724) ### Changed diff --git a/README.md b/README.md index ba544d8e..e7c0c4b8 100644 --- a/README.md +++ b/README.md @@ -186,6 +186,7 @@ fn main() -> css_inline::Result<()> { - `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

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

Test

") == 0); +} + int main(void) { test_default_options(); test_output_size_too_small(); @@ -196,5 +206,6 @@ int main(void) { test_keep_classes(); test_resolve_css_variables(); test_media_type(); + test_remove_unused_at_rules(); return 0; } diff --git a/crates/css-inline-java/CHANGELOG.md b/crates/css-inline-java/CHANGELOG.md index cc0caa1d..a4ad7811 100644 --- a/crates/css-inline-java/CHANGELOG.md +++ b/crates/css-inline-java/CHANGELOG.md @@ -6,6 +6,7 @@ - `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) +- `setRemoveUnusedAtRules` option to drop rules inside kept at-rules that match no element. [#724](https://github.com/Stranger6667/css-inline/issues/724) ### Changed diff --git a/crates/css-inline-java/README.md b/crates/css-inline-java/README.md index 1e96fde8..7c1d7058 100644 --- a/crates/css-inline-java/README.md +++ b/crates/css-inline-java/README.md @@ -182,6 +182,7 @@ public class ConfigExample { - **`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

"; + + CssInlineConfig cfg = new CssInlineConfig.Builder().setKeepAtRules(true).setRemoveUnusedAtRules(true).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 d56e6b4c..87e43c9f 100644 --- a/crates/css-inline-js/CHANGELOG.md +++ b/crates/css-inline-js/CHANGELOG.md @@ -6,6 +6,7 @@ - `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) +- `removeUnusedAtRules` option to drop rules inside kept at-rules that match no element. [#724](https://github.com/Stranger6667/css-inline/issues/724) ### Changed diff --git a/crates/css-inline-js/README.md b/crates/css-inline-js/README.md index 93585e98..33e42daf 100644 --- a/crates/css-inline-js/README.md +++ b/crates/css-inline-js/README.md @@ -95,6 +95,7 @@ Pass options as the second argument to `inline` or the third to `inlineFragment` - `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

"; +test("remove unused at-rules", (t) => { + t.is( + inline( + "

Test

", + { keepAtRules: true, removeUnusedAtRules: true }, + ), + "

Test

", + ); +}); + test("resolve css variables is off by default", (t) => { t.is( inline(cssVariablesHtml), diff --git a/crates/css-inline-js/index.d.ts b/crates/css-inline-js/index.d.ts index ab7ac2c9..b54d28a8 100644 --- a/crates/css-inline-js/index.d.ts +++ b/crates/css-inline-js/index.d.ts @@ -53,6 +53,8 @@ export interface Options { mediaType?: string /** Keep `class` attributes in the output. */ keepClasses?: boolean + /** Remove rules that match no element from the conditional at-rules (`@media`, `@supports`, ...) kept in the output. */ + removeUnusedAtRules?: 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 8b2a53f2..0984938d 100644 --- a/crates/css-inline-js/js-binding.d.ts +++ b/crates/css-inline-js/js-binding.d.ts @@ -72,6 +72,8 @@ export interface Options { mediaType?: string /** Keep `class` attributes in the output. */ keepClasses?: boolean + /** Remove rules that match no element from the conditional at-rules (`@media`, `@supports`, ...) kept in the output. */ + removeUnusedAtRules?: boolean } export interface StylesheetCache { diff --git a/crates/css-inline-js/src/lib.rs b/crates/css-inline-js/src/lib.rs index d1f73f4c..fde6f133 100644 --- a/crates/css-inline-js/src/lib.rs +++ b/crates/css-inline-js/src/lib.rs @@ -140,6 +140,7 @@ const INLINE: &'static str = r#"export interface InlineOptions { resolveCssVariables?: boolean, mediaType?: string, keepClasses?: boolean, + removeUnusedAtRules?: 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 b598413a..d27bda50 100644 --- a/crates/css-inline-js/src/options.rs +++ b/crates/css-inline-js/src/options.rs @@ -79,6 +79,8 @@ pub struct Options { pub media_type: Option, /// Keep `class` attributes in the output. pub keep_classes: Option, + /// Remove rules that match no element from the conditional at-rules (`@media`, `@supports`, ...) kept in the output. + pub remove_unused_at_rules: Option, } impl TryFrom for css_inline::InlineOptions<'_> { @@ -171,6 +173,7 @@ impl TryFrom for css_inline::InlineOptions<'_> { resolve_css_variables: value.resolve_css_variables.unwrap_or(false), media_type: value.media_type, keep_classes: value.keep_classes.unwrap_or(true), + remove_unused_at_rules: value.remove_unused_at_rules.unwrap_or(false), }) } } diff --git a/crates/css-inline-js/wasm/README.md b/crates/css-inline-js/wasm/README.md index 2f365cd8..f9fedf69 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`, `keepClasses`. +`inlineStyleTags`, `keepStyleTags`, `keepLinkTags`, `keepAtRules`, `minifyCss`, `baseUrl`, `loadRemoteStylesheets`, `extraCss`, `preallocateNodeCapacity`, `removeInlinedSelectors`, `applyWidthAttributes`, `applyHeightAttributes`, `resolveCssVariables`, `mediaType`, `keepClasses`, `removeUnusedAtRules`. ## Playground diff --git a/crates/css-inline-js/wasm/index.d.ts b/crates/css-inline-js/wasm/index.d.ts index 518fd8be..25085759 100644 --- a/crates/css-inline-js/wasm/index.d.ts +++ b/crates/css-inline-js/wasm/index.d.ts @@ -18,6 +18,7 @@ export interface InlineOptions { resolveCssVariables?: boolean; mediaType?: string; keepClasses?: boolean; + removeUnusedAtRules?: 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 00adc15c..a38e49e3 100644 --- a/crates/css-inline-php/CHANGELOG.md +++ b/crates/css-inline-php/CHANGELOG.md @@ -6,6 +6,7 @@ - `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) +- `removeUnusedAtRules` option to drop rules inside kept at-rules that match no element. [#724](https://github.com/Stranger6667/css-inline/issues/724) ### Changed diff --git a/crates/css-inline-php/README.md b/crates/css-inline-php/README.md index 5f109829..903bda1a 100644 --- a/crates/css-inline-php/README.md +++ b/crates/css-inline-php/README.md @@ -206,6 +206,7 @@ $inliner->inline($html); - `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 87b6fc34..65fe01b4 100644 --- a/crates/css-inline-python/CHANGELOG.md +++ b/crates/css-inline-python/CHANGELOG.md @@ -6,6 +6,7 @@ - `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) +- `remove_unused_at_rules` option to drop rules inside kept at-rules that match no element. [#724](https://github.com/Stranger6667/css-inline/issues/724) ### Changed diff --git a/crates/css-inline-python/README.md b/crates/css-inline-python/README.md index 34042dd6..83088085 100644 --- a/crates/css-inline-python/README.md +++ b/crates/css-inline-python/README.md @@ -168,6 +168,7 @@ inliner.inline("...") - `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

" +UNUSED_AT_RULES_EXPECTED = ( + "

Test

" +) + + +def test_remove_unused_at_rules(): + result = css_inline.inline(UNUSED_AT_RULES_HTML, keep_at_rules=True, remove_unused_at_rules=True) + assert result == UNUSED_AT_RULES_EXPECTED + + +def test_remove_unused_at_rules_inliner(): + inliner = css_inline.CSSInliner(keep_at_rules=True, remove_unused_at_rules=True) + assert inliner.inline(UNUSED_AT_RULES_HTML) == UNUSED_AT_RULES_EXPECTED + + CSS_VARIABLES_HTML = ( "

Test

" ) diff --git a/crates/css-inline-ruby/CHANGELOG.md b/crates/css-inline-ruby/CHANGELOG.md index 966d4429..1bdfea63 100644 --- a/crates/css-inline-ruby/CHANGELOG.md +++ b/crates/css-inline-ruby/CHANGELOG.md @@ -6,6 +6,7 @@ - `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) +- `remove_unused_at_rules` option to drop rules inside kept at-rules that match no element. [#724](https://github.com/Stranger6667/css-inline/issues/724) ### Changed diff --git a/crates/css-inline-ruby/README.md b/crates/css-inline-ruby/README.md index 1e7df16f..6667ef5b 100644 --- a/crates/css-inline-ruby/README.md +++ b/crates/css-inline-ruby/README.md @@ -163,6 +163,7 @@ The module functions accept the same keyword arguments, for example `CSSInline.i - `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

' + inliner = CSSInline::CSSInliner.new(keep_at_rules: true, remove_unused_at_rules: true) + 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 e7bac0d8..5b9b04b1 100644 --- a/crates/css-inline-ruby/src/lib.rs +++ b/crates/css-inline-ruby/src/lib.rs @@ -80,6 +80,8 @@ struct Options { media_type: Option, /// Keep `class` attributes in the output. keep_classes: Option, + /// Remove rules that match no element from the conditional at-rules (`@media`, `@supports`, ...) kept in the output. + remove_unused_at_rules: Option, } impl TryConvert for Options { @@ -109,6 +111,8 @@ impl TryConvert for Options { .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"))?, + remove_unused_at_rules: h + .aref::<_, Option>(ruby.to_symbol("remove_unused_at_rules"))?, }) } } @@ -137,6 +141,7 @@ fn parse_options( resolve_css_variables: kwargs.resolve_css_variables.unwrap_or(false), media_type: kwargs.media_type, keep_classes: kwargs.keep_classes.unwrap_or(true), + remove_unused_at_rules: kwargs.remove_unused_at_rules.unwrap_or(false), }) } diff --git a/crates/css-inline/src/lib.rs b/crates/css-inline/src/lib.rs index cb110193..d2c1e35e 100644 --- a/crates/css-inline/src/lib.rs +++ b/crates/css-inline/src/lib.rs @@ -86,6 +86,9 @@ pub struct InlineOptions<'a> { pub resolver: Arc, /// Remove selectors that were successfully inlined from inline `

Test

"#) + .arg("--keep-at-rules") + .arg("--remove-unused-at-rules") + .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 0afc0960..8420af15 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, keep_classes: true, .. } }"); + 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, remove_unused_at_rules: false, apply_width_attributes: false, apply_height_attributes: false, resolve_css_variables: false, media_type: None, keep_classes: true, .. } }"); } #[test] @@ -3321,3 +3321,275 @@ fn media_type_in_extra_css_and_fragments() { r#"

x

"# ); } + +#[test_case( + "@media print { .h { color: red } .zzz { color: blue } }", + "@media print { .h { color: red } }"; + "unused rule" +)] +#[test_case( + "@media print { h1 { color: red } h2 { color: blue } }", + "@media print { h1 { color: red } }"; + "unused tag" +)] +#[test_case( + "@media print { .zzz { color: blue } .h { color: red } }", + "@media print { .h { color: red } }"; + "unused rule first" +)] +#[test_case( + "@supports (display: grid) { @media print { .zzz { color: blue } .h { color: red } } }", + "@supports (display: grid) { @media print { .h { color: red } } }"; + "nested conditional" +)] +#[test_case( + "@supports (display: grid) { @media print { .zzz { color: blue } } .h { color: red } }", + "@supports (display: grid) { .h { color: red } }"; + "nested conditional left empty" +)] +#[test_case("@media print { .zzz { color: blue } }", ""; "block left empty")] +#[test_case( + "@supports (display: grid) { @media print { .zzz { color: blue } } }", + ""; + "nested blocks left empty" +)] +#[test_case("@media print { }", ""; "empty block")] +#[test_case("@container (min-width: 1px) { .zzz { color: blue } }", ""; "container")] +#[test_case("@-moz-document url-prefix() { .zzz { color: blue } }", ""; "moz document")] +#[test_case("@MEDIA print { .zzz { color: blue } }", ""; "uppercase name")] +#[test_case( + "@media print { .h:hover { color: red } .h:focus-within { color: red } }", + "@media print { .h:hover { color: red } .h:focus-within { color: red } }"; + "state pseudo-class" +)] +#[test_case( + "@media print { .zzz:hover { color: red } .zzz:HOVER:target { color: red } .h:checked { color: red } }", + "@media print { .h:checked { color: red } }"; + "state pseudo-class on a missing class" +)] +#[test_case( + "@media print { .h::after { content: 'x' } .h:before { content: 'x' } }", + "@media print { .h::after { content: 'x' } .h:before { content: 'x' } }"; + "pseudo-element" +)] +#[test_case( + "@media print { .zzz::after { content: 'x' } .zzz:before { content: 'x' } .h::selection { color: red } }", + "@media print { .h::selection { color: red } }"; + "pseudo-element on a missing class" +)] +#[test_case( + "@media print { :hover { color: red } ::selection { color: red } h2 > :hover { color: red } }", + "@media print { :hover { color: red } ::selection { color: red } h2 > :hover { color: red } }"; + "pseudo without a compound" +)] +#[test_case( + "@media print { .zzz:not(:hover) { color: red } .zzz:is(.a:focus) { color: red } }", + "@media print { .zzz:not(:hover) { color: red } .zzz:is(.a:focus) { color: red } }"; + "nested state pseudo-class" +)] +#[test_case( + "@media print { .h:not(.zzz) { color: red } .h:not(.h) { color: red } .h:first-child { color: red } .h:empty { color: red } a:link { color: red } }", + "@media print { .h:not(.zzz) { color: red } .h:first-child { color: red } }"; + "static pseudo-class" +)] +#[test_case( + "@media print { .zzz..x { color: red } .zzz:lang(en) { color: red } { color: red } }", + "@media print { .zzz..x { color: red } .zzz:lang(en) { color: red } { color: red } }"; + "unparsable selector" +)] +#[test_case( + r#"@media print { .zzz\:hover:hover { color: red } [title=":hover"].zzz:hover { color: red } .zzz/**/:hover { color: red } & .zzz { color: red } }"#, + r#"@media print { .zzz\:hover:hover { color: red } [title=":hover"].zzz:hover { color: red } .zzz/**/:hover { color: red } & .zzz { color: red } }"#; + "undecided selector syntax" +)] +#[test_case( + "@media print { .zzz, .h , .yyy:hover { color: red } }", + "@media print { .h { color: red } }"; + "selector list" +)] +#[test_case( + "@media print { .zzz, .yyy { color: red } }", + ""; + "selector list without matches" +)] +#[test_case( + "@media print { .zzz, .h:hover, ..x { color: red } }", + "@media print { .h:hover, ..x { color: red } }"; + "selector list with undecided selectors" +)] +#[test_case( + "@media print {\n /* c */\n .zzz { color: blue }\n /* d */\n .h { color: red }\n}", + "@media print {\n /* d */\n .h { color: red }\n}"; + "comments and whitespace" +)] +#[test_case( + "@font-face { font-family: X; src: url(x.woff) } @keyframes spin { from { color: red } to { color: blue } } @page { margin: 0 }", + "@font-face { font-family: X; src: url(x.woff) } @keyframes spin { from { color: red } to { color: blue } } @page { margin: 0 }"; + "other at-rules" +)] +#[test_case( + "@media print { @font-face { font-family: X } @keyframes spin { from { color: red } to { color: blue } } .zzz { color: blue } }", + "@media print { @font-face { font-family: X } @keyframes spin { from { color: red } to { color: blue } } }"; + "other at-rules in a conditional" +)] +#[test_case( + "@media print { @layer a { .zzz { color: blue } } @layer { .zzz { color: blue } } @layer b.c { .h { color: red } .zzz { color: blue } } }", + "@media print { @layer a; @layer b.c { .h { color: red } } }"; + "layers in a conditional" +)] +#[test_case( + "@layer a { @media print { .zzz { color: blue } .h { color: red } } }", + "@layer a { @media print { .h { color: red } } }"; + "conditional in a layer" +)] +#[test_case( + "@layer a { @media print { .zzz { color: blue } } }", + ""; + "conditional in a layer left empty" +)] +#[test_case( + "@layer a, b; @layer a { @media print { .zzz { color: blue } } } @layer b { @media print { .h { color: red } } }", + "@layer a, b; @layer b { @media print { .h { color: red } } }"; + "layer statement" +)] +#[test_case( + "@media print { .zzz { color: blue }", + "@media print { .zzz { color: blue }"; + "unterminated" +)] +#[test_case( + "@media print { .zzz { color: blue } } /* c */", + ""; + "trailing comment" +)] +fn remove_unused_at_rules(css: &str, expected: &str) { + let inliner = CSSInliner::options() + .keep_at_rules(true) + .remove_unused_at_rules(true) + .build(); + // Small documents have no element indexes + for padding in ["", PADDING] { + let html = format!( + r#"

t

"# + ); + let style = if expected.is_empty() { + String::new() + } else { + format!("") + }; + assert_eq!( + inliner.inline(&html).unwrap(), + format!( + r#"{style}

t

"# + ) + ); + } +} + +#[test] +fn remove_unused_at_rules_is_off_by_default() { + let html = r#"

t

"#; + assert_eq!( + CSSInliner::options() + .keep_at_rules(true) + .build() + .inline(html) + .unwrap(), + r#"

t

"# + ); +} + +#[test] +fn remove_unused_at_rules_without_kept_at_rules() { + let html = r#"

t

"#; + assert_eq!( + CSSInliner::options() + .remove_unused_at_rules(true) + .build() + .inline(html) + .unwrap(), + r#"

t

"# + ); +} + +#[test_case( + "@media print { .h { margin: 0 } .zzz { margin: 1px } } .h { color: red } .q { color: blue }", + false, + ""; + "at-rule and leftover" +)] +#[test_case( + "@media print { .zzz { margin: 1px } } .h { color: red } .q { color: blue }", + false, + ""; + "leftover only" +)] +#[test_case("@media print { .zzz { margin: 1px } } .h { color: red }", false, ""; "nothing left")] +#[test_case( + "@media print { .zzz { margin: 1px } } .h { color: red }", + true, + ""; + "nothing left with keep_style_tags" +)] +#[test_case( + "@layer a { @media print { .zzz { margin: 1px } } .h { color: red } } @layer b { .q { color: blue } }", + false, + ""; + "emptied layer" +)] +fn remove_unused_at_rules_with_remove_inlined_selectors( + css: &str, + keep_style_tags: bool, + expected_head: &str, +) { + // With and without `keep_at_rules` the `

t

"# + ); + assert_eq!( + inliner.inline(&html).unwrap(), + format!( + r#"{expected_head}

t

"# + ) + ); + } +} + +#[test] +fn remove_unused_at_rules_with_keep_style_tags() { + // Kept `

t

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

t

"# + ); +} + +#[test] +fn remove_unused_at_rules_keeps_at_rule_split_across_style_tags() { + let html = r#"

t

"#; + let options = || CSSInliner::options().remove_inlined_selectors(true); + assert_eq!( + options() + .remove_unused_at_rules(true) + .build() + .inline(html) + .unwrap(), + options().build().inline(html).unwrap() + ); +}