#[cfg(feature = "export-html")]
use std::fmt::{self, Write as _};
#[cfg(feature = "export-html")]
use std::io::Write;
use crate::export::ExportError;
#[cfg(feature = "export-html")]
use crate::export::schema::ENGINE_NAME;
#[cfg(feature = "export-html")]
pub(crate) struct Text<'a>(pub(crate) &'a str);
#[cfg(feature = "export-html")]
impl fmt::Display for Text<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for character in self.0.chars() {
match character {
'&' => f.write_str("&")?,
'<' => f.write_str("<")?,
'>' => f.write_str(">")?,
'"' => f.write_str(""")?,
'\'' => f.write_str("'")?,
character if is_neutralized(character) => write!(
f,
"<span class=\"ctl\">U+{:04X}</span>",
u32::from(character)
)?,
character => f.write_char(character)?,
}
}
Ok(())
}
}
#[cfg(feature = "export-html")]
pub(crate) struct Plain<'a>(pub(crate) &'a str);
#[cfg(feature = "export-html")]
impl fmt::Display for Plain<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for character in self.0.chars() {
match character {
'&' => f.write_str("&")?,
'<' => f.write_str("<")?,
'>' => f.write_str(">")?,
'"' => f.write_str(""")?,
'\'' => f.write_str("'")?,
character if is_neutralized(character) => f.write_char('\u{fffd}')?,
character => f.write_char(character)?,
}
}
Ok(())
}
}
#[cfg(feature = "export-html")]
fn is_neutralized(character: char) -> bool {
matches!(character,
'\u{0}'..='\u{8}'
| '\u{b}' | '\u{c}'
| '\u{e}'..='\u{1f}'
| '\u{7f}'..='\u{9f}'
| '\u{61c}'
| '\u{200e}' | '\u{200f}'
| '\u{202a}'..='\u{202e}'
| '\u{2066}'..='\u{2069}')
}
#[cfg(feature = "export-html")]
pub(crate) fn esc(text: &str) -> String {
Text(text).to_string()
}
#[cfg(feature = "export-html")]
pub(crate) const STYLE: &str = include_str!("../../assets/html/report.css");
#[cfg(feature = "export-html")]
pub(crate) const TONE_FOUND: &str = "s-found";
#[cfg(feature = "export-html")]
pub(crate) const TONE_PARTIAL: &str = "s-partial";
#[cfg(feature = "export-html")]
pub(crate) const TONE_INERT: &str = "s-inert";
#[cfg(feature = "export-html")]
pub(crate) const TONE_NONE: &str = "s-none";
#[cfg(feature = "export-html")]
pub(crate) fn head(out: &mut dyn Write, title: &str) -> Result<(), ExportError> {
writeln!(
out,
r#"<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="generator" content="{engine} {version}">
<meta name="robots" content="noindex, nofollow">
<title>{title}</title>
<style>
{style}</style>
</head>
<body>
<input type="checkbox" id="zond-theme" class="theme-switch" aria-label="Use the other colour scheme">
<div class="sheet">"#,
engine = ENGINE_NAME,
version = crate::report::ENGINE_VERSION,
title = Plain(title),
style = STYLE,
)?;
Ok(())
}
#[cfg(feature = "export-html")]
pub(crate) fn masthead(
out: &mut dyn Write,
heading: &str,
subtitle: &str,
) -> Result<(), ExportError> {
writeln!(
out,
r#"<header class="masthead">
<div class="brand">zond<span class="brand-mark">_</span></div>
<div class="masthead-title">
<h1>{heading}</h1>
<p class="subtitle">{subtitle}</p>
</div>
<label class="theme-label" for="zond-theme" title="Switch between light and dark"><span class="theme-icon"></span>theme</label>
</header>"#,
heading = Text(heading),
)?;
Ok(())
}
#[cfg(feature = "export-html")]
pub(crate) fn notice(
out: &mut dyn Write,
alert: bool,
key: &str,
text: &str,
) -> Result<(), ExportError> {
let class = if alert {
"notice notice-alert"
} else {
"notice"
};
writeln!(
out,
"<span class=\"{class}\"><span class=\"notice-key\">{key}</span><span>{text}</span></span>",
key = Text(key),
text = Text(text),
)?;
Ok(())
}
#[cfg(feature = "export-html")]
pub(crate) fn tile(
out: &mut dyn Write,
value: usize,
label: &str,
note: &str,
) -> Result<(), ExportError> {
writeln!(
out,
"<div class=\"tile\"><div class=\"tile-value\">{value}</div><div class=\"tile-label\">{label}</div><div class=\"tile-note\">{note}</div></div>",
label = Text(label),
)?;
Ok(())
}
#[cfg(feature = "export-html")]
pub(crate) fn foot(out: &mut dyn Write) -> Result<(), ExportError> {
out.write_all(b"</div>\n</body>\n</html>\n")?;
Ok(())
}
#[cfg(any(feature = "export-json", feature = "export-jsonl"))]
pub(crate) fn render_error(format: &'static str, error: serde_json::Error) -> ExportError {
if error.is_io() {
ExportError::Io(error.into())
} else {
ExportError::Render {
format,
message: error.to_string(),
}
}
}
#[cfg(all(test, feature = "export-html"))]
mod tests {
use super::*;
#[test]
fn a_hostname_that_would_execute_is_escaped() {
let hostile = "<script>alert('pwned')</script>";
assert_eq!(
esc(hostile),
"<script>alert('pwned')</script>"
);
assert_eq!(esc("a & b"), "a & b");
assert_eq!(esc("say \"hi\""), "say "hi"");
}
#[test]
fn direction_and_control_characters_are_shown_rather_than_obeyed() {
assert_eq!(
esc("host\u{202e}txt.exe"),
"host<span class=\"ctl\">U+202E</span>txt.exe"
);
assert_eq!(esc("bell\u{7}"), "bell<span class=\"ctl\">U+0007</span>");
assert_eq!(esc("two\nlines\tapart"), "two\nlines\tapart");
assert_eq!(Plain("host\u{202e}txt").to_string(), "host\u{fffd}txt");
}
#[test]
fn both_escapers_neutralize_the_same_characters() {
for code in (0u32..0x2100).chain([0xfeff, 0x1f600]) {
let Some(character) = char::from_u32(code) else {
continue;
};
let value = character.to_string();
assert_eq!(
Text(&value).to_string().contains("class=\"ctl\""),
Plain(&value).to_string().contains('\u{fffd}'),
"U+{code:04X} is neutralized by one escaper and not the other"
);
}
}
#[test]
fn nothing_reaches_a_title_still_carrying_markup() {
for code in (0u32..0x2100).chain([0xfeff, 0x1f600]) {
let Some(character) = char::from_u32(code) else {
continue;
};
let plain = Plain(&character.to_string()).to_string();
for carrier in ['<', '>', '"', '\''] {
assert!(
!plain.contains(carrier),
"U+{code:04X} reaches a title as {carrier}"
);
}
}
}
}