Markdown parser fork with extended syntax for personal use.
at main 1141 lines 31 kB view raw
1use markdown::{ 2 mdast::{Html, Node, Root}, 3 message, to_html, to_html_with_options, to_mdast, 4 unist::Position, 5 CompileOptions, Constructs, Options, ParseOptions, 6}; 7use pretty_assertions::assert_eq; 8 9#[test] 10fn html_flow() -> Result<(), message::Message> { 11 let danger = Options { 12 compile: CompileOptions { 13 allow_dangerous_html: true, 14 allow_dangerous_protocol: true, 15 ..Default::default() 16 }, 17 ..Default::default() 18 }; 19 20 assert_eq!( 21 to_html("<!-- asd -->"), 22 "&lt;!-- asd --&gt;", 23 "should support a heading w/ rank 1" 24 ); 25 26 assert_eq!( 27 to_html_with_options("<!-- asd -->", &danger)?, 28 "<!-- asd -->", 29 "should support a heading w/ rank 1" 30 ); 31 32 assert_eq!( 33 to_html_with_options( 34 "<x>", 35 &Options { 36 parse: ParseOptions { 37 constructs: Constructs { 38 html_flow: false, 39 ..Default::default() 40 }, 41 ..Default::default() 42 }, 43 ..Default::default() 44 } 45 )?, 46 "<p>&lt;x&gt;</p>", 47 "should support turning off html (flow)" 48 ); 49 50 assert_eq!( 51 to_mdast("<div>\nstuff\n</div>", &Default::default())?, 52 Node::Root(Root { 53 children: vec![Node::Html(Html { 54 value: "<div>\nstuff\n</div>".into(), 55 position: Some(Position::new(1, 1, 0, 3, 7, 18)) 56 })], 57 position: Some(Position::new(1, 1, 0, 3, 7, 18)) 58 }), 59 "should support HTML (flow) as `Html`s in mdast" 60 ); 61 62 Ok(()) 63} 64 65#[test] 66fn html_flow_1_raw() -> Result<(), message::Message> { 67 let danger = Options { 68 compile: CompileOptions { 69 allow_dangerous_html: true, 70 allow_dangerous_protocol: true, 71 ..Default::default() 72 }, 73 ..Default::default() 74 }; 75 76 assert_eq!( 77 to_html_with_options( 78 "<pre language=\"haskell\"><code> 79import Text.HTML.TagSoup 80 81main :: IO () 82main = print $ parseTags tags 83</code></pre> 84okay", 85 &danger 86 )?, 87 "<pre language=\"haskell\"><code> 88import Text.HTML.TagSoup 89 90main :: IO () 91main = print $ parseTags tags 92</code></pre> 93<p>okay</p>", 94 "should support raw pre tags (type 1)" 95 ); 96 97 assert_eq!( 98 to_html_with_options( 99 "<script type=\"text/javascript\"> 100// JavaScript example 101 102document.getElementById(\"demo\").innerHTML = \"Hello JavaScript!\"; 103</script> 104okay", 105 &danger 106 )?, 107 "<script type=\"text/javascript\"> 108// JavaScript example 109 110document.getElementById(\"demo\").innerHTML = \"Hello JavaScript!\"; 111</script> 112<p>okay</p>", 113 "should support raw script tags" 114 ); 115 116 assert_eq!( 117 to_html_with_options( 118 "<style 119 type=\"text/css\"> 120h1 {color:red;} 121 122p {color:blue;} 123</style> 124okay", 125 &danger 126 )?, 127 "<style 128 type=\"text/css\"> 129h1 {color:red;} 130 131p {color:blue;} 132</style> 133<p>okay</p>", 134 "should support raw style tags" 135 ); 136 137 assert_eq!( 138 to_html_with_options("<style\n type=\"text/css\">\n\nfoo", &danger)?, 139 "<style\n type=\"text/css\">\n\nfoo", 140 "should support raw tags w/o ending" 141 ); 142 143 assert_eq!( 144 to_html_with_options("<style>p{color:red;}</style>\n*foo*", &danger)?, 145 "<style>p{color:red;}</style>\n<p><em>foo</em></p>", 146 "should support raw tags w/ start and end on a single line" 147 ); 148 149 assert_eq!( 150 to_html_with_options("<script>\nfoo\n</script>1. *bar*", &danger)?, 151 "<script>\nfoo\n</script>1. *bar*", 152 "should support raw tags w/ more data on ending line" 153 ); 154 155 assert_eq!( 156 to_html_with_options("<script", &danger)?, 157 "<script", 158 "should support an eof directly after a raw tag name" 159 ); 160 161 assert_eq!( 162 to_html_with_options("</script\nmore", &danger)?, 163 "<p>&lt;/script\nmore</p>", 164 "should not support a raw closing tag" 165 ); 166 167 assert_eq!( 168 to_html_with_options("<script/", &danger)?, 169 "<p>&lt;script/</p>", 170 "should not support an eof after a self-closing slash" 171 ); 172 173 assert_eq!( 174 to_html_with_options("<script/\n*asd*", &danger)?, 175 "<p>&lt;script/\n<em>asd</em></p>", 176 "should not support a line ending after a self-closing slash" 177 ); 178 179 assert_eq!( 180 to_html_with_options("<script/>", &danger)?, 181 "<script/>", 182 "should support an eof after a self-closing tag" 183 ); 184 185 assert_eq!( 186 to_html_with_options("<script/>\na", &danger)?, 187 "<script/>\na", 188 "should support a line ending after a self-closing tag" 189 ); 190 191 assert_eq!( 192 to_html_with_options("<script/>a", &danger)?, 193 "<p><script/>a</p>", 194 "should not support other characters after a self-closing tag" 195 ); 196 197 assert_eq!( 198 to_html_with_options("<script>a", &danger)?, 199 "<script>a", 200 "should support other characters after a raw opening tag" 201 ); 202 203 // Extra. 204 assert_eq!( 205 to_html_with_options("Foo\n<script", &danger)?, 206 "<p>Foo</p>\n<script", 207 "should support interrupting paragraphs w/ raw tags" 208 ); 209 210 assert_eq!( 211 to_html_with_options("<script>a</script\nb", &danger)?, 212 "<script>a</script\nb", 213 "should not support stopping raw if the closing tag does not have a `>`" 214 ); 215 216 assert_eq!( 217 to_html_with_options("<script>\n \n \n</script>", &danger)?, 218 "<script>\n \n \n</script>", 219 "should support blank lines in raw" 220 ); 221 222 assert_eq!( 223 to_html_with_options("> <script>\na", &danger)?, 224 "<blockquote>\n<script>\n</blockquote>\n<p>a</p>", 225 "should not support lazyness (1)" 226 ); 227 228 assert_eq!( 229 to_html_with_options("> a\n<script>", &danger)?, 230 "<blockquote>\n<p>a</p>\n</blockquote>\n<script>", 231 "should not support lazyness (2)" 232 ); 233 234 Ok(()) 235} 236 237#[test] 238fn html_flow_2_comment() -> Result<(), message::Message> { 239 let danger = Options { 240 compile: CompileOptions { 241 allow_dangerous_html: true, 242 allow_dangerous_protocol: true, 243 ..Default::default() 244 }, 245 ..Default::default() 246 }; 247 248 assert_eq!( 249 to_html_with_options("<!-- Foo\n\nbar\n baz -->\nokay", &danger)?, 250 "<!-- Foo\n\nbar\n baz -->\n<p>okay</p>", 251 "should support comments (type 2)" 252 ); 253 254 assert_eq!( 255 to_html_with_options("<!-- foo -->*bar*\n*baz*", &danger)?, 256 "<!-- foo -->*bar*\n<p><em>baz</em></p>", 257 "should support comments w/ start and end on a single line" 258 ); 259 260 assert_eq!( 261 to_html_with_options("<!-asd-->", &danger)?, 262 "<p>&lt;!-asd--&gt;</p>", 263 "should not support a single dash to start comments" 264 ); 265 266 assert_eq!( 267 to_html_with_options("<!-->", &danger)?, 268 "<!-->", 269 "should support comments where the start dashes are the end dashes (1)" 270 ); 271 272 assert_eq!( 273 to_html_with_options("<!--->", &danger)?, 274 "<!--->", 275 "should support comments where the start dashes are the end dashes (2)" 276 ); 277 278 assert_eq!( 279 to_html_with_options("<!---->", &danger)?, 280 "<!---->", 281 "should support empty comments" 282 ); 283 284 // If the `\"` is encoded, we’re in text. If it remains, we’re in HTML. 285 assert_eq!( 286 to_html_with_options("<!--\n->\n\"", &danger)?, 287 "<!--\n->\n\"", 288 "should not end a comment at one dash (`->`)" 289 ); 290 assert_eq!( 291 to_html_with_options("<!--\n-->\n\"", &danger)?, 292 "<!--\n-->\n<p>&quot;</p>", 293 "should end a comment at two dashes (`-->`)" 294 ); 295 assert_eq!( 296 to_html_with_options("<!--\n--->\n\"", &danger)?, 297 "<!--\n--->\n<p>&quot;</p>", 298 "should end a comment at three dashes (`--->`)" 299 ); 300 assert_eq!( 301 to_html_with_options("<!--\n---->\n\"", &danger)?, 302 "<!--\n---->\n<p>&quot;</p>", 303 "should end a comment at four dashes (`---->`)" 304 ); 305 306 assert_eq!( 307 to_html_with_options(" <!-- foo -->", &danger)?, 308 " <!-- foo -->", 309 "should support comments w/ indent" 310 ); 311 312 assert_eq!( 313 to_html_with_options(" <!-- foo -->", &danger)?, 314 "<pre><code>&lt;!-- foo --&gt;\n</code></pre>", 315 "should not support comments w/ a 4 character indent" 316 ); 317 318 // Extra. 319 assert_eq!( 320 to_html_with_options("Foo\n<!--", &danger)?, 321 "<p>Foo</p>\n<!--", 322 "should support interrupting paragraphs w/ comments" 323 ); 324 325 assert_eq!( 326 to_html_with_options("<!--\n \n \n-->", &danger)?, 327 "<!--\n \n \n-->", 328 "should support blank lines in comments" 329 ); 330 331 assert_eq!( 332 to_html_with_options("> <!--\na", &danger)?, 333 "<blockquote>\n<!--\n</blockquote>\n<p>a</p>", 334 "should not support lazyness (1)" 335 ); 336 337 assert_eq!( 338 to_html_with_options("> a\n<!--", &danger)?, 339 "<blockquote>\n<p>a</p>\n</blockquote>\n<!--", 340 "should not support lazyness (2)" 341 ); 342 343 Ok(()) 344} 345 346#[test] 347fn html_flow_3_instruction() -> Result<(), message::Message> { 348 let danger = Options { 349 compile: CompileOptions { 350 allow_dangerous_html: true, 351 allow_dangerous_protocol: true, 352 ..Default::default() 353 }, 354 ..Default::default() 355 }; 356 357 assert_eq!( 358 to_html_with_options("<?php\n\n echo \">\";\n\n?>\nokay", &danger)?, 359 "<?php\n\n echo \">\";\n\n?>\n<p>okay</p>", 360 "should support instructions (type 3)" 361 ); 362 363 assert_eq!( 364 to_html_with_options("<?>", &danger)?, 365 "<?>", 366 "should support empty instructions where the `?` is part of both the start and the end" 367 ); 368 369 assert_eq!( 370 to_html_with_options("<??>", &danger)?, 371 "<??>", 372 "should support empty instructions" 373 ); 374 375 // Extra. 376 assert_eq!( 377 to_html_with_options("Foo\n<?", &danger)?, 378 "<p>Foo</p>\n<?", 379 "should support interrupting paragraphs w/ instructions" 380 ); 381 382 assert_eq!( 383 to_html_with_options("<?\n \n \n?>", &danger)?, 384 "<?\n \n \n?>", 385 "should support blank lines in instructions" 386 ); 387 388 assert_eq!( 389 to_html_with_options("> <?\na", &danger)?, 390 "<blockquote>\n<?\n</blockquote>\n<p>a</p>", 391 "should not support lazyness (1)" 392 ); 393 394 assert_eq!( 395 to_html_with_options("> a\n<?", &danger)?, 396 "<blockquote>\n<p>a</p>\n</blockquote>\n<?", 397 "should not support lazyness (2)" 398 ); 399 400 Ok(()) 401} 402 403#[test] 404fn html_flow_4_declaration() -> Result<(), message::Message> { 405 let danger = Options { 406 compile: CompileOptions { 407 allow_dangerous_html: true, 408 allow_dangerous_protocol: true, 409 ..Default::default() 410 }, 411 ..Default::default() 412 }; 413 414 assert_eq!( 415 to_html_with_options("<!DOCTYPE html>", &danger)?, 416 "<!DOCTYPE html>", 417 "should support declarations (type 4)" 418 ); 419 420 assert_eq!( 421 to_html_with_options("<!123>", &danger)?, 422 "<p>&lt;!123&gt;</p>", 423 "should not support declarations that start w/o an alpha" 424 ); 425 426 assert_eq!( 427 to_html_with_options("<!>", &danger)?, 428 "<p>&lt;!&gt;</p>", 429 "should not support declarations w/o an identifier" 430 ); 431 432 assert_eq!( 433 to_html_with_options("<!a>", &danger)?, 434 "<!a>", 435 "should support declarations w/o a single alpha as identifier" 436 ); 437 438 // Extra. 439 assert_eq!( 440 to_html_with_options("Foo\n<!d", &danger)?, 441 "<p>Foo</p>\n<!d", 442 "should support interrupting paragraphs w/ declarations" 443 ); 444 445 // Note about the lower letter: 446 // <https://github.com/commonmark/commonmark-spec/pull/621> 447 assert_eq!( 448 to_html_with_options("<!a\n \n \n>", &danger)?, 449 "<!a\n \n \n>", 450 "should support blank lines in declarations" 451 ); 452 453 assert_eq!( 454 to_html_with_options("> <!a\nb", &danger)?, 455 "<blockquote>\n<!a\n</blockquote>\n<p>b</p>", 456 "should not support lazyness (1)" 457 ); 458 459 assert_eq!( 460 to_html_with_options("> a\n<!b", &danger)?, 461 "<blockquote>\n<p>a</p>\n</blockquote>\n<!b", 462 "should not support lazyness (2)" 463 ); 464 465 Ok(()) 466} 467 468#[test] 469fn html_flow_5_cdata() -> Result<(), message::Message> { 470 let danger = Options { 471 compile: CompileOptions { 472 allow_dangerous_html: true, 473 allow_dangerous_protocol: true, 474 ..Default::default() 475 }, 476 ..Default::default() 477 }; 478 479 assert_eq!( 480 to_html_with_options( 481 "<![CDATA[\nfunction matchwo(a,b)\n{\n if (a < b && a < 0) then {\n return 1;\n\n } else {\n\n return 0;\n }\n}\n]]>\nokay", 482 &danger 483 )?, 484 "<![CDATA[\nfunction matchwo(a,b)\n{\n if (a < b && a < 0) then {\n return 1;\n\n } else {\n\n return 0;\n }\n}\n]]>\n<p>okay</p>", 485 "should support cdata (type 5)" 486 ); 487 488 assert_eq!( 489 to_html_with_options("<![CDATA[]]>", &danger)?, 490 "<![CDATA[]]>", 491 "should support empty cdata" 492 ); 493 494 assert_eq!( 495 to_html_with_options("<![CDATA]]>", &danger)?, 496 "<p>&lt;![CDATA]]&gt;</p>", 497 "should not support cdata w/ a missing `[`" 498 ); 499 500 assert_eq!( 501 to_html_with_options("<![CDATA[]]]>", &danger)?, 502 "<![CDATA[]]]>", 503 "should support cdata w/ a single `]` as content" 504 ); 505 506 // Extra. 507 assert_eq!( 508 to_html_with_options("Foo\n<![CDATA[", &danger)?, 509 "<p>Foo</p>\n<![CDATA[", 510 "should support interrupting paragraphs w/ cdata" 511 ); 512 513 // Note: cmjs parses this differently. 514 // See: <https://github.com/commonmark/commonmark.js/issues/193> 515 assert_eq!( 516 to_html_with_options("<![cdata[]]>", &danger)?, 517 "<p>&lt;![cdata[]]&gt;</p>", 518 "should not support lowercase cdata" 519 ); 520 521 assert_eq!( 522 to_html_with_options("<![CDATA[\n \n \n]]>", &danger)?, 523 "<![CDATA[\n \n \n]]>", 524 "should support blank lines in cdata" 525 ); 526 527 assert_eq!( 528 to_html_with_options("> <![CDATA[\na", &danger)?, 529 "<blockquote>\n<![CDATA[\n</blockquote>\n<p>a</p>", 530 "should not support lazyness (1)" 531 ); 532 533 assert_eq!( 534 to_html_with_options("> a\n<![CDATA[", &danger)?, 535 "<blockquote>\n<p>a</p>\n</blockquote>\n<![CDATA[", 536 "should not support lazyness (2)" 537 ); 538 539 Ok(()) 540} 541 542#[test] 543fn html_flow_6_basic() -> Result<(), message::Message> { 544 let danger = Options { 545 compile: CompileOptions { 546 allow_dangerous_html: true, 547 allow_dangerous_protocol: true, 548 ..Default::default() 549 }, 550 ..Default::default() 551 }; 552 553 assert_eq!( 554 to_html_with_options( 555 "<table><tr><td>\n<pre>\n**Hello**,\n\n_world_.\n</pre>\n</td></tr></table>", 556 &danger 557 )?, 558 "<table><tr><td>\n<pre>\n**Hello**,\n<p><em>world</em>.\n</pre></p>\n</td></tr></table>", 559 "should support html (basic)" 560 ); 561 562 assert_eq!( 563 to_html_with_options( 564 "<table> 565 <tr> 566 <td> 567 hi 568 </td> 569 </tr> 570</table> 571 572okay.", 573 &danger 574 )?, 575 "<table> 576 <tr> 577 <td> 578 hi 579 </td> 580 </tr> 581</table> 582<p>okay.</p>", 583 "should support html of type 6 (1)" 584 ); 585 586 assert_eq!( 587 to_html_with_options(" <div>\n *hello*\n <foo><a>", &danger)?, 588 " <div>\n *hello*\n <foo><a>", 589 "should support html of type 6 (2)" 590 ); 591 592 assert_eq!( 593 to_html_with_options("</div>\n*foo*", &danger)?, 594 "</div>\n*foo*", 595 "should support html starting w/ a closing tag" 596 ); 597 598 assert_eq!( 599 to_html_with_options("<DIV CLASS=\"foo\">\n\n*Markdown*\n\n</DIV>", &danger)?, 600 "<DIV CLASS=\"foo\">\n<p><em>Markdown</em></p>\n</DIV>", 601 "should support html w/ markdown in between" 602 ); 603 604 assert_eq!( 605 to_html_with_options("<div id=\"foo\"\n class=\"bar\">\n</div>", &danger)?, 606 "<div id=\"foo\"\n class=\"bar\">\n</div>", 607 "should support html w/ line endings (1)" 608 ); 609 610 assert_eq!( 611 to_html_with_options("<div id=\"foo\" class=\"bar\n baz\">\n</div>", &danger)?, 612 "<div id=\"foo\" class=\"bar\n baz\">\n</div>", 613 "should support html w/ line endings (2)" 614 ); 615 616 assert_eq!( 617 to_html_with_options("<div>\n*foo*\n\n*bar*", &danger)?, 618 "<div>\n*foo*\n<p><em>bar</em></p>", 619 "should support an unclosed html element" 620 ); 621 622 assert_eq!( 623 to_html_with_options("<div id=\"foo\"\n*hi*", &danger)?, 624 "<div id=\"foo\"\n*hi*", 625 "should support garbage html (1)" 626 ); 627 628 assert_eq!( 629 to_html_with_options("<div class\nfoo", &danger)?, 630 "<div class\nfoo", 631 "should support garbage html (2)" 632 ); 633 634 assert_eq!( 635 to_html_with_options("<div *???-&&&-<---\n*foo*", &danger)?, 636 "<div *???-&&&-<---\n*foo*", 637 "should support garbage html (3)" 638 ); 639 640 assert_eq!( 641 to_html_with_options("<div><a href=\"bar\">*foo*</a></div>", &danger)?, 642 "<div><a href=\"bar\">*foo*</a></div>", 643 "should support other tags in the opening (1)" 644 ); 645 646 assert_eq!( 647 to_html_with_options("<table><tr><td>\nfoo\n</td></tr></table>", &danger)?, 648 "<table><tr><td>\nfoo\n</td></tr></table>", 649 "should support other tags in the opening (2)" 650 ); 651 652 assert_eq!( 653 to_html_with_options("<div></div>\n``` c\nint x = 33;\n```", &danger)?, 654 "<div></div>\n``` c\nint x = 33;\n```", 655 "should include everything ’till a blank line" 656 ); 657 658 assert_eq!( 659 to_html_with_options("> <div>\n> foo\n\nbar", &danger)?, 660 "<blockquote>\n<div>\nfoo\n</blockquote>\n<p>bar</p>", 661 "should support basic tags w/o ending in containers (1)" 662 ); 663 664 assert_eq!( 665 to_html_with_options("- <div>\n- foo", &danger)?, 666 "<ul>\n<li>\n<div>\n</li>\n<li>foo</li>\n</ul>", 667 "should support basic tags w/o ending in containers (2)" 668 ); 669 670 assert_eq!( 671 to_html_with_options(" <div>", &danger)?, 672 " <div>", 673 "should support basic tags w/ indent" 674 ); 675 676 assert_eq!( 677 to_html_with_options(" <div>", &danger)?, 678 "<pre><code>&lt;div&gt;\n</code></pre>", 679 "should not support basic tags w/ a 4 character indent" 680 ); 681 682 assert_eq!( 683 to_html_with_options("Foo\n<div>\nbar\n</div>", &danger)?, 684 "<p>Foo</p>\n<div>\nbar\n</div>", 685 "should support interrupting paragraphs w/ basic tags" 686 ); 687 688 assert_eq!( 689 to_html_with_options("<div>\nbar\n</div>\n*foo*", &danger)?, 690 "<div>\nbar\n</div>\n*foo*", 691 "should require a blank line to end" 692 ); 693 694 assert_eq!( 695 to_html_with_options("<div>\n\n*Emphasized* text.\n\n</div>", &danger)?, 696 "<div>\n<p><em>Emphasized</em> text.</p>\n</div>", 697 "should support interleaving w/ blank lines" 698 ); 699 700 assert_eq!( 701 to_html_with_options("<div>\n*Emphasized* text.\n</div>", &danger)?, 702 "<div>\n*Emphasized* text.\n</div>", 703 "should not support interleaving w/o blank lines" 704 ); 705 706 assert_eq!( 707 to_html_with_options( 708 "<table>\n\n<tr>\n\n<td>\nHi\n</td>\n\n</tr>\n\n</table>", 709 &danger 710 )?, 711 "<table>\n<tr>\n<td>\nHi\n</td>\n</tr>\n</table>", 712 "should support blank lines between adjacent html" 713 ); 714 715 assert_eq!( 716 to_html_with_options( 717 "<table> 718 719 <tr> 720 721 <td> 722 Hi 723 </td> 724 725 </tr> 726 727</table>", 728 &danger 729 )?, 730 "<table> 731 <tr> 732<pre><code>&lt;td&gt; 733 Hi 734&lt;/td&gt; 735</code></pre> 736 </tr> 737</table>", 738 "should not support indented, blank-line delimited, adjacent html" 739 ); 740 741 assert_eq!( 742 to_html_with_options("</1>", &danger)?, 743 "<p>&lt;/1&gt;</p>", 744 "should not support basic tags w/ an incorrect name start character" 745 ); 746 747 assert_eq!( 748 to_html_with_options("<div", &danger)?, 749 "<div", 750 "should support an eof directly after a basic tag name" 751 ); 752 753 assert_eq!( 754 to_html_with_options("<div\n", &danger)?, 755 "<div\n", 756 "should support a line ending directly after a tag name" 757 ); 758 759 assert_eq!( 760 to_html_with_options("<div ", &danger)?, 761 "<div ", 762 "should support an eof after a space directly after a tag name" 763 ); 764 765 assert_eq!( 766 to_html_with_options("<div/", &danger)?, 767 "<p>&lt;div/</p>", 768 "should not support an eof directly after a self-closing slash" 769 ); 770 771 assert_eq!( 772 to_html_with_options("<div/\n*asd*", &danger)?, 773 "<p>&lt;div/\n<em>asd</em></p>", 774 "should not support a line ending after a self-closing slash" 775 ); 776 777 assert_eq!( 778 to_html_with_options("<div/>", &danger)?, 779 "<div/>", 780 "should support an eof after a self-closing tag" 781 ); 782 783 assert_eq!( 784 to_html_with_options("<div/>\na", &danger)?, 785 "<div/>\na", 786 "should support a line ending after a self-closing tag" 787 ); 788 789 assert_eq!( 790 to_html_with_options("<div/>a", &danger)?, 791 "<div/>a", 792 "should support another character after a self-closing tag" 793 ); 794 795 assert_eq!( 796 to_html_with_options("<div>a", &danger)?, 797 "<div>a", 798 "should support another character after a basic opening tag" 799 ); 800 801 // Extra. 802 assert_eq!( 803 to_html_with_options("Foo\n<div/>", &danger)?, 804 "<p>Foo</p>\n<div/>", 805 "should support interrupting paragraphs w/ self-closing basic tags" 806 ); 807 808 assert_eq!( 809 to_html_with_options("<div\n \n \n>", &danger)?, 810 "<div\n<blockquote>\n</blockquote>", 811 "should not support blank lines in basic" 812 ); 813 814 assert_eq!( 815 to_html_with_options("> <div\na", &danger)?, 816 "<blockquote>\n<div\n</blockquote>\n<p>a</p>", 817 "should not support lazyness (1)" 818 ); 819 820 assert_eq!( 821 to_html_with_options("> a\n<div", &danger)?, 822 "<blockquote>\n<p>a</p>\n</blockquote>\n<div", 823 "should not support lazyness (2)" 824 ); 825 826 Ok(()) 827} 828 829#[test] 830fn html_flow_7_complete() -> Result<(), message::Message> { 831 let danger = Options { 832 compile: CompileOptions { 833 allow_dangerous_html: true, 834 allow_dangerous_protocol: true, 835 ..Default::default() 836 }, 837 ..Default::default() 838 }; 839 840 assert_eq!( 841 to_html_with_options("<a href=\"foo\">\n*bar*\n</a>", &danger)?, 842 "<a href=\"foo\">\n*bar*\n</a>", 843 "should support complete tags (type 7)" 844 ); 845 846 assert_eq!( 847 to_html_with_options("<Warning>\n*bar*\n</Warning>", &danger)?, 848 "<Warning>\n*bar*\n</Warning>", 849 "should support non-html tag names" 850 ); 851 852 assert_eq!( 853 to_html_with_options("<i class=\"foo\">\n*bar*\n</i>", &danger)?, 854 "<i class=\"foo\">\n*bar*\n</i>", 855 "should support non-“block” html tag names (1)" 856 ); 857 858 assert_eq!( 859 to_html_with_options("<del>\n*foo*\n</del>", &danger)?, 860 "<del>\n*foo*\n</del>", 861 "should support non-“block” html tag names (2)" 862 ); 863 864 assert_eq!( 865 to_html_with_options("</ins>\n*bar*", &danger)?, 866 "</ins>\n*bar*", 867 "should support closing tags" 868 ); 869 870 assert_eq!( 871 to_html_with_options("<del>\n\n*foo*\n\n</del>", &danger)?, 872 "<del>\n<p><em>foo</em></p>\n</del>", 873 "should support interleaving" 874 ); 875 876 assert_eq!( 877 to_html_with_options("<del>*foo*</del>", &danger)?, 878 "<p><del><em>foo</em></del></p>", 879 "should not support interleaving w/o blank lines" 880 ); 881 882 assert_eq!( 883 to_html_with_options("<div>\n \nasd", &danger)?, 884 "<div>\n<p>asd</p>", 885 "should support interleaving w/ whitespace-only blank lines" 886 ); 887 888 assert_eq!( 889 to_html_with_options("Foo\n<a href=\"bar\">\nbaz", &danger)?, 890 "<p>Foo\n<a href=\"bar\">\nbaz</p>", 891 "should not support interrupting paragraphs w/ complete tags" 892 ); 893 894 assert_eq!( 895 to_html_with_options("<x", &danger)?, 896 "<p>&lt;x</p>", 897 "should not support an eof directly after a tag name" 898 ); 899 900 assert_eq!( 901 to_html_with_options("<x/", &danger)?, 902 "<p>&lt;x/</p>", 903 "should not support an eof directly after a self-closing slash" 904 ); 905 906 assert_eq!( 907 to_html_with_options("<x\n", &danger)?, 908 "<p>&lt;x</p>\n", 909 "should not support a line ending directly after a tag name" 910 ); 911 912 assert_eq!( 913 to_html_with_options("<x ", &danger)?, 914 "<p>&lt;x</p>", 915 "should not support an eof after a space directly after a tag name" 916 ); 917 918 assert_eq!( 919 to_html_with_options("<x/", &danger)?, 920 "<p>&lt;x/</p>", 921 "should not support an eof directly after a self-closing slash" 922 ); 923 924 assert_eq!( 925 to_html_with_options("<x/\n*asd*", &danger)?, 926 "<p>&lt;x/\n<em>asd</em></p>", 927 "should not support a line ending after a self-closing slash" 928 ); 929 930 assert_eq!( 931 to_html_with_options("<x/>", &danger)?, 932 "<x/>", 933 "should support an eof after a self-closing tag" 934 ); 935 936 assert_eq!( 937 to_html_with_options("<x/>\na", &danger)?, 938 "<x/>\na", 939 "should support a line ending after a self-closing tag" 940 ); 941 942 assert_eq!( 943 to_html_with_options("<x/>a", &danger)?, 944 "<p><x/>a</p>", 945 "should not support another character after a self-closing tag" 946 ); 947 948 assert_eq!( 949 to_html_with_options("<x>a", &danger)?, 950 "<p><x>a</p>", 951 "should not support another character after an opening tag" 952 ); 953 954 assert_eq!( 955 to_html_with_options("<x y>", &danger)?, 956 "<x y>", 957 "should support boolean attributes in a complete tag" 958 ); 959 960 assert_eq!( 961 to_html_with_options("<x\ny>", &danger)?, 962 "<p><x\ny></p>", 963 "should not support a line ending before an attribute name" 964 ); 965 966 assert_eq!( 967 to_html_with_options("<x\n y>", &danger)?, 968 "<p><x\ny></p>", 969 "should not support a line ending w/ whitespace before an attribute name" 970 ); 971 972 assert_eq!( 973 to_html_with_options("<x\n \ny>", &danger)?, 974 "<p>&lt;x</p>\n<p>y&gt;</p>", 975 "should not support a line ending w/ whitespace and another line ending before an attribute name" 976 ); 977 978 assert_eq!( 979 to_html_with_options("<x y\nz>", &danger)?, 980 "<p><x y\nz></p>", 981 "should not support a line ending between attribute names" 982 ); 983 984 assert_eq!( 985 to_html_with_options("<x y z>", &danger)?, 986 "<x y z>", 987 "should support whitespace between attribute names" 988 ); 989 990 assert_eq!( 991 to_html_with_options("<x:y>", &danger)?, 992 "<p>&lt;x:y&gt;</p>", 993 "should not support a colon in a tag name" 994 ); 995 996 assert_eq!( 997 to_html_with_options("<x_y>", &danger)?, 998 "<p>&lt;x_y&gt;</p>", 999 "should not support an underscore in a tag name" 1000 ); 1001 1002 assert_eq!( 1003 to_html_with_options("<x.y>", &danger)?, 1004 "<p>&lt;x.y&gt;</p>", 1005 "should not support a dot in a tag name" 1006 ); 1007 1008 assert_eq!( 1009 to_html_with_options("<x :y>", &danger)?, 1010 "<x :y>", 1011 "should support a colon to start an attribute name" 1012 ); 1013 1014 assert_eq!( 1015 to_html_with_options("<x _y>", &danger)?, 1016 "<x _y>", 1017 "should support an underscore to start an attribute name" 1018 ); 1019 1020 assert_eq!( 1021 to_html_with_options("<x .y>", &danger)?, 1022 "<p>&lt;x .y&gt;</p>", 1023 "should not support a dot to start an attribute name" 1024 ); 1025 1026 assert_eq!( 1027 to_html_with_options("<x y:>", &danger)?, 1028 "<x y:>", 1029 "should support a colon to end an attribute name" 1030 ); 1031 1032 assert_eq!( 1033 to_html_with_options("<x y_>", &danger)?, 1034 "<x y_>", 1035 "should support an underscore to end an attribute name" 1036 ); 1037 1038 assert_eq!( 1039 to_html_with_options("<x y.>", &danger)?, 1040 "<x y.>", 1041 "should support a dot to end an attribute name" 1042 ); 1043 1044 assert_eq!( 1045 to_html_with_options("<x y123>", &danger)?, 1046 "<x y123>", 1047 "should support numbers to end an attribute name" 1048 ); 1049 1050 assert_eq!( 1051 to_html_with_options("<x data->", &danger)?, 1052 "<x data->", 1053 "should support a dash to end an attribute name" 1054 ); 1055 1056 assert_eq!( 1057 to_html_with_options("<x y=>", &danger)?, 1058 "<p>&lt;x y=&gt;</p>", 1059 "should not upport an initializer w/o a value" 1060 ); 1061 1062 assert_eq!( 1063 to_html_with_options("<x y==>", &danger)?, 1064 "<p>&lt;x y==&gt;</p>", 1065 "should not support an equals to as an initializer" 1066 ); 1067 1068 assert_eq!( 1069 to_html_with_options("<x y=z>", &danger)?, 1070 "<x y=z>", 1071 "should support a single character as an unquoted attribute value" 1072 ); 1073 1074 assert_eq!( 1075 to_html_with_options("<x y=\"\">", &danger)?, 1076 "<x y=\"\">", 1077 "should support an empty double quoted attribute value" 1078 ); 1079 1080 assert_eq!( 1081 to_html_with_options("<x y=\"\">", &danger)?, 1082 "<x y=\"\">", 1083 "should support an empty single quoted attribute value" 1084 ); 1085 1086 assert_eq!( 1087 to_html_with_options("<x y=\"\n\">", &danger)?, 1088 "<p><x y=\"\n\"></p>", 1089 "should not support a line ending in a double quoted attribute value" 1090 ); 1091 1092 assert_eq!( 1093 to_html_with_options("<x y=\"\n\">", &danger)?, 1094 "<p><x y=\"\n\"></p>", 1095 "should not support a line ending in a single quoted attribute value" 1096 ); 1097 1098 assert_eq!( 1099 to_html_with_options("<w x=y\nz>", &danger)?, 1100 "<p><w x=y\nz></p>", 1101 "should not support a line ending in/after an unquoted attribute value" 1102 ); 1103 1104 assert_eq!( 1105 to_html_with_options("<w x=y\"z>", &danger)?, 1106 "<p>&lt;w x=y&quot;z&gt;</p>", 1107 "should not support a double quote in/after an unquoted attribute value" 1108 ); 1109 1110 assert_eq!( 1111 to_html_with_options("<w x=y'z>", &danger)?, 1112 "<p>&lt;w x=y'z&gt;</p>", 1113 "should not support a single quote in/after an unquoted attribute value" 1114 ); 1115 1116 assert_eq!( 1117 to_html_with_options("<x y=\"\"z>", &danger)?, 1118 "<p>&lt;x y=&quot;&quot;z&gt;</p>", 1119 "should not support an attribute after a double quoted attribute value" 1120 ); 1121 1122 assert_eq!( 1123 to_html_with_options("<x>\n \n \n>", &danger)?, 1124 "<x>\n<blockquote>\n</blockquote>", 1125 "should not support blank lines in complete" 1126 ); 1127 1128 assert_eq!( 1129 to_html_with_options("> <a>\n*bar*", &danger)?, 1130 "<blockquote>\n<a>\n</blockquote>\n<p><em>bar</em></p>", 1131 "should not support lazyness (1)" 1132 ); 1133 1134 assert_eq!( 1135 to_html_with_options("> a\n<a>", &danger)?, 1136 "<blockquote>\n<p>a</p>\n</blockquote>\n<a>", 1137 "should not support lazyness (2)" 1138 ); 1139 1140 Ok(()) 1141}