forked from Y2Z/monolith
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml.rs
More file actions
1252 lines (1157 loc) · 49.8 KB
/
html.rs
File metadata and controls
1252 lines (1157 loc) · 49.8 KB
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use base64::prelude::*;
use chrono::prelude::*;
use encoding_rs::Encoding;
use html5ever::interface::QualName;
use html5ever::parse_document;
use html5ever::serialize::{serialize, SerializeOpts};
use html5ever::tendril::{format_tendril, TendrilSink};
use html5ever::tree_builder::{Attribute, TreeSink};
use html5ever::{local_name, namespace_url, ns, LocalName};
use markup5ever_rcdom::{Handle, NodeData, RcDom, SerializableHandle};
use regex::Regex;
use reqwest::blocking::Client;
use reqwest::Url;
use sha2::{Digest, Sha256, Sha384, Sha512};
use std::default::Default;
use crate::cache::Cache;
use crate::core::{parse_content_type, retrieve_asset, Options};
use crate::css::embed_css;
use crate::js::attr_is_event_handler;
use crate::url::{
clean_url, create_data_url, is_url_and_has_protocol, resolve_url, EMPTY_IMAGE_DATA_URL,
};
#[derive(PartialEq, Eq)]
pub enum LinkType {
Alternate,
AppleTouchIcon,
DnsPrefetch,
Favicon,
Preload,
Stylesheet,
}
struct SrcSetItem<'a> {
path: &'a str,
descriptor: &'a str,
}
const FAVICON_VALUES: &[&str] = &["icon", "shortcut icon"];
const WHITESPACES: &[char] = &['\t', '\n', '\x0c', '\r', ' '];
pub fn add_favicon(document: &Handle, favicon_data_url: String) -> RcDom {
let mut buf: Vec<u8> = Vec::new();
serialize(
&mut buf,
&SerializableHandle::from(document.clone()),
SerializeOpts::default(),
)
.expect("unable to serialize DOM into buffer");
let mut dom = html_to_dom(&buf, "utf-8".to_string());
let doc = dom.get_document();
if let Some(html) = get_child_node_by_name(&doc, "html") {
if let Some(head) = get_child_node_by_name(&html, "head") {
let favicon_node = dom.create_element(
QualName::new(None, ns!(), local_name!("link")),
vec![
Attribute {
name: QualName::new(None, ns!(), local_name!("rel")),
value: format_tendril!("icon"),
},
Attribute {
name: QualName::new(None, ns!(), local_name!("href")),
value: format_tendril!("{}", favicon_data_url),
},
],
Default::default(),
);
// Insert favicon LINK tag into HEAD
head.children.borrow_mut().push(favicon_node.clone());
}
}
dom
}
pub fn check_integrity(data: &[u8], integrity: &str) -> bool {
if integrity.starts_with("sha256-") {
let mut hasher = Sha256::new();
hasher.update(data);
BASE64_STANDARD.encode(hasher.finalize()) == integrity[7..]
} else if integrity.starts_with("sha384-") {
let mut hasher = Sha384::new();
hasher.update(data);
BASE64_STANDARD.encode(hasher.finalize()) == integrity[7..]
} else if integrity.starts_with("sha512-") {
let mut hasher = Sha512::new();
hasher.update(data);
BASE64_STANDARD.encode(hasher.finalize()) == integrity[7..]
} else {
false
}
}
pub fn compose_csp(options: &Options) -> String {
let mut string_list = vec![];
if options.isolate {
string_list.push("default-src 'unsafe-eval' 'unsafe-inline' data:;");
}
if options.no_css {
string_list.push("style-src 'none';");
}
if options.no_fonts {
string_list.push("font-src 'none';");
}
if options.no_frames {
string_list.push("frame-src 'none';");
string_list.push("child-src 'none';");
}
if options.no_js {
string_list.push("script-src 'none';");
}
if options.no_images {
// Note: "data:" is required for transparent pixel images to work
string_list.push("img-src data:;");
}
string_list.join(" ")
}
pub fn create_metadata_tag(url: &Url) -> String {
let timestamp = Utc::now().to_rfc3339_opts(SecondsFormat::Secs, true);
let mut clean_url: Url = clean_url(url.clone());
// Prevent credentials from getting into metadata
if clean_url.scheme() == "http" || clean_url.scheme() == "https" {
// Only HTTP(S) URLs can contain credentials
clean_url.set_username("").unwrap();
clean_url.set_password(None).unwrap();
}
format!(
"<!-- Saved from {} at {} using {} v{} -->",
if clean_url.scheme() == "http" || clean_url.scheme() == "https" {
clean_url.as_str()
} else {
"local source"
},
timestamp,
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION"),
)
}
pub fn embed_srcset(
cache: &mut Option<Cache>,
client: &Client,
document_url: &Url,
srcset: &str,
options: &Options,
) -> String {
let mut array: Vec<SrcSetItem> = vec![];
// Parse srcset attribute according to the specs
// https://html.spec.whatwg.org/multipage/images.html#srcset-attribute
let mut offset = 0;
let size = srcset.chars().count();
while offset < size {
let mut has_descriptor = true;
// Zero or more whitespaces + skip leading comma
let url_start = offset
+ srcset[offset..]
.chars()
.take_while(|&c| WHITESPACES.contains(&c) || c == ',')
.count();
if url_start >= size {
break;
}
// A valid non-empty URL that does not start or end with comma
let mut url_end = url_start
+ srcset[url_start..]
.chars()
.take_while(|&c| !WHITESPACES.contains(&c))
.count();
while (url_end - 1) > url_start && srcset.chars().nth(url_end - 1).unwrap() == ',' {
has_descriptor = false;
url_end -= 1;
}
offset = url_end;
// If the URL wasn't terminated by comma there may also be a descriptor
if has_descriptor {
offset += srcset[url_end..].chars().take_while(|&c| c != ',').count();
}
// Collect SrcSetItem
if url_end > url_start {
let path = &srcset[url_start..url_end];
let descriptor = &srcset[url_end..offset].trim();
let srcset_real_item = SrcSetItem { path, descriptor };
array.push(srcset_real_item);
}
}
let mut result: String = "".to_string();
let mut i: usize = array.len();
for part in array {
if options.no_images {
result.push_str(EMPTY_IMAGE_DATA_URL);
} else {
let image_full_url: Url = resolve_url(document_url, part.path);
match retrieve_asset(cache, client, document_url, &image_full_url, options) {
Ok((image_data, image_final_url, image_media_type, image_charset)) => {
let mut image_data_url = create_data_url(
&image_media_type,
&image_charset,
&image_data,
&image_final_url,
);
// Append retrieved asset as a data URL
image_data_url.set_fragment(image_full_url.fragment());
result.push_str(image_data_url.as_ref());
}
Err(_) => {
// Keep remote reference if unable to retrieve the asset
if image_full_url.scheme() == "http" || image_full_url.scheme() == "https" {
result.push_str(image_full_url.as_ref());
} else {
// Avoid breaking the structure in case if not an HTTP(S) URL
result.push_str(EMPTY_IMAGE_DATA_URL);
}
}
}
}
if !part.descriptor.is_empty() {
result.push(' ');
result.push_str(part.descriptor);
}
if i > 1 {
result.push_str(", ");
}
i -= 1;
}
result
}
pub fn find_base_node(node: &Handle) -> Option<Handle> {
match node.data {
NodeData::Document => {
// Dig deeper
for child in node.children.borrow().iter() {
if let Some(base_node) = find_base_node(child) {
return Some(base_node);
}
}
}
NodeData::Element { ref name, .. } => {
if name.local.as_ref() == "head" {
return get_child_node_by_name(node, "base");
}
// Dig deeper
for child in node.children.borrow().iter() {
if let Some(base_node) = find_base_node(child) {
return Some(base_node);
}
}
}
_ => {}
}
None
}
pub fn find_meta_charset_or_content_type_node(node: &Handle) -> Option<Handle> {
match node.data {
NodeData::Document => {
// Dig deeper
for child in node.children.borrow().iter() {
if let Some(meta_charset_node) = find_meta_charset_or_content_type_node(child) {
return Some(meta_charset_node);
}
}
}
NodeData::Element { ref name, .. } => {
if name.local.as_ref() == "head" {
if let Some(meta_node) = get_child_node_by_name(node, "meta") {
if get_node_attr(&meta_node, "charset").is_some() {
return Some(meta_node);
} else if let Some(meta_node_http_equiv_attr_value) =
get_node_attr(&meta_node, "http-equiv")
{
if meta_node_http_equiv_attr_value.eq_ignore_ascii_case("content-type") {
return Some(meta_node);
}
}
}
}
// Dig deeper
for child in node.children.borrow().iter() {
if let Some(meta_charset_node) = find_meta_charset_or_content_type_node(child) {
return Some(meta_charset_node);
}
}
}
_ => {}
}
None
}
pub fn get_base_url(handle: &Handle) -> Option<String> {
if let Some(base_node) = find_base_node(handle) {
get_node_attr(&base_node, "href")
} else {
None
}
}
pub fn get_charset(node: &Handle) -> Option<String> {
if let Some(meta_charset_node) = find_meta_charset_or_content_type_node(node) {
if let Some(meta_charset_node_attr_value) = get_node_attr(&meta_charset_node, "charset") {
// Processing <meta charset="..." />
return Some(meta_charset_node_attr_value);
} else if let Some(meta_content_type_node_attr_value) =
get_node_attr(&meta_charset_node, "content")
{
// Processing <meta http-equiv="content-type" content="text/html; charset=..." />
let (_media_type, charset, _is_base64) =
parse_content_type(&meta_content_type_node_attr_value);
return Some(charset);
}
}
None
}
pub fn get_child_node_by_name(parent: &Handle, node_name: &str) -> Option<Handle> {
let children = parent.children.borrow();
let matching_children = children.iter().find(|child| match child.data {
NodeData::Element { ref name, .. } => &*name.local == node_name,
_ => false,
});
matching_children.cloned()
}
pub fn get_node_attr(node: &Handle, attr_name: &str) -> Option<String> {
match &node.data {
NodeData::Element { attrs, .. } => {
for attr in attrs.borrow().iter() {
if &*attr.name.local == attr_name {
return Some(attr.value.to_string());
}
}
None
}
_ => None,
}
}
pub fn get_node_name(node: &Handle) -> Option<&'_ str> {
match &node.data {
NodeData::Element { name, .. } => Some(name.local.as_ref()),
_ => None,
}
}
pub fn get_parent_node(child: &Handle) -> Handle {
let parent = child.parent.take().clone();
parent.and_then(|node| node.upgrade()).unwrap()
}
pub fn has_favicon(handle: &Handle) -> bool {
let mut found_favicon: bool = false;
match handle.data {
NodeData::Document => {
// Dig deeper
for child in handle.children.borrow().iter() {
if has_favicon(child) {
found_favicon = true;
break;
}
}
}
NodeData::Element { ref name, .. } => {
if name.local.as_ref() == "link" {
if let Some(attr_value) = get_node_attr(handle, "rel") {
if is_favicon(attr_value.trim()) {
found_favicon = true;
}
}
}
if !found_favicon {
// Dig deeper
for child in handle.children.borrow().iter() {
if has_favicon(child) {
found_favicon = true;
break;
}
}
}
}
_ => {}
}
found_favicon
}
pub fn html_to_dom(data: &Vec<u8>, document_encoding: String) -> RcDom {
let s: String;
if let Some(encoding) = Encoding::for_label(document_encoding.as_bytes()) {
let (string, _, _) = encoding.decode(data);
s = string.to_string();
} else {
s = String::from_utf8_lossy(data).to_string();
}
parse_document(RcDom::default(), Default::default())
.from_utf8()
.read_from(&mut s.as_bytes())
.unwrap()
}
pub fn is_favicon(attr_value: &str) -> bool {
FAVICON_VALUES.contains(&attr_value.to_lowercase().as_str())
}
pub fn parse_link_type(link_attr_rel_value: &str) -> Vec<LinkType> {
let mut types: Vec<LinkType> = vec![];
for link_attr_rel_type in link_attr_rel_value.split_whitespace() {
if link_attr_rel_type.eq_ignore_ascii_case("alternate") {
types.push(LinkType::Alternate);
} else if link_attr_rel_type.eq_ignore_ascii_case("dns-prefetch") {
types.push(LinkType::DnsPrefetch);
} else if link_attr_rel_type.eq_ignore_ascii_case("preload") {
types.push(LinkType::Preload);
} else if link_attr_rel_type.eq_ignore_ascii_case("stylesheet") {
types.push(LinkType::Stylesheet);
} else if is_favicon(link_attr_rel_type) {
types.push(LinkType::Favicon);
} else if link_attr_rel_type.eq_ignore_ascii_case("apple-touch-icon") {
types.push(LinkType::AppleTouchIcon);
}
}
types
}
pub fn set_base_url(document: &Handle, desired_base_href: String) -> RcDom {
let mut buf: Vec<u8> = Vec::new();
serialize(
&mut buf,
&SerializableHandle::from(document.clone()),
SerializeOpts::default(),
)
.expect("unable to serialize DOM into buffer");
let mut dom = html_to_dom(&buf, "utf-8".to_string());
let doc = dom.get_document();
if let Some(html_node) = get_child_node_by_name(&doc, "html") {
if let Some(head_node) = get_child_node_by_name(&html_node, "head") {
// Check if BASE node already exists in the DOM tree
if let Some(base_node) = get_child_node_by_name(&head_node, "base") {
set_node_attr(&base_node, "href", Some(desired_base_href));
} else {
let base_node = dom.create_element(
QualName::new(None, ns!(), local_name!("base")),
vec![Attribute {
name: QualName::new(None, ns!(), local_name!("href")),
value: format_tendril!("{}", desired_base_href),
}],
Default::default(),
);
// Insert newly created BASE node into HEAD
head_node.children.borrow_mut().push(base_node.clone());
}
}
}
dom
}
pub fn set_charset(mut dom: RcDom, desired_charset: String) -> RcDom {
if let Some(meta_charset_node) = find_meta_charset_or_content_type_node(&dom.document) {
if get_node_attr(&meta_charset_node, "charset").is_some() {
set_node_attr(&meta_charset_node, "charset", Some(desired_charset));
} else if get_node_attr(&meta_charset_node, "content").is_some() {
set_node_attr(
&meta_charset_node,
"content",
Some(format!("text/html;charset={}", desired_charset)),
);
}
} else {
let meta_charset_node = dom.create_element(
QualName::new(None, ns!(), local_name!("meta")),
vec![Attribute {
name: QualName::new(None, ns!(), local_name!("charset")),
value: format_tendril!("{}", desired_charset),
}],
Default::default(),
);
// Insert newly created META charset node into HEAD
if let Some(html_node) = get_child_node_by_name(&dom.document, "html") {
if let Some(head_node) = get_child_node_by_name(&html_node, "head") {
head_node
.children
.borrow_mut()
.push(meta_charset_node.clone());
}
}
}
dom
}
pub fn set_node_attr(node: &Handle, attr_name: &str, attr_value: Option<String>) {
if let NodeData::Element { attrs, .. } = &node.data {
let attrs_mut = &mut attrs.borrow_mut();
let mut i = 0;
let mut found_existing_attr: bool = false;
while i < attrs_mut.len() {
if &attrs_mut[i].name.local == attr_name {
found_existing_attr = true;
if let Some(attr_value) = attr_value.clone() {
let _ = &attrs_mut[i].value.clear();
let _ = &attrs_mut[i].value.push_slice(attr_value.as_str());
} else {
// Remove attr completely if attr_value is not defined
attrs_mut.remove(i);
continue;
}
}
i += 1;
}
if !found_existing_attr {
// Add new attribute (since originally the target node didn't have it)
if let Some(attr_value) = attr_value.clone() {
let name = LocalName::from(attr_name);
attrs_mut.push(Attribute {
name: QualName::new(None, ns!(), name),
value: format_tendril!("{}", attr_value),
});
}
}
};
}
pub fn serialize_document(mut dom: RcDom, document_encoding: String, options: &Options) -> Vec<u8> {
let mut buf: Vec<u8> = Vec::new();
let document = dom.get_document();
if options.isolate
|| options.no_css
|| options.no_fonts
|| options.no_frames
|| options.no_js
|| options.no_images
{
// Take care of CSP
if let Some(html) = get_child_node_by_name(&document, "html") {
if let Some(head) = get_child_node_by_name(&html, "head") {
let meta = dom.create_element(
QualName::new(None, ns!(), local_name!("meta")),
vec![
Attribute {
name: QualName::new(None, ns!(), local_name!("http-equiv")),
value: format_tendril!("Content-Security-Policy"),
},
Attribute {
name: QualName::new(None, ns!(), local_name!("content")),
value: format_tendril!("{}", compose_csp(options)),
},
],
Default::default(),
);
// The CSP meta-tag has to be prepended, never appended,
// since there already may be one defined in the original document,
// and browsers don't allow re-defining them (for obvious reasons)
head.children.borrow_mut().reverse();
head.children.borrow_mut().push(meta.clone());
head.children.borrow_mut().reverse();
}
}
}
serialize(
&mut buf,
&SerializableHandle::from(document.clone()),
SerializeOpts::default(),
)
.expect("Unable to serialize DOM into buffer");
// Unwrap NOSCRIPT elements
if options.unwrap_noscript {
let s: &str = &String::from_utf8_lossy(&buf);
let noscript_re = Regex::new(r"<(?P<c>/?noscript[^>]*)>").unwrap();
buf = noscript_re.replace_all(s, "<!--$c-->").as_bytes().to_vec();
}
if !document_encoding.is_empty() {
if let Some(encoding) = Encoding::for_label(document_encoding.as_bytes()) {
let s: &str = &String::from_utf8_lossy(&buf);
let (data, _, _) = encoding.encode(s);
buf = data.to_vec();
}
}
buf
}
pub fn retrieve_and_embed_asset(
cache: &mut Option<Cache>,
client: &Client,
document_url: &Url,
node: &Handle,
attr_name: &str,
attr_value: &str,
options: &Options,
) {
let resolved_url: Url = resolve_url(document_url, attr_value);
match retrieve_asset(cache, client, &document_url.clone(), &resolved_url, options) {
Ok((data, final_url, mut media_type, charset)) => {
let node_name: &str = get_node_name(node).unwrap();
// Check integrity if it's a LINK or SCRIPT element
let mut ok_to_include: bool = true;
if node_name == "link" || node_name == "script" {
// Check integrity
if let Some(node_integrity_attr_value) = get_node_attr(node, "integrity") {
if !node_integrity_attr_value.is_empty() {
ok_to_include = check_integrity(&data, &node_integrity_attr_value);
}
// Wipe the integrity attribute
set_node_attr(node, "integrity", None);
}
}
if ok_to_include {
let s: String;
if let Some(encoding) = Encoding::for_label(charset.as_bytes()) {
let (string, _, _) = encoding.decode(&data);
s = string.to_string();
} else {
s = String::from_utf8_lossy(&data).to_string();
}
if node_name == "link"
&& parse_link_type(&get_node_attr(node, "rel").unwrap_or(String::from("")))
.contains(&LinkType::Stylesheet)
{
// Stylesheet LINK elements require special treatment
let css: String = embed_css(cache, client, &final_url, &s, options);
// Create and embed data URL
let css_data_url =
create_data_url(&media_type, &charset, css.as_bytes(), &final_url);
set_node_attr(node, attr_name, Some(css_data_url.to_string()));
} else if node_name == "frame" || node_name == "iframe" {
// (I)FRAMEs are also quite different from conventional resources
let frame_dom = html_to_dom(&data, charset.clone());
walk_and_embed_assets(cache, client, &final_url, &frame_dom.document, options);
let mut frame_data: Vec<u8> = Vec::new();
serialize(
&mut frame_data,
&SerializableHandle::from(frame_dom.document.clone()),
SerializeOpts::default(),
)
.unwrap();
// Create and embed data URL
let mut frame_data_url =
create_data_url(&media_type, &charset, &frame_data, &final_url);
frame_data_url.set_fragment(resolved_url.fragment());
set_node_attr(node, attr_name, Some(frame_data_url.to_string()));
} else {
// Every other type of element gets processed here
// Parse media type for SCRIPT elements
if node_name == "script" && get_node_attr(node, "src").is_some() {
if let Some(script_node_type_attr_value) = get_node_attr(node, "type") {
media_type = script_node_type_attr_value.to_string();
} else {
// Fallback to default one if it's not specified
media_type = "application/javascript".to_string();
}
}
// Create and embed data URL
let mut data_url = create_data_url(&media_type, &charset, &data, &final_url);
data_url.set_fragment(resolved_url.fragment());
set_node_attr(node, attr_name, Some(data_url.to_string()));
}
}
}
Err(_) => {
if resolved_url.scheme() == "http" || resolved_url.scheme() == "https" {
// Keep remote references if unable to retrieve the asset
set_node_attr(node, attr_name, Some(resolved_url.to_string()));
} else {
// Remove local references if they can't be successfully embedded as data URLs
set_node_attr(node, attr_name, None);
}
}
}
}
pub fn walk_and_embed_assets(
cache: &mut Option<Cache>,
client: &Client,
document_url: &Url,
node: &Handle,
options: &Options,
) {
match node.data {
NodeData::Document => {
// Dig deeper
for child in node.children.borrow().iter() {
walk_and_embed_assets(cache, client, document_url, child, options);
}
}
NodeData::Element {
ref name,
ref attrs,
..
} => {
match name.local.as_ref() {
"meta" => {
if let Some(meta_attr_http_equiv_value) = get_node_attr(node, "http-equiv") {
let meta_attr_http_equiv_value: &str = &meta_attr_http_equiv_value;
if meta_attr_http_equiv_value.eq_ignore_ascii_case("refresh")
|| meta_attr_http_equiv_value.eq_ignore_ascii_case("location")
{
// Remove http-equiv attributes from META nodes if they're able to control the page
set_node_attr(node, "http-equiv", None);
}
}
}
"link" => {
let link_node_types: Vec<LinkType> =
parse_link_type(&get_node_attr(node, "rel").unwrap_or(String::from("")));
if link_node_types.contains(&LinkType::Favicon)
|| link_node_types.contains(&LinkType::AppleTouchIcon)
{
// Find and resolve LINK's href attribute
if let Some(link_attr_href_value) = get_node_attr(node, "href") {
if !options.no_images && !link_attr_href_value.is_empty() {
retrieve_and_embed_asset(
cache,
client,
document_url,
node,
"href",
&link_attr_href_value,
options,
);
} else {
set_node_attr(node, "href", None);
}
}
} else if link_node_types.contains(&LinkType::Stylesheet) {
// Resolve LINK's href attribute
if let Some(link_attr_href_value) = get_node_attr(node, "href") {
if options.no_css {
set_node_attr(node, "href", None);
// Wipe integrity attribute
set_node_attr(node, "integrity", None);
} else if !link_attr_href_value.is_empty() {
retrieve_and_embed_asset(
cache,
client,
document_url,
node,
"href",
&link_attr_href_value,
options,
);
}
}
} else if link_node_types.contains(&LinkType::Preload)
|| link_node_types.contains(&LinkType::DnsPrefetch)
{
// Since all resources are embedded as data URLs, preloading and prefetching are not necessary
set_node_attr(node, "rel", None);
} else {
// Make sure that all other LINKs' href attributes are full URLs
if let Some(link_attr_href_value) = get_node_attr(node, "href") {
let href_full_url: Url =
resolve_url(document_url, &link_attr_href_value);
set_node_attr(node, "href", Some(href_full_url.to_string()));
}
}
}
"base" => {
if document_url.scheme() == "http" || document_url.scheme() == "https" {
// Ensure the BASE node doesn't have a relative URL
if let Some(base_attr_href_value) = get_node_attr(node, "href") {
let href_full_url: Url =
resolve_url(document_url, &base_attr_href_value);
set_node_attr(node, "href", Some(href_full_url.to_string()));
}
}
}
"body" => {
// Read and remember background attribute value of this BODY node
if let Some(body_attr_background_value) = get_node_attr(node, "background") {
// Remove background BODY node attribute by default
set_node_attr(node, "background", None);
if !options.no_images && !body_attr_background_value.is_empty() {
retrieve_and_embed_asset(
cache,
client,
document_url,
node,
"background",
&body_attr_background_value,
options,
);
}
}
}
"img" => {
// Find src and data-src attribute(s)
let img_attr_src_value: Option<String> = get_node_attr(node, "src");
let img_attr_data_src_value: Option<String> = get_node_attr(node, "data-src");
if options.no_images {
// Put empty images into src and data-src attributes
if img_attr_src_value.is_some() {
set_node_attr(node, "src", Some(EMPTY_IMAGE_DATA_URL.to_string()));
}
if img_attr_data_src_value.is_some() {
set_node_attr(node, "data-src", Some(EMPTY_IMAGE_DATA_URL.to_string()));
}
} else if img_attr_src_value.clone().unwrap_or_default().is_empty()
&& img_attr_data_src_value
.clone()
.unwrap_or_default()
.is_empty()
{
// Add empty src attribute
set_node_attr(node, "src", Some("".to_string()));
} else {
// Add data URL src attribute
let img_full_url: String = if !img_attr_data_src_value
.clone()
.unwrap_or_default()
.is_empty()
{
img_attr_data_src_value.unwrap_or_default()
} else {
img_attr_src_value.unwrap_or_default()
};
retrieve_and_embed_asset(
cache,
client,
document_url,
node,
"src",
&img_full_url,
options,
);
}
// Resolve srcset attribute
if let Some(img_srcset) = get_node_attr(node, "srcset") {
if !img_srcset.is_empty() {
let resolved_srcset: String =
embed_srcset(cache, client, document_url, &img_srcset, options);
set_node_attr(node, "srcset", Some(resolved_srcset));
}
}
}
"svg" => {
if options.no_images {
node.children.borrow_mut().clear();
}
}
"input" => {
if let Some(input_attr_type_value) = get_node_attr(node, "type") {
if input_attr_type_value.eq_ignore_ascii_case("image") {
if let Some(input_attr_src_value) = get_node_attr(node, "src") {
if options.no_images || input_attr_src_value.is_empty() {
let value = if input_attr_src_value.is_empty() {
""
} else {
EMPTY_IMAGE_DATA_URL
};
set_node_attr(node, "src", Some(value.to_string()));
} else {
retrieve_and_embed_asset(
cache,
client,
document_url,
node,
"src",
&input_attr_src_value,
options,
);
}
}
}
}
}
"image" | "use" => {
if let Some(image_attr_href_value) = get_node_attr(node, "href") {
if options.no_images {
set_node_attr(node, "href", None);
} else {
retrieve_and_embed_asset(
cache,
client,
document_url,
node,
"href",
&image_attr_href_value,
options,
);
}
}
if let Some(image_attr_xlink_href_value) = get_node_attr(node, "xlink:href") {
if options.no_images {
set_node_attr(node, "xlink:href", None);
} else {
retrieve_and_embed_asset(
cache,
client,
document_url,
node,
"xlink:href",
&image_attr_xlink_href_value,
options,
);
}
}
}
"source" => {
let parent_node = get_parent_node(node);
let parent_node_name: &str = get_node_name(&parent_node).unwrap_or_default();
if let Some(source_attr_src_value) = get_node_attr(node, "src") {
if parent_node_name == "audio" {
if options.no_audio {
set_node_attr(node, "src", None);
} else {
retrieve_and_embed_asset(
cache,
client,
document_url,
node,
"src",
&source_attr_src_value,
options,
);
}
} else if parent_node_name == "video" {
if options.no_video {
set_node_attr(node, "src", None);
} else {
retrieve_and_embed_asset(
cache,
client,
document_url,
node,
"src",
&source_attr_src_value,
options,
);
}
}
}
if let Some(source_attr_srcset_value) = get_node_attr(node, "srcset") {
if parent_node_name == "picture" && !source_attr_srcset_value.is_empty() {
if options.no_images {
set_node_attr(
node,
"srcset",
Some(EMPTY_IMAGE_DATA_URL.to_string()),
);
} else {