pub fn escape(text: &str) -> String {
text.replace('&', "&")
.replace('<', "<")
.replace('>', ">")
.replace('"', """)
}
pub(crate) fn unescape(html: &str) -> String {
html.replace("<", "<")
.replace(">", ">")
.replace(""", "\"")
.replace("&", "&")
}
pub(crate) fn tag_end(html: &str) -> usize {
let mut quote = None;
for (i, c) in html.char_indices() {
match (quote, c) {
(None, '"' | '\'') => quote = Some(c),
(Some(q), c) if c == q => quote = None,
(None, '>') => return i + 1,
_ => {}
}
}
html.len()
}
pub(crate) fn attributes(tag: &str) -> Vec<(String, String)> {
let mut attributes = Vec::new();
let mut chars = tag.char_indices().peekable();
loop {
while chars
.next_if(|(_, c)| c.is_ascii_whitespace() || *c == '/')
.is_some()
{}
let Some(&(start, c)) = chars.peek() else {
break;
};
if c == '>' {
break;
}
let mut end = start;
while let Some((i, c)) =
chars.next_if(|(_, c)| !c.is_ascii_whitespace() && !matches!(c, '=' | '>' | '/'))
{
end = i + c.len_utf8();
}
let name = tag[start..end].to_string();
while chars.next_if(|(_, c)| c.is_ascii_whitespace()).is_some() {}
let mut value = String::new();
if chars.next_if(|(_, c)| *c == '=').is_some() {
while chars.next_if(|(_, c)| c.is_ascii_whitespace()).is_some() {}
match chars.peek() {
Some(&(_, quote @ ('"' | '\''))) => {
chars.next();
for (_, c) in chars.by_ref() {
if c == quote {
break;
}
value.push(c);
}
}
_ => {
while let Some((_, c)) =
chars.next_if(|(_, c)| !c.is_ascii_whitespace() && *c != '>')
{
value.push(c);
}
}
}
}
if name.is_empty() {
chars.next();
continue;
}
attributes.push((name, value));
}
attributes
}
pub(crate) fn values(html: &str, name: &str) -> Vec<String> {
let mut values = Vec::new();
let mut rest = html;
while let Some(at) = rest.find('<') {
rest = &rest[at..];
if let Some(comment) = rest.strip_prefix("<!--") {
rest = comment.find("-->").map_or("", |end| &comment[end + 3..]);
continue;
}
let named = rest[1..].len()
- rest[1..]
.trim_start_matches(|c: char| c.is_ascii_alphanumeric())
.len();
if named == 0 {
rest = &rest[1..];
continue;
}
let end = tag_end(rest);
values.extend(
attributes(&rest[1 + named..end])
.into_iter()
.filter(|(attribute, _)| attribute.eq_ignore_ascii_case(name))
.map(|(_, value)| unescape(&value)),
);
rest = &rest[end..];
}
values
}
#[derive(Debug, PartialEq)]
pub(crate) enum Link<'a> {
Web(&'a str),
Elsewhere,
File { path: &'a str, fragment: &'a str },
}
impl<'a> Link<'a> {
pub(crate) fn classify(address: &'a str) -> Link<'a> {
let scheme = address
.split_once(':')
.map(|(scheme, _)| scheme)
.filter(|scheme| {
scheme.starts_with(|c: char| c.is_ascii_alphabetic())
&& scheme
.chars()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.'))
});
match scheme {
Some(scheme)
if scheme.eq_ignore_ascii_case("http") || scheme.eq_ignore_ascii_case("https") =>
{
Link::Web(address)
}
Some(_) => Link::Elsewhere,
None => {
let (path, fragment) = address.split_once('#').unwrap_or((address, ""));
Link::File { path, fragment }
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn escaped_text_is_safe_in_text_and_in_a_quoted_attribute_and_comes_back() {
let text = r#"<a href="x">Tom & "Jerry"</a>"#;
let escaped = escape(text);
assert_eq!(
escaped,
"<a href="x">Tom & "Jerry"</a>"
);
assert_eq!(unescape(&escaped), text);
assert_eq!(unescape(&escape("<")), "<");
}
#[test]
fn a_tags_attributes_are_read_however_they_are_quoted_and_spaced() {
assert_eq!(
attributes(r#" SRC = 'a b.png' alt="x > y" width=10 hidden/>"#),
[
("SRC".to_string(), "a b.png".to_string()),
("alt".to_string(), "x > y".to_string()),
("width".to_string(), "10".to_string()),
("hidden".to_string(), String::new()),
]
);
assert_eq!(tag_end(r#"<a title="a>b">rest"#), 15);
}
#[test]
fn every_value_of_an_attribute_is_found_in_tags_only() {
let html = concat!(
r#"<p><a href="one.md">1</a> <a class="x" HREF='two.md?a=1&b=2'>2</a></p>"#,
r#"<!-- <a href="commented.md"> -->"#,
r#"<pre><code><a href="code.md"></code></pre>"#,
r#"<p>a < b and href="text.md"</p>"#,
r#"<img src="pic.png"><svg><a xlink:href="drawn.md"></a></svg>"#,
);
assert_eq!(values(html, "href"), ["one.md", "two.md?a=1&b=2"]);
assert_eq!(values(html, "src"), ["pic.png"]);
}
#[test]
fn an_address_is_on_the_web_elsewhere_or_a_file() {
for web in ["http://example.com", "https://example.com/a#b", "HTTPS://x"] {
assert_eq!(Link::classify(web), Link::Web(web));
}
for elsewhere in [
"mailto:a@b.c",
"tel:+1",
"data:image/png;base64,AA",
"ftp://x",
] {
assert_eq!(Link::classify(elsewhere), Link::Elsewhere, "{elsewhere}");
}
for (address, path, fragment) in [
("b.md", "b.md", ""),
("../b.md#two", "../b.md", "two"),
("#top", "", "top"),
("", "", ""),
("notes/a:b.md", "notes/a:b.md", ""),
("b.md#at:10", "b.md", "at:10"),
("/root.md", "/root.md", ""),
] {
assert_eq!(
Link::classify(address),
Link::File { path, fragment },
"{address}"
);
}
}
}