use anyhow::{Result, bail};
use quick_xml::events::{BytesStart, Event};
use quick_xml::{Reader, Writer, XmlVersion};
use regex::Regex;
use std::sync::LazyLock;
const FONT_EXTS: [&str; 4] = [".ttf", ".otf", ".woff", ".woff2"];
const FONT_MEDIA_TYPES: [&str; 4] = [
"font/",
"application/font-",
"application/x-font-",
"application/vnd.ms-opentype",
];
static FONT_FACE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"(?is)@font-face\s*\{[^}]*\}").unwrap());
pub fn is_font(name: &str) -> bool {
let lower = name.to_ascii_lowercase();
FONT_EXTS.iter().any(|ext| lower.ends_with(ext))
}
pub fn strip_font_faces(css: &str) -> Result<Option<String>> {
Ok(Some(FONT_FACE.replace_all(css, "").into_owned()))
}
fn is_font_item(e: &BytesStart) -> Result<bool> {
if e.local_name().as_ref() != "item" {
return Ok(false);
}
if let Some(href) = e.try_get_attribute("href")?
&& is_font(&href.normalized_value(XmlVersion::Implicit1_0)?)
{
return Ok(true);
}
if let Some(media) = e.try_get_attribute("media-type")? {
let media = media
.normalized_value(XmlVersion::Implicit1_0)?
.to_ascii_lowercase();
return Ok(FONT_MEDIA_TYPES.iter().any(|t| media.starts_with(t)));
}
Ok(false)
}
pub fn remove_manifest_items(opf: &str) -> Result<Option<String>> {
let mut reader = Reader::from_str(opf);
let mut writer = Writer::new(Vec::new());
loop {
match reader.read_event()? {
Event::Eof => break,
Event::Empty(e) if is_font_item(&e)? => {}
Event::Start(e) if is_font_item(&e)? => {
reader.read_to_end(e.name())?;
}
event => writer.write_event(event)?,
}
}
Ok(Some(String::from_utf8(writer.into_inner())?))
}
pub fn clean_encryption(xml: &str) -> Result<Option<String>> {
let mut reader = Reader::from_str(xml);
let mut writer = Writer::new(Vec::new());
let mut kept = 0;
loop {
match reader.read_event()? {
Event::Eof => break,
Event::Start(start) if start.local_name().as_ref() == "EncryptedData" => {
let mut block = vec![Event::Start(start)];
let mut points_to_font = false;
loop {
let event = reader.read_event()?;
match &event {
Event::Start(e) | Event::Empty(e)
if e.local_name().as_ref() == "CipherReference" =>
{
if let Some(uri) = e.try_get_attribute("URI")? {
points_to_font |=
is_font(&uri.normalized_value(XmlVersion::Implicit1_0)?);
}
}
Event::Eof => bail!("encryption.xml: unclosed <EncryptedData>"),
_ => {}
}
let is_end = matches!(&event, Event::End(e) if e.local_name().as_ref() == "EncryptedData");
block.push(event);
if is_end {
break;
}
}
if !points_to_font {
kept += 1;
for event in block {
writer.write_event(event)?;
}
}
}
event => writer.write_event(event)?,
}
}
if kept == 0 {
return Ok(None);
}
Ok(Some(String::from_utf8(writer.into_inner())?))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn detects_fonts_case_insensitively() {
assert!(is_font("fonts/00001.TTF"));
assert!(is_font("OEBPS/UbuntuMono-Bold.otf"));
assert!(is_font("a.woff2"));
assert!(!is_font("styles/fonts.css"));
}
#[test]
fn strips_font_face_and_keeps_the_rest() {
let css = "body { margin: 0 }\n@font-face {\n font-family: \"Charis\";\n src: url(x.ttf);\n}\np { color: red }";
let out = strip_font_faces(css).unwrap().unwrap();
assert_eq!(out, "body { margin: 0 }\n\np { color: red }");
}
#[test]
fn removes_only_font_items_from_manifest() {
let opf = r#"<?xml version="1.0"?><package><manifest>
<item href="a.css" id="c" media-type="text/css"/>
<item href="Font.otf" id="f" media-type="application/vnd.ms-opentype"/>
<item href="no-extension" id="g" media-type="font/ttf"></item>
<item href="img.png" id="i" media-type="image/png"/>
</manifest></package>"#;
let out = remove_manifest_items(opf).unwrap().unwrap();
assert!(!out.contains("Font.otf") && !out.contains("no-extension"));
assert!(out.contains(r#"<item href="a.css" id="c" media-type="text/css"/>"#));
assert!(out.contains(r#"<item href="img.png" id="i" media-type="image/png"/>"#));
assert!(out.starts_with(r#"<?xml version="1.0"?>"#));
}
#[test]
fn opf_without_fonts_is_unchanged() {
let opf = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<package version=\"3.0\">\n <metadata><dc:title>A & B</dc:title></metadata>\n <manifest><item href=\"a.xhtml\" id=\"a\" media-type=\"application/xhtml+xml\"/></manifest>\n</package>";
assert_eq!(remove_manifest_items(opf).unwrap().unwrap(), opf);
}
#[test]
fn encryption_with_only_fonts_is_dropped() {
let xml = r#"<encryption xmlns:enc="x"><enc:EncryptedData><enc:CipherData>
<enc:CipherReference URI="fonts/00001.ttf"/></enc:CipherData></enc:EncryptedData></encryption>"#;
assert_eq!(clean_encryption(xml).unwrap(), None);
}
#[test]
fn encryption_with_other_entries_is_kept() {
let xml = r#"<encryption><EncryptedData><CipherReference URI="a.ttf"/></EncryptedData><EncryptedData><CipherReference URI="ch01.xhtml"/></EncryptedData></encryption>"#;
let out = clean_encryption(xml)
.unwrap()
.expect("still has encrypted content");
assert_eq!(
out,
r#"<encryption><EncryptedData><CipherReference URI="ch01.xhtml"/></EncryptedData></encryption>"#
);
}
#[test]
fn broken_xml_is_an_error() {
assert!(remove_manifest_items("<package><manifest></package>").is_err());
}
}