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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- `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)

### Changed

- **BREAKING**: Error on `keep_at_rules` in `inline_fragment` (`InlineError::UnsupportedFragmentOption`). [#514](https://github.com/Stranger6667/css-inline/issues/514)

### Fixed

- `extra_css`, fragment CSS and linked stylesheets not inlined when `keep_at_rules` is enabled and `inline_style_tags` is disabled.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fn main() -> css_inline::Result<()> {
- `inline_style_tags`. Inline CSS from `style` tags. With `false`, `css-inline` ignores their CSS and still removes the tags unless `keep_style_tags` is `true`. Default: `true`
- `keep_style_tags`. Keep `style` tags after inlining. Default: `false`
- `keep_link_tags`. Keep `link` tags after inlining. Default: `false`
- `keep_at_rules`. Keep at-rules (starting with `@`) after inlining. Default: `false`
- `keep_at_rules`. Keep at-rules (starting with `@`) after inlining. Errors in fragment inlining. Default: `false`
- `minify_css`. Remove trailing semicolons and spaces between properties and values. Default: `false`
- `base_url`. Base URL for resolving relative URLs. Use the `file://` scheme to load stylesheets from the filesystem. Default: `None`
- `load_remote_stylesheets`. Load stylesheets from `link` tags, over the network or from `file://` paths. Default: `true`
Expand Down
4 changes: 4 additions & 0 deletions crates/css-inline-c/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- `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)

### Changed

- **BREAKING**: `CSS_RESULT_UNSUPPORTED_FRAGMENT_OPTION` error on `keep_at_rules` in `css_inline_fragment_to`. [#514](https://github.com/Stranger6667/css-inline/issues/514)

### Fixed

- `extra_css`, fragment CSS and linked stylesheets not inlined when `keep_at_rules` is enabled and `inline_style_tags` is disabled.
Expand Down
3 changes: 2 additions & 1 deletion crates/css-inline-c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ Both functions return a `CssResult`. `CSS_RESULT_OK` is `0`; any other value is
| `CSS_RESULT_INVALID_INPUT_STRING` | The HTML or CSS input is not UTF-8 |
| `CSS_RESULT_INVALID_CACHE_SIZE` | The stylesheet cache size is `0` |
| `CSS_RESULT_INVALID_MEDIA_TYPE` | `media_type` is not UTF-8 |
| `CSS_RESULT_UNSUPPORTED_FRAGMENT_OPTION` | `keep_at_rules` is set in `css_inline_fragment_to()` |

### Configuration

Expand All @@ -227,7 +228,7 @@ Fields:
- `inline_style_tags`. Inline CSS from `style` tags. Default: `true`
- `keep_style_tags`. Keep `style` tags after inlining. Default: `false`
- `keep_link_tags`. Keep `link` tags after inlining. Default: `false`
- `keep_at_rules`. Keep at-rules (starting with `@`) after inlining. Default: `false`
- `keep_at_rules`. Keep at-rules (starting with `@`) after inlining. Errors in fragment inlining. Default: `false`
- `minify_css`. Remove trailing semicolons and spaces between properties and values. Default: `false`
- `base_url` (`const char *`). Base URL for resolving relative URLs. Use the `file://` scheme to load stylesheets from your filesystem. Default: `NULL`
- `load_remote_stylesheets`. Load stylesheets from `link` tags, over the network or from `file://` paths. Default: `true`
Expand Down
3 changes: 3 additions & 0 deletions crates/css-inline-c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub enum CssResult {
InvalidCacheSize,
/// Invalid media_type parameter.
InvalidMediaType,
/// Option that is not supported for fragment inlining.
UnsupportedFragmentOption,
}

impl From<InlineError> for CssResult {
Expand All @@ -45,6 +47,7 @@ impl From<InlineError> for CssResult {
InlineError::Network { .. } => CssResult::RemoteStylesheetNotAvailable,
InlineError::ParseError(_) => CssResult::InternalSelectorParseError,
InlineError::MissingStyleSheet { .. } => CssResult::MissingStylesheet,
InlineError::UnsupportedFragmentOption => CssResult::UnsupportedFragmentOption,
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions crates/css-inline-c/tests/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ static void test_inline_fragment(void) {
assert(strcmp(output, SAMPLE_INLINED_FRAGMENT) == 0);
}

static void test_inline_fragment_keep_at_rules(void) {
CssInlinerOptions options = css_inliner_default_options();
options.keep_at_rules = true;
char output[MAX_SIZE];
assert(css_inline_fragment_to(&options, SAMPLE_FRAGMENT, SAMPLE_FRAGMENT_STYLE, output, sizeof(output)) ==
CSS_RESULT_UNSUPPORTED_FRAGMENT_OPTION);
}

static void test_remove_inlined_selectors(void) {
char html[] = "<html><head><style>h1 { color: blue; } h2 { color: red; }</style></head><body><h1>Test</h1></body></html>";
CssInlinerOptions options = css_inliner_default_options();
Expand Down Expand Up @@ -174,6 +182,7 @@ int main(void) {
test_cache_valid();
test_cache_invalid();
test_inline_fragment();
test_inline_fragment_keep_at_rules();
test_remove_inlined_selectors();
test_resolve_css_variables();
test_media_type();
Expand Down
4 changes: 4 additions & 0 deletions crates/css-inline-java/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- `setMediaType` option to inline rules from `@media` blocks that apply to the given media type. [#410](https://github.com/Stranger6667/css-inline/issues/410)

### Changed

- **BREAKING**: Error on `setKeepAtRules(true)` in `inlineFragment`. [#514](https://github.com/Stranger6667/css-inline/issues/514)

### Fixed

- `setExtraCss`, fragment CSS and linked stylesheets not inlined when `setKeepAtRules` is enabled and `setInlineStyleTags` is disabled.
Expand Down
2 changes: 1 addition & 1 deletion crates/css-inline-java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public class ConfigExample {
- **`setInlineStyleTags(boolean)`** - Inline CSS from `<style>` tags (default: `true`)
- **`setKeepStyleTags(boolean)`** - Keep `<style>` tags after inlining (default: `false`)
- **`setKeepLinkTags(boolean)`** - Keep `<link>` tags after inlining (default: `false`)
- **`setKeepAtRules(boolean)`** - Keep `at-rules` (starting with `@`) after inlining (default: `false`)
- **`setKeepAtRules(boolean)`** - Keep `at-rules` (starting with `@`) after inlining; errors in fragment inlining (default: `false`)
- **`setMinifyCss(boolean)`** - Remove trailing semicolons and spaces between properties and values (default: `false`)
- **`setBaseUrl(String)`** - Base URL for resolving relative URLs (default: `null`)
- **`setLoadRemoteStylesheets(boolean)`** - Load stylesheets from `link` tags, over the network or from `file://` paths (default: `true`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ void inlineFragmentWithConfig() {
"Should apply extra CSS");
}

@Test
void inlineFragmentKeepAtRulesThrows() {
CssInlineConfig config = new CssInlineConfig.Builder().setKeepAtRules(true).build();

CssInlineException ex = assertThrows(CssInlineException.class,
() -> CssInline.inlineFragment("<h1>Test</h1>", "h1 { color: blue; }", config));
assertEquals("`keep_at_rules` is not supported for fragment inlining", ex.getMessage());
}

@Test
void inlineFragmentEmpty() {
String result = CssInline.inlineFragment("", "");
Expand Down
4 changes: 4 additions & 0 deletions crates/css-inline-js/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- `mediaType` option to inline rules from `@media` blocks that apply to the given media type. [#410](https://github.com/Stranger6667/css-inline/issues/410)

### Changed

- **BREAKING**: Error on `keepAtRules` in `inlineFragment` and `inlineFragmentAsync`. [#514](https://github.com/Stranger6667/css-inline/issues/514)

### Fixed

- `extraCss`, fragment CSS and linked stylesheets not inlined when `keepAtRules` is enabled and `inlineStyleTags` is disabled.
Expand Down
2 changes: 1 addition & 1 deletion crates/css-inline-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Pass options as the second argument to `inline` or the third to `inlineFragment`
- `inlineStyleTags`. Inline CSS from `style` tags. Default: `true`
- `keepStyleTags`. Keep `style` tags after inlining. Default: `false`
- `keepLinkTags`. Keep `link` tags after inlining. Default: `false`
- `keepAtRules`. Keep at-rules (`@media`, `@font-face`, ...) after inlining. Default: `false`
- `keepAtRules`. Keep at-rules (`@media`, `@font-face`, ...) after inlining. Errors in fragment inlining. Default: `false`
- `minifyCss`. Remove trailing semicolons and spaces between properties and values. Default: `false`
- `baseUrl`. Base URL for resolving relative URLs. Use the `file://` scheme to load stylesheets from the filesystem; end directory URLs with `/` (`file:///path/to/styles/`), or `css-inline` resolves relative to the parent directory. Default: `null`
- `loadRemoteStylesheets`. Load stylesheets from `link` tags, over the network or from `file://` paths. `false` skips every `link` tag. Default: `true`
Expand Down
9 changes: 9 additions & 0 deletions crates/css-inline-js/__test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,15 @@ h1 {
);
});

test("inline fragment with keepAtRules", (t) => {
const error = t.throws(() => {
inlineFragment("<h1>Hello</h1>", "h1 { color: blue; }", {
keepAtRules: true,
});
});
t.is(error.message, "`keep_at_rules` is not supported for fragment inlining");
});

test("remove inlined selectors", (t) => {
t.is(
inline(
Expand Down
4 changes: 4 additions & 0 deletions crates/css-inline-php/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- `mediaType` option to inline rules from `@media` blocks that apply to the given media type. [#410](https://github.com/Stranger6667/css-inline/issues/410)

### Changed

- **BREAKING**: Error on `keepAtRules` in `inlineFragment` and `inlineManyFragments`. [#514](https://github.com/Stranger6667/css-inline/issues/514)

### Fixed

- `extraCss`, fragment CSS and linked stylesheets not inlined when `keepAtRules` is enabled and `inlineStyleTags` is disabled.
Expand Down
2 changes: 1 addition & 1 deletion crates/css-inline-php/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ $inliner->inline($html);
- `inlineStyleTags`. Inline CSS from `style` tags. Default: `true`
- `keepStyleTags`. Keep `style` tags after inlining. Default: `false`
- `keepLinkTags`. Keep `link` tags after inlining. Default: `false`
- `keepAtRules`. Keep at-rules (rules starting with `@`) after inlining. Default: `false`
- `keepAtRules`. Keep at-rules (rules starting with `@`) after inlining. Errors in fragment inlining. Default: `false`
- `minifyCss`. Remove trailing semicolons and spaces between properties and values. Default: `false`
- `baseUrl`. Base URL for resolving relative URLs. Use the `file://` scheme to load stylesheets from the filesystem. Default: `null`
- `loadRemoteStylesheets`. Load stylesheets from `link` tags, over the network or from `file://` paths. Default: `true`
Expand Down
9 changes: 9 additions & 0 deletions crates/css-inline-php/tests/CssInlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ public function testCssInlinerInlineFragment(): void
$this->assertStringContainsString('style="color: purple;"', $result);
}

public function testInlineFragmentKeepAtRules(): void
{
$inliner = new CssInliner(keepAtRules: true);

$this->expectExceptionMessage('`keep_at_rules` is not supported for fragment inlining');

$inliner->inlineFragment('<h1>Hello</h1>', 'h1 { color: purple; }');
}

public function testCssInlinerInlineMany(): void
{
$inliner = new CssInliner();
Expand Down
4 changes: 4 additions & 0 deletions crates/css-inline-python/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- `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)

### Changed

- **BREAKING**: Error on `keep_at_rules` in `inline_fragment` and `inline_many_fragments`. [#514](https://github.com/Stranger6667/css-inline/issues/514)

### Fixed

- `extra_css`, fragment CSS and linked stylesheets not inlined when `keep_at_rules` is enabled and `inline_style_tags` is disabled.
Expand Down
2 changes: 1 addition & 1 deletion crates/css-inline-python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ inliner.inline("...")
- `inline_style_tags`. Inline CSS from `style` tags. Default: `True`
- `keep_style_tags`. Keep `style` tags after inlining. Default: `False`
- `keep_link_tags`. Keep `link` tags after inlining. Default: `False`
- `keep_at_rules`. Keep at-rules (starting with `@`) after inlining. Default: `False`
- `keep_at_rules`. Keep at-rules (starting with `@`) after inlining. Errors in fragment inlining. Default: `False`
- `minify_css`. Remove trailing semicolons and spaces between properties and values. Default: `False`
- `base_url`. Base URL for resolving relative URLs. Use the `file://` scheme to load stylesheets from the filesystem. Default: `None`
- `load_remote_stylesheets`. Load stylesheets from `link` tags, over the network or from `file://` paths. Default: `True`
Expand Down
3 changes: 2 additions & 1 deletion crates/css-inline-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ impl From<InlineErrorWrapper> for PyErr {
rust_inline::InlineError::ParseError(message) => {
InlineError::new_err(message.to_string())
}
rust_inline::InlineError::MissingStyleSheet { .. } => {
rust_inline::InlineError::MissingStyleSheet { .. }
| rust_inline::InlineError::UnsupportedFragmentOption => {
InlineError::new_err(error.0.to_string())
}
}
Expand Down
5 changes: 5 additions & 0 deletions crates/css-inline-python/tests-py/test_inlining.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ def test_inline_fragment_method():
assert css_inline.CSSInliner().inline_fragment(FRAGMENT, CSS) == EXPECTED_INLINED_FRAGMENT


def test_inline_fragment_keep_at_rules():
with pytest.raises(css_inline.InlineError, match="`keep_at_rules` is not supported for fragment inlining"):
css_inline.inline_fragment(FRAGMENT, CSS, keep_at_rules=True)


def test_inline_many_fragments():
assert css_inline.inline_many_fragments([FRAGMENT, FRAGMENT], [CSS, CSS]) == [
EXPECTED_INLINED_FRAGMENT,
Expand Down
4 changes: 4 additions & 0 deletions crates/css-inline-ruby/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- `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)

### Changed

- **BREAKING**: Error on `keep_at_rules` in `inline_fragment` and `inline_many_fragments`. [#514](https://github.com/Stranger6667/css-inline/issues/514)

### Fixed

- `extra_css`, fragment CSS and linked stylesheets not inlined when `keep_at_rules` is enabled and `inline_style_tags` is disabled.
Expand Down
2 changes: 1 addition & 1 deletion crates/css-inline-ruby/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ The module functions accept the same keyword arguments, for example `CSSInline.i
- `inline_style_tags`. Inline CSS from `style` tags. Default: `true`
- `keep_style_tags`. Keep `style` tags after inlining. Default: `false`
- `keep_link_tags`. Keep `link` tags after inlining. Default: `false`
- `keep_at_rules`. Keep at-rules (starting with `@`) after inlining. Default: `false`
- `keep_at_rules`. Keep at-rules (starting with `@`) after inlining. Errors in fragment inlining. Default: `false`
- `minify_css`. Remove trailing semicolons and spaces between properties and values. Default: `false`
- `base_url`. Base URL for resolving relative URLs. Use the `file://` scheme to load stylesheets from the filesystem. Default: `nil`
- `load_remote_stylesheets`. Load stylesheets from `link` tags, over the network or from `file://` paths. Default: `true`
Expand Down
6 changes: 6 additions & 0 deletions crates/css-inline-ruby/spec/css_inline_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ def make_html(style, body)
expect(inlined).to eq(EXPECTED_INLINED_FRAGMENT)
end

it 'Raises an error on HTML fragments with keep_at_rules' do
expect {
CSSInline::inline_fragment(FRAGMENT, CSS, keep_at_rules: true)
}.to raise_error('`keep_at_rules` is not supported for fragment inlining')
end

it 'Inlines CSS into multiple HTML fragments in parallel' do
inlined = CSSInline::inline_many_fragments([FRAGMENT, FRAGMENT], [CSS, CSS])
expect(inlined[0]).to eq(EXPECTED_INLINED_FRAGMENT)
Expand Down
3 changes: 2 additions & 1 deletion crates/css-inline-ruby/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ impl From<InlineErrorWrapper> for magnus::Error {
rust_inline::InlineError::ParseError(message) => {
magnus::Error::new(ruby.exception_arg_error(), message.to_string())
}
rust_inline::InlineError::MissingStyleSheet { .. } => {
rust_inline::InlineError::MissingStyleSheet { .. }
| rust_inline::InlineError::UnsupportedFragmentOption => {
magnus::Error::new(ruby.exception_arg_error(), error.0.to_string())
}
}
Expand Down
9 changes: 8 additions & 1 deletion crates/css-inline/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub enum InlineError {
},
/// Syntax errors or unsupported selectors.
ParseError(Cow<'static, str>),
/// Option that is not supported for fragment inlining.
UnsupportedFragmentOption,
}

impl From<io::Error> for InlineError {
Expand All @@ -43,7 +45,9 @@ impl Error for InlineError {
InlineError::IO(error) => Some(error),
#[cfg(feature = "http")]
InlineError::Network { error, .. } => Some(error),
InlineError::MissingStyleSheet { .. } | InlineError::ParseError(_) => None,
InlineError::MissingStyleSheet { .. }
| InlineError::ParseError(_)
| InlineError::UnsupportedFragmentOption => None,
}
}
}
Expand All @@ -58,6 +62,9 @@ impl Display for InlineError {
Self::MissingStyleSheet { path } => {
f.write_fmt(format_args!("Missing stylesheet file: {path}"))
}
Self::UnsupportedFragmentOption => {
f.write_str("`keep_at_rules` is not supported for fragment inlining")
}
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions crates/css-inline/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ impl<'a> InlineOptions<'a> {
}

/// Override whether "at-rules" should be kept after processing.
/// Errors in fragment inlining.
#[must_use]
pub fn keep_at_rules(mut self, keep_at_rules: bool) -> Self {
self.keep_at_rules = keep_at_rules;
Expand Down Expand Up @@ -825,6 +826,7 @@ impl<'a> CSSInliner<'a> {
/// - Remote stylesheet is not available;
/// - IO errors;
/// - Internal CSS selector parsing error;
/// - `keep_at_rules` is enabled;
///
/// # Panics
///
Expand All @@ -847,6 +849,7 @@ impl<'a> CSSInliner<'a> {
/// - Remote stylesheet is not available;
/// - IO errors;
/// - Internal CSS selector parsing error;
/// - `keep_at_rules` is enabled;
///
/// # Panics
///
Expand All @@ -858,11 +861,20 @@ impl<'a> CSSInliner<'a> {
css: &str,
target: &mut W,
) -> Result<()> {
self.check_fragment_options()?;
self.inline_to_impl(html, Some(css), target, InliningMode::Fragment, &|url| {
self.options.resolver.retrieve(url)
})
}

/// Kept `@`-rules go to `<head>`, which fragments do not have.
fn check_fragment_options(&self) -> Result<()> {
if self.options.keep_at_rules {
return Err(InlineError::UnsupportedFragmentOption);
}
Ok(())
}

/// Non-blocking version of [`CSSInliner::inline`]. External stylesheets are fetched concurrently
/// via [`StylesheetResolver::retrieve_async`].
///
Expand Down Expand Up @@ -930,6 +942,7 @@ impl<'a> CSSInliner<'a> {
css: &str,
target: &mut W,
) -> Result<()> {
self.check_fragment_options()?;
let stylesheets = self.fetch_stylesheets(html, InliningMode::Fragment).await?;
self.inline_to_impl(html, Some(css), target, InliningMode::Fragment, &|url| {
// `fetch_stylesheets` collects the same URLs from the same HTML
Expand Down
Loading
Loading