Debian Codegraph @

src/tests/escape.rs

raw
1 use crate::cm::{escape_inline, escape_link_destination};
2 use crate::{Arena, Options, entity, format_commonmark, markdown_to_html, parse_document};
3
4 /// Assert that the input text escapes to the expected result in inline context,
5 /// and that the expected result renders to HTML which displays the input text.
6 #[track_caller]
7 fn assert_escape_inline(input: &str, expected: &str) {
8 let actual = escape_inline(input);
9 assert_eq!(expected, actual);
10 let mut html = markdown_to_html(expected, &Options::default());
11 html = html
12 .strip_prefix("<p>")
13 .expect("html should be one paragraph")
14 .to_string();
15 html = html
16 .strip_suffix("</p>\n")
17 .expect("html should be one paragraph")
18 .to_string();
19 assert_eq!(input, entity::unescape_html(&html));
20 }
21
22 #[test]
23 fn escape_inline_baseline() {
24 assert_escape_inline("abcdefg", "abcdefg");
25 assert_escape_inline("*hello*", r#"\*hello\*"#);
26 assert_escape_inline(
27 "[A link](https://link.com)",
28 r#"\[A link\]\(https://link\.com\)"#,
29 );
30 assert_escape_inline(
31 r#"some <"complicated"> & '/problematic\' input"#,
32 r#"some \<\"complicated\"\> \& '/problematic\\' input"#,
33 );
34 }
35
36 /// Assert that the URL is escaped as expected, and that the result is rendered
37 /// into HTML in such a way that preserves the meaning of the input.
38 ///
39 /// [link destination]: https://spec.commonmark.org/0.31.2/#link-destination
40 #[test]
41 fn escape_link_target() {
42 let url = "rabbits) <cup\rcakes\n> [%7Bhya%cinth%7d](";
43 let escaped = r#"<rabbits) \<cup%0Dcakes%0A\> [%7Bhya%cinth%7d](>"#;
44 let decoded = "rabbits) <cup\rcakes\n> [{hya%cinth}](";
45
46 assert_eq!(escaped, escape_link_destination(url));
47
48 let md = format!("[link]({escaped})");
49 let mut html = markdown_to_html(&md, &Options::default());
50 html = html
51 .strip_prefix("<p><a href=\"")
52 .expect("html should be one anchor in a paragraph")
53 .to_string();
54 html = html
55 .strip_suffix("\">link</a></p>\n")
56 .expect("html should be one anchor in a paragraph")
57 .to_string();
58
59 assert_eq!(
60 "rabbits)%20%3Ccup%0Dcakes%0A%3E%20%5B%7Bhya%25cinth%7d%5D(",
61 html
62 );
63 assert_eq!(
64 decoded,
65 percent_encoding_rfc3986::percent_decode_str(&html)
66 .unwrap()
67 .decode_utf8()
68 .unwrap()
69 );
70 }
71
72 #[test]
73 fn escape_strikethrough() {
74 assert_escape_inline("~~text~~", "\\~\\~text\\~\\~");
75 }
76
77 #[test]
78 fn escape_block_directive_colon() {
79 // Block directives use ::: syntax at line start, so : is only escaped
80 // when at the beginning of content
81 let arena = Arena::new();
82 let mut options = Options::default();
83 options.extension.block_directive = true;
84
85 let input = ":::foo bar\ncontent\n:::\n";
86 let root = parse_document(&arena, input, &options);
87 let mut output = String::new();
88 format_commonmark(root, &options, &mut output).unwrap();
89 assert!(
90 output.contains(r#"\:\:\:"#) || output.contains(r#":::foo"#),
91 "Output was: {}",
92 output
93 );
94 }