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 @@ -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

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<style>` blocks and at-rules match nothing. Default: `true`

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 @@ -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

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 @@ -240,6 +240,7 @@ Fields:
- `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` (`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`

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 @@ -116,6 +116,8 @@ pub struct CssInlinerOptions {
/// Inline rules from `@media` blocks that apply to this media type, e.g. `print`.
/// Queries with media features, such as `(min-width: 600px)`, never apply.
pub media_type: *const c_char,
/// Keep `class` attributes in the output.
pub keep_classes: bool,
}

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

Expand Down Expand Up @@ -292,6 +295,7 @@ impl TryFrom<&CssInlinerOptions> for InlineOptions<'_> {
apply_height_attributes: value.apply_height_attributes,
resolve_css_variables: value.resolve_css_variables,
media_type,
keep_classes: value.keep_classes,
})
}
}
Expand Down
10 changes: 10 additions & 0 deletions crates/css-inline-c/tests/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ static void test_remove_inlined_selectors(void) {
assert(strcmp(output, "<html><head><style>h2 { color: red; }</style></head><body><h1 style=\"color: blue;\">Test</h1></body></html>") == 0);
}

static void test_keep_classes(void) {
char html[] = "<html><head><style>.a { color: blue; }</style></head><body><h1 class=\"a\">Test</h1></body></html>";
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, "<html><head></head><body><h1 style=\"color: blue;\">Test</h1></body></html>") == 0);
}

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

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 @@ -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 `<style>` blocks and at-rules match nothing (default: `true`)

`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 @@ -46,12 +46,14 @@ public class CssInlineConfig {

/** Inline rules from @media blocks that apply to this media type. */
public final String mediaType;
/** Keep class attributes in the output. */
public final boolean keepClasses;

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) {
String mediaType, boolean keepClasses) {
this.inlineStyleTags = inlineStyleTags;
this.keepStyleTags = keepStyleTags;
this.keepLinkTags = keepLinkTags;
Expand All @@ -67,6 +69,7 @@ private CssInlineConfig(boolean inlineStyleTags, boolean keepStyleTags, boolean
this.applyHeightAttributes = applyHeightAttributes;
this.resolveCssVariables = resolveCssVariables;
this.mediaType = mediaType;
this.keepClasses = keepClasses;
}

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

/**
* Creates a new builder with default configuration values.
Expand Down Expand Up @@ -283,6 +287,17 @@ public Builder setMediaType(String mediaType) {
return this;
}

/**
* Keep class attributes in the output.
*
* @param b true to keep class attributes, false to remove them
* @return this builder instance for method chaining
*/
public Builder setKeepClasses(boolean b) {
this.keepClasses = b;
return this;
}

/**
* Creates a new {@link CssInlineConfig} instance with the current builder settings.
*
Expand All @@ -291,7 +306,7 @@ public Builder setMediaType(String mediaType) {
public CssInlineConfig build() {
return new CssInlineConfig(inlineStyleTags, keepStyleTags, keepLinkTags, keepAtRules, minifyCss, loadRemoteStylesheets, baseUrl,
extraCss, cacheSize, preallocateNodeCapacity, removeInlinedSelectors, applyWidthAttributes, applyHeightAttributes,
resolveCssVariables, mediaType);
resolveCssVariables, mediaType, keepClasses);
}
}
}
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 @@ -115,6 +115,7 @@ fn build_inliner(env: &mut Env, cfg: &JObject) -> Result<CSSInliner<'static>, Er
let apply_width_attributes = env.get_bool_field(cfg, jni_str!("applyWidthAttributes"))?;
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 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 @@ -131,7 +132,8 @@ fn build_inliner(env: &mut Env, cfg: &JObject) -> Result<CSSInliner<'static>, Er
.remove_inlined_selectors(remove_inlined_selectors)
.apply_width_attributes(apply_width_attributes)
.apply_height_attributes(apply_height_attributes)
.resolve_css_variables(resolve_css_variables);
.resolve_css_variables(resolve_css_variables)
.keep_classes(keep_classes);

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 @@ -163,6 +163,17 @@ void removeInlinedSelectors() {
assertEquals("<html><head><style>h2 { color: red; }</style></head><body><h1 style=\"color: blue;\">Test</h1></body></html>", out);
}

@Test
void removeClasses() {
String html = "<html><head><style>.a { color: blue; }</style></head><body><h1 class=\"a\">Test</h1></body></html>";

CssInlineConfig cfg = new CssInlineConfig.Builder().setKeepClasses(false).build();

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

assertEquals("<html><head></head><body><h1 style=\"color: blue;\">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 @@ -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

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 @@ -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 `<style>` blocks and at-rules match nothing. Default: `true`

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 @@ -212,6 +212,16 @@ test("remove inlined selectors", (t) => {
);
});

test("remove classes", (t) => {
t.is(
inline(
'<html><head><style>.a { color: blue; }</style></head><body><h1 class="a">Test</h1></body></html>',
{ keepClasses: false },
),
'<html><head></head><body><h1 style="color: blue;">Test</h1></body></html>',
);
});

const cssVariablesHtml =
"<html><head><style>:root{--c:red} h1{color:var(--c)}</style></head><body><h1>Test</h1></body></html>";

Expand Down
10 changes: 10 additions & 0 deletions crates/css-inline-js/__test__/wasm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ h1 {
);
});

test("remove classes", (t) => {
t.is(
inline(
'<html><head><style>.a { color: blue; }</style></head><body><h1 class="a">Test</h1></body></html>',
{ keepClasses: false },
),
'<html><head></head><body><h1 style="color: blue;">Test</h1></body></html>',
);
});

const cssVariablesHtml =
"<html><head><style>:root{--c:red} h1{color:var(--c)}</style></head><body><h1>Test</h1></body></html>";

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 @@ -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 {
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 @@ -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 {
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 @@ -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;
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 @@ -77,6 +77,8 @@ pub struct Options {
///
/// Queries with media features, such as `(min-width: 600px)`, never apply.
pub media_type: Option<String>,
/// Keep `class` attributes in the output.
pub keep_classes: Option<bool>,
}

impl TryFrom<Options> for css_inline::InlineOptions<'_> {
Expand Down Expand Up @@ -168,6 +170,7 @@ impl TryFrom<Options> 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),
})
}
}
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`.
`inlineStyleTags`, `keepStyleTags`, `keepLinkTags`, `keepAtRules`, `minifyCss`, `baseUrl`, `loadRemoteStylesheets`, `extraCss`, `preallocateNodeCapacity`, `removeInlinedSelectors`, `applyWidthAttributes`, `applyHeightAttributes`, `resolveCssVariables`, `mediaType`, `keepClasses`.

## 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 @@ -17,6 +17,7 @@ export interface InlineOptions {
applyHeightAttributes?: boolean;
resolveCssVariables?: boolean;
mediaType?: string;
keepClasses?: 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 @@ -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

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 @@ -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 `<style>` blocks and at-rules match nothing. Default: `true`

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 @@ -60,6 +60,7 @@ impl CssInliner {
applyHeightAttributes = false,
resolveCssVariables = false,
mediaType = None,
keepClasses = true,
))]
#[php(optional = inlineStyleTags)]
#[allow(clippy::too_many_arguments)]
Expand All @@ -79,6 +80,7 @@ impl CssInliner {
applyHeightAttributes: bool,
resolveCssVariables: bool,
mediaType: Option<String>,
keepClasses: bool,
) -> PhpResult<CssInliner> {
if preallocateNodeCapacity < 0 {
return Err(PhpException::from_message(
Expand Down Expand Up @@ -112,6 +114,7 @@ impl CssInliner {
apply_height_attributes: applyHeightAttributes,
resolve_css_variables: resolveCssVariables,
media_type: mediaType,
keep_classes: keepClasses,
..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 @@ -72,6 +72,7 @@ class CssInliner
* @param bool $applyHeightAttributes Add height HTML attributes from CSS height properties on supported elements
* @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
*/
public function __construct(
bool $inlineStyleTags = true,
Expand All @@ -89,6 +90,7 @@ public function __construct(
bool $applyHeightAttributes = false,
bool $resolveCssVariables = false,
?string $mediaType = null,
bool $keepClasses = true,
) {}

/**
Expand Down
15 changes: 15 additions & 0 deletions crates/css-inline-php/tests/CssInlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,21 @@ public function testResolveCssVariables(): void
$this->assertStringNotContainsString('--c', $result);
}

public function testRemoveClasses(): void
{
$inliner = new CssInliner(
keepClasses: false,
);

$html = '<html><head><style>.a { color: blue; }</style></head><body><h1 class="a">Test</h1></body></html>';
$result = $inliner->inline($html);

$this->assertEquals(
'<html><head></head><body><h1 style="color: blue;">Test</h1></body></html>',
$result
);
}

public function testRemoveInlinedSelectors(): void
{
$inliner = new CssInliner(
Expand Down
1 change: 1 addition & 0 deletions crates/css-inline-python/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading
Loading