2020-03-29 09:54:20 +02:00
|
|
|
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
|
|
|
|
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
|
|
|
|
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
|
|
|
|
// ██╔═══╝ ██╔══██║╚════██║╚════██║██║██║╚██╗██║██║ ██║
|
|
|
|
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
|
|
|
|
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
|
|
|
|
|
2020-05-23 09:49:04 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod passing {
|
|
|
|
use reqwest::blocking::Client;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2020-06-28 07:36:41 +02:00
|
|
|
use crate::css;
|
|
|
|
use crate::opts::Options;
|
|
|
|
|
2020-05-23 09:49:04 +02:00
|
|
|
#[test]
|
|
|
|
fn empty_input() {
|
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
let client = Client::new();
|
2020-06-28 07:36:41 +02:00
|
|
|
let options = Options::default();
|
2020-05-23 09:49:04 +02:00
|
|
|
|
2020-06-28 22:11:15 +02:00
|
|
|
assert_eq!(css::embed_css(cache, &client, "", "", &options, 0), "");
|
2020-05-23 09:49:04 +02:00
|
|
|
}
|
|
|
|
|
2020-06-27 05:22:35 +02:00
|
|
|
#[test]
|
|
|
|
fn trim_if_empty() {
|
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
let client = Client::new();
|
2020-06-28 07:36:41 +02:00
|
|
|
let options = Options::default();
|
2020-06-27 05:22:35 +02:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
css::embed_css(
|
|
|
|
cache,
|
|
|
|
&client,
|
|
|
|
"https://doesntmatter.local/",
|
|
|
|
"\t \t ",
|
2020-06-28 07:36:41 +02:00
|
|
|
&options,
|
2020-06-28 22:11:15 +02:00
|
|
|
0,
|
2020-06-27 05:22:35 +02:00
|
|
|
),
|
|
|
|
""
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-23 09:49:04 +02:00
|
|
|
#[test]
|
|
|
|
fn style_exclude_unquoted_images() {
|
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
let client = Client::new();
|
2020-06-28 07:36:41 +02:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.no_images = true;
|
|
|
|
options.silent = true;
|
2020-05-23 09:49:04 +02:00
|
|
|
|
|
|
|
const STYLE: &str = "/* border: none;*/\
|
|
|
|
background-image: url(https://somewhere.com/bg.png); \
|
|
|
|
list-style: url(/assets/images/bullet.svg);\
|
|
|
|
width:99.998%; \
|
|
|
|
margin-top: -20px; \
|
|
|
|
line-height: -1; \
|
|
|
|
height: calc(100vh - 10pt)";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
css::embed_css(
|
|
|
|
cache,
|
|
|
|
&client,
|
|
|
|
"https://doesntmatter.local/",
|
|
|
|
&STYLE,
|
2020-06-28 07:36:41 +02:00
|
|
|
&options,
|
2020-06-28 22:11:15 +02:00
|
|
|
0,
|
2020-05-23 09:49:04 +02:00
|
|
|
),
|
|
|
|
format!(
|
|
|
|
"/* border: none;*/\
|
|
|
|
background-image: url('{empty_image}'); \
|
|
|
|
list-style: url('{empty_image}');\
|
|
|
|
width:99.998%; \
|
|
|
|
margin-top: -20px; \
|
|
|
|
line-height: -1; \
|
|
|
|
height: calc(100vh - 10pt)",
|
|
|
|
empty_image = empty_image!()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn style_exclude_single_quoted_images() {
|
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
let client = Client::new();
|
2020-06-28 07:36:41 +02:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.no_images = true;
|
|
|
|
options.silent = true;
|
2020-05-23 09:49:04 +02:00
|
|
|
|
|
|
|
const STYLE: &str = "/* border: none;*/\
|
|
|
|
background-image: url('https://somewhere.com/bg.png'); \
|
|
|
|
list-style: url('/assets/images/bullet.svg');\
|
|
|
|
width:99.998%; \
|
|
|
|
margin-top: -20px; \
|
|
|
|
line-height: -1; \
|
|
|
|
height: calc(100vh - 10pt)";
|
|
|
|
|
|
|
|
assert_eq!(
|
2020-06-28 22:11:15 +02:00
|
|
|
css::embed_css(cache, &client, "", &STYLE, &options, 0),
|
2020-05-23 09:49:04 +02:00
|
|
|
format!(
|
|
|
|
"/* border: none;*/\
|
|
|
|
background-image: url('{empty_image}'); \
|
|
|
|
list-style: url('{empty_image}');\
|
|
|
|
width:99.998%; \
|
|
|
|
margin-top: -20px; \
|
|
|
|
line-height: -1; \
|
|
|
|
height: calc(100vh - 10pt)",
|
|
|
|
empty_image = empty_image!()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn style_block() {
|
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
let client = Client::new();
|
2020-06-28 07:36:41 +02:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.silent = true;
|
2020-05-23 09:49:04 +02:00
|
|
|
|
|
|
|
const CSS: &str = "\
|
|
|
|
#id.class-name:not(:nth-child(3n+0)) {\n \
|
|
|
|
// border: none;\n \
|
|
|
|
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=');\n\
|
|
|
|
}\n\
|
|
|
|
\n\
|
|
|
|
html > body {}";
|
|
|
|
|
|
|
|
assert_eq!(
|
2020-06-28 22:11:15 +02:00
|
|
|
css::embed_css(cache, &client, "file:///", &CSS, &options, 0),
|
2020-05-23 09:49:04 +02:00
|
|
|
CSS
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn attribute_selectors() {
|
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
let client = Client::new();
|
2020-06-28 07:36:41 +02:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.silent = true;
|
2020-05-23 09:49:04 +02:00
|
|
|
|
|
|
|
const CSS: &str = "\
|
|
|
|
[data-value] {
|
|
|
|
/* Attribute exists */
|
|
|
|
}
|
|
|
|
|
|
|
|
[data-value='foo'] {
|
|
|
|
/* Attribute has this exact value */
|
|
|
|
}
|
|
|
|
|
|
|
|
[data-value*='foo'] {
|
|
|
|
/* Attribute value contains this value somewhere in it */
|
|
|
|
}
|
|
|
|
|
|
|
|
[data-value~='foo'] {
|
|
|
|
/* Attribute has this value in a space-separated list somewhere */
|
|
|
|
}
|
|
|
|
|
|
|
|
[data-value^='foo'] {
|
|
|
|
/* Attribute value starts with this */
|
|
|
|
}
|
|
|
|
|
|
|
|
[data-value|='foo'] {
|
|
|
|
/* Attribute value starts with this in a dash-separated list */
|
|
|
|
}
|
|
|
|
|
|
|
|
[data-value$='foo'] {
|
|
|
|
/* Attribute value ends with this */
|
|
|
|
}
|
|
|
|
";
|
|
|
|
|
2020-06-28 22:11:15 +02:00
|
|
|
assert_eq!(css::embed_css(cache, &client, "", &CSS, &options, 0), CSS);
|
2020-05-23 09:49:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn import_string() {
|
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
let client = Client::new();
|
2020-06-28 07:36:41 +02:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.silent = true;
|
2020-05-23 09:49:04 +02:00
|
|
|
|
|
|
|
const CSS: &str = "\
|
|
|
|
@charset 'UTF-8';\n\
|
|
|
|
\n\
|
|
|
|
@import 'data:text/css,html{background-color:%23000}';\n\
|
|
|
|
\n\
|
|
|
|
@import url('data:text/css,html{color:%23fff}')\n\
|
|
|
|
";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
css::embed_css(
|
|
|
|
cache,
|
|
|
|
&client,
|
|
|
|
"https://doesntmatter.local/",
|
|
|
|
&CSS,
|
2020-06-28 07:36:41 +02:00
|
|
|
&options,
|
2020-06-28 22:11:15 +02:00
|
|
|
0,
|
2020-05-23 09:49:04 +02:00
|
|
|
),
|
|
|
|
"\
|
|
|
|
@charset 'UTF-8';\n\
|
|
|
|
\n\
|
|
|
|
@import 'data:text/css;base64,aHRtbHtiYWNrZ3JvdW5kLWNvbG9yOiMwMDB9';\n\
|
|
|
|
\n\
|
|
|
|
@import url('data:text/css;base64,aHRtbHtjb2xvcjojZmZmfQ==')\n\
|
|
|
|
"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn hash_urls() {
|
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
let client = Client::new();
|
2020-06-28 07:36:41 +02:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.silent = true;
|
2020-05-23 09:49:04 +02:00
|
|
|
|
|
|
|
const CSS: &str = "\
|
|
|
|
body {\n \
|
|
|
|
behavior: url(#default#something);\n\
|
|
|
|
}\n\
|
|
|
|
\n\
|
|
|
|
.scissorHalf {\n \
|
|
|
|
offset-path: url(#somePath);\n\
|
|
|
|
}\n\
|
|
|
|
";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
css::embed_css(
|
|
|
|
cache,
|
|
|
|
&client,
|
|
|
|
"https://doesntmatter.local/",
|
|
|
|
&CSS,
|
2020-06-28 07:36:41 +02:00
|
|
|
&options,
|
2020-06-28 22:11:15 +02:00
|
|
|
0,
|
2020-05-23 09:49:04 +02:00
|
|
|
),
|
|
|
|
CSS
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn transform_percentages_and_degrees() {
|
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
let client = Client::new();
|
2020-06-28 07:36:41 +02:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.silent = true;
|
2020-05-23 09:49:04 +02:00
|
|
|
|
|
|
|
const CSS: &str = "\
|
|
|
|
div {\n \
|
|
|
|
transform: translate(-50%, -50%) rotate(-45deg);\n\
|
|
|
|
transform: translate(50%, 50%) rotate(45deg);\n\
|
|
|
|
transform: translate(+50%, +50%) rotate(+45deg);\n\
|
|
|
|
}\n\
|
|
|
|
";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
css::embed_css(
|
|
|
|
cache,
|
|
|
|
&client,
|
|
|
|
"https://doesntmatter.local/",
|
|
|
|
&CSS,
|
2020-06-28 07:36:41 +02:00
|
|
|
&options,
|
2020-06-28 22:11:15 +02:00
|
|
|
0,
|
2020-05-23 09:49:04 +02:00
|
|
|
),
|
|
|
|
CSS
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unusual_indents() {
|
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
let client = Client::new();
|
2020-06-28 07:36:41 +02:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.silent = true;
|
2020-05-23 09:49:04 +02:00
|
|
|
|
|
|
|
const CSS: &str = "\
|
|
|
|
.is\\:good:hover {\n \
|
|
|
|
color: green\n\
|
|
|
|
}\n\
|
|
|
|
\n\
|
|
|
|
#\\~\\!\\@\\$\\%\\^\\&\\*\\(\\)\\+\\=\\,\\.\\/\\\\\\'\\\"\\;\\:\\?\\>\\<\\[\\]\\{\\}\\|\\`\\# {\n \
|
|
|
|
color: black\n\
|
|
|
|
}\n\
|
|
|
|
";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
css::embed_css(
|
|
|
|
cache,
|
|
|
|
&client,
|
|
|
|
"https://doesntmatter.local/",
|
|
|
|
&CSS,
|
2020-06-28 07:36:41 +02:00
|
|
|
&options,
|
2020-06-28 22:11:15 +02:00
|
|
|
0,
|
2020-05-23 09:49:04 +02:00
|
|
|
),
|
|
|
|
CSS
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn exclude_fonts() {
|
|
|
|
let cache = &mut HashMap::new();
|
|
|
|
let client = Client::new();
|
2020-06-28 07:36:41 +02:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.no_fonts = true;
|
|
|
|
options.silent = true;
|
2020-05-23 09:49:04 +02:00
|
|
|
|
|
|
|
const CSS: &str = "\
|
|
|
|
@font-face {\n \
|
|
|
|
font-family: 'My Font';\n \
|
|
|
|
src: url(my_font.woff);\n\
|
|
|
|
}\n\
|
|
|
|
\n\
|
|
|
|
#identifier {\n \
|
|
|
|
font-family: 'My Font' Arial\n\
|
|
|
|
}\n\
|
|
|
|
\n\
|
|
|
|
@font-face {\n \
|
|
|
|
font-family: 'My Font';\n \
|
|
|
|
src: url(my_font.woff);\n\
|
|
|
|
}\n\
|
|
|
|
\n\
|
|
|
|
div {\n \
|
|
|
|
font-family: 'My Font' Verdana\n\
|
|
|
|
}\n\
|
|
|
|
";
|
|
|
|
|
|
|
|
const CSS_OUT: &str = " \
|
|
|
|
\n\
|
|
|
|
\n\
|
|
|
|
#identifier {\n \
|
|
|
|
font-family: 'My Font' Arial\n\
|
|
|
|
}\n\
|
|
|
|
\n \
|
|
|
|
\n\
|
|
|
|
\n\
|
|
|
|
div {\n \
|
|
|
|
font-family: 'My Font' Verdana\n\
|
|
|
|
}\n\
|
|
|
|
";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
css::embed_css(
|
|
|
|
cache,
|
|
|
|
&client,
|
|
|
|
"https://doesntmatter.local/",
|
|
|
|
&CSS,
|
2020-06-28 07:36:41 +02:00
|
|
|
&options,
|
2020-06-28 22:11:15 +02:00
|
|
|
0,
|
2020-05-23 09:49:04 +02:00
|
|
|
),
|
|
|
|
CSS_OUT
|
|
|
|
);
|
|
|
|
}
|
2020-04-22 09:37:02 +02:00
|
|
|
}
|