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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<style>` blocks and at-rules match nothing. Default: `true`
- `remove_unused_at_rules`. Remove rules that match no element from kept at-rules (`@media`, `@supports`, ...), along with blocks left empty. Needs `keep_at_rules` or `remove_inlined_selectors`. Matching ignores `:hover`, `::before` and similar. This also removes rules that target markup injected by an email client (e.g. Gmail's `u ~ div .x`). Default: `false`

Add `data-css-inline="ignore"` to a tag to skip inlining styles into it:

Expand Down
1 change: 1 addition & 0 deletions crates/css-inline-c/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions crates/css-inline-c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ Fields:
- `resolve_css_variables`. Replace `var()` references with the values of the custom properties they refer to and drop the `--*` declarations. Default: `false`
- `media_type` (`const char *`). 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: `NULL`
- `keep_classes`. Keep `class` attributes in the output. When disabled, class selectors in kept `<style>` blocks and at-rules match nothing. Default: `true`
- `remove_unused_at_rules`. Remove rules that match no element from kept at-rules (`@media`, `@supports`, ...), along with blocks left empty. Needs `keep_at_rules` or `remove_inlined_selectors`. Matching ignores `:hover`, `::before` and similar. This also removes rules that target markup injected by an email client (e.g. Gmail's `u ~ div .x`). Default: `false`

With the defaults, the library loads every stylesheet that a `link` tag references, over the network or from local files. Set `load_remote_stylesheets` to `false` to block both network and file access for `link` tags.

Expand Down
4 changes: 4 additions & 0 deletions crates/css-inline-c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ pub struct CssInlinerOptions {
pub media_type: *const c_char,
/// Keep `class` attributes in the output.
pub keep_classes: bool,
/// Remove rules that match no element from the conditional at-rules (`@media`, `@supports`, ...) kept in the output.
pub remove_unused_at_rules: bool,
}

macro_rules! inliner {
Expand Down Expand Up @@ -225,6 +227,7 @@ pub extern "C" fn css_inliner_default_options() -> CssInlinerOptions {
resolve_css_variables: false,
media_type: ptr::null(),
keep_classes: true,
remove_unused_at_rules: false,
}
}

Expand Down Expand Up @@ -296,6 +299,7 @@ impl TryFrom<&CssInlinerOptions> for InlineOptions<'_> {
resolve_css_variables: value.resolve_css_variables,
media_type,
keep_classes: value.keep_classes,
remove_unused_at_rules: value.remove_unused_at_rules,
})
}
}
Expand Down
11 changes: 11 additions & 0 deletions crates/css-inline-c/tests/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,16 @@ static void test_media_type(void) {
assert(strcmp(output, "<html><head></head><body><h1 style=\"color: red;\">Test</h1></body></html>") == 0);
}

static void test_remove_unused_at_rules(void) {
char html[] = "<html><head><style>@media print { h1 { color: blue; } h2 { color: red; } }</style></head><body><h1>Test</h1></body></html>";
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, "<html><head><style>@media print { h1 { color: blue; } } </style></head><body><h1>Test</h1></body></html>") == 0);
}

int main(void) {
test_default_options();
test_output_size_too_small();
Expand All @@ -196,5 +206,6 @@ int main(void) {
test_keep_classes();
test_resolve_css_variables();
test_media_type();
test_remove_unused_at_rules();
return 0;
}
1 change: 1 addition & 0 deletions crates/css-inline-java/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions crates/css-inline-java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<style>` blocks and at-rules match nothing (default: `true`)
- **`setRemoveUnusedAtRules(boolean)`** - Remove rules that match no element from kept at-rules (`@media`, `@supports`, ...), along with blocks left empty. Needs `setKeepAtRules` or `setRemoveInlinedSelectors`. Matching ignores `:hover`, `::before` and similar. This also removes rules that target markup injected by an email client (e.g. Gmail's `u ~ div .x`) (default: `false`)

`inline` and `inlineFragment` throw `org.cssinline.CssInlineException` (a `RuntimeException`) when inlining fails. A malformed `setBaseUrl` value passes `build()` and fails here, as does a stylesheet that cannot be loaded.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ public class CssInlineConfig {
public final String mediaType;
/** Keep class attributes in the output. */
public final boolean keepClasses;
/** Remove rules that match no element from the conditional at-rules (@media, @supports, ...) kept in the output. */
public final boolean removeUnusedAtRules;

private CssInlineConfig(boolean inlineStyleTags, boolean keepStyleTags, boolean keepLinkTags,
boolean keepAtRules, boolean minifyCss, boolean loadRemoteStylesheets, String baseUrl, String extraCss,
int cacheSize, int preallocateNodeCapacity, boolean removeInlinedSelectors,
boolean applyWidthAttributes, boolean applyHeightAttributes, boolean resolveCssVariables,
String mediaType, boolean keepClasses) {
String mediaType, boolean keepClasses, boolean removeUnusedAtRules) {
this.inlineStyleTags = inlineStyleTags;
this.keepStyleTags = keepStyleTags;
this.keepLinkTags = keepLinkTags;
Expand All @@ -70,6 +72,7 @@ private CssInlineConfig(boolean inlineStyleTags, boolean keepStyleTags, boolean
this.resolveCssVariables = resolveCssVariables;
this.mediaType = mediaType;
this.keepClasses = keepClasses;
this.removeUnusedAtRules = removeUnusedAtRules;
}

/**
Expand All @@ -92,6 +95,7 @@ public static class Builder {
private boolean resolveCssVariables = false;
private String mediaType = null;
private boolean keepClasses = true;
private boolean removeUnusedAtRules = false;

/**
* Creates a new builder with default configuration values.
Expand Down Expand Up @@ -298,6 +302,17 @@ public Builder setKeepClasses(boolean b) {
return this;
}

/**
* Remove rules that match no element from the conditional at-rules (@media, @supports, ...) kept in the output.
*
* @param b true to remove unused rules, false to keep the at-rules as is
* @return this builder instance for method chaining
*/
public Builder setRemoveUnusedAtRules(boolean b) {
this.removeUnusedAtRules = b;
return this;
}

/**
* Creates a new {@link CssInlineConfig} instance with the current builder settings.
*
Expand All @@ -306,7 +321,7 @@ public Builder setKeepClasses(boolean b) {
public CssInlineConfig build() {
return new CssInlineConfig(inlineStyleTags, keepStyleTags, keepLinkTags, keepAtRules, minifyCss, loadRemoteStylesheets, baseUrl,
extraCss, cacheSize, preallocateNodeCapacity, removeInlinedSelectors, applyWidthAttributes, applyHeightAttributes,
resolveCssVariables, mediaType, keepClasses);
resolveCssVariables, mediaType, keepClasses, removeUnusedAtRules);
}
}
}
4 changes: 3 additions & 1 deletion crates/css-inline-java/src/main/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ fn build_inliner(env: &mut Env, cfg: &JObject) -> Result<CSSInliner<'static>, Er
let apply_height_attributes = env.get_bool_field(cfg, jni_str!("applyHeightAttributes"))?;
let resolve_css_variables = env.get_bool_field(cfg, jni_str!("resolveCssVariables"))?;
let keep_classes = env.get_bool_field(cfg, jni_str!("keepClasses"))?;
let remove_unused_at_rules = env.get_bool_field(cfg, jni_str!("removeUnusedAtRules"))?;

let extra_css = env.get_string_field_opt(cfg, jni_str!("extraCss"))?;
let base_url = env.get_string_field_opt(cfg, jni_str!("baseUrl"))?;
Expand All @@ -133,7 +134,8 @@ fn build_inliner(env: &mut Env, cfg: &JObject) -> Result<CSSInliner<'static>, Er
.apply_width_attributes(apply_width_attributes)
.apply_height_attributes(apply_height_attributes)
.resolve_css_variables(resolve_css_variables)
.keep_classes(keep_classes);
.keep_classes(keep_classes)
.remove_unused_at_rules(remove_unused_at_rules);

if let Some(url) = base_url {
match css_inline::Url::parse(&url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ void removeClasses() {
assertEquals("<html><head></head><body><h1 style=\"color: blue;\">Test</h1></body></html>", out);
}

@Test
void removeUnusedAtRules() {
String html = "<html><head><style>@media print { h1 { color: blue; } h2 { color: red; } }</style></head><body><h1>Test</h1></body></html>";

CssInlineConfig cfg = new CssInlineConfig.Builder().setKeepAtRules(true).setRemoveUnusedAtRules(true).build();

String out = CssInline.inline(html, cfg);

assertEquals("<html><head><style>@media print { h1 { color: blue; } } </style></head><body><h1>Test</h1></body></html>", out);
}

@Test
void keepsCssVariablesByDefault() {
String html = "<html><head><style>:root{--c:red} h1{color:var(--c)}</style></head><body><h1>Test</h1></body></html>";
Expand Down
1 change: 1 addition & 0 deletions crates/css-inline-js/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions crates/css-inline-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<style>` blocks and at-rules match nothing. Default: `true`
- `removeUnusedAtRules`. Remove rules that match no element from kept at-rules (`@media`, `@supports`, ...), along with blocks left empty. Needs `keepAtRules` or `removeInlinedSelectors`. Matching ignores `:hover`, `::before` and similar. This also removes rules that target markup injected by an email client (e.g. Gmail's `u ~ div .x`). Default: `false`

To skip inlining for a tag, add the `data-css-inline="ignore"` attribute:

Expand Down
10 changes: 10 additions & 0 deletions crates/css-inline-js/__test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ test("remove classes", (t) => {
const cssVariablesHtml =
"<html><head><style>:root{--c:red} h1{color:var(--c)}</style></head><body><h1>Test</h1></body></html>";

test("remove unused at-rules", (t) => {
t.is(
inline(
"<html><head><style>@media print { h1 { color: blue; } h2 { color: red; } }</style></head><body><h1>Test</h1></body></html>",
{ keepAtRules: true, removeUnusedAtRules: true },
),
"<html><head><style>@media print { h1 { color: blue; } } </style></head><body><h1>Test</h1></body></html>",
);
});

test("resolve css variables is off by default", (t) => {
t.is(
inline(cssVariablesHtml),
Expand Down
2 changes: 2 additions & 0 deletions crates/css-inline-js/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions crates/css-inline-js/js-binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions crates/css-inline-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions crates/css-inline-js/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ pub struct Options {
pub media_type: Option<String>,
/// Keep `class` attributes in the output.
pub keep_classes: Option<bool>,
/// Remove rules that match no element from the conditional at-rules (`@media`, `@supports`, ...) kept in the output.
pub remove_unused_at_rules: Option<bool>,
}

impl TryFrom<Options> for css_inline::InlineOptions<'_> {
Expand Down Expand Up @@ -171,6 +173,7 @@ impl TryFrom<Options> 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),
})
}
}
2 changes: 1 addition & 1 deletion crates/css-inline-js/wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions crates/css-inline-js/wasm/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface InlineOptions {
resolveCssVariables?: boolean;
mediaType?: string;
keepClasses?: boolean;
removeUnusedAtRules?: boolean;
}
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
/**
Expand Down
1 change: 1 addition & 0 deletions crates/css-inline-php/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions crates/css-inline-php/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<style>` blocks and at-rules match nothing. Default: `true`
- `removeUnusedAtRules`. Remove rules that match no element from kept at-rules (`@media`, `@supports`, ...), along with blocks left empty. Needs `keepAtRules` or `removeInlinedSelectors`. Matching ignores `:hover`, `::before` and similar. This also removes rules that target markup injected by an email client (e.g. Gmail's `u ~ div .x`). Default: `false`

To skip CSS inlining for an HTML tag, add the `data-css-inline="ignore"` attribute to it:

Expand Down
3 changes: 3 additions & 0 deletions crates/css-inline-php/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl CssInliner {
resolveCssVariables = false,
mediaType = None,
keepClasses = true,
removeUnusedAtRules = false,
))]
#[php(optional = inlineStyleTags)]
#[allow(clippy::too_many_arguments)]
Expand All @@ -81,6 +82,7 @@ impl CssInliner {
resolveCssVariables: bool,
mediaType: Option<String>,
keepClasses: bool,
removeUnusedAtRules: bool,
) -> PhpResult<CssInliner> {
if preallocateNodeCapacity < 0 {
return Err(PhpException::from_message(
Expand Down Expand Up @@ -115,6 +117,7 @@ impl CssInliner {
resolve_css_variables: resolveCssVariables,
media_type: mediaType,
keep_classes: keepClasses,
remove_unused_at_rules: removeUnusedAtRules,
..Default::default()
};

Expand Down
2 changes: 2 additions & 0 deletions crates/css-inline-php/stubs/css_inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class CssInliner
* @param bool $resolveCssVariables Replace var() references with the values of the custom properties they refer to and drop the --* declarations
* @param string|null $mediaType Inline rules from @media blocks that apply to this media type
* @param bool $keepClasses Keep class attributes in the output
* @param bool $removeUnusedAtRules Remove rules that match no element from the conditional at-rules (@media, @supports, ...) kept in the output
*/
public function __construct(
bool $inlineStyleTags = true,
Expand All @@ -91,6 +92,7 @@ public function __construct(
bool $resolveCssVariables = false,
?string $mediaType = null,
bool $keepClasses = true,
bool $removeUnusedAtRules = false,
) {}

/**
Expand Down
Loading
Loading