feat: allow to inject styles
This commit is contained in:
parent
6e091a32fc
commit
40ebc1ddea
@ -81,16 +81,9 @@ impl Newspaper for CourrierInternational {
|
|||||||
None => bail!("404 not found"),
|
None => bail!("404 not found"),
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: Move to const
|
|
||||||
let elements_to_remove = [
|
|
||||||
// navigation elements
|
|
||||||
"#entete.connecte",
|
|
||||||
];
|
|
||||||
|
|
||||||
let single_page_html = tools::self_contained_html::Config {
|
let single_page_html = tools::self_contained_html::Config {
|
||||||
downloader: Some(&downloader),
|
downloader: Some(&downloader),
|
||||||
base_url: Some(&url),
|
base_url: Some(&url),
|
||||||
elements_to_remove: &elements_to_remove,
|
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
.run(&html)
|
.run(&html)
|
||||||
|
@ -169,6 +169,15 @@ where
|
|||||||
for element in self.elements_to_remove {
|
for element in self.elements_to_remove {
|
||||||
document.select(element.as_ref()).remove();
|
document.select(element.as_ref()).remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---- Add additional styles ----
|
||||||
|
//
|
||||||
|
for style in self.styles_to_add {
|
||||||
|
document
|
||||||
|
.select("head")
|
||||||
|
.append_html(format!("\n<style>{}</style>\n", style.as_ref()));
|
||||||
|
}
|
||||||
|
|
||||||
String::from(document.html())
|
String::from(document.html())
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -444,4 +453,58 @@ mod tests {
|
|||||||
);
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn add_style() -> Result<()> {
|
||||||
|
let html = indoc! {"
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset=\"UTF-8\">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
The body
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
"};
|
||||||
|
|
||||||
|
let wanted_html = indoc! {"
|
||||||
|
<html><head>
|
||||||
|
<meta charset=\"UTF-8\">
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
margin: 3em;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
The body
|
||||||
|
</body></html>
|
||||||
|
"};
|
||||||
|
|
||||||
|
let style_to_add = indoc! {"
|
||||||
|
body {
|
||||||
|
margin: 3em;
|
||||||
|
}
|
||||||
|
"};
|
||||||
|
|
||||||
|
let base_url = Url::parse("http://example.com")?;
|
||||||
|
let downloader = DummyDownloader {};
|
||||||
|
|
||||||
|
let mut minifier = HTMLMinifier::new();
|
||||||
|
minifier.digest(wanted_html)?;
|
||||||
|
let minified = String::from_utf8(minifier.get_html().into())?;
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
Config {
|
||||||
|
downloader: Some(&downloader),
|
||||||
|
base_url: Some(&base_url),
|
||||||
|
styles_to_add: &[style_to_add],
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
.run(html)
|
||||||
|
.await,
|
||||||
|
minified
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user