#![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
)]
use kuchiki::{parse_html, traits::TendrilSink, NodeRef};
pub mod error;
mod parser;
use cssparser::CowRcStr;
pub use error::InlineError;
use smallvec::{smallvec, SmallVec};
use std::{
borrow::Cow,
fs::File,
io::{Read, Write},
};
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 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 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 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);
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())?;
}
}
if self.options.remove_style_tags {
style_tag.as_node().detach()
}
}
} else if self.options.remove_style_tags {
for style_tag in document
.select("style")
.map_err(|_| error::InlineError::ParseError(Cow::from("Unknown error")))?
{
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 = self.load_external(url.as_ref())?;
process_css(&document, css.as_str())?;
}
}
}
if let Some(extra_css) = &self.options.extra_css {
process_css(&document, extra_css)?;
}
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_string());
}
}
};
Cow::Borrowed(href)
}
fn load_external(&self, 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)
}
}
}
fn process_css(document: &NodeRef, css: &str) -> 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 parsed in rule_list {
if let Ok((selector, declarations)) = parsed {
if let Ok(matching_elements) = document.select(selector) {
for matching_element in matching_elements {
if let Ok(mut attributes) = matching_element.attributes.try_borrow_mut() {
if let Some(existing_style) = attributes.get_mut("style") {
*existing_style = merge_styles(existing_style, &declarations)?
} else {
let mut final_styles = String::with_capacity(64);
for (name, value) in &declarations {
final_styles.push_str(name);
final_styles.push(':');
final_styles.push_str(value);
final_styles.push(';');
}
attributes.insert("style", final_styles);
};
}
}
}
}
}
Ok(())
}
impl Default for CSSInliner<'_> {
#[inline]
fn default() -> Self {
CSSInliner::new(Default::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: &[parser::Declaration]) -> 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<[&CowRcStr; 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) {
final_styles.push_str(&name);
final_styles.push(':');
final_styles.push_str(value);
final_styles.push(';');
}
}
Ok(final_styles)
}