|
| 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) |
| 4 | +[<img alt="ruby gems" src="https://img.shields.io/gem/v/css_inline?logo=ruby&style=flat-square" height="20">](https://rubygems.org/gems/css_inline) |
| 5 | +[<img alt="codecov.io" src="https://img.shields.io/codecov/c/gh/Stranger6667/css-inline?logo=codecov&style=flat-square&token=tOzvV4kDY0" height="20">](https://app.codecov.io/github/Stranger6667/css-inline) |
| 6 | +[<img alt="gitter" src="https://img.shields.io/gitter/room/Stranger6667/css-inline?style=flat-square" height="20">](https://gitter.im/Stranger6667/css-inline) |
| 7 | + |
| 8 | +`css_inline` inlines CSS into HTML documents, using components from Mozilla's Servo project. |
| 9 | + |
| 10 | +This process is essential for sending HTML emails as you need to use "style" attributes instead of "style" tags. |
| 11 | + |
| 12 | +For instance, the library transforms HTML like this: |
| 13 | + |
| 14 | +```html |
| 15 | +<html> |
| 16 | + <head> |
| 17 | + <title>Test</title> |
| 18 | + <style>h1 { color:blue; }</style> |
| 19 | + </head> |
| 20 | + <body> |
| 21 | + <h1>Big Text</h1> |
| 22 | + </body> |
| 23 | +</html> |
| 24 | +``` |
| 25 | + |
| 26 | +into: |
| 27 | + |
| 28 | +```html |
| 29 | +<html> |
| 30 | + <head> |
| 31 | + <title>Test</title> |
| 32 | + </head> |
| 33 | + <body> |
| 34 | + <h1 style="color:blue;">Big Text</h1> |
| 35 | + </body> |
| 36 | +</html> |
| 37 | +``` |
| 38 | + |
| 39 | +- Uses reliable components from Mozilla's Servo |
| 40 | +- Inlines CSS from `style` and `link` tags |
| 41 | +- Removes `style` and `link` tags |
| 42 | +- Resolves external stylesheets (including local files) |
| 43 | +- Can process multiple documents in parallel |
| 44 | +- Works on Linux, Windows, and macOS |
| 45 | +- Supports HTML5 & CSS3 |
| 46 | + |
| 47 | +## Installation |
| 48 | + |
| 49 | +Add this line to your application's `Gemfile`: |
| 50 | + |
| 51 | +``` |
| 52 | +gem 'css_inline' |
| 53 | +``` |
| 54 | + |
| 55 | +## Usage |
| 56 | + |
| 57 | +To inline CSS in an HTML document: |
| 58 | + |
| 59 | +```ruby |
| 60 | +require 'css_inline' |
| 61 | + |
| 62 | +html = "<html><head><style>h1 { color:blue; }</style></head><body><h1>Big Text</h1></body></html>" |
| 63 | +inlined = CSSInline.inline(html) |
| 64 | + |
| 65 | +puts inlined |
| 66 | +# Outputs: "<html><head></head><body><h1 style=\"color:blue;\">Big Text</h1></body></html>" |
| 67 | +``` |
| 68 | + |
| 69 | +## Configuration |
| 70 | + |
| 71 | +For customization options use the `CSSInliner` class: |
| 72 | + |
| 73 | +```python |
| 74 | +require 'css_inline' |
| 75 | + |
| 76 | +inliner = CSSInline::CSSInliner.new(keep_style_tags: true) |
| 77 | +inliner.inline("...") |
| 78 | +``` |
| 79 | + |
| 80 | +- `keep_style_tags`. Specifies whether to keep "style" tags after inlining. Default: `False` |
| 81 | +- `keep_link_tags`. Specifies whether to keep "link" tags after inlining. Default: `False` |
| 82 | +- `base_url`. The base URL used to resolve relative URLs. If you'd like to load stylesheets from your filesystem, use the `file://` scheme. Default: `nil` |
| 83 | +- `load_remote_stylesheets`. Specifies whether remote stylesheets should be loaded. Default: `True` |
| 84 | +- `extra_css`. Extra CSS to be inlined. Default: `nil` |
| 85 | +- `preallocate_node_capacity`. **Advanced**. Preallocates capacity for HTML nodes during parsing. This can improve performance when you have an estimate of the number of nodes in your HTML document. Default: `8` |
| 86 | + |
| 87 | +You can also skip CSS inlining for an HTML tag by adding the `data-css-inline="ignore"` attribute to it: |
| 88 | + |
| 89 | +```html |
| 90 | +<head> |
| 91 | + <title>Test</title> |
| 92 | + <style>h1 { color:blue; }</style> |
| 93 | +</head> |
| 94 | +<body> |
| 95 | + <!-- The tag below won't receive additional styles --> |
| 96 | + <h1 data-css-inline="ignore">Big Text</h1> |
| 97 | +</body> |
| 98 | +``` |
| 99 | + |
| 100 | +The `data-css-inline="ignore"` attribute also allows you to skip `link` and `style` tags: |
| 101 | + |
| 102 | +```html |
| 103 | +<head> |
| 104 | + <title>Test</title> |
| 105 | + <!-- Styles below are ignored --> |
| 106 | + <style data-css-inline="ignore">h1 { color:blue; }</style> |
| 107 | +</head> |
| 108 | +<body> |
| 109 | + <h1>Big Text</h1> |
| 110 | +</body> |
| 111 | +``` |
| 112 | + |
| 113 | +If you'd like to load stylesheets from your filesystem, use the `file://` scheme: |
| 114 | + |
| 115 | +```ruby |
| 116 | +require 'css_inline' |
| 117 | + |
| 118 | +# styles/email is relative to the current directory |
| 119 | +inliner = CSSInline::CSSInliner.new(base_url: "file://styles/email/") |
| 120 | +inliner.inline("...") |
| 121 | +``` |
| 122 | + |
| 123 | +## Performance |
| 124 | + |
| 125 | +Leveraging efficient tools from Mozilla's Servo project, this library delivers superior performance. |
| 126 | +It consistently outperforms `premailer`, offering speed increases often exceeding **30 times**. |
| 127 | + |
| 128 | +The table below provides a detailed comparison between `css_inline` and `premailer` when inlining CSS into an HTML document (like in the Usage section above): |
| 129 | + |
| 130 | +| | `css_inline 0.10.0` | `premailer 1.21.0 with Nokogiri 1.15.2` | Difference | |
| 131 | +|-------------------|---------------------|------------------------------------------------|------------| |
| 132 | +| Basic usage | 11 µs | 448 µs | **40.6x** | |
| 133 | +| Realistic email 1 | 290 µs | 9.72 ms | **33.5x** | |
| 134 | +| Realistic email 2 | 167.50 µs | Error: Cannot parse 0 calc((100% - 500px) / 2) | - | |
| 135 | + |
| 136 | +Please refer to the `test/bench.rb` file to review the benchmark code. |
| 137 | +The results displayed above were measured using stable `rustc 1.70` on Ruby `3.2.2`. |
| 138 | + |
| 139 | +## Ruby support |
| 140 | + |
| 141 | +`css_inline` supports Ruby 2.7 and 3.2. |
| 142 | + |
| 143 | +## Extra materials |
| 144 | + |
| 145 | +If you want to know how this library was created & how it works internally, you could take a look at these articles: |
| 146 | + |
| 147 | +- [Rust crate](https://dygalo.dev/blog/rust-for-a-pythonista-2/) |
| 148 | +- [Python bindings](https://dygalo.dev/blog/rust-for-a-pythonista-3/) |
| 149 | + |
| 150 | +## License |
| 151 | + |
| 152 | +This project is licensed under the terms of the [MIT license](https://opensource.org/licenses/MIT). |
0 commit comments