use std::fmt::Write;
use std::sync::Arc;
use canvist_model::Block;
use canvist_model::Content;
use canvist_model::Document;
use canvist_model::Embed;
use canvist_model::InlineText;
use canvist_model::Mark;
use canvist_model::RunRef;
use canvist_model::Value;
use canvist_model::is_safe_link_url;
use super::attribute;
use super::escape_into;
use super::num;
use crate::Color;
use crate::document::InkDrawing;
#[cfg(feature = "tables")]
use crate::document::column_widths;
use crate::document::span_attr;
#[cfg(feature = "tables")]
use crate::document::table_cells;
#[cfg(feature = "tables")]
use crate::document::table_map;
use crate::style::BlockStyle;
use crate::style::StyleId;
use crate::style::Styles;
use crate::style::attr;
use crate::style::attr_px;
use crate::style::line_height;
use crate::text::slice;
use crate::theme::AttrBinding;
use crate::theme::EmbedStyle;
use crate::theme::FontSize;
use crate::theme::INK_WIDTH;
use crate::theme::LeafStyle;
use crate::theme::MarkerKind;
use crate::theme::Motion;
use crate::theme::TableRole;
use crate::theme::TextPatch;
use crate::theme::TextStyle;
use crate::theme::Theme;
const ALIGNMENTS: [&str; 4] = ["left", "center", "right", "justify"];
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct HtmlOptions {
pub width: f32,
}
impl HtmlOptions {
#[must_use]
pub const fn new(width: f32) -> Self {
Self { width }
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct HtmlDocument {
pub html: String,
pub css: String,
}
impl HtmlDocument {
#[must_use]
pub fn page(&self) -> String {
format!(
"<!doctype html><html><head><meta charset=\"utf-8\"><meta name=\"viewport\" \
content=\"width=device-width\"><style>{}</style></head><body \
style=\"margin:0\">{}</body></html>",
self.css, self.html
)
}
}
#[must_use]
pub fn to_html(doc: &Document, theme: &Arc<Theme>, options: &HtmlOptions) -> HtmlDocument {
let mut writer = Writer {
doc,
styles: Styles::new(theme.clone(), doc.schema().clone()),
out: String::new(),
stacked_tables: 0,
header_scope: None,
};
writer.out.push_str("<div class=\"cv-doc\">");
writer.children(None, None, 0, options.width);
writer.out.push_str("</div>");
let css = stylesheet(&writer.styles, options.width);
HtmlDocument {
html: writer.out,
css,
}
}
struct Writer<'a> {
doc: &'a Document,
styles: Styles,
out: String,
stacked_tables: usize,
header_scope: Option<&'static str>,
}
impl Writer<'_> {
fn children(
&mut self,
parent: Option<&Block>,
parent_style: Option<StyleId>,
lists: usize,
width: f32,
) {
let doc = self.doc;
let marker = parent_style.and_then(|style| self.styles.get(style).marker);
let open = parent.is_none_or(|parent| crate::document::is_open(doc, parent));
let shown = if open { usize::MAX } else { 1 };
let mut previous_after = None;
for (index, &id) in doc
.children(parent.map(Block::id))
.iter()
.enumerate()
.take(shown)
{
let Some(block) = doc.block(id) else {
continue;
};
let marker = marker.filter(|&kind| kind != MarkerKind::Disclosure || index == 0);
let style = self
.styles
.resolve(block, parent.map(Block::kind), parent_style);
let resolved = self.styles.get(style);
let margin = previous_after.map_or(0.0, |after: f32| after.max(resolved.space_before));
previous_after = Some(resolved.space_after);
let marker = marker.map(|kind| {
let depth = if kind == MarkerKind::Disclosure {
usize::from(open)
} else {
lists.saturating_sub(1)
};
(kind, depth)
});
self.block(block, style, margin, marker, (lists, width));
}
}
fn block(
&mut self,
block: &Block,
style: StyleId,
margin: f32,
marker: Option<(MarkerKind, usize)>,
(lists, width): (usize, f32),
) {
let resolved = self.styles.get(style).clone();
let tag = tag_name(&resolved.tag);
if self.table(block, (style, &resolved), margin, (lists, width)) {
return;
}
self.open_tag(block, style, tag, marker);
if let (Content::Leaf, LeafStyle::Slot { .. }) = (block.content(), resolved.leaf)
&& let Some(alt) = block
.attrs()
.get("alt")
.and_then(Value::as_str)
.filter(|alt| !alt.is_empty())
{
attribute(&mut self.out, "role", "img");
attribute(&mut self.out, "aria-label", alt);
}
let mut inline_style = self.inline_style(block, &resolved, margin);
if self.stacks(&resolved) {
inline_style.push_str("display:flow-root;");
}
let inner = (width - resolved.inset_left() - resolved.inset_right()).max(0.0);
if let (Content::Leaf, LeafStyle::Ink { height, .. }) = (block.content(), resolved.leaf) {
let height = attr_px(block.attrs(), "height").unwrap_or(height) * inner / INK_WIDTH;
let _ = write!(inline_style, "height:{}px;", num(height));
attribute(&mut self.out, "role", "img");
attribute(&mut self.out, "aria-label", "Drawing");
}
if !inline_style.is_empty() {
attribute(&mut self.out, "style", &inline_style);
}
self.out.push('>');
if is_void(tag) {
return;
}
if marker.is_some_and(|(kind, _)| kind == MarkerKind::Task) {
self.checkbox(block, &resolved);
}
if let Content::Inline(inline) = block.content()
&& tag == "pre"
&& inline.as_str().starts_with('\n')
{
self.out.push('\n');
}
match block.content() {
Content::Inline(inline) => self.inline(inline, &resolved.text),
Content::Blocks(_) => self.child_blocks(block, (style, &resolved), (lists, width)),
Content::Leaf => self.ink(block, &resolved),
}
let _ = write!(self.out, "</{tag}>");
}
fn ink(&mut self, block: &Block, resolved: &BlockStyle) {
let LeafStyle::Ink { height, stroke } = resolved.leaf else {
return;
};
let Some(drawing) = cfg!(feature = "ink")
.then(|| InkDrawing::from_attrs(block.attrs()))
.flatten()
else {
return;
};
let height = attr_px(block.attrs(), "height").unwrap_or(height);
let _ = write!(
self.out,
"<svg class=\"cv-ink\" viewBox=\"0 0 {} {}\" preserveAspectRatio=\"none\" \
aria-hidden=\"true\">",
num(INK_WIDTH),
num(height)
);
for line in &drawing.strokes {
let color = line
.color
.map_or_else(|| "currentColor".to_owned(), |color| color.to_string());
let _ = write!(
self.out,
"<path d=\"{}\" fill=\"none\" stroke=\"{color}\" stroke-width=\"{}\" \
stroke-linecap=\"round\" stroke-linejoin=\"round\"/>",
super::stroke_path(&line.points),
num(line.width.unwrap_or(stroke))
);
}
self.out.push_str("</svg>");
}
fn child_blocks(
&mut self,
block: &Block,
(style, resolved): (StyleId, &BlockStyle),
(lists, width): (usize, f32),
) {
let lists = lists + usize::from(resolved.marker.is_some());
let inner = (width - resolved.inset_left() - resolved.inset_right()).max(0.0);
let table = matches!(resolved.table, Some(TableRole::Table { .. }));
self.stacked_tables += usize::from(table);
self.children(Some(block), Some(style), lists, inner);
self.stacked_tables -= usize::from(table);
}
fn stacks(&self, resolved: &BlockStyle) -> bool {
match resolved.table {
Some(TableRole::Table { .. }) => true,
Some(_) => self.stacked_tables > 0,
None => false,
}
}
#[cfg(feature = "tables")]
fn table(
&mut self,
block: &Block,
(style, resolved): (StyleId, &BlockStyle),
margin: f32,
(lists, width): (usize, f32),
) -> bool {
let Some(TableRole::Table { rule }) = resolved.table else {
return false;
};
let rule = rule.width;
let doc = self.doc;
let cells = table_cells(doc, block);
let (map, hints) = table_map(doc, &cells);
if self.stacked_tables > 0 || map.cells().is_empty() {
return false;
}
let inner = (width - resolved.inset_left() - resolved.inset_right()).max(0.0);
let widths = column_widths(&map, &hints, (inner - rule).max(0.0));
let tag = tag_name(&resolved.tag);
self.open_tag(block, style, tag, None);
let mut inline_style = self.inline_style(block, resolved, margin);
let _ = write!(
inline_style,
"width:{}px;",
num(widths.iter().sum::<f32>() + rule)
);
attribute(&mut self.out, "style", &inline_style);
self.out.push_str("><colgroup>");
for width in &widths {
let _ = write!(self.out, "<col style=\"width:{}px\">", num(*width));
}
self.out.push_str("</colgroup><tbody>");
for (row, (row_block, row_cells)) in cells.iter().enumerate() {
let row_style = self
.styles
.resolve(row_block, Some(block.kind()), Some(style));
let row_tag = tag_name(&self.styles.get(row_style).tag).to_owned();
self.open_tag(row_block, row_style, &row_tag, None);
self.out.push('>');
for (index, cell) in row_cells.iter().enumerate() {
let Some(placed) = map.find(row, index).map(|found| map.cells()[found]) else {
continue;
};
let cell_width: f32 = widths[placed.columns()].iter().sum::<f32>() - rule;
let cell_style = self
.styles
.resolve(cell, Some(row_block.kind()), Some(row_style));
self.header_scope = match (placed.row, placed.column) {
(0, _) => Some("col"),
(_, 0) => Some("row"),
_ => None,
};
self.block(cell, cell_style, 0.0, None, (lists, cell_width.max(0.0)));
}
let _ = write!(self.out, "</{row_tag}>");
}
let _ = write!(self.out, "</tbody></{tag}>");
true
}
#[cfg(not(feature = "tables"))]
#[allow(
clippy::unused_self,
clippy::trivially_copy_pass_by_ref,
reason = "the same signature as the grid writer"
)]
fn table(
&mut self,
_block: &Block,
_style: (StyleId, &BlockStyle),
_margin: f32,
_frame: (usize, f32),
) -> bool {
false
}
fn open_tag(
&mut self,
block: &Block,
style: StyleId,
tag: &str,
marker: Option<(MarkerKind, usize)>,
) {
let defaults = &self.doc.schema().block(block.kind()).attrs;
let type_name = class_name(&self.doc.schema().block(block.kind()).name);
let _ = write!(
self.out,
"<{tag} class=\"cv-b cv-s{} cv-t-{type_name}",
style.index()
);
if let Some((kind, depth)) = marker {
let bullets = self.styles.theme().markers().bullets.len().max(1);
let _ = match kind {
MarkerKind::Bullet => write!(self.out, " cv-mk cv-mk-b{}", depth % bullets),
MarkerKind::Ordered => write!(self.out, " cv-mk cv-mk-o"),
MarkerKind::Task => write!(self.out, " cv-mk-t"),
MarkerKind::Disclosure => write!(self.out, " cv-mk cv-mk-d{depth}"),
};
}
self.out.push('"');
if cfg!(feature = "theme-extensions") {
self.accessibility(block);
}
attribute(&mut self.out, "data-id", &block.id().to_string());
if cfg!(feature = "tables") && self.styles.get(style).table == Some(TableRole::Cell) {
self.cell_spans(block, tag);
}
if matches!(block.content(), Content::Inline(_)) {
let dir = attr(block.attrs(), defaults, "dir")
.and_then(Value::as_str)
.filter(|dir| matches!(*dir, "ltr" | "rtl"))
.unwrap_or("auto");
attribute(&mut self.out, "dir", dir);
}
}
fn cell_spans(&mut self, cell: &Block, tag: &str) {
if let Some(scope) = self.header_scope.take().filter(|_| tag == "th") {
attribute(&mut self.out, "scope", scope);
}
for key in ["colspan", "rowspan"] {
let span = span_attr(self.doc, cell, key);
if span > 1 {
attribute(&mut self.out, key, &span.to_string());
}
}
}
fn inline_style(&self, block: &Block, resolved: &BlockStyle, margin: f32) -> String {
let defaults = &self.doc.schema().block(block.kind()).attrs;
let mut inline_style = String::new();
if margin > 0.0 {
let _ = write!(inline_style, "margin-top:{}px;", num(margin));
}
if resolved.marker == Some(MarkerKind::Ordered) {
let start = attr(block.attrs(), defaults, "start")
.and_then(Value::as_int)
.unwrap_or(1);
let _ = write!(inline_style, "counter-reset:cv-n {};", start - 1);
}
let align = attr(block.attrs(), defaults, "textAlign").and_then(Value::as_str);
if let Some(align) = align.filter(|align| ALIGNMENTS.contains(align))
&& matches!(block.content(), Content::Inline(_))
{
let _ = write!(inline_style, "text-align:{align};");
}
if let (Content::Leaf, LeafStyle::Slot { height }) = (block.content(), resolved.leaf) {
let height = attr_px(block.attrs(), "height").unwrap_or(height);
let _ = write!(inline_style, "height:{}px;", num(height));
}
inline_style
}
fn checkbox(&mut self, block: &Block, style: &BlockStyle) {
let defaults = &self.doc.schema().block(block.kind()).attrs;
let checked = attr(block.attrs(), defaults, "checked")
.and_then(Value::as_bool)
.unwrap_or(false);
let size = self.styles.theme().markers().checkbox;
let top = (style.text.line_height_px() - size) / 2.0;
let _ = write!(
self.out,
"<input type=\"checkbox\" class=\"cv-check\" disabled{} style=\"top:{}px\">",
if checked { " checked" } else { "" },
num(top)
);
}
fn accessibility(&mut self, block: &Block) {
let doc = self.doc;
let schema = doc.schema();
let defaults = &schema.block(block.kind()).attrs;
let string = |key| attr(block.attrs(), defaults, key).and_then(Value::as_str);
match &*schema.block(block.kind()).name {
"callout" => {
attribute(&mut self.out, "role", "note");
let label = match string("type") {
Some("blank") => None,
Some("warning") => Some("Warning"),
Some("error") => Some("Error"),
Some("success") => Some("Success"),
_ => Some("Info"),
};
if let Some(label) = label {
attribute(&mut self.out, "aria-label", label);
}
}
"blockMath" => {
attribute(&mut self.out, "role", "math");
attribute(
&mut self.out,
"aria-label",
string("latex").unwrap_or_default(),
);
}
_ => {}
}
}
fn inline(&mut self, inline: &InlineText, base: &TextStyle) {
let text = inline.as_str();
let mut open: Vec<Mark> = Vec::new();
let mut ends_with_break = text.is_empty();
for run in inline.runs() {
self.sync_marks(&mut open, run.marks.as_slice());
ends_with_break = self.run_content(text, &run, base);
}
self.sync_marks(&mut open, &[]);
if ends_with_break {
self.out.push_str("<br class=\"cv-br\">");
}
}
fn run_content(&mut self, text: &str, run: &RunRef<'_>, base: &TextStyle) -> bool {
if let Some(embed) = run.embed {
return self.embed(embed, base, run.marks);
}
escape_into(&mut self.out, slice(text, &run.range));
run.text.ends_with(crate::text::is_newline)
}
fn sync_marks(&mut self, open: &mut Vec<Mark>, marks: &[Mark]) {
let common = open
.iter()
.zip(marks)
.take_while(|(open_mark, mark)| open_mark == mark)
.count();
for mark in open.drain(common..).rev() {
let _ = write!(self.out, "</{}>", self.mark_tag(&mark));
}
for mark in &marks[common..] {
self.open_mark(mark);
open.push(mark.clone());
}
}
fn mark_tag(&self, mark: &Mark) -> String {
self.styles
.mark_tag(mark.kind)
.map_or("span", |tag| tag_name(tag))
.to_owned()
}
fn open_mark(&mut self, mark: &Mark) {
let tag = self.mark_tag(mark);
let name = class_name(&self.doc.schema().mark(mark.kind).name);
let _ = write!(self.out, "<{tag} class=\"cv-m cv-m-{name}\"");
let href = mark.attrs.get("href").and_then(Value::as_str);
if let Some(href) = href.filter(|href| tag == "a" && is_safe_link_url(href)) {
attribute(&mut self.out, "href", href);
}
let style = bindings_css(&self.styles, mark);
if !style.is_empty() {
attribute(&mut self.out, "style", &style);
}
self.out.push('>');
}
fn embed(&mut self, embed: &Embed, base: &TextStyle, marks: &canvist_model::MarkSet) -> bool {
let name = class_name(&self.doc.schema().embed(embed.kind).name);
let (width, height) = match self.styles.embed_style(embed.kind) {
Some(EmbedStyle::LineBreak) => {
self.out.push_str("<br>");
return true;
}
Some(label @ EmbedStyle::Label { .. }) => {
let text = label.label_text(&embed.attrs).unwrap_or_default();
self.label(embed, &name, &text);
return false;
}
Some(EmbedStyle::Box { width, height }) => (*width, *height),
None => {
let size = self.styles.run_style(base, marks).font.size;
(size, size)
}
};
let width = attr_px(&embed.attrs, "width").unwrap_or(width);
let height = attr_px(&embed.attrs, "height").unwrap_or(height);
let _ = write!(
self.out,
"<span class=\"cv-e cv-e-{name}\" style=\"width:{}px;height:{}px\"",
num(width),
num(height)
);
attribute(
&mut self.out,
"data-type",
&self.doc.schema().embed(embed.kind).name,
);
self.out.push_str("></span>");
false
}
fn label(&mut self, embed: &Embed, name: &str, text: &str) {
let _ = write!(self.out, "<span class=\"cv-e cv-e-{name}\" dir=\"auto\"");
attribute(
&mut self.out,
"data-type",
&self.doc.schema().embed(embed.kind).name,
);
self.out.push('>');
escape_into(&mut self.out, text);
self.out.push_str("</span>");
}
}
fn bindings_css(styles: &Styles, mark: &Mark) -> String {
let Some(rule) = styles.mark_rule(mark.kind) else {
return String::new();
};
rule.bindings
.iter()
.filter_map(|binding| binding_css(binding, mark))
.collect()
}
fn binding_css(binding: &AttrBinding, mark: &Mark) -> Option<String> {
let string = |key: &str| mark.attrs.get(key).and_then(Value::as_str);
match binding {
AttrBinding::Color(key) => {
string(key)
.and_then(Color::parse_hex)
.map(|color| format!("color:{color};"))
}
AttrBinding::Background(key) => {
string(key)
.and_then(Color::parse_hex)
.map(|color| format!("background-color:{color};"))
}
AttrBinding::FontFamily(key) => {
string(key)
.filter(|family| is_safe_family(family))
.map(|family| format!("font-family:{family};"))
}
AttrBinding::FontSize(key) => {
attr_px(&mark.attrs, key)
.filter(|size| *size > 0.0)
.map(|size| format!("font-size:{}px;", num(size)))
}
AttrBinding::LineHeight(key) => {
line_height(&mark.attrs, key).map(|height| format!("line-height:{};", num(height)))
}
AttrBinding::Motion(key) if cfg!(feature = "kinetic") => {
string(key).and_then(Motion::from_name).map(motion_css)
}
AttrBinding::Motion(_) => None,
}
}
fn motion_css(motion: Motion) -> String {
let timing = match motion {
Motion::Shake => "0.4s steps(4)",
Motion::Pulse => "1.25s ease-in-out",
Motion::Rainbow => "4s linear",
Motion::Wave | Motion::None => "1.1s ease-in-out",
};
format!(
"position:relative;animation:cv-k-{} {timing} infinite;",
motion.name()
)
}
const MOTION_CSS: &str = "@keyframes cv-k-wave{0%,100%{top:0}50%{top:-0.16em}}@keyframes \
cv-k-shake{0%{left:0;top:0}25%{left:0.05em;top:-0.04em}50%{left:-0.04em;\
top:0.05em}75%{left:0.04em;top:0.03em}100%{left:0;top:0}}@keyframes \
cv-k-pulse{0%,100%{opacity:1}50%{opacity:0.35}}@keyframes \
cv-k-rainbow{from{filter:hue-rotate(0deg)}to{filter:\
hue-rotate(360deg)}}@media (prefers-reduced-motion:reduce){.cv-doc \
[style*=\"cv-k-\"]{animation:none!important}}";
fn stylesheet(styles: &Styles, width: f32) -> String {
let theme = styles.theme();
let base = theme.base();
let mut css = String::new();
let _ = write!(
css,
".cv-doc{{box-sizing:content-box;width:{}px;margin:0;padding:0;background-color:{};\
{}white-space:pre-wrap;overflow-wrap:break-word;word-break:normal;line-break:strict;\
hyphens:manual;text-align:start;text-indent:0;text-transform:none;word-spacing:0;\
tab-size:{};font-kerning:normal;font-variant:normal;font-variant-ligatures:normal;\
font-feature-settings:normal;font-variation-settings:normal;font-size-adjust:none;\
font-stretch:normal;text-rendering:auto;text-wrap:wrap;text-autospace:no-autospace;\
text-spacing-trim:normal;-webkit-text-size-adjust:100%;text-size-adjust:100%}}",
num(width),
theme.chrome().background,
text_css(base),
theme.tab_size()
);
css.push_str(
".cv-doc :where(*){margin:0;padding:0;border:0 solid \
transparent;font:inherit;color:inherit;background:none;text-decoration:none;list-style:\
none;vertical-align:baseline;white-space:inherit;letter-spacing:inherit;box-sizing:\
content-box}",
);
css.push_str(".cv-doc :where(.cv-b){display:flow-root}");
css.push_str(".cv-doc :where(.cv-e){display:inline-block;white-space:nowrap;text-indent:0}");
for (id, style) in styles.all() {
let _ = write!(
css,
".cv-doc .cv-s{}{{{}",
id.index(),
text_css(&style.text)
);
box_css(&mut css, style);
css.push('}');
grid_css(&mut css, id, style);
}
marker_css(&mut css, theme);
let schema = styles.schema();
let moves = theme.mark_rules().iter().any(|rule| {
schema.mark_type(&rule.mark).is_some()
&& rule
.bindings
.iter()
.any(|binding| matches!(binding, AttrBinding::Motion(_)))
});
if moves {
css.push_str(MOTION_CSS);
}
let draws = theme.block_rules().iter().any(|rule| {
schema.block_type(&rule.block).is_some()
&& matches!(rule.patch.leaf, Some(LeafStyle::Ink { .. }))
});
if draws {
css.push_str(".cv-doc .cv-ink{display:block;width:100%;height:100%;overflow:visible}");
}
for rule in theme.mark_rules() {
let _ = write!(
css,
".cv-doc .cv-m-{}{{{}}}",
class_name(&rule.mark),
patch_css(&rule.patch)
);
}
for rule in theme.embed_rules() {
if let EmbedStyle::Label { patch, .. } = &rule.style {
let _ = write!(
css,
".cv-doc .cv-e-{}{{{}}}",
class_name(&rule.embed),
patch_css(patch)
);
}
}
css
}
fn text_css(style: &TextStyle) -> String {
let font = &style.font;
let mut css = format!(
"font-family:{};font-size:{}px;font-weight:{};font-style:{};line-height:{};color:{};\
letter-spacing:{}px;",
font.family,
num(font.size),
font.weight,
font_style(font.italic),
num(style.line_height),
style.color,
num(style.letter_spacing)
);
let decorations = style.decorations;
if decorations.any() {
css.push_str(&decoration_css(
decorations.underline || decorations.squiggle,
decorations.strike,
decorations.squiggle,
));
}
css
}
fn font_style(italic: bool) -> &'static str {
if italic { "italic" } else { "normal" }
}
fn decoration_css(underline: bool, strike: bool, wavy: bool) -> String {
let lines: Vec<&str> = [(underline, "underline"), (strike, "line-through")]
.into_iter()
.filter_map(|(on, name)| on.then_some(name))
.collect();
if lines.is_empty() {
return String::new();
}
format!(
"text-decoration-line:{};text-decoration-style:{};",
lines.join(" "),
if wavy { "wavy" } else { "solid" }
)
}
fn box_css(css: &mut String, style: &BlockStyle) {
let padding = style.padding;
if padding != crate::Insets::ZERO {
let _ = write!(
css,
"padding:{}px {}px {}px {}px;",
num(padding.top),
num(padding.right),
num(padding.bottom),
num(padding.left)
);
}
if let Some(border) = style.border_left {
let _ = write!(
css,
"border-left:{}px solid {};",
num(border.width),
border.color
);
}
if let Some(background) = style.background {
let _ = write!(css, "background-color:{background};");
}
if style.radius > 0.0 {
let _ = write!(css, "border-radius:{}px;", num(style.radius));
}
if let LeafStyle::Rule { thickness, color } = style.leaf {
let _ = write!(css, "height:{}px;background-color:{color};", num(thickness));
}
match style.table {
Some(TableRole::Table { .. }) => {
css.push_str("display:table;table-layout:fixed;border-collapse:collapse;");
}
Some(TableRole::Row) => css.push_str("display:table-row;"),
Some(TableRole::Cell) => {
css.push_str("display:table-cell;vertical-align:top;text-align:start;");
}
None => {}
}
}
fn grid_css(css: &mut String, id: StyleId, style: &BlockStyle) {
if let Some(TableRole::Table { rule }) = style.table {
let _ = write!(
css,
".cv-doc .cv-s{}>tbody>tr>.cv-b{{border:{}px solid {}}}",
id.index(),
num(rule.width),
rule.color
);
}
}
fn marker_css(css: &mut String, theme: &Theme) {
let markers = theme.markers();
let color = markers
.color
.map(|color| format!("color:{color};"))
.unwrap_or_default();
let gap = num(markers.gap);
let size = num(markers.checkbox);
let _ = write!(
css,
".cv-doc .cv-mk,.cv-doc .cv-mk-t{{position:relative}}.cv-doc \
.cv-mk::before{{position:absolute;top:0;right:100%;margin-right:{gap}px;white-space:\
nowrap;{color}}}.cv-doc .cv-mk-o{{counter-increment:cv-n}}.cv-doc \
.cv-mk-o::before{{content:counter(cv-n) \".\"}}"
);
for open in [false, true] {
let _ = write!(
css,
".cv-doc .cv-mk-d{}::before{{content:\"{}\"}}",
usize::from(open),
crate::document::disclosure(open)
);
}
for (index, bullet) in markers.bullets.iter().enumerate() {
let _ = write!(
css,
".cv-doc .cv-mk-b{index}::before{{content:\"{}\"}}",
css_string(bullet)
);
}
let _ = write!(
css,
".cv-doc .cv-check{{appearance:none;-webkit-appearance:none;position:absolute;right:100%;\
margin:0 {gap}px 0 0;width:{size}px;height:{size}px;box-sizing:border-box;border:1px \
solid #8a8580;border-radius:3px;background:none}}.cv-doc \
.cv-check:checked{{background:#2e2b29}}"
);
}
fn patch_css(patch: &TextPatch) -> String {
let declarations = [
patch
.family
.as_ref()
.map(|family| format!("font-family:{family};")),
patch.size.map(|size| {
match size {
FontSize::Px(px) => format!("font-size:{}px;", num(px)),
FontSize::Em(em) => format!("font-size:{}em;", num(em)),
}
}),
patch.weight.map(|weight| format!("font-weight:{weight};")),
patch
.italic
.map(|italic| format!("font-style:{};", font_style(italic))),
patch
.line_height
.map(|line_height| format!("line-height:{};", num(line_height))),
patch.color.map(|color| format!("color:{color};")),
patch
.background
.map(|background| format!("background-color:{background};")),
patch
.letter_spacing
.map(|spacing| format!("letter-spacing:{}px;", num(spacing))),
patch
.baseline_shift
.map(|em| format!("vertical-align:{}em;", num(em))),
];
let mut css: String = declarations.into_iter().flatten().collect();
let underline = patch.underline.unwrap_or(false) || patch.squiggle.unwrap_or(false);
css.push_str(&decoration_css(
underline,
patch.strike.unwrap_or(false),
patch.squiggle.unwrap_or(false),
));
css
}
fn tag_name(tag: &str) -> &str {
let valid = !tag.is_empty()
&& tag
.bytes()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit())
&& tag.as_bytes()[0].is_ascii_lowercase();
if valid { tag } else { "div" }
}
fn is_void(tag: &str) -> bool {
matches!(tag, "hr" | "br" | "img" | "input")
}
fn class_name(name: &str) -> String {
name.chars()
.map(|character| {
if character.is_ascii_alphanumeric() || matches!(character, '-' | '_') {
character
} else {
'_'
}
})
.collect()
}
fn css_string(value: &str) -> String {
value.replace('\\', "\\\\").replace('"', "\\\"")
}
fn is_safe_family(family: &str) -> bool {
family.chars().all(|character| {
character.is_alphanumeric() || matches!(character, ' ' | ',' | '-' | '_' | '"' | '\'')
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rejects_script_urls() {
assert!(is_safe_link_url("https://example.com"));
assert!(is_safe_link_url("/docs#top"));
assert!(is_safe_link_url("mailto:a@b.c"));
assert!(!is_safe_link_url("javascript:alert(1)"));
assert!(!is_safe_link_url(" JavaScript:alert(1)"));
assert!(!is_safe_link_url("data:text/html,x"));
}
#[test]
fn sanitizes_names() {
assert_eq!(tag_name("h2"), "h2");
assert_eq!(tag_name("script onload"), "div");
assert_eq!(class_name("bullet list"), "bullet_list");
assert!(!is_safe_family("x;background:url(evil)"));
}
}