diff options
author | Titus Wormer <tituswormer@gmail.com> | 2022-06-24 17:57:10 +0200 |
---|---|---|
committer | Titus Wormer <tituswormer@gmail.com> | 2022-06-24 17:57:10 +0200 |
commit | a3dd207e3b1ebcbcb6cec0f703a695e51ae4ece0 (patch) | |
tree | 7b4bf040da23a03f38efe92a252e187a630a14f6 /tests | |
parent | e7b3761c8cd6f0f902dd9927e4fbf2589465ed57 (diff) | |
download | markdown-rs-a3dd207e3b1ebcbcb6cec0f703a695e51ae4ece0.tar.gz markdown-rs-a3dd207e3b1ebcbcb6cec0f703a695e51ae4ece0.tar.bz2 markdown-rs-a3dd207e3b1ebcbcb6cec0f703a695e51ae4ece0.zip |
Add link, images (resource)
This is still some messy code that needs cleaning up, but it adds support for
links and images, of the resource kind (`[a](b)`).
References (`[a][b]`) are parsed and will soon be supported, but need matching.
* Fix bug to pad percent-encoded bytes when normalizing urls
* Fix bug with escapes counting as balancing in destination
* Add `space_or_tab_one_line_ending`, to parse whitespace including up to
one line ending (but not a blank line)
* Add `ParserState` to share codes, definitions, etc
Diffstat (limited to 'tests')
-rw-r--r-- | tests/character_escape.rs | 11 | ||||
-rw-r--r-- | tests/character_reference.rs | 24 | ||||
-rw-r--r-- | tests/image.rs | 229 | ||||
-rw-r--r-- | tests/link_resource.rs | 464 | ||||
-rw-r--r-- | tests/misc_dangerous_protocol.rs | 324 | ||||
-rw-r--r-- | tests/misc_tabs.rs | 66 | ||||
-rw-r--r-- | tests/misc_url.rs | 107 |
7 files changed, 951 insertions, 274 deletions
diff --git a/tests/character_escape.rs b/tests/character_escape.rs index e4f23d2..3e3e839 100644 --- a/tests/character_escape.rs +++ b/tests/character_escape.rs @@ -61,12 +61,11 @@ fn character_escape() { "should not escape in flow html" ); - // To do: link (reference). - // assert_eq!( - // micromark("[foo](/bar\\* \"ti\\*tle\")"), - // "<p><a href=\"/bar*\" title=\"ti*tle\">foo</a></p>", - // "should escape in resource and title" - // ); + assert_eq!( + micromark("[foo](/bar\\* \"ti\\*tle\")"), + "<p><a href=\"/bar*\" title=\"ti*tle\">foo</a></p>", + "should escape in resource and title" + ); // To do: link (reference). // assert_eq!( diff --git a/tests/character_reference.rs b/tests/character_reference.rs index 136ce17..3d2111e 100644 --- a/tests/character_reference.rs +++ b/tests/character_reference.rs @@ -55,14 +55,13 @@ fn character_reference() { "should not care about character references in html" ); - // To do: link (resource). - // assert_eq!( - // micromark("[foo](/föö \"föö\")"), - // "<p><a href=\"/f%C3%B6%C3%B6\" title=\"föö\">foo</a></p>", - // "should support character references in resource URLs and titles" - // ); + assert_eq!( + micromark("[foo](/föö \"föö\")"), + "<p><a href=\"/f%C3%B6%C3%B6\" title=\"föö\">foo</a></p>", + "should support character references in resource URLs and titles" + ); - // To do: link (resource). + // To do: link (reference). // assert_eq!( // micromark("[foo]: /föö \"föö\"\n\n[foo]"), // "<p><a href=\"/f%C3%B6%C3%B6\" title=\"föö\">foo</a></p>", @@ -101,12 +100,11 @@ fn character_reference() { // "should not support character references as construct markers (2)" // ); - // To do: link (resource). - // assert_eq!( - // micromark("[a](url "tit")"), - // "<p>[a](url "tit")</p>", - // "should not support character references as construct markers (3)" - // ); + assert_eq!( + micromark("[a](url "tit")"), + "<p>[a](url "tit")</p>", + "should not support character references as construct markers (3)" + ); assert_eq!( micromark("foo bar"), diff --git a/tests/image.rs b/tests/image.rs new file mode 100644 index 0000000..a54c8d2 --- /dev/null +++ b/tests/image.rs @@ -0,0 +1,229 @@ +extern crate micromark; +use micromark::micromark; + +#[test] +fn image() { + assert_eq!( + micromark("[link](/uri \"title\")"), + "<p><a href=\"/uri\" title=\"title\">link</a></p>", + "should support links" + ); + assert_eq!( + micromark("![foo](/url \"title\")"), + "<p><img src=\"/url\" alt=\"foo\" title=\"title\" /></p>", + "should support image w/ resource" + ); + + // To do: attention. + // assert_eq!( + // micromark("[foo *bar*]: train.jpg \"train & tracks\"\n\n![foo *bar*]"), + // "<p><img src=\"train.jpg\" alt=\"foo bar\" title=\"train & tracks\" /></p>", + // "should support image as shortcut reference" + // ); + + // To do: tags in images. + // assert_eq!( + // micromark("![foo ![bar](/url)](/url2)"), + // "<p><img src=\"/url2\" alt=\"foo bar\" /></p>", + // "should “support” images in images" + // ); + + // To do: tags in images. + // assert_eq!( + // micromark("![foo [bar](/url)](/url2)"), + // "<p><img src=\"/url2\" alt=\"foo bar\" /></p>", + // "should “support” links in images" + // ); + + // To do: tags in images. + // assert_eq!( + // micromark("[foo *bar*]: train.jpg \"train & tracks\"\n\n![foo *bar*][]"), + // "<p><img src=\"train.jpg\" alt=\"foo bar\" title=\"train & tracks\" /></p>", + // "should support “content” in images" + // ); + + // To do: tags in images, attention, references. + // assert_eq!( + // micromark("[FOOBAR]: train.jpg \"train & tracks\"\n\n![foo *bar*][foobar]"), + // "<p><img src=\"train.jpg\" alt=\"foo bar\" title=\"train & tracks\" /></p>", + // "should support “content” in images" + // ); + + assert_eq!( + micromark("![foo](train.jpg)"), + "<p><img src=\"train.jpg\" alt=\"foo\" /></p>", + "should support images w/o title" + ); + + assert_eq!( + micromark("My ![foo bar](/path/to/train.jpg \"title\" )"), + "<p>My <img src=\"/path/to/train.jpg\" alt=\"foo bar\" title=\"title\" /></p>", + "should support images w/ lots of whitespace" + ); + + assert_eq!( + micromark("![foo](<url>)"), + "<p><img src=\"url\" alt=\"foo\" /></p>", + "should support images w/ enclosed destinations" + ); + + assert_eq!( + micromark("![](/url)"), + "<p><img src=\"/url\" alt=\"\" /></p>", + "should support images w/ empty labels" + ); + + // To do: references. + // assert_eq!( + // micromark("[bar]: /url\n\n![foo][bar]"), + // "<p><img src=\"/url\" alt=\"foo\" /></p>", + // "should support full references (1)" + // ); + + // To do: references. + // assert_eq!( + // micromark("[BAR]: /url\n\n![foo][bar]"), + // "<p><img src=\"/url\" alt=\"foo\" /></p>", + // "should support full references (2)" + // ); + + // To do: references. + // assert_eq!( + // micromark("[foo]: /url \"title\"\n\n![foo][]"), + // "<p><img src=\"/url\" alt=\"foo\" title=\"title\" /></p>", + // "should support collapsed references (1)" + // ); + + // To do: references, attention, tags in images. + // assert_eq!( + // micromark("[*foo* bar]: /url \"title\"\n\n![*foo* bar][]"), + // "<p><img src=\"/url\" alt=\"foo bar\" title=\"title\" /></p>", + // "should support collapsed references (2)" + // ); + + // To do: references. + // assert_eq!( + // micromark("[foo]: /url \"title\"\n\n![Foo][]"), + // "<p><img src=\"/url\" alt=\"Foo\" title=\"title\" /></p>", + // "should support case-insensitive labels" + // ); + + // To do: references. + // assert_eq!( + // micromark("[foo]: /url \"title\"\n\n![foo] \n[]"), + // "<p><img src=\"/url\" alt=\"foo\" title=\"title\" />\n[]</p>", + // "should not support whitespace between sets of brackets" + // ); + + // To do: references. + // assert_eq!( + // micromark("[foo]: /url \"title\"\n\n![foo]"), + // "<p><img src=\"/url\" alt=\"foo\" title=\"title\" /></p>", + // "should support shortcut references (1)" + // ); + + // To do: references, tags in images, attention. + // assert_eq!( + // micromark("[*foo* bar]: /url \"title\"\n\n![*foo* bar]"), + // "<p><img src=\"/url\" alt=\"foo bar\" title=\"title\" /></p>", + // "should support shortcut references (2)" + // ); + + assert_eq!( + micromark("[[foo]]: /url \"title\"\n\n![[foo]]"), + "<p>[[foo]]: /url "title"</p>\n<p>![[foo]]</p>", + "should not support link labels w/ unescaped brackets" + ); + + // To do: references. + // assert_eq!( + // micromark("[foo]: /url \"title\"\n\n![Foo]"), + // "<p><img src=\"/url\" alt=\"Foo\" title=\"title\" /></p>", + // "should support case-insensitive label matching" + // ); + + // To do: references. + // assert_eq!( + // micromark("[foo]: /url \"title\"\n\n!\\[foo]"), + // "<p>![foo]</p>", + // "should “support” an escaped bracket instead of an image" + // ); + + // To do: references. + // assert_eq!( + // micromark("[foo]: /url \"title\"\n\n\\![foo]"), + // "<p>!<a href=\"/url\" title=\"title\">foo</a></p>", + // "should support an escaped bang instead of an image, but still have a link" + // ); + + // Extra + assert_eq!( + micromark("![foo]()"), + "<p><img src=\"\" alt=\"foo\" /></p>", + "should support images w/o destination" + ); + + assert_eq!( + micromark("![foo](<>)"), + "<p><img src=\"\" alt=\"foo\" /></p>", + "should support images w/ explicit empty destination" + ); + + assert_eq!( + micromark("![](example.png)"), + "<p><img src=\"example.png\" alt=\"\" /></p>", + "should support images w/o alt" + ); + + assert_eq!( + micromark("![alpha](bravo.png \"\")"), + "<p><img src=\"bravo.png\" alt=\"alpha\" /></p>", + "should support images w/ empty title (1)" + ); + + assert_eq!( + micromark("![alpha](bravo.png '')"), + "<p><img src=\"bravo.png\" alt=\"alpha\" /></p>", + "should support images w/ empty title (2)" + ); + + assert_eq!( + micromark("![alpha](bravo.png ())"), + "<p><img src=\"bravo.png\" alt=\"alpha\" /></p>", + "should support images w/ empty title (3)" + ); + + assert_eq!( + micromark("![&©&](example.com/&©& \"&©&\")"), + "<p><img src=\"example.com/&%C2%A9&\" alt=\"&©&\" title=\"&©&\" /></p>", + "should support character references in images" + ); + + // Extra + // See: <https://github.com/commonmark/commonmark.js/issues/192> + assert_eq!( + micromark("![](<> \"\")"), + "<p><img src=\"\" alt=\"\" /></p>", + "should ignore an empty title" + ); + + // To do: extensions + // assert_eq!( + // micromark("![x]()", {extensions: [{disable: {null: ["labelStartImage"]}}]}), + // "<p>!<a href=\"\">x</a></p>", + // "should support turning off label start (image)" + // ); + + assert_eq!( + micromark("![](javascript:alert(1))"), + "<p><img src=\"\" alt=\"\" /></p>", + "should ignore non-http protocols by default" + ); + + // To do: extensions + // assert_eq!( + // micromark("![](javascript:alert(1))", {allowDangerousProtocol: true}), + // "<p><img src=\"javascript:alert(1)\" alt=\"\" /></p>", + // "should allow non-http protocols w/ `allowDangerousProtocol`" + // ); +} diff --git a/tests/link_resource.rs b/tests/link_resource.rs new file mode 100644 index 0000000..b1e1905 --- /dev/null +++ b/tests/link_resource.rs @@ -0,0 +1,464 @@ +extern crate micromark; +use micromark::{micromark, micromark_with_options, Options}; + +const DANGER: &Options = &Options { + allow_dangerous_html: true, + allow_dangerous_protocol: true, + default_line_ending: None, +}; + +#[test] +fn link_resource() { + assert_eq!( + micromark("[link](/uri \"title\")"), + "<p><a href=\"/uri\" title=\"title\">link</a></p>", + "should support links" + ); + + assert_eq!( + micromark("[link](/uri)"), + "<p><a href=\"/uri\">link</a></p>", + "should support links w/o title" + ); + + assert_eq!( + micromark("[link]()"), + "<p><a href=\"\">link</a></p>", + "should support links w/o destination" + ); + + assert_eq!( + micromark("[link](<>)"), + "<p><a href=\"\">link</a></p>", + "should support links w/ empty enclosed destination" + ); + + assert_eq!( + micromark("[link](/my uri)"), + "<p>[link](/my uri)</p>", + "should not support links w/ spaces in destination" + ); + + assert_eq!( + micromark("[link](</my uri>)"), + "<p><a href=\"/my%20uri\">link</a></p>", + "should support links w/ spaces in enclosed destination" + ); + + assert_eq!( + micromark("[link](foo\nbar)"), + "<p>[link](foo\nbar)</p>", + "should not support links w/ line endings in destination" + ); + + assert_eq!( + micromark_with_options("[link](<foo\nbar>)", DANGER), + "<p>[link](<foo\nbar>)</p>", + "should not support links w/ line endings in enclosed destination" + ); + + assert_eq!( + micromark("[a](<b)c>)"), + "<p><a href=\"b)c\">a</a></p>", + "should support links w/ closing parens in destination" + ); + + assert_eq!( + micromark("[link](<foo\\>)"), + "<p>[link](<foo>)</p>", + "should not support links w/ enclosed destinations w/o end" + ); + + assert_eq!( + micromark_with_options("[a](<b)c\n[a](<b)c>\n[a](<b>c)", DANGER), + "<p>[a](<b)c\n[a](<b)c>\n[a](<b>c)</p>", + "should not support links w/ unmatched enclosed destinations" + ); + + assert_eq!( + micromark("[link](\\(foo\\))"), + "<p><a href=\"(foo)\">link</a></p>", + "should support links w/ destinations w/ escaped parens" + ); + + assert_eq!( + micromark("[link](foo(and(bar)))"), + "<p><a href=\"foo(and(bar))\">link</a></p>", + "should support links w/ destinations w/ balanced parens" + ); + + assert_eq!( + micromark("[link](foo\\(and\\(bar\\))"), + "<p><a href=\"foo(and(bar)\">link</a></p>", + "should support links w/ destinations w/ escaped parens" + ); + + assert_eq!( + micromark("[link](<foo(and(bar)>)"), + "<p><a href=\"foo(and(bar)\">link</a></p>", + "should support links w/ enclosed destinations w/ parens" + ); + + assert_eq!( + micromark_with_options("[link](foo\\)\\:)", DANGER), + "<p><a href=\"foo):\">link</a></p>", + "should support links w/ escapes in destinations" + ); + + assert_eq!( + micromark("[link](#fragment)"), + "<p><a href=\"#fragment\">link</a></p>", + "should support links w/ destinations to fragments" + ); + + assert_eq!( + micromark("[link](http://example.com#fragment)"), + "<p><a href=\"http://example.com#fragment\">link</a></p>", + "should support links w/ destinations to URLs w/ fragments" + ); + + assert_eq!( + micromark("[link](http://example.com?foo=3#frag)"), + "<p><a href=\"http://example.com?foo=3#frag\">link</a></p>", + "should support links w/ destinations to URLs w/ search and fragments" + ); + + assert_eq!( + micromark("[link](foo\\bar)"), + "<p><a href=\"foo%5Cbar\">link</a></p>", + "should not support non-punctuation character escapes in links" + ); + + assert_eq!( + micromark("[link](foo%20bä)"), + "<p><a href=\"foo%20b%C3%A4\">link</a></p>", + "should support character references in links" + ); + + assert_eq!( + micromark("[link](\"title\")"), + "<p><a href=\"%22title%22\">link</a></p>", + "should not support links w/ only a title" + ); + + assert_eq!( + micromark("[link](/url \"title\")"), + "<p><a href=\"/url\" title=\"title\">link</a></p>", + "should support titles w/ double quotes" + ); + + assert_eq!( + micromark("[link](/url 'title')"), + "<p><a href=\"/url\" title=\"title\">link</a></p>", + "should support titles w/ single quotes" + ); + + assert_eq!( + micromark("[link](/url (title))"), + "<p><a href=\"/url\" title=\"title\">link</a></p>", + "should support titles w/ parens" + ); + + assert_eq!( + micromark("[link](/url \"title \\\""\")"), + "<p><a href=\"/url\" title=\"title ""\">link</a></p>", + "should support character references and escapes in titles" + ); + + assert_eq!( + micromark("[link](/url \"title\")"), + "<p><a href=\"/url%C2%A0%22title%22\">link</a></p>", + "should not support unicode whitespace between destination and title" + ); + + assert_eq!( + micromark("[link](/url \"title \"and\" title\")"), + "<p>[link](/url "title "and" title")</p>", + "should not support nested balanced quotes in title" + ); + + assert_eq!( + micromark("[link](/url 'title \"and\" title')"), + "<p><a href=\"/url\" title=\"title "and" title\">link</a></p>", + "should support the other quotes in titles" + ); + + assert_eq!( + micromark("[link]( /uri\n \"title\" )"), + "<p><a href=\"/uri\" title=\"title\">link</a></p>", + "should support whitespace around destination and title (1)" + ); + + assert_eq!( + micromark("[link](\t\n/uri \"title\")"), + "<p><a href=\"/uri\" title=\"title\">link</a></p>", + "should support whitespace around destination and title (2)" + ); + + assert_eq!( + micromark("[link](/uri \"title\"\t\n)"), + "<p><a href=\"/uri\" title=\"title\">link</a></p>", + "should support whitespace around destination and title (3)" + ); + + assert_eq!( + micromark("[link] (/uri)"), + "<p>[link] (/uri)</p>", + "should not support whitespace between label and resource" + ); + + assert_eq!( + micromark("[link [foo [bar]]](/uri)"), + "<p><a href=\"/uri\">link [foo [bar]]</a></p>", + "should support balanced brackets" + ); + + assert_eq!( + micromark("[link] bar](/uri)"), + "<p>[link] bar](/uri)</p>", + "should not support unbalanced brackets (1)" + ); + + assert_eq!( + micromark("[link [bar](/uri)"), + "<p>[link <a href=\"/uri\">bar</a></p>", + "should not support unbalanced brackets (2)" + ); + + assert_eq!( + micromark("[link \\[bar](/uri)"), + "<p><a href=\"/uri\">link [bar</a></p>", + "should support characer escapes" + ); + + // To do: attention. + // assert_eq!( + // micromark("[link *foo **bar** `#`*](/uri)"), + // "<p><a href=\"/uri\">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>", + // "should support content" + // ); + + assert_eq!( + micromark("[![moon](moon.jpg)](/uri)"), + "<p><a href=\"/uri\"><img src=\"moon.jpg\" alt=\"moon\" /></a></p>", + "should support an image as content" + ); + + assert_eq!( + micromark("[foo [bar](/uri)](/uri)"), + "<p>[foo <a href=\"/uri\">bar</a>](/uri)</p>", + "should not support links in links (1)" + ); + + // To do: attention. + // assert_eq!( + // micromark("[foo *[bar [baz](/uri)](/uri)*](/uri)"), + // "<p>[foo <em>[bar <a href=\"/uri\">baz</a>](/uri)</em>](/uri)</p>", + // "should not support links in links (2)" + // ); + + // To do: tags in images. + // assert_eq!( + // micromark("![[[foo](uri1)](uri2)](uri3)"), + // "<p><img src=\"uri3\" alt=\"[foo](uri2)\" /></p>", + // "should not support links in links (3)" + // ); + + assert_eq!( + micromark("*[foo*](/uri)"), + "<p>*<a href=\"/uri\">foo*</a></p>", + "should prefer links over emphasis (1)" + ); + + assert_eq!( + micromark("[foo *bar](baz*)"), + "<p><a href=\"baz*\">foo *bar</a></p>", + "should prefer links over emphasis (2)" + ); + + assert_eq!( + micromark_with_options("[foo <bar attr=\"](baz)\">", DANGER), + "<p>[foo <bar attr=\"](baz)\"></p>", + "should prefer HTML over links" + ); + + assert_eq!( + micromark("[foo`](/uri)`"), + "<p>[foo<code>](/uri)</code></p>", + "should prefer code over links" + ); + + assert_eq!( + micromark("[foo<http://example.com/?search=](uri)>"), + "<p>[foo<a href=\"http://example.com/?search=%5D(uri)\">http://example.com/?search=](uri)</a></p>", + "should prefer autolinks over links" + ); + + assert_eq!( + micromark("[foo<http://example.com/?search=](uri)>"), + "<p>[foo<a href=\"http://example.com/?search=%5D(uri)\">http://example.com/?search=](uri)</a></p>", + "should prefer autolinks over links" + ); + + // Extra + assert_eq!( + micromark("[]()"), + "<p><a href=\"\"></a></p>", + "should support an empty link" + ); + + // See: <https://github.com/commonmark/commonmark.js/issues/192> + assert_eq!( + micromark("[](<> \"\")"), + "<p><a href=\"\"></a></p>", + "should ignore an empty title" + ); + + assert_eq!( + micromark_with_options("[a](<b>\"c\")", DANGER), + "<p>[a](<b>"c")</p>", + "should require whitespace between enclosed destination and title" + ); + + assert_eq!( + micromark("[](<"), + "<p>[](<</p>", + "should not support an unclosed enclosed destination" + ); + + assert_eq!( + micromark("[]("), + "<p>[](</p>", + "should not support an unclosed destination" + ); + + assert_eq!( + micromark("[](\\<)"), + "<p><a href=\"%3C\"></a></p>", + "should support unenclosed link destination starting w/ escapes" + ); + + assert_eq!( + micromark("[](<\\<>)"), + "<p><a href=\"%3C\"></a></p>", + "should support enclosed link destination starting w/ escapes" + ); + + assert_eq!( + micromark("[](\\"), + "<p>[](\\</p>", + "should not support unenclosed link destination starting w/ an incorrect escape" + ); + + assert_eq!( + micromark("[](<\\"), + "<p>[](<\\</p>", + "should not support enclosed link destination starting w/ an incorrect escape" + ); + + assert_eq!( + micromark("[](a \""), + "<p>[](a "</p>", + "should not support an eof in a link title (1)" + ); + + assert_eq!( + micromark("[](a '"), + "<p>[](a '</p>", + "should not support an eof in a link title (2)" + ); + + assert_eq!( + micromark("[](a ("), + "<p>[](a (</p>", + "should not support an eof in a link title (3)" + ); + + assert_eq!( + micromark("[](a \"\\"), + "<p>[](a "\\</p>", + "should not support an eof in a link title escape (1)" + ); + + assert_eq!( + micromark("[](a '\\"), + "<p>[](a '\\</p>", + "should not support an eof in a link title escape (2)" + ); + + assert_eq!( + micromark("[](a (\\"), + "<p>[](a (\\</p>", + "should not support an eof in a link title escape (3)" + ); + + assert_eq!( + micromark("[](a \"\\\"\")"), + "<p><a href=\"a\" title=\""\"></a></p>", + "should support a character escape to start a link title (1)" + ); + + assert_eq!( + micromark("[](a '\\'')"), + "<p><a href=\"a\" title=\"\'\"></a></p>", + "should support a character escape to start a link title (2)" + ); + + assert_eq!( + micromark("[](a (\\)))"), + "<p><a href=\"a\" title=\")\"></a></p>", + "should support a character escape to start a link title (3)" + ); + + assert_eq!( + micromark("[&©&](example.com/&©& \"&©&\")"), + "<p><a href=\"example.com/&%C2%A9&\" title=\"&©&\">&©&</a></p>", + "should support character references in links" + ); + + assert_eq!( + micromark("[a](1())"), + "<p><a href=\"1()\">a</a></p>", + "should support 1 set of parens" + ); + + assert_eq!( + micromark("[a](1(2()))"), + "<p><a href=\"1(2())\">a</a></p>", + "should support 2 sets of parens" + ); + + assert_eq!( + micromark( + "[a](1(2(3(4(5(6(7(8(9(10(11(12(13(14(15(16(17(18(19(20(21(22(23(24(25(26(27(28(29(30(31(32()))))))))))))))))))))))))))))))))" + ), + "<p><a href=\"1(2(3(4(5(6(7(8(9(10(11(12(13(14(15(16(17(18(19(20(21(22(23(24(25(26(27(28(29(30(31(32())))))))))))))))))))))))))))))))\">a</a></p>", + "should support 32 sets of parens" + ); + + assert_eq!( + micromark( + "[a](1(2(3(4(5(6(7(8(9(10(11(12(13(14(15(16(17(18(19(20(21(22(23(24(25(26(27(28(29(30(31(32(33())))))))))))))))))))))))))))))))))" + ), + "<p>[a](1(2(3(4(5(6(7(8(9(10(11(12(13(14(15(16(17(18(19(20(21(22(23(24(25(26(27(28(29(30(31(32(33())))))))))))))))))))))))))))))))))</p>", + "should not support 33 or more sets of parens" + ); + + assert_eq!( + micromark("[a](b \"\n c\")"), + "<p><a href=\"b\" title=\"\nc\">a</a></p>", + "should support an eol at the start of a title" + ); + + assert_eq!( + micromark("[a](b( \"c\")"), + "<p>[a](b( "c")</p>", + "should not support whitespace when unbalanced in a raw destination" + ); + + assert_eq!( + micromark("[a](\0)"), + "<p><a href=\"%EF%BF%BD\">a</a></p>", + "should support a single NUL character as a link resource" + ); +} diff --git a/tests/misc_dangerous_protocol.rs b/tests/misc_dangerous_protocol.rs index 6f759e3..3aa042a 100644 --- a/tests/misc_dangerous_protocol.rs +++ b/tests/misc_dangerous_protocol.rs @@ -34,166 +34,164 @@ fn dangerous_protocol_autolink() { ); } -// To do: image. -// #[test] -// fn dangerous_protocol_image() { -// assert_eq!( -// micromark("![](javascript:alert(1))"), -// "<p><img src=\"\" alt=\"\" /></p>", -// "should be safe by default" -// ); - -// assert_eq!( -// micromark("![](http://a)"), -// "<p><img src=\"http://a\" alt=\"\" /></p>", -// "should allow `http:`" -// ); - -// assert_eq!( -// micromark("![](https://a)"), -// "<p><img src=\"https://a\" alt=\"\" /></p>", -// "should allow `https:`" -// ); - -// assert_eq!( -// micromark("![](irc:///help)"), -// "<p><img src=\"\" alt=\"\" /></p>", -// "should not allow `irc:`" -// ); - -// assert_eq!( -// micromark("![](mailto:a)"), -// "<p><img src=\"\" alt=\"\" /></p>", -// "should not allow `mailto:`" -// ); - -// assert_eq!( -// micromark("![](#a)"), -// "<p><img src=\"#a\" alt=\"\" /></p>", -// "should allow a hash" -// ); - -// assert_eq!( -// micromark("![](?a)"), -// "<p><img src=\"?a\" alt=\"\" /></p>", -// "should allow a search" -// ); - -// assert_eq!( -// micromark("![](/a)"), -// "<p><img src=\"/a\" alt=\"\" /></p>", -// "should allow an absolute" -// ); - -// assert_eq!( -// micromark("![](./a)"), -// "<p><img src=\"./a\" alt=\"\" /></p>", -// "should allow an relative" -// ); - -// assert_eq!( -// micromark("![](../a)"), -// "<p><img src=\"../a\" alt=\"\" /></p>", -// "should allow an upwards relative" -// ); - -// assert_eq!( -// micromark("![](a#b:c)"), -// "<p><img src=\"a#b:c\" alt=\"\" /></p>", -// "should allow a colon in a hash" -// ); - -// assert_eq!( -// micromark("![](a?b:c)"), -// "<p><img src=\"a?b:c\" alt=\"\" /></p>", -// "should allow a colon in a search" -// ); - -// assert_eq!( -// micromark("![](a/b:c)"), -// "<p><img src=\"a/b:c\" alt=\"\" /></p>", -// "should allow a colon in a path" -// ); -// } - -// To do: link. -// #[test] -// fn dangerous_protocol_link() { -// assert_eq!( -// micromark("[](javascript:alert(1))"), -// "<p><a href=\"\"></a></p>", -// "should be safe by default" -// ); - -// assert_eq!( -// micromark("[](http://a)"), -// "<p><a href=\"http://a\"></a></p>", -// "should allow `http:`" -// ); - -// assert_eq!( -// micromark("[](https://a)"), -// "<p><a href=\"https://a\"></a></p>", -// "should allow `https:`" -// ); - -// assert_eq!( -// micromark("[](irc:///help)"), -// "<p><a href=\"irc:///help\"></a></p>", -// "should allow `irc:`" -// ); - -// assert_eq!( -// micromark("[](mailto:a)"), -// "<p><a href=\"mailto:a\"></a></p>", -// "should allow `mailto:`" -// ); - -// assert_eq!( -// micromark("[](#a)"), -// "<p><a href=\"#a\"></a></p>", -// "should allow a hash" -// ); - -// assert_eq!( -// micromark("[](?a)"), -// "<p><a href=\"?a\"></a></p>", -// "should allow a search" -// ); - -// assert_eq!( -// micromark("[](/a)"), -// "<p><a href=\"/a\"></a></p>", -// "should allow an absolute" -// ); - -// assert_eq!( -// micromark("[](./a)"), -// "<p><a href=\"./a\"></a></p>", -// "should allow an relative" -// ); - -// assert_eq!( -// micromark("[](../a)"), -// "<p><a href=\"../a\"></a></p>", -// "should allow an upwards relative" -// ); - -// assert_eq!( -// micromark("[](a#b:c)"), -// "<p><a href=\"a#b:c\"></a></p>", -// "should allow a colon in a hash" -// ); - -// assert_eq!( -// micromark("[](a?b:c)"), -// "<p><a href=\"a?b:c\"></a></p>", -// "should allow a colon in a search" -// ); - -// assert_eq!( -// micromark("[](a/b:c)"), -// "<p><a href=\"a/b:c\"></a></p>", -// "should allow a colon in a path" -// ); -// } +#[test] +fn dangerous_protocol_image() { + assert_eq!( + micromark("![](javascript:alert(1))"), + "<p><img src=\"\" alt=\"\" /></p>", + "should be safe by default" + ); + + assert_eq!( + micromark("![](http://a)"), + "<p><img src=\"http://a\" alt=\"\" /></p>", + "should allow `http:`" + ); + + assert_eq!( + micromark("![](https://a)"), + "<p><img src=\"https://a\" alt=\"\" /></p>", + "should allow `https:`" + ); + + assert_eq!( + micromark("![](irc:///help)"), + "<p><img src=\"\" alt=\"\" /></p>", + "should not allow `irc:`" + ); + + assert_eq!( + micromark("![](mailto:a)"), + "<p><img src=\"\" alt=\"\" /></p>", + "should not allow `mailto:`" + ); + + assert_eq!( + micromark("![](#a)"), + "<p><img src=\"#a\" alt=\"\" /></p>", + "should allow a hash" + ); + + assert_eq!( + micromark("![](?a)"), + "<p><img src=\"?a\" alt=\"\" /></p>", + "should allow a search" + ); + + assert_eq!( + micromark("![](/a)"), + "<p><img src=\"/a\" alt=\"\" /></p>", + "should allow an absolute" + ); + + assert_eq!( + micromark("![](./a)"), + "<p><img src=\"./a\" alt=\"\" /></p>", + "should allow an relative" + ); + + assert_eq!( + micromark("![](../a)"), + "<p><img src=\"../a\" alt=\"\" /></p>", + "should allow an upwards relative" + ); + + assert_eq!( + micromark("![](a#b:c)"), + "<p><img src=\"a#b:c\" alt=\"\" /></p>", + "should allow a colon in a hash" + ); + + assert_eq!( + micromark("![](a?b:c)"), + "<p><img src=\"a?b:c\" alt=\"\" /></p>", + "should allow a colon in a search" + ); + + assert_eq!( + micromark("![](a/b:c)"), + "<p><img src=\"a/b:c\" alt=\"\" /></p>", + "should allow a colon in a path" + ); +} + +#[test] +fn dangerous_protocol_link() { + assert_eq!( + micromark("[](javascript:alert(1))"), + "<p><a href=\"\"></a></p>", + "should be safe by default" + ); + + assert_eq!( + micromark("[](http://a)"), + "<p><a href=\"http://a\"></a></p>", + "should allow `http:`" + ); + + assert_eq!( + micromark("[](https://a)"), + "<p><a href=\"https://a\"></a></p>", + "should allow `https:`" + ); + + assert_eq!( + micromark("[](irc:///help)"), + "<p><a href=\"irc:///help\"></a></p>", + "should allow `irc:`" + ); + + assert_eq!( + micromark("[](mailto:a)"), + "<p><a href=\"mailto:a\"></a></p>", + "should allow `mailto:`" + ); + + assert_eq!( + micromark("[](#a)"), + "<p><a href=\"#a\"></a></p>", + "should allow a hash" + ); + + assert_eq!( + micromark("[](?a)"), + "<p><a href=\"?a\"></a></p>", + "should allow a search" + ); + + assert_eq!( + micromark("[](/a)"), + "<p><a href=\"/a\"></a></p>", + "should allow an absolute" + ); + + assert_eq!( + micromark("[](./a)"), + "<p><a href=\"./a\"></a></p>", + "should allow an relative" + ); + + assert_eq!( + micromark("[](../a)"), + "<p><a href=\"../a\"></a></p>", + "should allow an upwards relative" + ); + + assert_eq!( + micromark("[](a#b:c)"), + "<p><a href=\"a#b:c\"></a></p>", + "should allow a colon in a hash" + ); + + assert_eq!( + micromark("[](a?b:c)"), + "<p><a href=\"a?b:c\"></a></p>", + "should allow a colon in a search" + ); + + assert_eq!( + micromark("[](a/b:c)"), + "<p><a href=\"a/b:c\"></a></p>", + "should allow a colon in a path" + ); +} diff --git a/tests/misc_tabs.rs b/tests/misc_tabs.rs index e9a0b72..568172e 100644 --- a/tests/misc_tabs.rs +++ b/tests/misc_tabs.rs @@ -221,48 +221,42 @@ fn tabs_text() { // "should support an initial tab after a line ending in a paragraph" // ); - // To do: link (reference). - // assert_eq!( - // micromark("x[\ty](z)"), - // "<p>x<a href=\"z\">\ty</a></p>", - // "should support an initial tab in a link label" - // ); + assert_eq!( + micromark("x[\ty](z)"), + "<p>x<a href=\"z\">\ty</a></p>", + "should support an initial tab in a link label" + ); - // To do: link (reference). - // assert_eq!( - // micromark("x[y\t](z)"), - // "<p>x<a href=\"z\">y\t</a></p>", - // "should support a final tab in a link label" - // ); + assert_eq!( + micromark("x[y\t](z)"), + "<p>x<a href=\"z\">y\t</a></p>", + "should support a final tab in a link label" + ); - // To do: link (reference). - // assert_eq!( - // micromark("[x\ty](z)"), - // "<p><a href=\"z\">x\ty</a></p>", - // "should support a tab in a link label" - // ); + assert_eq!( + micromark("[x\ty](z)"), + "<p><a href=\"z\">x\ty</a></p>", + "should support a tab in a link label" + ); - // To do: link (resource). // Note: CM.js bug, see: <https://github.com/commonmark/commonmark.js/issues/191> - // assert_eq!( - // micromark("[x](\ty)"), - // "<p><a href=\"y\">x</a></p>", - // "should support a tab starting a link resource" - // ); + assert_eq!( + micromark("[x](\ty)"), + "<p><a href=\"y\">x</a></p>", + "should support a tab starting a link resource" + ); - // To do: link (resource). - // assert_eq!( - // micromark("[x](y\t)"), - // "<p><a href=\"y\">x</a></p>", - // "should support a tab ending a link resource" - // ); + assert_eq!( + micromark("[x](y\t)"), + "<p><a href=\"y\">x</a></p>", + "should support a tab ending a link resource" + ); - // To do: link (resource). - // assert_eq!( - // micromark("[x](y\t\"z\")"), - // "<p><a href=\"y\" title=\"z\">x</a></p>", - // "should support a tab between a link destination and title" - // ); + assert_eq!( + micromark("[x](y\t\"z\")"), + "<p><a href=\"y\" title=\"z\">x</a></p>", + "should support a tab between a link destination and title" + ); } #[test] diff --git a/tests/misc_url.rs b/tests/misc_url.rs index a6f8ead..5e94366 100644 --- a/tests/misc_url.rs +++ b/tests/misc_url.rs @@ -9,28 +9,25 @@ fn url() { "should support incorrect percentage encoded values (0)" ); - // To do: link. - // assert_eq!( - // micromark("[](<%>)"), - // "<p><a href=\"%25\"></a></p>", - // "should support incorrect percentage encoded values (1)" - // ); - - // To do: link. - // assert_eq!( - // micromark("[](<%%20>)"), - // "<p><a href=\"%25%20\"></a></p>", - // "should support incorrect percentage encoded values (2)" - // ); - - // To do: link. - // assert_eq!( - // micromark("[](<%a%20>)"), - // "<p><a href=\"%25a%20\"></a></p>", - // "should support incorrect percentage encoded values (3)" - // ); + assert_eq!( + micromark("[](<%>)"), + "<p><a href=\"%25\"></a></p>", + "should support incorrect percentage encoded values (1)" + ); - // Surrogate handling not needed in Rust. + assert_eq!( + micromark("[](<%%20>)"), + "<p><a href=\"%25%20\"></a></p>", + "should support incorrect percentage encoded values (2)" + ); + + assert_eq!( + micromark("[](<%a%20>)"), + "<p><a href=\"%25a%20\"></a></p>", + "should support incorrect percentage encoded values (3)" + ); + + // Note: Surrogate handling not needed in Rust. // assert_eq!( // micromark("[](<foo\u{D800}bar>)"), // "<p><a href=\"foo%EF%BF%BDbar\"></a></p>", @@ -114,39 +111,37 @@ fn url() { // "should support a lone low surrogate at the end (highest)" // ); - // To do: link. - // assert_eq!( - // micromark("[](<🤔>)"), - // "<p><a href=\"%F0%9F%A4%94\"></a></p>", - // "should support an emoji" - // ); - - // To do: link. - // let mut ascii: Vec<char> = vec![]; - // let mut code = 0; - - // while code < 128 { - // // LF and CR can’t be in resources. - // if code == 10 || code == 13 { - // code += 1; - // continue; - // } - - // // `<`, `>`, `\` need to be escaped. - // if code == 60 || code == 62 || code == 92 { - // ascii.push('\\'); - // } - - // ascii.push(char::from_u32(code).unwrap()); - - // code += 1; - // } - - // let ascii_in = ascii.into_iter().collect::<String>(); - // 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&\"()*+,-./0123456789:;%3C=%3E?@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%7F"; - // assert_eq!( - // micromark(&format!("[](<{}>)", ascii_in)), - // format!("<p><a href=\"{}\"></a></p>", ascii_out), - // "should support ascii characters" - // ); + assert_eq!( + micromark("[](<🤔>)"), + "<p><a href=\"%F0%9F%A4%94\"></a></p>", + "should support an emoji" + ); + + let mut ascii: Vec<char> = vec![]; + let mut code = 0; + + while code < 128 { + // LF and CR can’t be in resources. + if code == 10 || code == 13 { + code += 1; + continue; + } + + // `<`, `>`, `\` need to be escaped. + if code == 60 || code == 62 || code == 92 { + ascii.push('\\'); + } + + ascii.push(char::from_u32(code).unwrap()); + + code += 1; + } + + let ascii_in = ascii.into_iter().collect::<String>(); + 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&'()*+,-./0123456789:;%3C=%3E?@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%7F"; + assert_eq!( + micromark(&format!("[](<{}>)", ascii_in)), + format!("<p><a href=\"{}\"></a></p>", ascii_out), + "should support ascii characters" + ); } |