Markdown parser fork with extended syntax for personal use.
at hack 147 lines 4.7 kB view raw
1use markdown::to_html; 2use pretty_assertions::assert_eq; 3 4#[test] 5fn url() { 6 assert_eq!( 7 to_html("<https://%>"), 8 "<p><a href=\"https://%25\">https://%</a></p>", 9 "should support incorrect percentage encoded values (0)" 10 ); 11 12 assert_eq!( 13 to_html("[](<%>)"), 14 "<p><a href=\"%25\"></a></p>", 15 "should support incorrect percentage encoded values (1)" 16 ); 17 18 assert_eq!( 19 to_html("[](<%%20>)"), 20 "<p><a href=\"%25%20\"></a></p>", 21 "should support incorrect percentage encoded values (2)" 22 ); 23 24 assert_eq!( 25 to_html("[](<%a%20>)"), 26 "<p><a href=\"%25a%20\"></a></p>", 27 "should support incorrect percentage encoded values (3)" 28 ); 29 30 // Note: Surrogate handling not needed in Rust. 31 // assert_eq!( 32 // to_html("[](<foo\u{D800}bar>)"), 33 // "<p><a href=\"foo%EF%BF%BDbar\"></a></p>", 34 // "should support a lone high surrogate (lowest)" 35 // ); 36 37 // Surrogate handling not needed in Rust. 38 // assert_eq!( 39 // to_html("[](<foo\u{DBFF}bar>)"), 40 // "<p><a href=\"foo%EF%BF%BDbar\"></a></p>", 41 // "should support a lone high surrogate (highest)" 42 // ); 43 44 // Surrogate handling not needed in Rust. 45 // assert_eq!( 46 // to_html("[](<\u{D800}bar>)"), 47 // "<p><a href=\"%EF%BF%BDbar\"></a></p>", 48 // "should support a lone high surrogate at the start (lowest)" 49 // ); 50 51 // Surrogate handling not needed in Rust. 52 // assert_eq!( 53 // to_html("[](<\u{DBFF}bar>)"), 54 // "<p><a href=\"%EF%BF%BDbar\"></a></p>", 55 // "should support a lone high surrogate at the start (highest)" 56 // ); 57 58 // Surrogate handling not needed in Rust. 59 // assert_eq!( 60 // to_html("[](<foo\u{D800}>)"), 61 // "<p><a href=\"foo%EF%BF%BD\"></a></p>", 62 // "should support a lone high surrogate at the end (lowest)" 63 // ); 64 65 // Surrogate handling not needed in Rust. 66 // assert_eq!( 67 // to_html("[](<foo\u{DBFF}>)"), 68 // "<p><a href=\"foo%EF%BF%BD\"></a></p>", 69 // "should support a lone high surrogate at the end (highest)" 70 // ); 71 72 // Surrogate handling not needed in Rust. 73 // assert_eq!( 74 // to_html("[](<foo\u{DC00}bar>)"), 75 // "<p><a href=\"foo%EF%BF%BDbar\"></a></p>", 76 // "should support a lone low surrogate (lowest)" 77 // ); 78 79 // Surrogate handling not needed in Rust. 80 // assert_eq!( 81 // to_html("[](<foo\u{DFFF}bar>)"), 82 // "<p><a href=\"foo%EF%BF%BDbar\"></a></p>", 83 // "should support a lone low surrogate (highest)" 84 // ); 85 86 // Surrogate handling not needed in Rust. 87 // assert_eq!( 88 // to_html("[](<\u{DC00}bar>)"), 89 // "<p><a href=\"%EF%BF%BDbar\"></a></p>", 90 // "should support a lone low surrogate at the start (lowest)" 91 // ); 92 93 // Surrogate handling not needed in Rust. 94 // assert_eq!( 95 // to_html("[](<\u{DFFF}bar>)"), 96 // "<p><a href=\"%EF%BF%BDbar\"></a></p>", 97 // "should support a lone low surrogate at the start (highest)" 98 // ); 99 100 // Surrogate handling not needed in Rust. 101 // assert_eq!( 102 // to_html("[](<foo\u{DC00}>)"), 103 // "<p><a href=\"foo%EF%BF%BD\"></a></p>", 104 // "should support a lone low surrogate at the end (lowest)" 105 // ); 106 107 // Surrogate handling not needed in Rust. 108 // assert_eq!( 109 // to_html("[](<foo\u{DFFF}>)"), 110 // "<p><a href=\"foo%EF%BF%BD\"></a></p>", 111 // "should support a lone low surrogate at the end (highest)" 112 // ); 113 114 assert_eq!( 115 to_html("[](<🤔>)"), 116 "<p><a href=\"%F0%9F%A4%94\"></a></p>", 117 "should support an emoji" 118 ); 119 120 let mut ascii = Vec::with_capacity(129); 121 let mut code = 0; 122 123 while code < 128 { 124 // LF and CR can’t be in resources. 125 if code == 10 || code == 13 { 126 code += 1; 127 continue; 128 } 129 130 // `<`, `>`, `\` need to be escaped. 131 if code == 60 || code == 62 || code == 92 { 132 ascii.push('\\'); 133 } 134 135 ascii.push(char::from_u32(code).unwrap()); 136 137 code += 1; 138 } 139 140 let ascii_in = ascii.into_iter().collect::<String>(); 141 let ascii_out = "%EF%BF%BD%01%02%03%04%05%06%07%08%09%0B%0C%0E%0F%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20!%22#$%25&amp;'()*+,-./0123456789:;%3C=%3E?@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%7F"; 142 assert_eq!( 143 to_html(&format!("[](<{}>)", ascii_in)), 144 format!("<p><a href=\"{}\"></a></p>", ascii_out), 145 "should support ascii characters" 146 ); 147}