mod link_url;
pub use link_url::*;
#[cfg(test)]
mod tests {
use super::*;
use crate::options::ConversionOptions;
use crate::options::validation::UrlEscapeStyle;
use std::borrow::Cow;
fn opts_with_style(style: UrlEscapeStyle) -> ConversionOptions {
ConversionOptions::builder().url_escape_style(style).build()
}
#[test]
fn has_uri_scheme_accepts_http() {
assert!(has_uri_scheme("http://example.com"));
assert!(has_uri_scheme("https://example.com/path"));
}
#[test]
fn has_uri_scheme_accepts_mailto() {
assert!(has_uri_scheme("mailto:a@b.com"));
}
#[test]
fn has_uri_scheme_accepts_uncommon_schemes() {
assert!(has_uri_scheme("ftp://host"));
assert!(has_uri_scheme("ssh://host"));
assert!(has_uri_scheme("data:text/plain,foo"));
assert!(has_uri_scheme("file:///etc/hosts"));
}
#[test]
fn has_uri_scheme_rejects_bare_paths() {
assert!(!has_uri_scheme("foobar.png"));
assert!(!has_uri_scheme("/relative/path"));
assert!(!has_uri_scheme("../up.html"));
assert!(!has_uri_scheme("#fragment"));
}
#[test]
fn has_uri_scheme_rejects_leading_digit_or_punct() {
assert!(!has_uri_scheme("9scheme:foo"));
assert!(!has_uri_scheme(":no-scheme"));
assert!(!has_uri_scheme(""));
}
#[test]
fn issue_397_filename_with_extension_is_not_autolinked() {
assert!(!has_uri_scheme("foobar.png"));
}
#[test]
fn percent_encode_url_leaves_unreserved_chars_unchanged() {
let result = percent_encode_url("/path-to_file.html~");
assert_eq!(result, "/path-to_file.html~");
}
#[test]
fn percent_encode_url_encodes_spaces() {
assert_eq!(percent_encode_url("/file (1).pdf"), "/file%20%281%29.pdf");
}
#[test]
fn percent_encode_url_encodes_angle_brackets() {
assert_eq!(percent_encode_url("/file <draft>.pdf"), "/file%20%3Cdraft%3E.pdf");
}
#[test]
fn percent_encode_url_full_issue_example() {
assert_eq!(
percent_encode_url("/file (1) <draft>.pdf"),
"/file%20%281%29%20%3Cdraft%3E.pdf"
);
}
#[test]
fn percent_encode_non_ascii_leaves_ascii_only_input_unchanged() {
assert_eq!(
percent_encode_non_ascii("/file (1) <draft>.pdf"),
"/file (1) <draft>.pdf"
);
}
#[test]
fn percent_encode_non_ascii_encodes_only_non_ascii_bytes() {
assert_eq!(percent_encode_non_ascii("/f\u{f6}\u{f6}.html"), "/f%C3%B6%C3%B6.html");
}
#[test]
fn append_markdown_link_angle_plain_url_unchanged() {
let mut out = String::new();
let options = opts_with_style(UrlEscapeStyle::Angle);
append_markdown_link(
&mut out,
&MarkdownLink {
label: "text",
href: "/file.pdf",
title: None,
raw_text: "text",
},
&options,
None,
);
assert_eq!(out, "[text](/file.pdf)");
}
#[test]
fn append_markdown_link_angle_percent_encodes_non_ascii() {
let mut out = String::new();
let options = opts_with_style(UrlEscapeStyle::Angle);
append_markdown_link(
&mut out,
&MarkdownLink {
label: "öö.html",
href: "öö.html",
title: None,
raw_text: "öö.html",
},
&options,
None,
);
assert_eq!(out, "[öö.html](%C3%B6%C3%B6.html)");
}
#[test]
fn append_markdown_link_angle_percent_encodes_non_ascii_but_not_ascii_reserved_chars() {
let mut out = String::new();
let options = opts_with_style(UrlEscapeStyle::Angle);
append_markdown_link(
&mut out,
&MarkdownLink {
label: "text",
href: "https://example.com/söq?a=1&b=2",
title: None,
raw_text: "text",
},
&options,
None,
);
assert_eq!(out, "[text](https://example.com/s%C3%B6q?a=1&b=2)");
}
#[test]
fn append_markdown_link_angle_does_not_percent_encode_a_fragment_destination() {
let mut out = String::new();
let options = opts_with_style(UrlEscapeStyle::Angle);
append_markdown_link(
&mut out,
&MarkdownLink {
label: "text",
href: "#walk-up-\u{fe0e}",
title: None,
raw_text: "text",
},
&options,
None,
);
assert_eq!(out, "[text](#walk-up-\u{fe0e})");
}
#[test]
fn append_markdown_link_angle_wraps_space_in_angle_brackets() {
let mut out = String::new();
let options = opts_with_style(UrlEscapeStyle::Angle);
append_markdown_link(
&mut out,
&MarkdownLink {
label: "file",
href: "/file (1).pdf",
title: None,
raw_text: "file",
},
&options,
None,
);
assert_eq!(out, "[file](</file (1).pdf>)");
}
#[test]
fn append_markdown_link_percent_encodes_spaces() {
let mut out = String::new();
let options = opts_with_style(UrlEscapeStyle::Percent);
append_markdown_link(
&mut out,
&MarkdownLink {
label: "file",
href: "/file (1).pdf",
title: None,
raw_text: "file",
},
&options,
None,
);
assert_eq!(out, "[file](/file%20%281%29.pdf)");
}
#[test]
fn append_markdown_link_percent_encodes_angle_brackets() {
let mut out = String::new();
let options = opts_with_style(UrlEscapeStyle::Percent);
append_markdown_link(
&mut out,
&MarkdownLink {
label: "file",
href: "/file <draft>.pdf",
title: None,
raw_text: "file",
},
&options,
None,
);
assert_eq!(out, "[file](/file%20%3Cdraft%3E.pdf)");
}
#[test]
fn append_markdown_link_percent_full_issue_example() {
let mut out = String::new();
let options = opts_with_style(UrlEscapeStyle::Percent);
append_markdown_link(
&mut out,
&MarkdownLink {
label: "file",
href: "/file (1) <draft>.pdf",
title: None,
raw_text: "file",
},
&options,
None,
);
assert_eq!(out, "[file](/file%20%281%29%20%3Cdraft%3E.pdf)");
}
#[test]
fn parens_are_balanced_accepts_nested_parens() {
assert!(parens_are_balanced("wiki/Rust_(programming_language)"));
assert!(parens_are_balanced("no/parens/here"));
}
#[test]
fn parens_are_balanced_rejects_equal_counts_out_of_order() {
assert!(!parens_are_balanced("a)b(c"));
}
#[test]
fn parens_are_balanced_rejects_unmatched_open_or_close() {
assert!(!parens_are_balanced("a(b"));
assert!(!parens_are_balanced("a)b"));
}
#[test]
fn append_markdown_link_angle_leaves_balanced_parens_unescaped_when_href_has_parens() {
let mut out = String::new();
let options = opts_with_style(UrlEscapeStyle::Angle);
append_markdown_link(
&mut out,
&MarkdownLink {
label: "Rust",
href: "https://en.wikipedia.org/wiki/Rust_(programming_language)",
title: None,
raw_text: "Rust",
},
&options,
None,
);
assert_eq!(out, "[Rust](https://en.wikipedia.org/wiki/Rust_(programming_language))");
}
#[test]
fn append_markdown_link_angle_escapes_out_of_order_parens_when_href_has_parens() {
let mut out = String::new();
let options = opts_with_style(UrlEscapeStyle::Angle);
append_markdown_link(
&mut out,
&MarkdownLink {
label: "link",
href: "http://example.com/a)(b",
title: None,
raw_text: "link",
},
&options,
None,
);
assert_eq!(out, "[link](http://example.com/a\\)\\(b)");
}
#[test]
fn append_markdown_link_angle_escapes_gt_inside_wrap_when_href_has_space_and_gt() {
let mut out = String::new();
let options = opts_with_style(UrlEscapeStyle::Angle);
append_markdown_link(
&mut out,
&MarkdownLink {
label: "text",
href: "/my file >.pdf",
title: None,
raw_text: "text",
},
&options,
None,
);
assert_eq!(out, "[text](</my file \\>.pdf>)");
}
#[test]
fn append_markdown_link_angle_produces_empty_angle_brackets_when_href_is_empty() {
let mut out = String::new();
let options = opts_with_style(UrlEscapeStyle::Angle);
append_markdown_link(
&mut out,
&MarkdownLink {
label: "text",
href: "",
title: None,
raw_text: "text",
},
&options,
None,
);
assert_eq!(out, "[text](<>)");
}
#[test]
fn append_markdown_link_percent_preserves_title() {
let mut out = String::new();
let options = opts_with_style(UrlEscapeStyle::Percent);
append_markdown_link(
&mut out,
&MarkdownLink {
label: "link",
href: "/path with spaces",
title: Some("My Title"),
raw_text: "link",
},
&options,
None,
);
assert_eq!(out, "[link](/path%20with%20spaces \"My Title\")");
}
#[test]
fn escape_entity_ampersands_escapes_a_named_reference() {
assert_eq!(escape_entity_ampersands("?+"), "?&plus;");
}
#[test]
fn escape_entity_ampersands_escapes_a_decimal_numeric_reference() {
assert_eq!(escape_entity_ampersands("?+"), "?&#43;");
}
#[test]
fn escape_entity_ampersands_escapes_a_hex_numeric_reference() {
assert_eq!(escape_entity_ampersands("?+"), "?&#x2B;");
}
#[test]
fn escape_entity_ampersands_leaves_a_bare_ampersand_unchanged() {
match escape_entity_ampersands("?a&b") {
Cow::Borrowed(unchanged) => assert_eq!(unchanged, "?a&b"),
Cow::Owned(owned) => panic!("expected no allocation, got {owned:?}"),
}
}
#[test]
fn escape_entity_ampersands_leaves_an_unrecognized_named_reference_unchanged() {
assert_eq!(escape_entity_ampersands("&foo;"), "&foo;");
}
#[test]
fn escape_entity_ampersands_escapes_amp_itself_since_it_is_a_valid_reference() {
assert_eq!(escape_entity_ampersands("&"), "&amp;");
}
#[test]
fn escape_markdown_title_escapes_backslash_before_quote_so_the_closing_quote_is_not_swallowed() {
assert_eq!(escape_markdown_title("foo\\"), "foo\\\\");
assert_eq!(escape_markdown_title("say \"hi\"\\"), "say \\\"hi\\\"\\\\");
}
#[test]
fn append_markdown_link_escapes_a_trailing_backslash_in_title_so_the_closing_quote_is_not_swallowed() {
let mut out = String::new();
let options = opts_with_style(UrlEscapeStyle::Angle);
append_markdown_link(
&mut out,
&MarkdownLink {
label: "text",
href: "/url",
title: Some("foo\\"),
raw_text: "text",
},
&options,
None,
);
assert_eq!(out, "[text](/url \"foo\\\\\")");
}
#[test]
fn append_markdown_link_escapes_a_trailing_backslash_in_default_title_so_the_closing_quote_is_not_swallowed() {
let mut out = String::new();
let mut options = opts_with_style(UrlEscapeStyle::Angle);
options.default_title = true;
append_markdown_link(
&mut out,
&MarkdownLink {
label: "text",
href: "http://a\\",
title: None,
raw_text: "http://a\\",
},
&options,
None,
);
assert_eq!(out, "[text](http://a\\ \"http://a\\\\\")");
}
#[test]
fn append_markdown_link_escapes_a_backslash_inside_the_angle_bracket_wrap_so_it_cannot_unescape_a_delimiter() {
let mut out = String::new();
let options = opts_with_style(UrlEscapeStyle::Angle);
append_markdown_link(
&mut out,
&MarkdownLink {
label: "text",
href: "/my file\\>.pdf",
title: None,
raw_text: "text",
},
&options,
None,
);
assert_eq!(out, "[text](</my file\\\\\\>.pdf>)");
}
#[test]
fn append_markdown_link_escapes_a_backslash_before_punctuation_in_a_balanced_destination() {
let mut out = String::new();
let options = opts_with_style(UrlEscapeStyle::Angle);
append_markdown_link(
&mut out,
&MarkdownLink {
label: "text",
href: "\\*",
title: None,
raw_text: "text",
},
&options,
None,
);
assert_eq!(out, "[text](\\\\*)");
}
#[test]
fn append_markdown_link_escapes_a_trailing_backslash_in_a_balanced_destination_with_no_title() {
let mut out = String::new();
let options = opts_with_style(UrlEscapeStyle::Angle);
append_markdown_link(
&mut out,
&MarkdownLink {
label: "text",
href: "/path\\",
title: None,
raw_text: "text",
},
&options,
None,
);
assert_eq!(out, "[text](/path\\\\)");
}
#[test]
fn append_markdown_link_leaves_a_trailing_backslash_in_a_balanced_destination_unescaped_when_a_title_follows() {
let mut out = String::new();
let options = opts_with_style(UrlEscapeStyle::Angle);
append_markdown_link(
&mut out,
&MarkdownLink {
label: "text",
href: "/path\\",
title: Some("t"),
raw_text: "text",
},
&options,
None,
);
assert_eq!(out, "[text](/path\\ \"t\")");
}
}