1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#![warn(
clippy::doc_markdown,
clippy::redundant_closure,
clippy::explicit_iter_loop,
clippy::match_same_arms,
clippy::needless_borrow,
clippy::print_stdout,
clippy::integer_arithmetic,
clippy::cast_possible_truncation,
clippy::unwrap_used,
clippy::map_unwrap_or,
clippy::trivially_copy_pass_by_ref,
clippy::needless_pass_by_value,
missing_docs,
missing_debug_implementations,
trivial_casts,
trivial_numeric_casts,
unreachable_pub,
unused_extern_crates,
unused_import_braces,
unused_qualifications,
variant_size_differences,
rust_2018_idioms,
rust_2018_compatibility
)]
use kuchiki::{
parse_html, traits::TendrilSink, ElementData, Node, NodeDataRef, NodeRef, Specificity,
};
pub mod error;
mod parser;
pub use error::InlineError;
use smallvec::{smallvec, SmallVec};
use std::{
borrow::Cow,
collections::{hash_map::Entry, HashMap},
fs::File,
io::{Read, Write},
ops::Deref,
};
pub use url::{ParseError, Url};
#[derive(Debug)]
pub struct InlineOptions<'a> {
pub inline_style_tags: bool,
pub remove_style_tags: bool,
pub base_url: Option<Url>,
pub load_remote_stylesheets: bool,
pub extra_css: Option<Cow<'a, str>>,
}
impl<'a> InlineOptions<'a> {
#[inline]
pub const fn compact() -> Self {
InlineOptions {
inline_style_tags: true,
remove_style_tags: true,
base_url: None,
load_remote_stylesheets: true,
extra_css: None,
}
}
pub fn inline_style_tags(mut self, inline_style_tags: bool) -> Self {
self.inline_style_tags = inline_style_tags;
self
}
pub fn remove_style_tags(mut self, remove_style_tags: bool) -> Self {
self.remove_style_tags = remove_style_tags;
self
}
pub fn base_url(mut self, base_url: Option<Url>) -> Self {
self.base_url = base_url;
self
}
pub fn load_remote_stylesheets(mut self, load_remote_stylesheets: bool) -> Self {
self.load_remote_stylesheets = load_remote_stylesheets;
self
}
pub fn extra_css(mut self, extra_css: Option<Cow<'a, str>>) -> Self {
self.extra_css = extra_css;
self
}
pub const fn build(self) -> CSSInliner<'a> {
CSSInliner::new(self)
}
}
impl Default for InlineOptions<'_> {
#[inline]
fn default() -> Self {
InlineOptions {
inline_style_tags: true,
remove_style_tags: false,
base_url: None,
load_remote_stylesheets: true,
extra_css: None,
}
}
}
type Result<T> = std::result::Result<T, InlineError>;
#[derive(Debug)]
pub struct CSSInliner<'a> {
options: InlineOptions<'a>,
}
impl<'a> CSSInliner<'a> {
#[inline]
pub const fn new(options: InlineOptions<'a>) -> Self {
CSSInliner { options }
}
#[inline]
pub fn options() -> InlineOptions<'a> {
InlineOptions::default()
}
#[inline]
pub fn compact() -> Self {
CSSInliner {
options: InlineOptions::compact(),
}
}
#[inline]
pub fn inline(&self, html: &str) -> Result<String> {
let mut out = Vec::with_capacity(html.len());
self.inline_to(html, &mut out)?;
Ok(String::from_utf8_lossy(&out).to_string())
}
#[inline]
pub fn inline_to<W: Write>(&self, html: &str, target: &mut W) -> Result<()> {
let document = parse_html().one(html);
#[allow(clippy::mutable_key_type)]
let mut styles = HashMap::with_capacity(128);
let mut style_tags: SmallVec<[NodeDataRef<ElementData>; 4]> = smallvec![];
if self.options.inline_style_tags {
for style_tag in document
.select("style")
.map_err(|_| error::InlineError::ParseError(Cow::from("Unknown error")))?
{
if let Some(first_child) = style_tag.as_node().first_child() {
if let Some(css_cell) = first_child.as_text() {
process_css(&document, css_cell.borrow().as_str(), &mut styles)?;
}
}
if self.options.remove_style_tags {
style_tags.push(style_tag)
}
}
}
if self.options.remove_style_tags {
if !self.options.inline_style_tags {
style_tags.extend(
document
.select("style")
.map_err(|_| error::InlineError::ParseError(Cow::from("Unknown error")))?,
)
}
for style_tag in &style_tags {
style_tag.as_node().detach()
}
}
if self.options.load_remote_stylesheets {
let mut links = document
.select("link[rel~=stylesheet]")
.map_err(|_| error::InlineError::ParseError(Cow::from("Unknown error")))?
.filter_map(|link_tag| link_tag.attributes.borrow().get("href").map(str::to_string))
.collect::<Vec<String>>();
links.sort_unstable();
links.dedup();
for href in &links {
if !href.is_empty() {
let url = self.get_full_url(href);
let css = load_external(url.as_ref())?;
process_css(&document, css.as_str(), &mut styles)?;
}
}
}
if let Some(extra_css) = &self.options.extra_css {
process_css(&document, extra_css, &mut styles)?;
}
for (node_id, styles) in styles {
let node = unsafe { &*node_id };
if let Ok(mut attributes) = node
.as_element()
.expect("Element is expected")
.attributes
.try_borrow_mut()
{
if let Some(existing_style) = attributes.get_mut("style") {
*existing_style = merge_styles(existing_style, &styles)?
} else {
let mut final_styles = String::with_capacity(128);
for (name, (_, value)) in styles {
final_styles.push_str(name.as_str());
final_styles.push(':');
final_styles.push_str(value.as_str());
final_styles.push(';');
}
attributes.insert("style", final_styles);
};
}
}
document.serialize(target)?;
Ok(())
}
fn get_full_url<'u>(&self, href: &'u str) -> Cow<'u, str> {
if Url::parse(href).is_ok() {
return Cow::Borrowed(href);
};
if let Some(base_url) = &self.options.base_url {
if href.starts_with("//") {
return Cow::Owned(format!("{}:{}", base_url.scheme(), href));
} else {
if let Ok(new_url) = base_url.join(href) {
return Cow::Owned(new_url.into());
}
}
};
Cow::Borrowed(href)
}
}
fn load_external(url: &str) -> Result<String> {
if url.starts_with("http") | url.starts_with("https") {
let response = attohttpc::get(url).send()?;
Ok(response.text()?)
} else {
let mut file = File::open(url)?;
let mut css = String::new();
file.read_to_string(&mut css)?;
Ok(css)
}
}
type NodeId = *const Node;
#[allow(clippy::mutable_key_type)]
fn process_css(
document: &NodeRef,
css: &str,
styles: &mut HashMap<NodeId, HashMap<String, (Specificity, String)>>,
) -> Result<()> {
let mut parse_input = cssparser::ParserInput::new(css);
let mut parser = cssparser::Parser::new(&mut parse_input);
let rule_list =
cssparser::RuleListParser::new_for_stylesheet(&mut parser, parser::CSSRuleListParser);
for (selectors, declarations) in rule_list.flatten() {
for selector in selectors.split(',') {
if let Ok(matching_elements) = document.select(selector) {
let specificity = matching_elements.selectors.0[0].specificity();
for matching_element in matching_elements {
let element_styles = styles
.entry(matching_element.as_node().deref())
.or_insert_with(|| HashMap::with_capacity(16));
for (name, value) in &declarations {
match element_styles.entry(name.to_string()) {
Entry::Occupied(mut entry) => {
if entry.get().0 <= specificity {
entry.insert((specificity, value.to_string()));
}
}
Entry::Vacant(entry) => {
entry.insert((specificity, value.to_string()));
}
}
}
}
}
}
}
Ok(())
}
impl Default for CSSInliner<'_> {
#[inline]
fn default() -> Self {
CSSInliner::new(InlineOptions::default())
}
}
#[inline]
pub fn inline(html: &str) -> Result<String> {
CSSInliner::default().inline(html)
}
#[inline]
pub fn inline_to<W: Write>(html: &str, target: &mut W) -> Result<()> {
CSSInliner::default().inline_to(html, target)
}
fn merge_styles(
existing_style: &str,
new_styles: &HashMap<String, (Specificity, String)>,
) -> Result<String> {
let mut input = cssparser::ParserInput::new(existing_style);
let mut parser = cssparser::Parser::new(&mut input);
let declarations =
cssparser::DeclarationListParser::new(&mut parser, parser::CSSDeclarationListParser);
let mut buffer: SmallVec<[&str; 8]> = smallvec![];
let mut final_styles = String::with_capacity(256);
for (property, (_, value)) in new_styles {
final_styles.push_str(property);
final_styles.push(':');
final_styles.push_str(value);
final_styles.push(';');
buffer.push(property);
}
for declaration in declarations {
let (name, value) = declaration?;
if !buffer.contains(&name.as_ref()) {
final_styles.push_str(&name);
final_styles.push(':');
final_styles.push_str(value);
final_styles.push(';');
}
}
Ok(final_styles)
}