|
| 1 | +# css-inline |
| 2 | + |
| 3 | +[<img alt="build status" src="https://img.shields.io/github/actions/workflow/status/Stranger6667/css-inline/build.yml?style=flat-square&labelColor=555555&logo=github" height="20">](https://github.com/Stranger6667/css-inline/actions/workflows/build.yml) |
| 4 | +[<img alt="maven central" src="https://img.shields.io/maven-central/v/org.cssinline/css-inline.svg?style=flat-square&color=fc8d62&logo=apachemaven" height="20">](#installation) |
| 5 | +[<img alt="javadoc" src="https://img.shields.io/badge/javadoc-css--inline-66c2a5?style=flat-square&labelColor=555555" height="20">](#documentation) |
| 6 | + |
| 7 | +Java bindings for the high-performance `css-inline` library that inlines CSS into HTML 'style' attributes. |
| 8 | + |
| 9 | +This library is designed for scenarios such as preparing HTML emails or embedding HTML into third-party web pages. |
| 10 | + |
| 11 | +Transforms HTML like this: |
| 12 | + |
| 13 | +```html |
| 14 | +<html> |
| 15 | + <head> |
| 16 | + <style>h1 { color:blue; }</style> |
| 17 | + </head> |
| 18 | + <body> |
| 19 | + <h1>Big Text</h1> |
| 20 | + </body> |
| 21 | +</html> |
| 22 | +``` |
| 23 | + |
| 24 | +into: |
| 25 | + |
| 26 | +```html |
| 27 | +<html> |
| 28 | + <head></head> |
| 29 | + <body> |
| 30 | + <h1 style="color:blue;">Big Text</h1> |
| 31 | + </body> |
| 32 | +</html> |
| 33 | +``` |
| 34 | + |
| 35 | +## Features |
| 36 | + |
| 37 | +- Uses reliable components from Mozilla's Servo project |
| 38 | +- Inlines CSS from `style` and `link` tags |
| 39 | +- Removes `style` and `link` tags |
| 40 | +- Resolves external stylesheets (including local files) |
| 41 | +- Optionally caches external stylesheets |
| 42 | +- Works on Linux, Windows, and macOS |
| 43 | +- Supports HTML5 & CSS3 |
| 44 | + |
| 45 | +## Installation |
| 46 | + |
| 47 | +**Maven:** |
| 48 | +```xml |
| 49 | +<dependency> |
| 50 | + <groupId>org.css-inline</groupId> |
| 51 | + <artifactId>css-inline</artifactId> |
| 52 | + <version>0.15.0</version> |
| 53 | +</dependency> |
| 54 | +``` |
| 55 | + |
| 56 | +**Gradle:** |
| 57 | +```gradle |
| 58 | +implementation 'org.css-inline:css-inline:0.15.0' |
| 59 | +``` |
| 60 | + |
| 61 | +## Usage |
| 62 | + |
| 63 | +```java |
| 64 | +import org.cssinline.CssInline; |
| 65 | + |
| 66 | +public class Example { |
| 67 | + public static void main(String[] args) { |
| 68 | + String html = """ |
| 69 | + <html> |
| 70 | + <head> |
| 71 | + <style>h1 { color:blue; }</style> |
| 72 | + </head> |
| 73 | + <body> |
| 74 | + <h1>Big Text</h1> |
| 75 | + </body> |
| 76 | + </html>"""; |
| 77 | + |
| 78 | + String inlined = CssInline.inline(html); |
| 79 | + System.out.println(inlined); |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +You can also configure the inlining process: |
| 85 | + |
| 86 | +```java |
| 87 | +import org.cssinline.CssInline; |
| 88 | +import org.cssinline.CssInlineConfig; |
| 89 | + |
| 90 | +public class ConfigExample { |
| 91 | + public static void main(String[] args) { |
| 92 | + CssInlineConfig config = new CssInlineConfig.Builder() |
| 93 | + .loadRemoteStylesheets(false) |
| 94 | + .keepStyleTags(true) |
| 95 | + .baseUrl("https://example.com/") |
| 96 | + .build(); |
| 97 | + |
| 98 | + String inlined = CssInline.inline(html, config); |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +- **`inlineStyleTags(boolean)`** - Inline CSS from `<style>` tags (default: `true`) |
| 104 | +- **`keepStyleTags(boolean)`** - Keep `<style>` tags after inlining (default: `false`) |
| 105 | +- **`keepLinkTags(boolean)`** - Keep `<link>` tags after inlining (default: `false`) |
| 106 | +- **`baseUrl(String)`** - Base URL for resolving relative URLs (default: `null`) |
| 107 | +- **`loadRemoteStylesheets(boolean)`** - Load external stylesheets (default: `true`) |
| 108 | +- **`extraCss(String)`** - Additional CSS to inline (default: `null`) |
| 109 | + |
| 110 | +You can also skip CSS inlining for an HTML tag by adding the `data-css-inline="ignore"` attribute to it: |
| 111 | + |
| 112 | +```html |
| 113 | +<head> |
| 114 | + <style>h1 { color:blue; }</style> |
| 115 | +</head> |
| 116 | +<body> |
| 117 | + <!-- The tag below won't receive additional styles --> |
| 118 | + <h1 data-css-inline="ignore">Big Text</h1> |
| 119 | +</body> |
| 120 | +``` |
| 121 | + |
| 122 | +The `data-css-inline="ignore"` attribute also allows you to skip `link` and `style` tags: |
| 123 | + |
| 124 | +```html |
| 125 | +<head> |
| 126 | + <!-- Styles below are ignored --> |
| 127 | + <style data-css-inline="ignore">h1 { color:blue; }</style> |
| 128 | +</head> |
| 129 | +<body> |
| 130 | + <h1>Big Text</h1> |
| 131 | +</body> |
| 132 | +``` |
| 133 | + |
| 134 | +Alternatively, you may keep `style` from being removed by using the `data-css-inline="keep"` attribute. |
| 135 | +This is useful if you want to keep `@media` queries for responsive emails in separate `style` tags: |
| 136 | + |
| 137 | +```html |
| 138 | +<head> |
| 139 | + <!-- Styles below are not removed --> |
| 140 | + <style data-css-inline="keep">h1 { color:blue; }</style> |
| 141 | +</head> |
| 142 | +<body> |
| 143 | + <h1>Big Text</h1> |
| 144 | +</body> |
| 145 | +``` |
| 146 | + |
| 147 | +Such tags will be kept in the resulting HTML even if the `keepStyleTags` option is set to `false`. |
| 148 | + |
| 149 | +## Performance |
| 150 | + |
| 151 | +`css-inline` is powered by efficient tooling from Mozilla's Servo project and significantly outperforms Java alternatives. |
| 152 | + |
| 153 | +Here is the performance comparison: |
| 154 | + |
| 155 | +| | Size | `css-inline 0.15.0` | `CSSBox 5.0.0` | |
| 156 | +|-------------|---------|---------------------|------------------------| |
| 157 | +| Basic | 230 B | 7.67 µs | 209.93 µs (**27.37x**) | |
| 158 | +| Realistic-1 | 8.58 KB | 123.18 µs | 1.92 ms (**15.58x**) | |
| 159 | +| Realistic-2 | 4.3 KB | 77.74 µs | 608.65 µs (**7.82x**) | |
| 160 | +| GitHub page | 1.81 MB | 168.43 ms | 316.21 ms (**1.87x**) | |
| 161 | + |
| 162 | +The benchmarking code is available in the `src/jmh/java/orf/cssinline/CSSInlineBench.java` file. The benchmarks were conducted using the stable `rustc 1.87`, `OpenJDK 24.0.1` on Ryzen 9 9950X. |
| 163 | + |
| 164 | +## License |
| 165 | + |
| 166 | +This project is licensed under the terms of the [MIT license](https://opensource.org/licenses/MIT). |
0 commit comments