|
| 1 | +use html_compare_rs::{assert_html_eq, presets::markdown}; |
| 2 | +use pulldown_cmark::{html, BrokenLink, Options, Parser}; |
| 3 | +use pulldown_html_ext::push_html; |
| 4 | +use pulldown_html_ext::HtmlConfig; |
| 5 | + |
| 6 | +#[test] |
| 7 | +fn test_script_block_single_line() { |
| 8 | + let original = r##"Little header |
| 9 | +
|
| 10 | +<script type="text/js"> |
| 11 | +function some_func() { |
| 12 | +console.log("teeeest"); |
| 13 | +} |
| 14 | +
|
| 15 | +
|
| 16 | +function another_func() { |
| 17 | +console.log("fooooo"); |
| 18 | +} |
| 19 | +</script>"##; |
| 20 | + let expected = r##"<p>Little header</p> |
| 21 | +<script type="text/js"> |
| 22 | +function some_func() { |
| 23 | +console.log("teeeest"); |
| 24 | +} |
| 25 | +
|
| 26 | +
|
| 27 | +function another_func() { |
| 28 | +console.log("fooooo"); |
| 29 | +} |
| 30 | +</script>"##; |
| 31 | + |
| 32 | + let html = push_html(original, &HtmlConfig::default()); |
| 33 | + assert_html_eq!(html, expected, markdown()); |
| 34 | +} |
| 35 | + |
| 36 | +#[test] |
| 37 | +fn test_script_block_multiline_type() { |
| 38 | + let original = r##"Little header |
| 39 | +
|
| 40 | +<script |
| 41 | +type="text/js"> |
| 42 | +function some_func() { |
| 43 | +console.log("teeeest"); |
| 44 | +} |
| 45 | +
|
| 46 | +
|
| 47 | +function another_func() { |
| 48 | +console.log("fooooo"); |
| 49 | +} |
| 50 | +</script>"##; |
| 51 | + let expected = r##"<p>Little header</p> |
| 52 | +<script |
| 53 | +type="text/js"> |
| 54 | +function some_func() { |
| 55 | +console.log("teeeest"); |
| 56 | +} |
| 57 | +
|
| 58 | +
|
| 59 | +function another_func() { |
| 60 | +console.log("fooooo"); |
| 61 | +} |
| 62 | +</script>"##; |
| 63 | + |
| 64 | + let html = push_html(original, &HtmlConfig::default()); |
| 65 | + assert_html_eq!(html, expected, markdown()); |
| 66 | +} |
| 67 | + |
| 68 | +#[test] |
| 69 | +fn test_processing_instruction() { |
| 70 | + let original = r##"Little header |
| 71 | +
|
| 72 | +<? |
| 73 | +<div></div> |
| 74 | +<p>Useless</p> |
| 75 | +?>"##; |
| 76 | + let expected = r##"<p>Little header</p> |
| 77 | +<? |
| 78 | +<div></div> |
| 79 | +<p>Useless</p> |
| 80 | +?>"##; |
| 81 | + |
| 82 | + let html = push_html(original, &HtmlConfig::default()); |
| 83 | + assert_html_eq!(html, expected, markdown()); |
| 84 | +} |
| 85 | + |
| 86 | +#[test] |
| 87 | +fn test_html_comment() { |
| 88 | + let original = r##"Little header |
| 89 | +
|
| 90 | +<!-- |
| 91 | +<div></div> |
| 92 | +<p>Useless</p> |
| 93 | +-->"##; |
| 94 | + let expected = r##"<p>Little header</p> |
| 95 | +<!-- |
| 96 | +<div></div> |
| 97 | +<p>Useless</p> |
| 98 | +-->"##; |
| 99 | + |
| 100 | + let html = push_html(original, &HtmlConfig::default()); |
| 101 | + assert_html_eq!(html, expected, markdown()); |
| 102 | +} |
| 103 | + |
| 104 | +#[test] |
| 105 | +fn test_cdata_section() { |
| 106 | + let original = r##"Little header |
| 107 | +
|
| 108 | +<![CDATA[ |
| 109 | +<div></div> |
| 110 | +<p>Useless</p> |
| 111 | +]]>"##; |
| 112 | + let expected = r##"<p>Little header</p> |
| 113 | +<![CDATA[ |
| 114 | +<div></div> |
| 115 | +<p>Useless</p> |
| 116 | +]]>"##; |
| 117 | + |
| 118 | + let html = push_html(original, &HtmlConfig::default()); |
| 119 | + assert_html_eq!(html, expected, markdown()); |
| 120 | +} |
| 121 | + |
| 122 | +#[test] |
| 123 | +fn test_declaration() { |
| 124 | + let original = r##"Little header |
| 125 | +
|
| 126 | +<!X |
| 127 | +Some things are here... |
| 128 | +>"##; |
| 129 | + let expected = r##"<p>Little header</p> |
| 130 | +<!X |
| 131 | +Some things are here... |
| 132 | +>"##; |
| 133 | + |
| 134 | + let html = push_html(original, &HtmlConfig::default()); |
| 135 | + assert_html_eq!(html, expected, markdown()); |
| 136 | +} |
| 137 | + |
| 138 | +#[test] |
| 139 | +fn test_header_with_script() { |
| 140 | + let original = r##"Little header |
| 141 | +----------- |
| 142 | +
|
| 143 | +<script> |
| 144 | +function some_func() { |
| 145 | +console.log("teeeest"); |
| 146 | +} |
| 147 | +
|
| 148 | +
|
| 149 | +function another_func() { |
| 150 | +console.log("fooooo"); |
| 151 | +} |
| 152 | +</script>"##; |
| 153 | + let expected = r##"<h2 id="heading-2">Little header</h2> |
| 154 | +<script> |
| 155 | +function some_func() { |
| 156 | +console.log("teeeest"); |
| 157 | +} |
| 158 | +
|
| 159 | +
|
| 160 | +function another_func() { |
| 161 | +console.log("fooooo"); |
| 162 | +} |
| 163 | +</script>"##; |
| 164 | + |
| 165 | + let html = push_html(original, &HtmlConfig::default()); |
| 166 | + assert_html_eq!(html, expected, markdown()); |
| 167 | +} |
| 168 | + |
| 169 | +#[test] |
| 170 | +#[ignore = "TODO - Broken"] |
| 171 | +fn test_table() { |
| 172 | + let original = "A | B\n---|---\nfoo | bar"; |
| 173 | + let expected = r##"<table> |
| 174 | +<thead><tr><th>A</th><th>B</th></tr></thead> |
| 175 | +<tbody><tr><td>foo</td><td>bar</td></tr></tbody> |
| 176 | +</table>"##; |
| 177 | + |
| 178 | + let mut opts = Options::empty(); |
| 179 | + opts.insert(Options::ENABLE_TABLES); |
| 180 | + let html = push_html(original, &HtmlConfig::default()); |
| 181 | + assert_html_eq!(html, expected, markdown()); |
| 182 | +} |
| 183 | + |
| 184 | +#[test] |
| 185 | +fn test_horizontal_rule_dash() { |
| 186 | + let original = "---"; |
| 187 | + let expected = "<hr>"; |
| 188 | + |
| 189 | + let html = push_html(original, &HtmlConfig::default()); |
| 190 | + assert_html_eq!(html, expected, markdown()); |
| 191 | +} |
| 192 | + |
| 193 | +#[test] |
| 194 | +fn test_horizontal_rule_asterisk() { |
| 195 | + let original = "* * *"; |
| 196 | + let expected = "<hr>"; |
| 197 | + |
| 198 | + let html = push_html(original, &HtmlConfig::default()); |
| 199 | + assert_html_eq!(html, expected, markdown()); |
| 200 | +} |
| 201 | + |
| 202 | +#[test] |
| 203 | +fn test_strikethrough_disabled() { |
| 204 | + let original = "hi ~~no~~"; |
| 205 | + let expected = "<p>hi ~~no~~</p>"; |
| 206 | + |
| 207 | + let html = push_html(original, &HtmlConfig::default()); |
| 208 | + assert_html_eq!(html, expected, markdown()); |
| 209 | +} |
| 210 | + |
| 211 | +#[test] |
| 212 | +fn test_broken_link_callback() { |
| 213 | + let original = r##"[foo], |
| 214 | +[bar], |
| 215 | +[baz], |
| 216 | +
|
| 217 | + [baz]: https://example.org |
| 218 | +"##; |
| 219 | + let expected = r##"<p><a href="https://replaced.example.org" title="some title">foo</a>, |
| 220 | +[bar], |
| 221 | +<a href="https://example.org">baz</a>,</p>"##; |
| 222 | + |
| 223 | + let mut callback = |broken_link: BrokenLink| { |
| 224 | + if &*broken_link.reference == "foo" || &*broken_link.reference == "baz" { |
| 225 | + Some(("https://replaced.example.org".into(), "some title".into())) |
| 226 | + } else { |
| 227 | + None |
| 228 | + } |
| 229 | + }; |
| 230 | + |
| 231 | + let p = Parser::new_with_broken_link_callback(original, Options::empty(), Some(&mut callback)); |
| 232 | + let mut s = String::new(); |
| 233 | + html::push_html(&mut s, p); |
| 234 | + assert_html_eq!(s, expected, markdown()); |
| 235 | +} |
| 236 | + |
| 237 | +#[test] |
| 238 | +fn test_code_with_newlines() { |
| 239 | + for original in ["`\n `x", "` \n`x"] { |
| 240 | + let expected = "<p><code> </code>x</p>"; |
| 241 | + let html = push_html(original, &HtmlConfig::default()); |
| 242 | + assert_html_eq!(html, expected, markdown()); |
| 243 | + } |
| 244 | +} |
| 245 | + |
| 246 | +#[test] |
| 247 | +fn test_code_with_newlines_at_boundaries() { |
| 248 | + let original = "`\nx\n`x"; |
| 249 | + let expected = "<p><code>x</code>x</p>"; |
| 250 | + |
| 251 | + let html = push_html(original, &HtmlConfig::default()); |
| 252 | + assert_html_eq!(html, expected, markdown()); |
| 253 | +} |
| 254 | + |
| 255 | +#[test] |
| 256 | +#[ignore = "TODO - Broken"] |
| 257 | +fn test_trim_whitespace_at_paragraph_end() { |
| 258 | + let original = "one\ntwo \t"; |
| 259 | + let expected = "<p>one\ntwo</p>"; |
| 260 | + |
| 261 | + let html = push_html(original, &HtmlConfig::default()); |
| 262 | + assert_html_eq!(html, expected, markdown()); |
| 263 | +} |
| 264 | + |
| 265 | +#[test] |
| 266 | +#[ignore = "TODO - Broken"] |
| 267 | +fn test_code_with_internal_newlines() { |
| 268 | + for original in ["`\nx \ny\n`x", "`x \ny`x", "`x\n y`x"] { |
| 269 | + let expected = "<p><code>x y</code>x</p>"; |
| 270 | + let html = push_html(original, &HtmlConfig::default()); |
| 271 | + assert_html_eq!(html, expected, markdown()); |
| 272 | + } |
| 273 | +} |
| 274 | + |
| 275 | +#[test] |
| 276 | +#[ignore = "TODO - Broken"] |
| 277 | +fn test_trim_whitespace_and_newline_at_paragraph_end() { |
| 278 | + let expected = "<p>one\ntwo</p>"; |
| 279 | + let mut config = HtmlConfig::default(); |
| 280 | + config.html.break_on_newline = false; |
| 281 | + |
| 282 | + let html = push_html("one\ntwo \t\n", &config); |
| 283 | + assert_html_eq!(html, expected, markdown()); |
| 284 | +} |
| 285 | + |
| 286 | +#[test] |
| 287 | +#[ignore = "TODO - Broken"] |
| 288 | +fn test_trim_space_before_newline_at_paragraph_end() { |
| 289 | + let original = "one\ntwo \n"; |
| 290 | + let expected = "<p>one\ntwo</p>"; |
| 291 | + |
| 292 | + let html = push_html(original, &HtmlConfig::default()); |
| 293 | + assert_html_eq!(html, expected, markdown()); |
| 294 | +} |
| 295 | + |
| 296 | +#[test] |
| 297 | +#[ignore = "TODO - Broken"] |
| 298 | +fn test_trim_space_before_soft_break() { |
| 299 | + let original = "one \ntwo"; |
| 300 | + let expected = "<p>one\ntwo</p>"; |
| 301 | + |
| 302 | + let html = push_html(original, &HtmlConfig::default()); |
| 303 | + assert_html_eq!(html, expected, markdown()); |
| 304 | +} |
0 commit comments