2020-03-23 02:08:41 +00:00
|
|
|
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
|
|
|
|
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
|
|
|
|
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
|
|
|
|
// ██╔═══╝ ██╔══██║╚════██║╚════██║██║██║╚██╗██║██║ ██║
|
|
|
|
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
|
|
|
|
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
|
2019-09-29 21:15:49 +00:00
|
|
|
|
2020-05-23 07:49:04 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod passing {
|
|
|
|
use html5ever::serialize::{serialize, SerializeOpts};
|
2024-01-14 16:30:36 +00:00
|
|
|
use markup5ever_rcdom::SerializableHandle;
|
2020-05-23 07:49:04 +00:00
|
|
|
use reqwest::blocking::Client;
|
|
|
|
use std::collections::HashMap;
|
2021-03-11 22:44:02 +00:00
|
|
|
use url::Url;
|
2020-05-23 07:49:04 +00:00
|
|
|
|
2021-10-17 07:16:37 +00:00
|
|
|
use monolith::html;
|
|
|
|
use monolith::opts::Options;
|
|
|
|
use monolith::url::EMPTY_IMAGE_DATA_URL;
|
2020-06-28 05:36:41 +00:00
|
|
|
|
2020-05-23 07:49:04 +00:00
|
|
|
#[test]
|
|
|
|
fn basic() {
|
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
|
2021-03-11 22:44:02 +00:00
|
|
|
let html: &str = "<div><P></P></div>";
|
2021-10-17 07:16:37 +00:00
|
|
|
let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string());
|
2021-03-11 22:44:02 +00:00
|
|
|
let url: Url = Url::parse("http://localhost").unwrap();
|
2020-05-23 07:49:04 +00:00
|
|
|
|
2020-06-28 05:36:41 +00:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.silent = true;
|
2020-05-23 07:49:04 +00:00
|
|
|
|
|
|
|
let client = Client::new();
|
|
|
|
|
2020-06-28 20:11:15 +00:00
|
|
|
html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0);
|
2020-05-23 07:49:04 +00:00
|
|
|
|
|
|
|
let mut buf: Vec<u8> = Vec::new();
|
2024-01-14 16:30:36 +00:00
|
|
|
serialize(
|
|
|
|
&mut buf,
|
|
|
|
&SerializableHandle::from(dom.document.clone()),
|
|
|
|
SerializeOpts::default(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-05-23 07:49:04 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
buf.iter().map(|&c| c as char).collect::<String>(),
|
|
|
|
"<html><head></head><body><div><p></p></div></body></html>"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ensure_no_recursive_iframe() {
|
|
|
|
let html = "<div><P></P><iframe src=\"\"></iframe></div>";
|
2021-10-17 07:16:37 +00:00
|
|
|
let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string());
|
2021-03-11 22:44:02 +00:00
|
|
|
let url: Url = Url::parse("http://localhost").unwrap();
|
2020-05-23 07:49:04 +00:00
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
|
2020-06-28 05:36:41 +00:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.silent = true;
|
2020-05-23 07:49:04 +00:00
|
|
|
|
|
|
|
let client = Client::new();
|
|
|
|
|
2020-06-28 20:11:15 +00:00
|
|
|
html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0);
|
2020-05-23 07:49:04 +00:00
|
|
|
|
|
|
|
let mut buf: Vec<u8> = Vec::new();
|
2024-01-14 16:30:36 +00:00
|
|
|
serialize(
|
|
|
|
&mut buf,
|
|
|
|
&SerializableHandle::from(dom.document.clone()),
|
|
|
|
SerializeOpts::default(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-05-23 07:49:04 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
buf.iter().map(|&c| c as char).collect::<String>(),
|
|
|
|
"<html><head></head><body><div><p></p><iframe src=\"\"></iframe></div></body></html>"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ensure_no_recursive_frame() {
|
|
|
|
let html = "<frameset><frame src=\"\"></frameset>";
|
2021-10-17 07:16:37 +00:00
|
|
|
let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string());
|
2021-03-11 22:44:02 +00:00
|
|
|
let url: Url = Url::parse("http://localhost").unwrap();
|
2020-05-23 07:49:04 +00:00
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
|
2020-06-28 05:36:41 +00:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.silent = true;
|
2020-05-23 07:49:04 +00:00
|
|
|
|
|
|
|
let client = Client::new();
|
|
|
|
|
2020-06-28 20:11:15 +00:00
|
|
|
html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0);
|
2020-05-23 07:49:04 +00:00
|
|
|
|
|
|
|
let mut buf: Vec<u8> = Vec::new();
|
2024-01-14 16:30:36 +00:00
|
|
|
serialize(
|
|
|
|
&mut buf,
|
|
|
|
&SerializableHandle::from(dom.document.clone()),
|
|
|
|
SerializeOpts::default(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-05-23 07:49:04 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
buf.iter().map(|&c| c as char).collect::<String>(),
|
|
|
|
"<html><head></head><frameset><frame src=\"\"></frameset></html>"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_css() {
|
2021-06-02 13:41:41 +00:00
|
|
|
let html = "\
|
|
|
|
<link rel=\"stylesheet\" href=\"main.css\">\
|
|
|
|
<link rel=\"alternate stylesheet\" href=\"main.css\">\
|
|
|
|
<style>html{background-color: #000;}</style>\
|
|
|
|
<div style=\"display: none;\"></div>\
|
|
|
|
";
|
2021-10-17 07:16:37 +00:00
|
|
|
let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string());
|
2021-03-11 22:44:02 +00:00
|
|
|
let url: Url = Url::parse("http://localhost").unwrap();
|
2020-05-23 07:49:04 +00:00
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
|
2020-06-28 05:36:41 +00:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.no_css = true;
|
|
|
|
options.silent = true;
|
|
|
|
|
2020-05-23 07:49:04 +00:00
|
|
|
let client = Client::new();
|
|
|
|
|
2020-06-28 20:11:15 +00:00
|
|
|
html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0);
|
2020-05-23 07:49:04 +00:00
|
|
|
|
|
|
|
let mut buf: Vec<u8> = Vec::new();
|
2024-01-14 16:30:36 +00:00
|
|
|
serialize(
|
|
|
|
&mut buf,
|
|
|
|
&SerializableHandle::from(dom.document.clone()),
|
|
|
|
SerializeOpts::default(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-05-23 07:49:04 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
buf.iter().map(|&c| c as char).collect::<String>(),
|
2021-06-02 13:41:41 +00:00
|
|
|
"\
|
|
|
|
<html>\
|
|
|
|
<head>\
|
|
|
|
<link rel=\"stylesheet\">\
|
|
|
|
<link rel=\"alternate stylesheet\">\
|
|
|
|
<style></style>\
|
|
|
|
</head>\
|
|
|
|
<body>\
|
|
|
|
<div></div>\
|
|
|
|
</body>\
|
|
|
|
</html>\
|
|
|
|
"
|
2020-05-23 07:49:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_images() {
|
|
|
|
let html = "<link rel=\"icon\" href=\"favicon.ico\">\
|
|
|
|
<div><img src=\"http://localhost/assets/mono_lisa.png\" /></div>";
|
2021-10-17 07:16:37 +00:00
|
|
|
let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string());
|
2021-03-11 22:44:02 +00:00
|
|
|
let url: Url = Url::parse("http://localhost").unwrap();
|
2020-05-23 07:49:04 +00:00
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
|
2020-06-28 05:36:41 +00:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.no_images = true;
|
|
|
|
options.silent = true;
|
2020-05-23 07:49:04 +00:00
|
|
|
|
|
|
|
let client = Client::new();
|
|
|
|
|
2020-06-28 20:11:15 +00:00
|
|
|
html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0);
|
2020-05-23 07:49:04 +00:00
|
|
|
|
|
|
|
let mut buf: Vec<u8> = Vec::new();
|
2024-01-14 16:30:36 +00:00
|
|
|
serialize(
|
|
|
|
&mut buf,
|
|
|
|
&SerializableHandle::from(dom.document.clone()),
|
|
|
|
SerializeOpts::default(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-05-23 07:49:04 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
buf.iter().map(|&c| c as char).collect::<String>(),
|
|
|
|
format!(
|
|
|
|
"<html>\
|
2020-06-26 03:53:20 +00:00
|
|
|
<head>\
|
|
|
|
<link rel=\"icon\">\
|
|
|
|
</head>\
|
|
|
|
<body>\
|
|
|
|
<div>\
|
|
|
|
<img src=\"{empty_image}\">\
|
|
|
|
</div>\
|
|
|
|
</body>\
|
|
|
|
</html>",
|
2021-10-17 07:16:37 +00:00
|
|
|
empty_image = EMPTY_IMAGE_DATA_URL
|
2020-05-23 07:49:04 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_body_background_images() {
|
|
|
|
let html =
|
|
|
|
"<body background=\"no/such/image.png\" background=\"no/such/image2.png\"></body>";
|
2021-10-17 07:16:37 +00:00
|
|
|
let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string());
|
2021-03-11 22:44:02 +00:00
|
|
|
let url: Url = Url::parse("http://localhost").unwrap();
|
2020-05-23 07:49:04 +00:00
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
|
2020-06-28 05:36:41 +00:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.no_images = true;
|
|
|
|
options.silent = true;
|
2020-05-23 07:49:04 +00:00
|
|
|
|
|
|
|
let client = Client::new();
|
|
|
|
|
2020-06-28 20:11:15 +00:00
|
|
|
html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0);
|
2020-05-23 07:49:04 +00:00
|
|
|
|
|
|
|
let mut buf: Vec<u8> = Vec::new();
|
2024-01-14 16:30:36 +00:00
|
|
|
serialize(
|
|
|
|
&mut buf,
|
|
|
|
&SerializableHandle::from(dom.document.clone()),
|
|
|
|
SerializeOpts::default(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-05-23 07:49:04 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
buf.iter().map(|&c| c as char).collect::<String>(),
|
|
|
|
"<html><head></head><body></body></html>"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_frames() {
|
|
|
|
let html = "<frameset><frame src=\"http://trackbook.com\"></frameset>";
|
2021-10-17 07:16:37 +00:00
|
|
|
let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string());
|
2021-03-11 22:44:02 +00:00
|
|
|
let url: Url = Url::parse("http://localhost").unwrap();
|
2020-05-23 07:49:04 +00:00
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
|
2020-06-28 05:36:41 +00:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.no_frames = true;
|
|
|
|
options.silent = true;
|
|
|
|
|
2020-05-23 07:49:04 +00:00
|
|
|
let client = Client::new();
|
|
|
|
|
2020-06-28 20:11:15 +00:00
|
|
|
html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0);
|
2020-05-23 07:49:04 +00:00
|
|
|
|
|
|
|
let mut buf: Vec<u8> = Vec::new();
|
2024-01-14 16:30:36 +00:00
|
|
|
serialize(
|
|
|
|
&mut buf,
|
|
|
|
&SerializableHandle::from(dom.document.clone()),
|
|
|
|
SerializeOpts::default(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-05-23 07:49:04 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
buf.iter().map(|&c| c as char).collect::<String>(),
|
2021-06-02 13:41:41 +00:00
|
|
|
"\
|
|
|
|
<html>\
|
|
|
|
<head>\
|
|
|
|
</head>\
|
|
|
|
<frameset>\
|
|
|
|
<frame src=\"\">\
|
|
|
|
</frameset>\
|
|
|
|
</html>\
|
|
|
|
"
|
2020-05-23 07:49:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_iframes() {
|
|
|
|
let html = "<iframe src=\"http://trackbook.com\"></iframe>";
|
2021-10-17 07:16:37 +00:00
|
|
|
let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string());
|
2021-03-11 22:44:02 +00:00
|
|
|
let url: Url = Url::parse("http://localhost").unwrap();
|
2020-05-23 07:49:04 +00:00
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
|
2020-06-28 05:36:41 +00:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.no_frames = true;
|
|
|
|
options.silent = true;
|
|
|
|
|
2020-05-23 07:49:04 +00:00
|
|
|
let client = Client::new();
|
|
|
|
|
2020-06-28 20:11:15 +00:00
|
|
|
html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0);
|
2020-05-23 07:49:04 +00:00
|
|
|
|
|
|
|
let mut buf: Vec<u8> = Vec::new();
|
2024-01-14 16:30:36 +00:00
|
|
|
serialize(
|
|
|
|
&mut buf,
|
|
|
|
&SerializableHandle::from(dom.document.clone()),
|
|
|
|
SerializeOpts::default(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-05-23 07:49:04 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
buf.iter().map(|&c| c as char).collect::<String>(),
|
2021-06-02 13:41:41 +00:00
|
|
|
"\
|
|
|
|
<html>\
|
|
|
|
<head></head>\
|
|
|
|
<body>\
|
|
|
|
<iframe src=\"\"></iframe>\
|
|
|
|
</body>\
|
|
|
|
</html>\
|
|
|
|
"
|
2020-05-23 07:49:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_js() {
|
2021-06-02 13:41:41 +00:00
|
|
|
let html = "\
|
|
|
|
<div onClick=\"void(0)\">\
|
|
|
|
<script src=\"http://localhost/assets/some.js\"></script>\
|
|
|
|
<script>alert(1)</script>\
|
|
|
|
</div>\
|
|
|
|
";
|
2021-10-17 07:16:37 +00:00
|
|
|
let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string());
|
2021-03-11 22:44:02 +00:00
|
|
|
let url: Url = Url::parse("http://localhost").unwrap();
|
2020-05-23 07:49:04 +00:00
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
|
2020-06-28 05:36:41 +00:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.no_js = true;
|
|
|
|
options.silent = true;
|
2020-05-23 07:49:04 +00:00
|
|
|
|
|
|
|
let client = Client::new();
|
|
|
|
|
2020-06-28 20:11:15 +00:00
|
|
|
html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0);
|
2020-05-23 07:49:04 +00:00
|
|
|
|
|
|
|
let mut buf: Vec<u8> = Vec::new();
|
2024-01-14 16:30:36 +00:00
|
|
|
serialize(
|
|
|
|
&mut buf,
|
|
|
|
&SerializableHandle::from(dom.document.clone()),
|
|
|
|
SerializeOpts::default(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-05-23 07:49:04 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
buf.iter().map(|&c| c as char).collect::<String>(),
|
2021-06-02 13:41:41 +00:00
|
|
|
"\
|
|
|
|
<html>\
|
|
|
|
<head></head>\
|
|
|
|
<body>\
|
|
|
|
<div>\
|
|
|
|
<script></script>\
|
|
|
|
<script></script>\
|
|
|
|
</div>\
|
|
|
|
</body>\
|
|
|
|
</html>\
|
|
|
|
"
|
2020-05-23 07:49:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-02 13:41:41 +00:00
|
|
|
#[test]
|
2021-06-08 22:25:19 +00:00
|
|
|
fn keeps_integrity_for_unfamiliar_links() {
|
2021-06-02 13:41:41 +00:00
|
|
|
let html = "<title>Has integrity</title>\
|
|
|
|
<link integrity=\"sha384-12345\" rel=\"something\" href=\"https://some-site.com/some-file.ext\" />";
|
2021-10-17 07:16:37 +00:00
|
|
|
let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string());
|
2021-06-02 13:41:41 +00:00
|
|
|
let url: Url = Url::parse("http://localhost").unwrap();
|
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
|
|
|
|
let mut options = Options::default();
|
|
|
|
options.silent = true;
|
|
|
|
|
|
|
|
let client = Client::new();
|
|
|
|
|
|
|
|
html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0);
|
|
|
|
|
|
|
|
let mut buf: Vec<u8> = Vec::new();
|
2024-01-14 16:30:36 +00:00
|
|
|
serialize(
|
|
|
|
&mut buf,
|
|
|
|
&SerializableHandle::from(dom.document.clone()),
|
|
|
|
SerializeOpts::default(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-06-02 13:41:41 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
buf.iter().map(|&c| c as char).collect::<String>(),
|
|
|
|
"\
|
|
|
|
<html>\
|
|
|
|
<head>\
|
|
|
|
<title>Has integrity</title>\
|
|
|
|
<link integrity=\"sha384-12345\" rel=\"something\" href=\"https://some-site.com/some-file.ext\">\
|
|
|
|
</head>\
|
|
|
|
<body></body>\
|
|
|
|
</html>\
|
|
|
|
"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-06-08 22:25:19 +00:00
|
|
|
fn discards_integrity_for_known_links_nojs_nocss() {
|
2021-06-02 13:41:41 +00:00
|
|
|
let html = "\
|
|
|
|
<title>No integrity</title>\
|
|
|
|
<link integrity=\"\" rel=\"stylesheet\" href=\"data:;\"/>\
|
|
|
|
<script integrity=\"\" src=\"some.js\"></script>\
|
|
|
|
";
|
2021-10-17 07:16:37 +00:00
|
|
|
let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string());
|
2021-06-02 13:41:41 +00:00
|
|
|
let url: Url = Url::parse("http://localhost").unwrap();
|
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
|
|
|
|
let mut options = Options::default();
|
|
|
|
options.no_css = true;
|
|
|
|
options.no_js = true;
|
|
|
|
options.silent = true;
|
|
|
|
|
|
|
|
let client = Client::new();
|
|
|
|
|
|
|
|
html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0);
|
|
|
|
|
|
|
|
let mut buf: Vec<u8> = Vec::new();
|
2024-01-14 16:30:36 +00:00
|
|
|
serialize(
|
|
|
|
&mut buf,
|
|
|
|
&SerializableHandle::from(dom.document.clone()),
|
|
|
|
SerializeOpts::default(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-06-02 13:41:41 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
buf.iter().map(|&c| c as char).collect::<String>(),
|
|
|
|
"\
|
|
|
|
<html>\
|
|
|
|
<head>\
|
|
|
|
<title>No integrity</title>\
|
|
|
|
<link rel=\"stylesheet\">\
|
|
|
|
<script></script>\
|
|
|
|
</head>\
|
|
|
|
<body></body>\
|
|
|
|
</html>\
|
|
|
|
"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn discards_integrity_for_embedded_assets() {
|
|
|
|
let html = "\
|
|
|
|
<title>No integrity</title>\
|
|
|
|
<link integrity=\"sha384-123\" rel=\"something\" href=\"data:;\"/>\
|
|
|
|
<script integrity=\"sha384-456\" src=\"some.js\"></script>\
|
|
|
|
";
|
2021-10-17 07:16:37 +00:00
|
|
|
let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string());
|
2021-06-02 13:41:41 +00:00
|
|
|
let url: Url = Url::parse("http://localhost").unwrap();
|
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
|
|
|
|
let mut options = Options::default();
|
|
|
|
options.no_css = true;
|
|
|
|
options.no_js = true;
|
|
|
|
options.silent = true;
|
|
|
|
|
|
|
|
let client = Client::new();
|
|
|
|
|
|
|
|
html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0);
|
|
|
|
|
|
|
|
let mut buf: Vec<u8> = Vec::new();
|
2024-01-14 16:30:36 +00:00
|
|
|
serialize(
|
|
|
|
&mut buf,
|
|
|
|
&SerializableHandle::from(dom.document.clone()),
|
|
|
|
SerializeOpts::default(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-06-02 13:41:41 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
buf.iter().map(|&c| c as char).collect::<String>(),
|
|
|
|
"\
|
|
|
|
<html>\
|
|
|
|
<head>\
|
|
|
|
<title>No integrity</title>\
|
|
|
|
<link integrity=\"sha384-123\" rel=\"something\" href=\"data:;\">\
|
|
|
|
<script></script>\
|
|
|
|
</head>\
|
|
|
|
<body>\
|
|
|
|
</body>\
|
|
|
|
</html>\
|
|
|
|
"
|
|
|
|
);
|
|
|
|
}
|
2020-06-26 03:53:20 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn removes_unwanted_meta_tags() {
|
2021-06-02 13:41:41 +00:00
|
|
|
let html = "\
|
|
|
|
<html>\
|
|
|
|
<head>\
|
2021-06-08 22:25:19 +00:00
|
|
|
<meta http-equiv=\"Refresh\" content=\"2\"/>\
|
|
|
|
<meta http-equiv=\"Location\" content=\"https://freebsd.org\"/>\
|
2021-06-02 13:41:41 +00:00
|
|
|
</head>\
|
|
|
|
<body>\
|
|
|
|
</body>\
|
|
|
|
</html>\
|
|
|
|
";
|
2021-10-17 07:16:37 +00:00
|
|
|
let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string());
|
2021-03-11 22:44:02 +00:00
|
|
|
let url: Url = Url::parse("http://localhost").unwrap();
|
2020-06-26 03:53:20 +00:00
|
|
|
let cache = &mut HashMap::new();
|
2020-06-28 05:36:41 +00:00
|
|
|
|
|
|
|
let mut options = Options::default();
|
|
|
|
options.no_css = true;
|
|
|
|
options.no_frames = true;
|
|
|
|
options.no_js = true;
|
|
|
|
options.no_images = true;
|
|
|
|
options.silent = true;
|
|
|
|
|
2020-06-26 03:53:20 +00:00
|
|
|
let client = Client::new();
|
2020-06-28 05:36:41 +00:00
|
|
|
|
2020-06-28 20:11:15 +00:00
|
|
|
html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0);
|
2020-06-26 03:53:20 +00:00
|
|
|
|
|
|
|
let mut buf: Vec<u8> = Vec::new();
|
2024-01-14 16:30:36 +00:00
|
|
|
serialize(
|
|
|
|
&mut buf,
|
|
|
|
&SerializableHandle::from(dom.document.clone()),
|
|
|
|
SerializeOpts::default(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-06-26 03:53:20 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
buf.iter().map(|&c| c as char).collect::<String>(),
|
2021-06-02 13:41:41 +00:00
|
|
|
"\
|
|
|
|
<html>\
|
2020-06-26 03:53:20 +00:00
|
|
|
<head>\
|
2021-06-08 22:25:19 +00:00
|
|
|
<meta content=\"2\">\
|
|
|
|
<meta content=\"https://freebsd.org\">\
|
2020-06-26 03:53:20 +00:00
|
|
|
</head>\
|
2021-06-02 13:41:41 +00:00
|
|
|
<body>\
|
|
|
|
</body>\
|
2020-05-23 07:49:04 +00:00
|
|
|
</html>"
|
|
|
|
);
|
|
|
|
}
|
2021-02-23 05:42:39 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn processes_noscript_tags() {
|
2021-06-02 13:41:41 +00:00
|
|
|
let html = "\
|
|
|
|
<html>\
|
2021-02-23 05:42:39 +00:00
|
|
|
<body>\
|
|
|
|
<noscript>\
|
|
|
|
<img src=\"image.png\" />\
|
|
|
|
</noscript>\
|
|
|
|
</body>\
|
|
|
|
</html>";
|
2021-10-17 07:16:37 +00:00
|
|
|
let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string());
|
2021-03-11 22:44:02 +00:00
|
|
|
let url: Url = Url::parse("http://localhost").unwrap();
|
2021-02-23 05:42:39 +00:00
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
|
|
|
|
let mut options = Options::default();
|
|
|
|
options.no_images = true;
|
|
|
|
options.silent = true;
|
|
|
|
|
|
|
|
let client = Client::new();
|
|
|
|
|
|
|
|
html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0);
|
|
|
|
|
|
|
|
let mut buf: Vec<u8> = Vec::new();
|
2024-01-14 16:30:36 +00:00
|
|
|
serialize(
|
|
|
|
&mut buf,
|
|
|
|
&SerializableHandle::from(dom.document.clone()),
|
|
|
|
SerializeOpts::default(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-02-23 05:42:39 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
buf.iter().map(|&c| c as char).collect::<String>(),
|
|
|
|
format!(
|
2021-06-02 13:41:41 +00:00
|
|
|
"\
|
|
|
|
<html>\
|
2021-02-23 05:42:39 +00:00
|
|
|
<head>\
|
|
|
|
</head>\
|
|
|
|
<body>\
|
|
|
|
<noscript>\
|
|
|
|
<img src=\"{}\">\
|
|
|
|
</noscript>\
|
|
|
|
</body>\
|
|
|
|
</html>",
|
2021-10-17 07:16:37 +00:00
|
|
|
EMPTY_IMAGE_DATA_URL,
|
2021-02-23 05:42:39 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2021-06-02 13:41:41 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn preserves_script_type_json() {
|
|
|
|
let html = "<script id=\"data\" type=\"application/json\">{\"mono\":\"lith\"}</script>";
|
2021-10-17 07:16:37 +00:00
|
|
|
let dom = html::html_to_dom(&html.as_bytes().to_vec(), "".to_string());
|
2021-06-02 13:41:41 +00:00
|
|
|
let url: Url = Url::parse("http://localhost").unwrap();
|
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
|
|
|
|
let mut options = Options::default();
|
|
|
|
options.silent = true;
|
|
|
|
|
|
|
|
let client = Client::new();
|
|
|
|
|
|
|
|
html::walk_and_embed_assets(cache, &client, &url, &dom.document, &options, 0);
|
|
|
|
|
|
|
|
let mut buf: Vec<u8> = Vec::new();
|
2024-01-14 16:30:36 +00:00
|
|
|
serialize(
|
|
|
|
&mut buf,
|
|
|
|
&SerializableHandle::from(dom.document.clone()),
|
|
|
|
SerializeOpts::default(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-06-02 13:41:41 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
buf.iter().map(|&c| c as char).collect::<String>(),
|
|
|
|
"\
|
|
|
|
<html>\
|
|
|
|
<head>\
|
|
|
|
<script id=\"data\" type=\"application/json\">{\"mono\":\"lith\"}</script>\
|
|
|
|
</head>\
|
|
|
|
<body>\
|
|
|
|
</body>\
|
|
|
|
</html>"
|
|
|
|
);
|
|
|
|
}
|
2019-12-26 14:44:01 +00:00
|
|
|
}
|