Markdown parser fork with extended syntax for personal use.
at hack 292 lines 7.8 kB view raw
1use markdown::{message, to_html, to_html_with_options, CompileOptions, Options}; 2use pretty_assertions::assert_eq; 3 4#[test] 5fn tabs_flow() -> Result<(), message::Message> { 6 let danger = &Options { 7 compile: CompileOptions { 8 allow_dangerous_html: true, 9 allow_dangerous_protocol: true, 10 ..Default::default() 11 }, 12 ..Default::default() 13 }; 14 15 assert_eq!( 16 to_html(" x"), 17 "<pre><code>x\n</code></pre>", 18 "should support a 4*SP to start code" 19 ); 20 21 assert_eq!( 22 to_html("\tx"), 23 "<pre><code>x\n</code></pre>", 24 "should support a HT to start code" 25 ); 26 27 assert_eq!( 28 to_html(" \tx"), 29 "<pre><code>x\n</code></pre>", 30 "should support a SP + HT to start code" 31 ); 32 33 assert_eq!( 34 to_html(" \tx"), 35 "<pre><code>x\n</code></pre>", 36 "should support a 2*SP + HT to start code" 37 ); 38 39 assert_eq!( 40 to_html(" \tx"), 41 "<pre><code>x\n</code></pre>", 42 "should support a 3*SP + HT to start code" 43 ); 44 45 assert_eq!( 46 to_html(" \tx"), 47 "<pre><code>\tx\n</code></pre>", 48 "should support a 4*SP to start code, and leave the next HT as code data" 49 ); 50 51 assert_eq!( 52 to_html(" \t# x"), 53 "<pre><code># x\n</code></pre>", 54 "should not support a 3*SP + HT to start an ATX heading" 55 ); 56 57 assert_eq!( 58 to_html(" \t> x"), 59 "<pre><code>&gt; x\n</code></pre>", 60 "should not support a 3*SP + HT to start a block quote" 61 ); 62 63 assert_eq!( 64 to_html(" \t- x"), 65 "<pre><code>- x\n</code></pre>", 66 "should not support a 3*SP + HT to start a list item" 67 ); 68 69 assert_eq!( 70 to_html(" \t---"), 71 "<pre><code>---\n</code></pre>", 72 "should not support a 3*SP + HT to start a thematic break" 73 ); 74 75 assert_eq!( 76 to_html(" \t```"), 77 "<pre><code>```\n</code></pre>", 78 "should not support a 3*SP + HT to start a fenced code" 79 ); 80 81 assert_eq!( 82 to_html(" \t<div>"), 83 "<pre><code>&lt;div&gt;\n</code></pre>", 84 "should not support a 3*SP + HT to start HTML" 85 ); 86 87 assert_eq!( 88 to_html("#\tx\t#\t"), 89 "<h1>x</h1>", 90 "should support tabs around ATX heading sequences" 91 ); 92 93 assert_eq!( 94 to_html("#\t\tx\t\t#\t\t"), 95 "<h1>x</h1>", 96 "should support arbitrary tabs around ATX heading sequences" 97 ); 98 99 assert_eq!( 100 to_html("```\tx\ty\t\n```\t"), 101 "<pre><code class=\"language-x\"></code></pre>", 102 "should support tabs around fenced code fences, info, and meta" 103 ); 104 105 assert_eq!( 106 to_html("```\t\tx\t\ty\t\t\n```\t\t"), 107 "<pre><code class=\"language-x\"></code></pre>", 108 "should support arbitrary tabs around fenced code fences, info, and meta" 109 ); 110 111 assert_eq!( 112 to_html("```x\n\t```"), 113 "<pre><code class=\"language-x\">\t```\n</code></pre>\n", 114 "should not support tabs before fenced code closing fences" 115 ); 116 117 assert_eq!( 118 to_html_with_options("<x\ty\tz\t=\t\"\tx\t\">", danger)?, 119 "<x\ty\tz\t=\t\"\tx\t\">", 120 "should support tabs in HTML (if whitespace is allowed)" 121 ); 122 123 assert_eq!( 124 to_html("*\t*\t*\t"), 125 "<hr />", 126 "should support tabs in thematic breaks" 127 ); 128 129 assert_eq!( 130 to_html("*\t\t*\t\t*\t\t"), 131 "<hr />", 132 "should support arbitrary tabs in thematic breaks" 133 ); 134 135 Ok(()) 136} 137 138#[test] 139fn tabs_text() { 140 assert_eq!( 141 to_html("<http:\t>"), 142 "<p>&lt;http:\t&gt;</p>", 143 "should not support a tab to start an autolink w/ protocol’s rest" 144 ); 145 146 assert_eq!( 147 to_html("<http:x\t>"), 148 "<p>&lt;http:x\t&gt;</p>", 149 "should not support a tab in an autolink w/ protocol’s rest" 150 ); 151 152 assert_eq!( 153 to_html("<example\t@x.com>"), 154 "<p>&lt;example\t@x.com&gt;</p>", 155 "should not support a tab in an email autolink’s local part" 156 ); 157 158 assert_eq!( 159 to_html("<example@x\ty.com>"), 160 "<p>&lt;example@x\ty.com&gt;</p>", 161 "should not support a tab in an email autolink’s label" 162 ); 163 164 assert_eq!( 165 to_html("\\\tx"), 166 "<p>\\\tx</p>", 167 "should not support character escaped tab" 168 ); 169 170 assert_eq!( 171 to_html("&#9;"), 172 "<p>\t</p>", 173 "should support character reference resolving to a tab" 174 ); 175 176 assert_eq!( 177 to_html("`\tx`"), 178 "<p><code>\tx</code></p>", 179 "should support a tab starting code" 180 ); 181 182 assert_eq!( 183 to_html("`x\t`"), 184 "<p><code>x\t</code></p>", 185 "should support a tab ending code" 186 ); 187 188 assert_eq!( 189 to_html("`\tx\t`"), 190 "<p><code>\tx\t</code></p>", 191 "should support tabs around code" 192 ); 193 194 assert_eq!( 195 to_html("`\tx `"), 196 "<p><code>\tx </code></p>", 197 "should support a tab starting, and a space ending, code" 198 ); 199 200 assert_eq!( 201 to_html("` x\t`"), 202 "<p><code> x\t</code></p>", 203 "should support a space starting, and a tab ending, code" 204 ); 205 206 // Note: CM does not strip it in this case. 207 // However, that should be a bug there: makes more sense to remove it like 208 // trailing spaces. 209 assert_eq!( 210 to_html("x\t\ny"), 211 "<p>x\ny</p>", 212 "should support a trailing tab at a line ending in a paragraph" 213 ); 214 215 assert_eq!( 216 to_html("x\n\ty"), 217 "<p>x\ny</p>", 218 "should support an initial tab after a line ending in a paragraph" 219 ); 220 221 assert_eq!( 222 to_html("x[\ty](z)"), 223 "<p>x<a href=\"z\">\ty</a></p>", 224 "should support an initial tab in a link label" 225 ); 226 227 assert_eq!( 228 to_html("x[y\t](z)"), 229 "<p>x<a href=\"z\">y\t</a></p>", 230 "should support a final tab in a link label" 231 ); 232 233 assert_eq!( 234 to_html("[x\ty](z)"), 235 "<p><a href=\"z\">x\ty</a></p>", 236 "should support a tab in a link label" 237 ); 238 239 // Note: CM.js bug, see: <https://github.com/commonmark/commonmark.js/issues/191> 240 assert_eq!( 241 to_html("[x](\ty)"), 242 "<p><a href=\"y\">x</a></p>", 243 "should support a tab starting a link resource" 244 ); 245 246 assert_eq!( 247 to_html("[x](y\t)"), 248 "<p><a href=\"y\">x</a></p>", 249 "should support a tab ending a link resource" 250 ); 251 252 assert_eq!( 253 to_html("[x](y\t\"z\")"), 254 "<p><a href=\"y\" title=\"z\">x</a></p>", 255 "should support a tab between a link destination and title" 256 ); 257} 258 259#[test] 260fn tabs_virtual_spaces() { 261 assert_eq!( 262 to_html("```\n\tx"), 263 "<pre><code>\tx\n</code></pre>\n", 264 "should support a tab in fenced code" 265 ); 266 267 assert_eq!( 268 to_html(" ```\n\tx"), 269 "<pre><code> x\n</code></pre>\n", 270 "should strip 1 space from an initial tab in fenced code if the opening fence is indented as such" 271 ); 272 273 assert_eq!( 274 to_html(" ```\n\tx"), 275 "<pre><code> x\n</code></pre>\n", 276 "should strip 2 spaces from an initial tab in fenced code if the opening fence is indented as such" 277 ); 278 279 assert_eq!( 280 to_html(" ```\n\tx"), 281 "<pre><code> x\n</code></pre>\n", 282 "should strip 3 spaces from an initial tab in fenced code if the opening fence is indented as such" 283 ); 284 285 assert_eq!( 286 to_html("-\ta\n\n\tb"), 287 "<ul>\n<li>\n<p>a</p>\n<p>\tb</p>\n</li>\n</ul>", 288 // To do: CM.js does not output the tab before `b`. See if that makes sense? 289 // "<ul>\n<li>\n<p>a</p>\n<p>b</p>\n</li>\n</ul>", 290 "should support a part of a tab as a container, and the rest of a tab as flow" 291 ); 292}