use std::collections::HashSet;
use crate::entities;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RichDisplay {
pub pictures: bool,
pub sounds: bool,
pub show_pictures: bool,
pub play_sounds: bool,
pub play_music: bool,
pub links: bool,
}
impl RichDisplay {
pub fn full() -> Self {
RichDisplay {
pictures: true,
sounds: true,
show_pictures: true,
play_sounds: true,
play_music: true,
links: true,
}
}
}
pub type Attr = (String, Option<String>);
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Item {
Text(String),
Open(String, Vec<Attr>),
Close(String),
Void(String, Vec<Attr>),
Title(String),
Input(String),
}
impl Item {
pub fn markup(&self) -> String {
match self {
Item::Text(text) => escape(text, false),
Item::Open(name, attrs) => format!("<{name}{}>", attrs_markup(attrs)),
Item::Close(name) => {
let end = if is_block(name) { "\n" } else { "" };
format!("</{name}>{end}")
}
Item::Void(name, attrs) => {
let end = if name == "br" || name == "hr" || name == "body" {
"\n"
} else {
""
};
format!("<{name}{}>{end}", attrs_markup(attrs))
}
Item::Title(text) => format!("<title>{}</title>\n", escape(text, false)),
Item::Input(text) => format!("<kbd>{}</kbd>\n", escape(text, false)),
}
}
}
fn attrs_markup(attrs: &[Attr]) -> String {
let mut out = String::new();
for (name, value) in attrs {
out.push(' ');
out.push_str(name);
if let Some(value) = value {
out.push_str("=\"");
out.push_str(&escape(value, true));
out.push('"');
}
}
out
}
fn escape(text: &str, attr: bool) -> String {
let mut out = String::with_capacity(text.len());
for c in text.chars() {
match c {
'&' => out.push_str("&"),
'<' => out.push_str("<"),
'>' if attr => out.push_str(">"),
'"' if attr => out.push_str("""),
'\u{a0}' => out.push_str(" "),
_ => out.push(c),
}
}
out
}
const VOID: &[&str] = &[
"br", "img", "hr", "area", "wbr", "sound", "body", "tab", "basefont",
];
const CONTAINERS: &[&str] = &[
"a",
"address",
"b",
"big",
"blockquote",
"caption",
"center",
"cite",
"code",
"dd",
"dfn",
"dir",
"div",
"dl",
"dt",
"em",
"font",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"i",
"kbd",
"li",
"map",
"menu",
"ol",
"p",
"pre",
"q",
"s",
"samp",
"small",
"strike",
"strong",
"sub",
"sup",
"table",
"td",
"th",
"tr",
"tt",
"u",
"ul",
"var",
"span",
"figure",
"figcaption",
"aboutbox",
"banner",
];
const DROPPED_WITH_CONTENTS: &[&str] = &[
"script", "style", "iframe", "object", "embed", "noscript", "template", "frameset", "frame",
"form", "select", "textarea", "button", "head", "svg", "math",
];
const GLOBAL_ATTRS: &[&str] = &["class", "style", "title", "lang", "dir"];
fn element_attrs(name: &str) -> &'static [&'static str] {
match name {
"a" => &["href", "plain", "append", "noenter"],
"img" => &[
"src", "alt", "width", "height", "border", "align", "hspace", "vspace", "usemap",
"ismap", "asrc", "hsrc",
],
"area" => &[
"shape", "coords", "href", "nohref", "alt", "append", "noenter",
],
"map" => &["name"],
"font" => &["face", "size", "color", "bgcolor"],
"basefont" => &["size", "face", "color"],
"table" => &[
"border",
"cellpadding",
"cellspacing",
"width",
"height",
"align",
"bgcolor",
"background",
],
"tr" => &["align", "valign", "bgcolor", "background"],
"td" | "th" => &[
"align",
"valign",
"width",
"height",
"colspan",
"rowspan",
"nowrap",
"bgcolor",
"background",
],
"caption" => &["align", "valign"],
"p" | "div" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "center" => &["align"],
"br" => &["clear", "height"],
"hr" => &["width", "size", "align", "noshade", "src"],
"ol" => &["start", "type", "compact"],
"ul" | "dir" | "menu" => &["type", "compact"],
"li" => &["type", "value"],
"dl" => &["compact"],
"body" => &[
"bgcolor",
"text",
"link",
"vlink",
"alink",
"hlink",
"background",
],
"sound" => &[
"src",
"layer",
"repeat",
"random",
"cancel",
"interrupt",
"sequence",
"alt",
"volume",
"fadein",
"fadeout",
"gapafter",
],
"tab" => &["id", "to", "align", "indent", "multiple", "dp"],
"banner" => &[
"id",
"align",
"width",
"height",
"border",
"remove",
"removeall",
],
_ => &[],
}
}
const RESOURCE_ATTRS: &[&str] = &["src", "asrc", "hsrc", "background"];
const CSS_PROPERTIES: &[&str] = &[
"color",
"background",
"background-color",
"background-image",
"background-repeat",
"background-position",
"background-size",
"font",
"font-family",
"font-size",
"font-weight",
"font-style",
"font-variant",
"text-align",
"text-decoration",
"text-transform",
"text-indent",
"line-height",
"letter-spacing",
"word-spacing",
"white-space",
"margin",
"margin-top",
"margin-right",
"margin-bottom",
"margin-left",
"padding",
"padding-top",
"padding-right",
"padding-bottom",
"padding-left",
"border",
"border-top",
"border-right",
"border-bottom",
"border-left",
"border-width",
"border-style",
"border-color",
"border-radius",
"width",
"height",
"min-width",
"max-width",
"min-height",
"max-height",
"display",
"float",
"clear",
"list-style",
"list-style-type",
"list-style-position",
"vertical-align",
"flex-direction",
"flex-wrap",
"justify-content",
"align-items",
"gap",
"border-collapse",
"border-spacing",
"image-rendering",
];
const DISPLAY_VALUES: &[&str] = &["block", "inline", "inline-block", "none", "flex"];
fn is_block(name: &str) -> bool {
matches!(
name,
"p" | "div"
| "blockquote"
| "center"
| "h1"
| "h2"
| "h3"
| "h4"
| "h5"
| "h6"
| "ul"
| "ol"
| "dl"
| "li"
| "dt"
| "dd"
| "dir"
| "menu"
| "table"
| "tr"
| "pre"
| "figure"
| "figcaption"
| "address"
| "aboutbox"
| "banner"
| "caption"
)
}
fn closes_paragraph(name: &str) -> bool {
matches!(
name,
"p" | "div"
| "blockquote"
| "center"
| "h1"
| "h2"
| "h3"
| "h4"
| "h5"
| "h6"
| "ul"
| "ol"
| "dl"
| "dir"
| "menu"
| "table"
| "pre"
| "figure"
| "address"
| "hr"
)
}
fn is_resource_name(name: &str) -> bool {
!name.is_empty()
&& !name.contains(':')
&& !name.contains('\\')
&& !name.starts_with('/')
&& !name.split('/').any(|part| part == "..")
}
fn is_command_link(href: &str) -> bool {
let scheme_end = href.find(':');
match scheme_end {
None => true,
Some(i) => href[..i].contains(' ') || href[..i].is_empty(),
}
}
fn check_style(style: &str) -> Option<String> {
let kept = check_declarations(style, CSS_PROPERTIES);
if kept.is_empty() {
None
} else {
Some(kept.join("; "))
}
}
fn check_declarations(declarations: &str, allowed: &[&str]) -> Vec<String> {
let mut kept = Vec::new();
for decl in declarations.split(';') {
let Some((prop, value)) = decl.split_once(':') else {
continue;
};
let prop = prop.trim().to_ascii_lowercase();
let mut value = value.trim();
if let Some(i) = value.to_ascii_lowercase().find("!important") {
value = value[..i].trim_end();
}
if value.is_empty() || !allowed.contains(&prop.as_str()) {
continue;
}
if !css_value_allowed(&prop, value) {
continue;
}
kept.push(format!("{prop}: {value}"));
}
kept
}
const FONT_FACE_PROPERTIES: &[&str] = &[
"font-family",
"src",
"font-weight",
"font-style",
"font-display",
"unicode-range",
];
const MEDIA_WORDS: &[&str] = &[
"all",
"screen",
"print",
"and",
"not",
"only",
"min-width",
"max-width",
"width",
"orientation",
"portrait",
"landscape",
"prefers-color-scheme",
"light",
"dark",
"prefers-reduced-motion",
"reduce",
"no-preference",
"prefers-contrast",
"more",
"less",
];
pub fn check_stylesheet(css: &str, scope: &str) -> String {
let css = strip_comments(css);
let mut out = String::new();
check_rules(&css, scope, &mut out, 0);
out
}
fn strip_comments(css: &str) -> String {
let mut out = String::with_capacity(css.len());
let mut rest = css;
while let Some(i) = rest.find("/*") {
out.push_str(&rest[..i]);
match rest[i + 2..].find("*/") {
Some(j) => rest = &rest[i + 2 + j + 2..],
None => return out,
}
}
out.push_str(rest);
out
}
fn check_rules(css: &str, scope: &str, out: &mut String, depth: usize) {
let mut rest = css;
loop {
rest = rest.trim_start();
if rest.is_empty() {
return;
}
let brace = rest.find('{');
if rest.starts_with('@')
&& let Some(semi) = rest.find(';')
&& brace.is_none_or(|b| semi < b)
{
rest = &rest[semi + 1..];
continue;
}
let Some(open) = brace else {
return;
};
let prelude = rest[..open].trim();
let Some(len) = block_length(&rest[open..]) else {
return;
};
let body = &rest[open + 1..open + len - 1];
rest = &rest[open + len..];
if let Some(at) = prelude.strip_prefix('@') {
let (keyword, query) = at.split_once(char::is_whitespace).unwrap_or((at, ""));
match keyword.to_ascii_lowercase().as_str() {
"media" if depth < 4 && media_query_allowed(query) => {
let mut inner = String::new();
check_rules(body, scope, &mut inner, depth + 1);
if !inner.is_empty() {
out.push_str(&format!("@media {} {{\n{inner}}}\n", query.trim()));
}
}
"font-face" => {
let kept = check_declarations(body, FONT_FACE_PROPERTIES);
if kept.iter().any(|d| d.starts_with("src:")) {
out.push_str(&format!("@font-face {{ {} }}\n", kept.join("; ")));
}
}
_ => {}
}
continue;
}
let selectors = scope_selectors(prelude, scope);
let kept = check_declarations(body, CSS_PROPERTIES);
if !selectors.is_empty() && !kept.is_empty() {
out.push_str(&format!("{selectors} {{ {} }}\n", kept.join("; ")));
}
}
}
fn block_length(css: &str) -> Option<usize> {
let mut depth = 0;
for (i, c) in css.char_indices() {
match c {
'{' => depth += 1,
'}' => {
depth -= 1;
if depth == 0 {
return Some(i + 1);
}
}
_ => {}
}
}
None
}
fn media_query_allowed(query: &str) -> bool {
query
.split(|c: char| c.is_whitespace() || "(),:".contains(c))
.filter(|word| !word.is_empty())
.all(|word| {
let word = word.to_ascii_lowercase();
MEDIA_WORDS.contains(&word.as_str())
|| word.chars().all(|c| c.is_ascii_digit() || c == '.')
|| ["px", "em", "rem", "ch"].iter().any(|unit| {
word.strip_suffix(unit).is_some_and(|n| {
!n.is_empty() && n.chars().all(|c| c.is_ascii_digit() || c == '.')
})
})
})
}
fn scope_selectors(prelude: &str, scope: &str) -> String {
let mut kept = Vec::new();
for selector in prelude.split(',') {
let selector = selector.trim();
if selector.is_empty() || selector.contains(['<', '\\', ';', '@']) {
continue;
}
let (first, rest) = match selector.find(|c: char| c.is_whitespace() || ">+~".contains(c)) {
Some(i) => (&selector[..i], &selector[i..]),
None => (selector, ""),
};
let first_lower = first.to_ascii_lowercase();
let scoped = if ["body", "html", ":root"].contains(&first_lower.as_str()) {
format!("{scope}{rest}")
} else if let Some(more) = first_lower
.strip_prefix("body")
.filter(|m| m.starts_with(['.', ':', '[']))
{
format!("{scope}{}{rest}", &first[first.len() - more.len()..])
} else {
format!("{scope} {selector}")
};
kept.push(scoped);
}
kept.join(", ")
}
fn css_value_allowed(prop: &str, value: &str) -> bool {
let lower = value.to_ascii_lowercase();
if lower.contains("expression")
|| lower.contains("javascript:")
|| lower.contains('\\')
|| lower.contains('<')
|| lower.contains("/*")
|| lower.contains('@')
{
return false;
}
if prop == "display" {
let v = lower.trim_end_matches("!important").trim();
return DISPLAY_VALUES.contains(&v);
}
let mut rest = lower.as_str();
let mut orig = value;
while let Some(i) = rest.find("url(") {
let after = &orig[i + 4..];
let Some(close) = after.find(')') else {
return false;
};
let target = after[..close].trim().trim_matches(['"', '\'']);
if !is_resource_name(target) {
return false;
}
rest = &rest[i + 4 + close + 1..];
orig = &orig[i + 4 + close + 1..];
}
true
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum State {
Text,
Tag,
Entity,
Comment,
}
#[derive(Debug)]
pub struct Checker {
state: State,
tag: String,
quote: Option<char>,
entity: String,
open: Vec<String>,
dropping: Option<(String, usize)>,
title: Option<String>,
log_depth: usize,
case_next: Option<bool>,
at_line_start: bool,
items: Vec<Item>,
aliases: Vec<(String, String)>,
warned: HashSet<String>,
}
impl Default for Checker {
fn default() -> Self {
Self::new()
}
}
impl Checker {
pub fn new() -> Self {
Checker {
state: State::Text,
tag: String::new(),
quote: None,
entity: String::new(),
open: Vec::new(),
dropping: None,
title: None,
log_depth: 0,
case_next: None,
at_line_start: true,
items: Vec::new(),
aliases: Vec::new(),
warned: HashSet::new(),
}
}
pub fn write(&mut self, markup: &str) {
for c in markup.chars() {
self.read_char(c);
}
}
pub fn input(&mut self, text: &str) {
self.push(Item::Input(text.to_owned()));
self.at_line_start = true;
}
pub fn take(&mut self) -> Vec<Item> {
std::mem::take(&mut self.items)
}
pub fn dropped(&self) -> impl Iterator<Item = &str> {
self.warned.iter().map(String::as_str)
}
fn read_char(&mut self, c: char) {
match self.state {
State::Tag => self.read_tag_char(c),
State::Entity => self.read_entity_char(c),
State::Comment => {
self.tag.push(c);
if self.tag.ends_with("-->") {
self.tag.clear();
self.state = State::Text;
}
}
State::Text => match c {
'<' => {
self.state = State::Tag;
self.tag.clear();
self.quote = None;
}
'&' => {
self.state = State::Entity;
self.entity.clear();
}
'\n' => self.line_break(),
'\u{b}' => self.blank_line(),
'\u{f}' => self.case_next = Some(true),
'\u{e}' => self.case_next = Some(false),
'\t' => self.text_char('\u{2003}'),
'\u{15}' => self.text_char('\u{a0}'),
c if (c as u32) < 0x20 => {}
c => self.text_char(c),
},
}
}
fn read_tag_char(&mut self, c: char) {
if self.tag.is_empty() && c != '/' && c != '!' && !c.is_ascii_alphabetic() {
self.state = State::Text;
self.text_char('<');
self.read_char(c);
return;
}
if self.tag == "!--" {
self.state = State::Comment;
self.tag.clear();
self.tag.push_str("--");
self.read_char(c);
return;
}
match self.quote {
Some(q) if c == q => self.quote = None,
Some(_) => {}
None if c == '"' || c == '\'' => {
if self.tag.trim_end().ends_with('=') {
self.quote = Some(c);
}
}
None if c == '>' => {
let tag = std::mem::take(&mut self.tag);
self.state = State::Text;
self.tag_done(&tag);
return;
}
None => {}
}
self.tag.push(c);
}
fn read_entity_char(&mut self, c: char) {
if c == ';' {
let name = std::mem::take(&mut self.entity);
self.state = State::Text;
match decode_entity(&name) {
Some(ch) => self.text_char(ch),
None => {
self.text_str(&format!("&{name};"));
}
}
return;
}
let ok = c.is_ascii_alphanumeric() || (c == '#' && self.entity.is_empty());
if ok && self.entity.len() < 12 {
self.entity.push(c);
return;
}
let name = std::mem::take(&mut self.entity);
self.state = State::Text;
self.text_str(&format!("&{name}"));
self.read_char(c);
}
fn text_char(&mut self, c: char) {
match self.case_next {
Some(upper) if c.is_alphabetic() => {
self.case_next = None;
let changed: String = if upper {
c.to_uppercase().collect()
} else {
c.to_lowercase().collect()
};
for c in changed.chars() {
self.text_char_raw(c);
}
}
_ => self.text_char_raw(c),
}
}
fn text_char_raw(&mut self, c: char) {
if let Some(title) = &mut self.title {
title.push(c);
return;
}
if self.dropping.is_some() || self.log_depth > 0 {
return;
}
if self.at_line_start && c == ' ' {
return;
}
self.at_line_start = false;
if let Some(Item::Text(text)) = self.items.last_mut() {
text.push(c);
} else {
self.items.push(Item::Text(c.to_string()));
}
}
fn text_str(&mut self, s: &str) {
for c in s.chars() {
self.text_char(c);
}
}
fn line_break(&mut self) {
if self.dropping.is_some() || self.log_depth > 0 || self.title.is_some() {
return;
}
if self.at_line_start && !self.open.iter().any(|name| name == "pre") {
return;
}
self.push(Item::Void("br".into(), Vec::new()));
self.at_line_start = true;
}
fn blank_line(&mut self) {
if self.dropping.is_some() || self.log_depth > 0 || self.title.is_some() {
return;
}
if !self.at_line_start {
self.push(Item::Void("br".into(), Vec::new()));
}
self.push(Item::Void("br".into(), Vec::new()));
self.at_line_start = true;
}
fn push(&mut self, item: Item) {
self.items.push(item);
}
fn tag_done(&mut self, tag: &str) {
let (end, name, attrs) = parse_tag(tag);
if name.is_empty() || name.starts_with('!') || name.starts_with('?') {
return;
}
if let Some((dropped, depth)) = &mut self.dropping {
if *dropped == name {
if end {
if *depth == 0 {
self.dropping = None;
} else {
*depth -= 1;
}
} else {
*depth += 1;
}
}
return;
}
match name.as_str() {
"title" => {
if end {
if let Some(title) = self.title.take() {
self.push(Item::Title(title.trim().to_owned()));
self.at_line_start = true;
}
} else {
self.title = Some(String::new());
}
return;
}
"log" => {
if end {
self.log_depth = self.log_depth.saturating_sub(1);
} else {
self.log_depth += 1;
}
return;
}
"nolog" | "wrap" | "hiliteon" | "hiliteoff" | "noenter" => return,
_ => {}
}
if self.title.is_some() || self.log_depth > 0 {
return;
}
if DROPPED_WITH_CONTENTS.contains(&name.as_str()) {
if !end {
self.warned.insert(name.clone());
self.dropping = Some((name, 0));
}
return;
}
let (page_name, extra_style): (&str, Option<&str>) = match name.as_str() {
"bq" => ("blockquote", None),
"credit" => ("cite", None),
"listing" | "xmp" => ("pre", None),
"nobr" => ("span", Some("white-space: nowrap")),
other => (other, None),
};
let page_name = page_name.to_owned();
if end {
self.end_tag(&name, &page_name);
return;
}
let is_void = VOID.contains(&page_name.as_str());
if !is_void && !CONTAINERS.contains(&page_name.as_str()) {
self.warned.insert(page_name);
return;
}
let mut attrs = self.check_attrs(&page_name, attrs);
if let Some(style) = extra_style {
attrs.push(("style".into(), Some(style.to_owned())));
}
let is_void = is_void
|| (page_name == "banner"
&& attrs.iter().any(|(n, _)| n == "remove" || n == "removeall"));
if page_name == "img" && !attrs.iter().any(|(n, _)| n == "src") {
if let Some((_, Some(alt))) = attrs.iter().find(|(n, _)| n == "alt") {
let alt = alt.clone();
self.text_str(&alt);
}
return;
}
if page_name == "sound" && !attrs.iter().any(|(n, _)| n == "src" || n == "cancel") {
return;
}
if is_void {
if closes_paragraph(&page_name) {
self.close_paragraph();
}
let breaks = page_name == "br" || page_name == "hr";
self.push(Item::Void(page_name, attrs));
if breaks {
self.at_line_start = true;
}
return;
}
if closes_paragraph(&page_name) {
self.close_paragraph();
}
self.close_siblings(&page_name);
if page_name != name {
self.aliases.push((name, page_name.clone()));
}
self.open.push(page_name.clone());
let starts_line = is_block(&page_name);
self.push(Item::Open(page_name, attrs));
if starts_line {
self.at_line_start = true;
}
}
fn end_tag(&mut self, written: &str, page_name: &str) {
let target = self
.aliases
.iter()
.rev()
.find(|(w, _)| w == written)
.map(|(_, p)| p.clone())
.unwrap_or_else(|| page_name.to_owned());
let Some(at) = self.open.iter().rposition(|n| *n == target) else {
return;
};
while self.open.len() > at {
self.close_top();
}
if let Some(i) = self.aliases.iter().rposition(|(w, _)| w == written) {
self.aliases.remove(i);
}
}
fn close_top(&mut self) {
if let Some(name) = self.open.pop() {
let ends_line = is_block(&name);
self.push(Item::Close(name));
if ends_line {
self.at_line_start = true;
}
}
}
fn close_paragraph(&mut self) {
if let Some(at) = self.open.iter().rposition(|n| n == "p") {
if self.open[at..]
.iter()
.all(|n| !matches!(n.as_str(), "td" | "th" | "li" | "blockquote" | "div"))
{
while self.open.len() > at {
self.close_top();
}
}
}
}
fn close_siblings(&mut self, name: &str) {
let (ends, within): (&[&str], &[&str]) = match name {
"li" => (&["li"], &["ul", "ol", "dir", "menu"]),
"dt" | "dd" => (&["dt", "dd"], &["dl"]),
"tr" => (&["tr", "td", "th"], &["table"]),
"td" | "th" => (&["td", "th"], &["tr", "table"]),
_ => return,
};
let Some(at) = self
.open
.iter()
.rposition(|n| ends.contains(&n.as_str()) || within.contains(&n.as_str()))
else {
return;
};
if ends.contains(&self.open[at].as_str()) {
while self.open.len() > at {
self.close_top();
}
}
}
fn check_attrs(&mut self, element: &str, attrs: Vec<Attr>) -> Vec<Attr> {
let own = element_attrs(element);
let mut kept = Vec::new();
for (name, value) in attrs {
if !GLOBAL_ATTRS.contains(&name.as_str()) && !own.contains(&name.as_str()) {
if name.starts_with("on") {
self.warned.insert(format!("{element} {name}"));
}
continue;
}
let value = match (name.as_str(), value) {
("style", Some(v)) => match check_style(&v) {
Some(v) => Some(v),
None => continue,
},
("href", Some(v)) => {
if !is_command_link(&v) {
self.warned.insert(format!("{element} href"));
continue;
}
Some(v)
}
(n, Some(v)) if RESOURCE_ATTRS.contains(&n) => {
if !is_resource_name(&v) {
self.warned.insert(format!("{element} {n}"));
continue;
}
Some(v)
}
(_, v) => v,
};
kept.push((name, value));
}
kept
}
}
fn parse_tag(tag: &str) -> (bool, String, Vec<Attr>) {
let mut rest = tag.trim();
let end = rest.starts_with('/');
if end {
rest = &rest[1..];
}
let rest = rest.trim_end_matches('/');
let name_len = rest
.find(|c: char| c.is_whitespace() || c == '/')
.unwrap_or(rest.len());
let name = rest[..name_len].to_ascii_lowercase();
let mut attrs = Vec::new();
let chars: Vec<char> = rest[name_len..].chars().collect();
let mut i = 0;
while i < chars.len() {
while i < chars.len() && (chars[i].is_whitespace() || chars[i] == '/') {
i += 1;
}
let start = i;
while i < chars.len() && !chars[i].is_whitespace() && chars[i] != '=' {
i += 1;
}
if start == i {
i += 1;
continue;
}
let attr: String = chars[start..i]
.iter()
.collect::<String>()
.to_ascii_lowercase();
while i < chars.len() && chars[i].is_whitespace() {
i += 1;
}
if i < chars.len() && chars[i] == '=' {
i += 1;
while i < chars.len() && chars[i].is_whitespace() {
i += 1;
}
let value: String = if i < chars.len() && (chars[i] == '"' || chars[i] == '\'') {
let q = chars[i];
i += 1;
let vs = i;
while i < chars.len() && chars[i] != q {
i += 1;
}
let v = chars[vs..i].iter().collect();
i += 1;
v
} else {
let vs = i;
while i < chars.len() && !chars[i].is_whitespace() {
i += 1;
}
chars[vs..i].iter().collect()
};
attrs.push((attr, Some(decode_entities(&value))));
} else {
attrs.push((attr, None));
}
}
(end, name, attrs)
}
fn decode_entity(name: &str) -> Option<char> {
if let Some(num) = name.strip_prefix('#') {
let code = if let Some(hex) = num.strip_prefix(['x', 'X']) {
u32::from_str_radix(hex, 16).ok()?
} else {
num.parse().ok()?
};
return char::from_u32(code);
}
entities::lookup(name)
}
fn decode_entities(value: &str) -> String {
let mut out = String::with_capacity(value.len());
let mut rest = value;
while let Some(i) = rest.find('&') {
out.push_str(&rest[..i]);
let after = &rest[i + 1..];
match after.find(';') {
Some(j) if j <= 12 => match decode_entity(&after[..j]) {
Some(c) => {
out.push(c);
rest = &after[j + 1..];
}
None => {
out.push('&');
rest = after;
}
},
_ => {
out.push('&');
rest = after;
}
}
}
out.push_str(rest);
out
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Event {
Main(Item),
PaneCreated(u32),
PaneDeleted(u32),
PaneCleared(u32),
Pane(u32, Vec<Item>),
}
impl Event {
pub fn markup(&self) -> String {
match self {
Event::Main(item) => item.markup(),
Event::PaneCreated(id) => format!("<pane id=\"{id}\" create></pane>\n"),
Event::PaneDeleted(id) => format!("<pane id=\"{id}\" delete></pane>\n"),
Event::PaneCleared(id) => format!("<pane id=\"{id}\" clear></pane>\n"),
Event::Pane(id, items) => {
let text: String = items.iter().map(Item::markup).collect();
format!("<pane id=\"{id}\">{text}</pane>\n")
}
}
}
}
#[derive(Debug, Default)]
pub struct Document {
main: Checker,
panes: std::collections::HashMap<u32, Checker>,
written: Vec<u32>,
events: Vec<Event>,
}
impl Document {
pub fn new() -> Self {
Self::default()
}
pub fn write(&mut self, markup: &str) {
self.show_panes();
self.main.write(markup);
self.take_main();
}
pub fn input(&mut self, text: &str) {
self.show_panes();
self.main.input(text);
self.take_main();
}
pub fn pane_write(&mut self, id: u32, markup: &str) {
self.panes.entry(id).or_default().write(markup);
if !self.written.contains(&id) {
self.written.push(id);
}
}
pub fn pane_flush(&mut self) {
self.show_panes();
}
pub fn pane_created(&mut self, id: u32) {
self.show_panes();
self.events.push(Event::PaneCreated(id));
}
pub fn pane_deleted(&mut self, id: u32) {
self.show_panes();
self.panes.remove(&id);
self.events.push(Event::PaneDeleted(id));
}
pub fn pane_cleared(&mut self, id: u32) {
self.show_panes();
self.panes.insert(id, Checker::new());
self.events.push(Event::PaneCleared(id));
}
pub fn take(&mut self) -> Vec<Event> {
std::mem::take(&mut self.events)
}
pub fn take_text(&mut self) -> String {
self.take().iter().map(Event::markup).collect()
}
fn take_main(&mut self) {
let items = self.main.take();
self.events.extend(items.into_iter().map(Event::Main));
}
fn show_panes(&mut self) {
for id in std::mem::take(&mut self.written) {
if let Some(checker) = self.panes.get_mut(&id) {
let items = checker.take();
if !items.is_empty() {
self.events.push(Event::Pane(id, items));
}
}
}
}
}
pub fn check(markup: &str) -> String {
let mut checker = Checker::new();
checker.write(markup);
checker.take().iter().map(Item::markup).collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn plain_text_and_entities() {
assert_eq!(
check("Fish & chips—! &bogus; a < b"),
"Fish & chips\u{2014}! &bogus; a < b"
);
}
#[test]
fn control_characters() {
assert_eq!(
check("one\ntwo\u{b}\u{f}three \u{e}FOUR"),
"one<br>\ntwo<br>\n<br>\nThree fOUR"
);
}
#[test]
fn tads_tags_become_the_pages() {
assert_eq!(
check("<bq>Quoted.</bq><nobr>12:30</nobr>"),
"<blockquote>Quoted.</blockquote>\n<span style=\"white-space: nowrap\">12:30</span>"
);
}
#[test]
fn a_newline_at_the_start_of_a_line_adds_nothing() {
assert_eq!(check("one\n\n\ntwo"), "one<br>\ntwo");
assert_eq!(check("<div>a</div>\nb"), "<div>a</div>\nb");
assert_eq!(check("one\u{b}two"), "one<br>\n<br>\ntwo");
assert_eq!(check("<pre>a\n\nb</pre>"), "<pre>a<br>\n<br>\nb</pre>\n");
}
#[test]
fn a_banner_removed_holds_nothing() {
assert_eq!(
check("<banner id=map align=left width=20%>Map</banner><banner remove id=map>After."),
"<banner id=\"map\" align=\"left\" width=\"20%\">Map</banner>\n<banner remove id=\"map\">After."
);
}
#[test]
fn the_pages_own_elements_are_kept() {
assert_eq!(
check(
"<title>The Skull</title><sound src='drip.wav' layer=ambient random=3><body bgcolor=black>Dark."
),
"<title>The Skull</title>\n<sound src=\"drip.wav\" layer=\"ambient\" random=\"3\"><body bgcolor=\"black\">\nDark."
);
}
#[test]
fn scripts_and_handlers_are_dropped() {
assert_eq!(
check(
"A<script>alert('x')</script>B<b onclick=\"steal()\">C</b><iframe src=x>D</iframe>E"
),
"AB<b>C</b>E"
);
}
#[test]
fn only_the_games_resources_and_commands() {
assert_eq!(
check(
"<img src='http://evil/x.png' alt='a cave'><img src='../x.png'><img src='cave.jpg'>"
),
"a cave<img src=\"cave.jpg\">"
);
assert_eq!(
check("<a href='north'>north</a> <a href='https://example.com'>web</a>"),
"<a href=\"north\">north</a> <a>web</a>"
);
}
#[test]
fn style_keeps_only_the_profile() {
assert_eq!(
check(
"<span style=\"color: red; position: fixed; background: url(http://x/y.png); background-image: url('stone.png'); display: grid\">x</span>"
),
"<span style=\"color: red; background-image: url('stone.png')\">x</span>"
);
assert_eq!(
check("<span style='behavior: url(x.htc)'>x</span>"),
"<span>x</span>"
);
}
#[test]
fn elements_outside_the_profile_go_but_their_text_stays() {
assert_eq!(check("<marquee>Hello</marquee>"), "Hello");
}
#[test]
fn a_tag_split_between_writes() {
let mut checker = Checker::new();
checker.write("<img src='clock/");
checker.write("9");
checker.write(".jpg' border=0>after &md");
checker.write("ash; done");
let doc: String = checker.take().iter().map(Item::markup).collect();
assert_eq!(
doc,
"<img src=\"clock/9.jpg\" border=\"0\">after \u{2014} done"
);
}
#[test]
fn elements_are_closed_in_order() {
assert_eq!(
check("<b>bold <i>both</b> plain</i>"),
"<b>bold <i>both</i></b> plain"
);
assert_eq!(
check("<ol><li>One<li>Two</ol>"),
"<ol><li>One</li>\n<li>Two</li>\n</ol>\n"
);
assert_eq!(check("<p>One<p>Two"), "<p>One</p>\n<p>Two");
}
#[test]
fn the_players_input() {
let mut checker = Checker::new();
checker.write(">");
checker.input("look <b>");
let doc: String = checker.take().iter().map(Item::markup).collect();
assert_eq!(doc, "><kbd>look <b></kbd>\n");
}
#[test]
fn a_style_attribute_loses_important() {
assert_eq!(
check("<span style='color: red !important; font-size: 2em'>x</span>"),
"<span style=\"color: red; font-size: 2em\">x</span>"
);
}
#[test]
fn a_stylesheet_is_scoped_and_held_to_the_profile() {
let css = "@import url(http://x/y.css);\n\
/* the page */ body { background: #000; position: absolute; color: #eee }\n\
.roomname, h1 { font-variant: small-caps !important }\n\
@media (max-width: 40em) { p { margin: 0 } }\n\
@media (hover: hover) { p { color: red } }\n\
@font-face { font-family: Uncial; src: url('uncial.woff2') format('woff2') }\n\
@font-face { font-family: Far; src: url(https://x/y.woff2) }\n\
@keyframes spin { from { color: red } }\n\
a { color: blue; behavior: url(x.htc) }";
assert_eq!(
check_stylesheet(css, "#game"),
"#game { background: #000; color: #eee }\n\
#game .roomname, #game h1 { font-variant: small-caps }\n\
@media (max-width: 40em) {\n#game p { margin: 0 }\n}\n\
@font-face { font-family: Uncial; src: url('uncial.woff2') format('woff2') }\n\
#game a { color: blue }\n"
);
}
#[test]
fn a_transcript_only_text_and_comments() {
assert_eq!(check("A<log>for the log</log>B<!-- a <b> note -->C"), "ABC");
}
}