css_inline 0.21.3-x64-mingw-ucrt
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Cargo.lock +1947 -0
- data/Cargo.toml +3 -0
- data/README.md +264 -0
- data/lib/css_inline/3.2/css_inline.so +0 -0
- data/lib/css_inline/3.3/css_inline.so +0 -0
- data/lib/css_inline/3.4/css_inline.so +0 -0
- data/lib/css_inline/4.0/css_inline.so +0 -0
- data/lib/css_inline.rb +7 -0
- metadata +80 -0
data/Cargo.toml
ADDED
data/README.md
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
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="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` is a high-performance library for inlining CSS into HTML 'style' attributes.
|
|
9
|
+
|
|
10
|
+
This library is designed for scenarios such as preparing HTML emails or embedding HTML into third-party web pages.
|
|
11
|
+
|
|
12
|
+
For instance, the library transforms HTML like this:
|
|
13
|
+
|
|
14
|
+
```html
|
|
15
|
+
<html>
|
|
16
|
+
<head>
|
|
17
|
+
<style>h1 { color:blue; }</style>
|
|
18
|
+
</head>
|
|
19
|
+
<body>
|
|
20
|
+
<h1>Big Text</h1>
|
|
21
|
+
</body>
|
|
22
|
+
</html>
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
into:
|
|
26
|
+
|
|
27
|
+
```html
|
|
28
|
+
<html>
|
|
29
|
+
<head></head>
|
|
30
|
+
<body>
|
|
31
|
+
<h1 style="color:blue;">Big Text</h1>
|
|
32
|
+
</body>
|
|
33
|
+
</html>
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
- Uses reliable components from Mozilla's Servo project
|
|
37
|
+
- Inlines CSS from `style` and `link` tags
|
|
38
|
+
- Removes `style` and `link` tags
|
|
39
|
+
- Resolves external stylesheets (including local files)
|
|
40
|
+
- Optionally caches external stylesheets
|
|
41
|
+
- Can process multiple documents in parallel
|
|
42
|
+
- Works on Linux, Windows, and macOS
|
|
43
|
+
- Supports HTML5 & CSS3
|
|
44
|
+
- Tested on Ruby 3.2, 3.3, and 3.4.
|
|
45
|
+
|
|
46
|
+
## Playground
|
|
47
|
+
|
|
48
|
+
If you'd like to try `css-inline`, you can check the WebAssembly-powered [playground](https://css-inline.org/) to see the results instantly.
|
|
49
|
+
|
|
50
|
+
## Installation
|
|
51
|
+
|
|
52
|
+
Add this line to your application's `Gemfile`:
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
gem 'css_inline'
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Usage
|
|
59
|
+
|
|
60
|
+
To inline CSS in an HTML document:
|
|
61
|
+
|
|
62
|
+
```ruby
|
|
63
|
+
require 'css_inline'
|
|
64
|
+
|
|
65
|
+
html = "<html><head><style>h1 { color:blue; }</style></head><body><h1>Big Text</h1></body></html>"
|
|
66
|
+
inlined = CSSInline.inline(html)
|
|
67
|
+
|
|
68
|
+
puts inlined
|
|
69
|
+
# Outputs: "<html><head></head><body><h1 style=\"color:blue;\">Big Text</h1></body></html>"
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Note that `css-inline` automatically adds missing `html` and `body` tags, so the output is a valid HTML document.
|
|
73
|
+
|
|
74
|
+
Alternatively, you can inline CSS into an HTML fragment. Structural tags (`<html>`, `<head>`, `<body>`) are stripped from the output; only their contents are preserved. Use `CSSInline.inline` if you need to keep the full document structure:
|
|
75
|
+
|
|
76
|
+
```ruby
|
|
77
|
+
require 'css_inline'
|
|
78
|
+
|
|
79
|
+
fragment = """
|
|
80
|
+
<main>
|
|
81
|
+
<h1>Hello</h1>
|
|
82
|
+
<section>
|
|
83
|
+
<p>who am i</p>
|
|
84
|
+
</section>
|
|
85
|
+
</main>
|
|
86
|
+
"""
|
|
87
|
+
|
|
88
|
+
css = """
|
|
89
|
+
p {
|
|
90
|
+
color: red;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
h1 {
|
|
94
|
+
color: blue;
|
|
95
|
+
}
|
|
96
|
+
"""
|
|
97
|
+
|
|
98
|
+
inlined = CSSInline.inline_fragment(fragment, css)
|
|
99
|
+
|
|
100
|
+
puts inlined
|
|
101
|
+
# HTML becomes this:
|
|
102
|
+
# <main>
|
|
103
|
+
# <h1 style="color: blue;">Hello</h1>
|
|
104
|
+
# <section>
|
|
105
|
+
# <p style="color: red;">who am i</p>
|
|
106
|
+
# </section>
|
|
107
|
+
# </main>
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
When there is a need to inline multiple HTML documents simultaneously, `css_inline` offers `inline_many` and `inline_many_fragments` functions.
|
|
111
|
+
This feature allows for concurrent processing of several inputs, significantly improving performance when dealing with a large number of documents.
|
|
112
|
+
|
|
113
|
+
```ruby
|
|
114
|
+
require 'css_inline'
|
|
115
|
+
|
|
116
|
+
inlined = CSSInline.inline_many(["...", "..."])
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Under the hood, `inline_many`, spawns threads at the Rust layer to handle the parallel processing of inputs.
|
|
120
|
+
This results in faster execution times compared to employing parallel processing techniques at the Ruby level.
|
|
121
|
+
|
|
122
|
+
**Note**: To fully benefit from `inline_many`, you should run your application on a multicore machine.
|
|
123
|
+
|
|
124
|
+
## Configuration
|
|
125
|
+
|
|
126
|
+
For customization options use the `CSSInliner` class:
|
|
127
|
+
|
|
128
|
+
```ruby
|
|
129
|
+
require 'css_inline'
|
|
130
|
+
|
|
131
|
+
inliner = CSSInline::CSSInliner.new(keep_style_tags: true)
|
|
132
|
+
inliner.inline("...")
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
- `inline_style_tags`. Specifies whether to inline CSS from "style" tags. Default: `true`
|
|
136
|
+
- `keep_style_tags`. Specifies whether to keep "style" tags after inlining. Default: `false`
|
|
137
|
+
- `keep_link_tags`. Specifies whether to keep "link" tags after inlining. Default: `false`
|
|
138
|
+
- `keep_at_rules`. Specifies whether to keep "at-rules" (starting with `@`) after inlining. Default: `false`
|
|
139
|
+
- `minify_css`. Specifies whether to remove trailing semicolons and spaces between properties and values. Default: `false`
|
|
140
|
+
- `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`
|
|
141
|
+
- `load_remote_stylesheets`. Specifies whether remote stylesheets should be loaded. Default: `true`
|
|
142
|
+
- `cache`. Specifies caching options for external stylesheets (for example, `StylesheetCache(size: 5)`). Default: `nil`
|
|
143
|
+
- `extra_css`. Extra CSS to be inlined. Default: `nil`
|
|
144
|
+
- `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: `32`
|
|
145
|
+
- `remove_inlined_selectors`. Specifies whether to remove selectors that were successfully inlined from `<style>` blocks. Default: `false`
|
|
146
|
+
- `apply_width_attributes`. Specifies whether to add `width` HTML attributes from CSS `width` properties on supported elements (`table`, `td`, `th`, `img`). Default: `false`
|
|
147
|
+
- `apply_height_attributes`. Specifies whether to add `height` HTML attributes from CSS `height` properties on supported elements (`table`, `td`, `th`, `img`). Default: `false`
|
|
148
|
+
|
|
149
|
+
You can also skip CSS inlining for an HTML tag by adding the `data-css-inline="ignore"` attribute to it:
|
|
150
|
+
|
|
151
|
+
```html
|
|
152
|
+
<head>
|
|
153
|
+
<style>h1 { color:blue; }</style>
|
|
154
|
+
</head>
|
|
155
|
+
<body>
|
|
156
|
+
<!-- The tag below won't receive additional styles -->
|
|
157
|
+
<h1 data-css-inline="ignore">Big Text</h1>
|
|
158
|
+
</body>
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
The `data-css-inline="ignore"` attribute also allows you to skip `link` and `style` tags:
|
|
162
|
+
|
|
163
|
+
```html
|
|
164
|
+
<head>
|
|
165
|
+
<!-- Styles below are ignored -->
|
|
166
|
+
<style data-css-inline="ignore">h1 { color:blue; }</style>
|
|
167
|
+
</head>
|
|
168
|
+
<body>
|
|
169
|
+
<h1>Big Text</h1>
|
|
170
|
+
</body>
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Alternatively, you may keep `style` from being removed by using the `data-css-inline="keep"` attribute.
|
|
174
|
+
This is useful if you want to keep `@media` queries for responsive emails in separate `style` tags.
|
|
175
|
+
Such tags will be kept in the resulting HTML even if the `keep_style_tags` option is set to `false`.
|
|
176
|
+
|
|
177
|
+
```html
|
|
178
|
+
<head>
|
|
179
|
+
<!-- Styles below are not removed -->
|
|
180
|
+
<style data-css-inline="keep">h1 { color:blue; }</style>
|
|
181
|
+
</head>
|
|
182
|
+
<body>
|
|
183
|
+
<h1>Big Text</h1>
|
|
184
|
+
</body>
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Another possibility is to set `keep_at_rules` option to `true`. At-rules cannot be inlined into HTML therefore they
|
|
188
|
+
get removed by default. This is useful if you want to keep at-rules, e.g. `@media` queries for responsive emails in
|
|
189
|
+
separate `style` tags but inline any styles which can be inlined.
|
|
190
|
+
Such tags will be kept in the resulting HTML even if the `keep_style_tags` option is explicitly set to `false`.
|
|
191
|
+
|
|
192
|
+
```html
|
|
193
|
+
<head>
|
|
194
|
+
<!-- With keep_at_rules=true "color:blue" will get inlined into <h1> but @media will be kept in <style> -->
|
|
195
|
+
<style>h1 { color: blue; } @media (max-width: 600px) { h1 { font-size: 18px; } }</style>
|
|
196
|
+
</head>
|
|
197
|
+
<body>
|
|
198
|
+
<h1>Big Text</h1>
|
|
199
|
+
</body>
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
If you set the the `minify_css` option to `true`, the inlined styles will be minified by removing trailing semicolons
|
|
203
|
+
and spaces between properties and values.
|
|
204
|
+
|
|
205
|
+
```html
|
|
206
|
+
<head>
|
|
207
|
+
<!-- With minify_css=true, the <h1> will have `style="color:blue;font-weight:bold"` -->
|
|
208
|
+
<style>h1 { color: blue; font-weight: bold; }</style>
|
|
209
|
+
</head>
|
|
210
|
+
<body>
|
|
211
|
+
<h1>Big Text</h1>
|
|
212
|
+
</body>
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
If you'd like to load stylesheets from your filesystem, use the `file://` scheme:
|
|
216
|
+
|
|
217
|
+
```ruby
|
|
218
|
+
require 'css_inline'
|
|
219
|
+
|
|
220
|
+
# styles/email is relative to the current directory
|
|
221
|
+
inliner = CSSInline::CSSInliner.new(base_url: "file://styles/email/")
|
|
222
|
+
inliner.inline("...")
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
You can also cache external stylesheets to avoid excessive network requests:
|
|
226
|
+
|
|
227
|
+
```ruby
|
|
228
|
+
require 'css_inline'
|
|
229
|
+
|
|
230
|
+
inliner = CSSInline::CSSInliner.new(
|
|
231
|
+
cache: CSSInline::StylesheetCache.new(size: 5)
|
|
232
|
+
)
|
|
233
|
+
inliner.inline("...")
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
Caching is disabled by default.
|
|
237
|
+
|
|
238
|
+
## Performance
|
|
239
|
+
|
|
240
|
+
This library uses components from Mozilla's Servo project for CSS parsing and matching.
|
|
241
|
+
Performance benchmarks show 50-100x faster execution than `roadie` and `premailer`.
|
|
242
|
+
|
|
243
|
+
The table below shows benchmark results comparing `css_inline`, `roadie`, and `premailer` on typical HTML documents:
|
|
244
|
+
|
|
245
|
+
| | Size | `css_inline 0.19.0` | `roadie 5.2.1` | `premailer 1.21.0` |
|
|
246
|
+
|-------------------|---------|---------------------|-------------------------|-------------------------|
|
|
247
|
+
| Basic usage | 230 B | 6.07 µs | 173.50 µs (**28.60x**) | 345.30 µs (**56.91x**) |
|
|
248
|
+
| Realistic email 1 | 8.58 KB | 91.23 µs | 713.40 µs (**7.82x**) | 6.80 ms (**74.53x**) |
|
|
249
|
+
| Realistic email 2 | 4.3 KB | 57.56 µs | 1.99 ms (**34.56x**) | ERROR |
|
|
250
|
+
| GitHub Page | 1.81 MB | 21.61 ms | 8.20 s (**379.45x**) | 2.40 s (**111.06x**) |
|
|
251
|
+
|
|
252
|
+
Please refer to the `test/bench.rb` file to review the benchmark code.
|
|
253
|
+
The results displayed above were measured using stable `rustc 1.91` on Ruby `3.4.7`.
|
|
254
|
+
|
|
255
|
+
## Further reading
|
|
256
|
+
|
|
257
|
+
If you want to know how this library was created & how it works internally, you could take a look at these articles:
|
|
258
|
+
|
|
259
|
+
- [Rust crate](https://dygalo.dev/blog/rust-for-a-pythonista-2/)
|
|
260
|
+
- [Python bindings](https://dygalo.dev/blog/rust-for-a-pythonista-3/)
|
|
261
|
+
|
|
262
|
+
## License
|
|
263
|
+
|
|
264
|
+
This project is licensed under the terms of the [MIT license](https://opensource.org/licenses/MIT).
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
data/lib/css_inline.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: css_inline
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.21.3
|
|
5
|
+
platform: x64-mingw-ucrt
|
|
6
|
+
authors:
|
|
7
|
+
- Dmitry Dygalo
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-08-28 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rake-compiler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 1.3.0
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 1.3.0
|
|
27
|
+
description: |2
|
|
28
|
+
`css_inline` inlines CSS into HTML documents, using components from Mozilla's Servo project.
|
|
29
|
+
This process is essential for sending HTML emails as you need to use "style" attributes instead of "style" tags.
|
|
30
|
+
email:
|
|
31
|
+
- dmitry@dygalo.dev
|
|
32
|
+
executables: []
|
|
33
|
+
extensions: []
|
|
34
|
+
extra_rdoc_files: []
|
|
35
|
+
files:
|
|
36
|
+
- Cargo.lock
|
|
37
|
+
- Cargo.toml
|
|
38
|
+
- README.md
|
|
39
|
+
- lib/css_inline.rb
|
|
40
|
+
- lib/css_inline/3.2/css_inline.so
|
|
41
|
+
- lib/css_inline/3.3/css_inline.so
|
|
42
|
+
- lib/css_inline/3.4/css_inline.so
|
|
43
|
+
- lib/css_inline/4.0/css_inline.so
|
|
44
|
+
homepage: https://github.com/Stranger6667/css-inline
|
|
45
|
+
licenses:
|
|
46
|
+
- MIT
|
|
47
|
+
metadata:
|
|
48
|
+
bug_tracker_uri: https://github.com/Stranger6667/css-inline/issues
|
|
49
|
+
changelog_uri: https://github.com/Stranger6667/css-inline/tree/master/bindings/ruby/CHANGELOG.md
|
|
50
|
+
source_code_uri: https://github.com/Stranger6667/css-inline/tree/master/bindings/ruby
|
|
51
|
+
funding_uri: https://github.com/sponsors/Stranger6667/
|
|
52
|
+
post_install_message:
|
|
53
|
+
rdoc_options:
|
|
54
|
+
- "--main"
|
|
55
|
+
- README.rdoc
|
|
56
|
+
- "--charset"
|
|
57
|
+
- utf-8
|
|
58
|
+
- "--exclude"
|
|
59
|
+
- ext/
|
|
60
|
+
require_paths:
|
|
61
|
+
- lib
|
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
63
|
+
requirements:
|
|
64
|
+
- - ">="
|
|
65
|
+
- !ruby/object:Gem::Version
|
|
66
|
+
version: '3.2'
|
|
67
|
+
- - "<"
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: 4.1.dev
|
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - ">="
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '0'
|
|
75
|
+
requirements: []
|
|
76
|
+
rubygems_version: 3.5.23
|
|
77
|
+
signing_key:
|
|
78
|
+
specification_version: 4
|
|
79
|
+
summary: High-performance library for inlining CSS into HTML 'style' attributes
|
|
80
|
+
test_files: []
|