From a3dd207e3b1ebcbcb6cec0f703a695e51ae4ece0 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Fri, 24 Jun 2022 17:57:10 +0200 Subject: 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 --- tests/character_escape.rs | 11 +- tests/character_reference.rs | 24 +- tests/image.rs | 229 +++++++++++++++++++ tests/link_resource.rs | 464 +++++++++++++++++++++++++++++++++++++++ tests/misc_dangerous_protocol.rs | 324 ++++++++++++++------------- tests/misc_tabs.rs | 66 +++--- tests/misc_url.rs | 107 +++++---- 7 files changed, 951 insertions(+), 274 deletions(-) create mode 100644 tests/image.rs create mode 100644 tests/link_resource.rs (limited to 'tests') 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\")"), - // "

foo

", - // "should escape in resource and title" - // ); + assert_eq!( + micromark("[foo](/bar\\* \"ti\\*tle\")"), + "

foo

", + "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öö\")"), - // "

foo

", - // "should support character references in resource URLs and titles" - // ); + assert_eq!( + micromark("[foo](/föö \"föö\")"), + "

foo

", + "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]"), // "

foo

", @@ -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")"), - // "

[a](url "tit")

", - // "should not support character references as construct markers (3)" - // ); + assert_eq!( + micromark("[a](url "tit")"), + "

[a](url "tit")

", + "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\")"), + "

link

", + "should support links" + ); + assert_eq!( + micromark("![foo](/url \"title\")"), + "

\"foo\"

", + "should support image w/ resource" + ); + + // To do: attention. + // assert_eq!( + // micromark("[foo *bar*]: train.jpg \"train & tracks\"\n\n![foo *bar*]"), + // "

\"foo

", + // "should support image as shortcut reference" + // ); + + // To do: tags in images. + // assert_eq!( + // micromark("![foo ![bar](/url)](/url2)"), + // "

\"foo

", + // "should “support” images in images" + // ); + + // To do: tags in images. + // assert_eq!( + // micromark("![foo [bar](/url)](/url2)"), + // "

\"foo

", + // "should “support” links in images" + // ); + + // To do: tags in images. + // assert_eq!( + // micromark("[foo *bar*]: train.jpg \"train & tracks\"\n\n![foo *bar*][]"), + // "

\"foo

", + // "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]"), + // "

\"foo

", + // "should support “content” in images" + // ); + + assert_eq!( + micromark("![foo](train.jpg)"), + "

\"foo\"

", + "should support images w/o title" + ); + + assert_eq!( + micromark("My ![foo bar](/path/to/train.jpg \"title\" )"), + "

My \"foo

", + "should support images w/ lots of whitespace" + ); + + assert_eq!( + micromark("![foo]()"), + "

\"foo\"

", + "should support images w/ enclosed destinations" + ); + + assert_eq!( + micromark("![](/url)"), + "

\"\"

", + "should support images w/ empty labels" + ); + + // To do: references. + // assert_eq!( + // micromark("[bar]: /url\n\n![foo][bar]"), + // "

\"foo\"

", + // "should support full references (1)" + // ); + + // To do: references. + // assert_eq!( + // micromark("[BAR]: /url\n\n![foo][bar]"), + // "

\"foo\"

", + // "should support full references (2)" + // ); + + // To do: references. + // assert_eq!( + // micromark("[foo]: /url \"title\"\n\n![foo][]"), + // "

\"foo\"

", + // "should support collapsed references (1)" + // ); + + // To do: references, attention, tags in images. + // assert_eq!( + // micromark("[*foo* bar]: /url \"title\"\n\n![*foo* bar][]"), + // "

\"foo

", + // "should support collapsed references (2)" + // ); + + // To do: references. + // assert_eq!( + // micromark("[foo]: /url \"title\"\n\n![Foo][]"), + // "

\"Foo\"

", + // "should support case-insensitive labels" + // ); + + // To do: references. + // assert_eq!( + // micromark("[foo]: /url \"title\"\n\n![foo] \n[]"), + // "

\"foo\"\n[]

", + // "should not support whitespace between sets of brackets" + // ); + + // To do: references. + // assert_eq!( + // micromark("[foo]: /url \"title\"\n\n![foo]"), + // "

\"foo\"

", + // "should support shortcut references (1)" + // ); + + // To do: references, tags in images, attention. + // assert_eq!( + // micromark("[*foo* bar]: /url \"title\"\n\n![*foo* bar]"), + // "

\"foo

", + // "should support shortcut references (2)" + // ); + + assert_eq!( + micromark("[[foo]]: /url \"title\"\n\n![[foo]]"), + "

[[foo]]: /url "title"

\n

![[foo]]

", + "should not support link labels w/ unescaped brackets" + ); + + // To do: references. + // assert_eq!( + // micromark("[foo]: /url \"title\"\n\n![Foo]"), + // "

\"Foo\"

", + // "should support case-insensitive label matching" + // ); + + // To do: references. + // assert_eq!( + // micromark("[foo]: /url \"title\"\n\n!\\[foo]"), + // "

![foo]

", + // "should “support” an escaped bracket instead of an image" + // ); + + // To do: references. + // assert_eq!( + // micromark("[foo]: /url \"title\"\n\n\\![foo]"), + // "

!foo

", + // "should support an escaped bang instead of an image, but still have a link" + // ); + + // Extra + assert_eq!( + micromark("![foo]()"), + "

\"foo\"

", + "should support images w/o destination" + ); + + assert_eq!( + micromark("![foo](<>)"), + "

\"foo\"

", + "should support images w/ explicit empty destination" + ); + + assert_eq!( + micromark("![](example.png)"), + "

\"\"

", + "should support images w/o alt" + ); + + assert_eq!( + micromark("![alpha](bravo.png \"\")"), + "

\"alpha\"

", + "should support images w/ empty title (1)" + ); + + assert_eq!( + micromark("![alpha](bravo.png '')"), + "

\"alpha\"

", + "should support images w/ empty title (2)" + ); + + assert_eq!( + micromark("![alpha](bravo.png ())"), + "

\"alpha\"

", + "should support images w/ empty title (3)" + ); + + assert_eq!( + micromark("![&©&](example.com/&©& \"&©&\")"), + "

\"&©&\"

", + "should support character references in images" + ); + + // Extra + // See: + assert_eq!( + micromark("![](<> \"\")"), + "

\"\"

", + "should ignore an empty title" + ); + + // To do: extensions + // assert_eq!( + // micromark("![x]()", {extensions: [{disable: {null: ["labelStartImage"]}}]}), + // "

!x

", + // "should support turning off label start (image)" + // ); + + assert_eq!( + micromark("![](javascript:alert(1))"), + "

\"\"

", + "should ignore non-http protocols by default" + ); + + // To do: extensions + // assert_eq!( + // micromark("![](javascript:alert(1))", {allowDangerousProtocol: true}), + // "

\"\"

", + // "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\")"), + "

link

", + "should support links" + ); + + assert_eq!( + micromark("[link](/uri)"), + "

link

", + "should support links w/o title" + ); + + assert_eq!( + micromark("[link]()"), + "

link

", + "should support links w/o destination" + ); + + assert_eq!( + micromark("[link](<>)"), + "

link

", + "should support links w/ empty enclosed destination" + ); + + assert_eq!( + micromark("[link](/my uri)"), + "

[link](/my uri)

", + "should not support links w/ spaces in destination" + ); + + assert_eq!( + micromark("[link]()"), + "

link

", + "should support links w/ spaces in enclosed destination" + ); + + assert_eq!( + micromark("[link](foo\nbar)"), + "

[link](foo\nbar)

", + "should not support links w/ line endings in destination" + ); + + assert_eq!( + micromark_with_options("[link]()", DANGER), + "

[link]()

", + "should not support links w/ line endings in enclosed destination" + ); + + assert_eq!( + micromark("[a]()"), + "

a

", + "should support links w/ closing parens in destination" + ); + + assert_eq!( + micromark("[link]()"), + "

[link](<foo>)

", + "should not support links w/ enclosed destinations w/o end" + ); + + assert_eq!( + micromark_with_options("[a](\n[a](c)", DANGER), + "

[a](<b)c\n[a](<b)c>\n[a](c)

", + "should not support links w/ unmatched enclosed destinations" + ); + + assert_eq!( + micromark("[link](\\(foo\\))"), + "

link

", + "should support links w/ destinations w/ escaped parens" + ); + + assert_eq!( + micromark("[link](foo(and(bar)))"), + "

link

", + "should support links w/ destinations w/ balanced parens" + ); + + assert_eq!( + micromark("[link](foo\\(and\\(bar\\))"), + "

link

", + "should support links w/ destinations w/ escaped parens" + ); + + assert_eq!( + micromark("[link]()"), + "

link

", + "should support links w/ enclosed destinations w/ parens" + ); + + assert_eq!( + micromark_with_options("[link](foo\\)\\:)", DANGER), + "

link

", + "should support links w/ escapes in destinations" + ); + + assert_eq!( + micromark("[link](#fragment)"), + "

link

", + "should support links w/ destinations to fragments" + ); + + assert_eq!( + micromark("[link](http://example.com#fragment)"), + "

link

", + "should support links w/ destinations to URLs w/ fragments" + ); + + assert_eq!( + micromark("[link](http://example.com?foo=3#frag)"), + "

link

", + "should support links w/ destinations to URLs w/ search and fragments" + ); + + assert_eq!( + micromark("[link](foo\\bar)"), + "

link

", + "should not support non-punctuation character escapes in links" + ); + + assert_eq!( + micromark("[link](foo%20bä)"), + "

link

", + "should support character references in links" + ); + + assert_eq!( + micromark("[link](\"title\")"), + "

link

", + "should not support links w/ only a title" + ); + + assert_eq!( + micromark("[link](/url \"title\")"), + "

link

", + "should support titles w/ double quotes" + ); + + assert_eq!( + micromark("[link](/url 'title')"), + "

link

", + "should support titles w/ single quotes" + ); + + assert_eq!( + micromark("[link](/url (title))"), + "

link

", + "should support titles w/ parens" + ); + + assert_eq!( + micromark("[link](/url \"title \\\""\")"), + "

link

", + "should support character references and escapes in titles" + ); + + assert_eq!( + micromark("[link](/url \"title\")"), + "

link

", + "should not support unicode whitespace between destination and title" + ); + + assert_eq!( + micromark("[link](/url \"title \"and\" title\")"), + "

[link](/url "title "and" title")

", + "should not support nested balanced quotes in title" + ); + + assert_eq!( + micromark("[link](/url 'title \"and\" title')"), + "

link

", + "should support the other quotes in titles" + ); + + assert_eq!( + micromark("[link]( /uri\n \"title\" )"), + "

link

", + "should support whitespace around destination and title (1)" + ); + + assert_eq!( + micromark("[link](\t\n/uri \"title\")"), + "

link

", + "should support whitespace around destination and title (2)" + ); + + assert_eq!( + micromark("[link](/uri \"title\"\t\n)"), + "

link

", + "should support whitespace around destination and title (3)" + ); + + assert_eq!( + micromark("[link] (/uri)"), + "

[link] (/uri)

", + "should not support whitespace between label and resource" + ); + + assert_eq!( + micromark("[link [foo [bar]]](/uri)"), + "

link [foo [bar]]

", + "should support balanced brackets" + ); + + assert_eq!( + micromark("[link] bar](/uri)"), + "

[link] bar](/uri)

", + "should not support unbalanced brackets (1)" + ); + + assert_eq!( + micromark("[link [bar](/uri)"), + "

[link bar

", + "should not support unbalanced brackets (2)" + ); + + assert_eq!( + micromark("[link \\[bar](/uri)"), + "

link [bar

", + "should support characer escapes" + ); + + // To do: attention. + // assert_eq!( + // micromark("[link *foo **bar** `#`*](/uri)"), + // "

link foo bar #

", + // "should support content" + // ); + + assert_eq!( + micromark("[![moon](moon.jpg)](/uri)"), + "

\"moon\"

", + "should support an image as content" + ); + + assert_eq!( + micromark("[foo [bar](/uri)](/uri)"), + "

[foo bar](/uri)

", + "should not support links in links (1)" + ); + + // To do: attention. + // assert_eq!( + // micromark("[foo *[bar [baz](/uri)](/uri)*](/uri)"), + // "

[foo [bar baz](/uri)](/uri)

", + // "should not support links in links (2)" + // ); + + // To do: tags in images. + // assert_eq!( + // micromark("![[[foo](uri1)](uri2)](uri3)"), + // "

\"[foo](uri2)\"

", + // "should not support links in links (3)" + // ); + + assert_eq!( + micromark("*[foo*](/uri)"), + "

*foo*

", + "should prefer links over emphasis (1)" + ); + + assert_eq!( + micromark("[foo *bar](baz*)"), + "

foo *bar

", + "should prefer links over emphasis (2)" + ); + + assert_eq!( + micromark_with_options("[foo ", DANGER), + "

[foo

", + "should prefer HTML over links" + ); + + assert_eq!( + micromark("[foo`](/uri)`"), + "

[foo](/uri)

", + "should prefer code over links" + ); + + assert_eq!( + micromark("[foo"), + "

[foohttp://example.com/?search=](uri)

", + "should prefer autolinks over links" + ); + + assert_eq!( + micromark("[foo"), + "

[foohttp://example.com/?search=](uri)

", + "should prefer autolinks over links" + ); + + // Extra + assert_eq!( + micromark("[]()"), + "

", + "should support an empty link" + ); + + // See: + assert_eq!( + micromark("[](<> \"\")"), + "

", + "should ignore an empty title" + ); + + assert_eq!( + micromark_with_options("[a](\"c\")", DANGER), + "

[a]("c")

", + "should require whitespace between enclosed destination and title" + ); + + assert_eq!( + micromark("[](<"), + "

[](<

", + "should not support an unclosed enclosed destination" + ); + + assert_eq!( + micromark("[]("), + "

[](

", + "should not support an unclosed destination" + ); + + assert_eq!( + micromark("[](\\<)"), + "

", + "should support unenclosed link destination starting w/ escapes" + ); + + assert_eq!( + micromark("[](<\\<>)"), + "

", + "should support enclosed link destination starting w/ escapes" + ); + + assert_eq!( + micromark("[](\\"), + "

[](\\

", + "should not support unenclosed link destination starting w/ an incorrect escape" + ); + + assert_eq!( + micromark("[](<\\"), + "

[](<\\

", + "should not support enclosed link destination starting w/ an incorrect escape" + ); + + assert_eq!( + micromark("[](a \""), + "

[](a "

", + "should not support an eof in a link title (1)" + ); + + assert_eq!( + micromark("[](a '"), + "

[](a '

", + "should not support an eof in a link title (2)" + ); + + assert_eq!( + micromark("[](a ("), + "

[](a (

", + "should not support an eof in a link title (3)" + ); + + assert_eq!( + micromark("[](a \"\\"), + "

[](a "\\

", + "should not support an eof in a link title escape (1)" + ); + + assert_eq!( + micromark("[](a '\\"), + "

[](a '\\

", + "should not support an eof in a link title escape (2)" + ); + + assert_eq!( + micromark("[](a (\\"), + "

[](a (\\

", + "should not support an eof in a link title escape (3)" + ); + + assert_eq!( + micromark("[](a \"\\\"\")"), + "

", + "should support a character escape to start a link title (1)" + ); + + assert_eq!( + micromark("[](a '\\'')"), + "

", + "should support a character escape to start a link title (2)" + ); + + assert_eq!( + micromark("[](a (\\)))"), + "

", + "should support a character escape to start a link title (3)" + ); + + assert_eq!( + micromark("[&©&](example.com/&©& \"&©&\")"), + "

&©&

", + "should support character references in links" + ); + + assert_eq!( + micromark("[a](1())"), + "

a

", + "should support 1 set of parens" + ); + + assert_eq!( + micromark("[a](1(2()))"), + "

a

", + "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()))))))))))))))))))))))))))))))))" + ), + "

a

", + "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())))))))))))))))))))))))))))))))))" + ), + "

[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())))))))))))))))))))))))))))))))))

", + "should not support 33 or more sets of parens" + ); + + assert_eq!( + micromark("[a](b \"\n c\")"), + "

a

", + "should support an eol at the start of a title" + ); + + assert_eq!( + micromark("[a](b( \"c\")"), + "

[a](b( "c")

", + "should not support whitespace when unbalanced in a raw destination" + ); + + assert_eq!( + micromark("[a](\0)"), + "

a

", + "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))"), -// "

\"\"

", -// "should be safe by default" -// ); - -// assert_eq!( -// micromark("![](http://a)"), -// "

\"\"

", -// "should allow `http:`" -// ); - -// assert_eq!( -// micromark("![](https://a)"), -// "

\"\"

", -// "should allow `https:`" -// ); - -// assert_eq!( -// micromark("![](irc:///help)"), -// "

\"\"

", -// "should not allow `irc:`" -// ); - -// assert_eq!( -// micromark("![](mailto:a)"), -// "

\"\"

", -// "should not allow `mailto:`" -// ); - -// assert_eq!( -// micromark("![](#a)"), -// "

\"\"

", -// "should allow a hash" -// ); - -// assert_eq!( -// micromark("![](?a)"), -// "

\"\"

", -// "should allow a search" -// ); - -// assert_eq!( -// micromark("![](/a)"), -// "

\"\"

", -// "should allow an absolute" -// ); - -// assert_eq!( -// micromark("![](./a)"), -// "

\"\"

", -// "should allow an relative" -// ); - -// assert_eq!( -// micromark("![](../a)"), -// "

\"\"

", -// "should allow an upwards relative" -// ); - -// assert_eq!( -// micromark("![](a#b:c)"), -// "

\"\"

", -// "should allow a colon in a hash" -// ); - -// assert_eq!( -// micromark("![](a?b:c)"), -// "

\"\"

", -// "should allow a colon in a search" -// ); - -// assert_eq!( -// micromark("![](a/b:c)"), -// "

\"\"

", -// "should allow a colon in a path" -// ); -// } - -// To do: link. -// #[test] -// fn dangerous_protocol_link() { -// assert_eq!( -// micromark("[](javascript:alert(1))"), -// "

", -// "should be safe by default" -// ); - -// assert_eq!( -// micromark("[](http://a)"), -// "

", -// "should allow `http:`" -// ); - -// assert_eq!( -// micromark("[](https://a)"), -// "

", -// "should allow `https:`" -// ); - -// assert_eq!( -// micromark("[](irc:///help)"), -// "

", -// "should allow `irc:`" -// ); - -// assert_eq!( -// micromark("[](mailto:a)"), -// "

", -// "should allow `mailto:`" -// ); - -// assert_eq!( -// micromark("[](#a)"), -// "

", -// "should allow a hash" -// ); - -// assert_eq!( -// micromark("[](?a)"), -// "

", -// "should allow a search" -// ); - -// assert_eq!( -// micromark("[](/a)"), -// "

", -// "should allow an absolute" -// ); - -// assert_eq!( -// micromark("[](./a)"), -// "

", -// "should allow an relative" -// ); - -// assert_eq!( -// micromark("[](../a)"), -// "

", -// "should allow an upwards relative" -// ); - -// assert_eq!( -// micromark("[](a#b:c)"), -// "

", -// "should allow a colon in a hash" -// ); - -// assert_eq!( -// micromark("[](a?b:c)"), -// "

", -// "should allow a colon in a search" -// ); - -// assert_eq!( -// micromark("[](a/b:c)"), -// "

", -// "should allow a colon in a path" -// ); -// } +#[test] +fn dangerous_protocol_image() { + assert_eq!( + micromark("![](javascript:alert(1))"), + "

\"\"

", + "should be safe by default" + ); + + assert_eq!( + micromark("![](http://a)"), + "

\"\"

", + "should allow `http:`" + ); + + assert_eq!( + micromark("![](https://a)"), + "

\"\"

", + "should allow `https:`" + ); + + assert_eq!( + micromark("![](irc:///help)"), + "

\"\"

", + "should not allow `irc:`" + ); + + assert_eq!( + micromark("![](mailto:a)"), + "

\"\"

", + "should not allow `mailto:`" + ); + + assert_eq!( + micromark("![](#a)"), + "

\"\"

", + "should allow a hash" + ); + + assert_eq!( + micromark("![](?a)"), + "

\"\"

", + "should allow a search" + ); + + assert_eq!( + micromark("![](/a)"), + "

\"\"

", + "should allow an absolute" + ); + + assert_eq!( + micromark("![](./a)"), + "

\"\"

", + "should allow an relative" + ); + + assert_eq!( + micromark("![](../a)"), + "

\"\"

", + "should allow an upwards relative" + ); + + assert_eq!( + micromark("![](a#b:c)"), + "

\"\"

", + "should allow a colon in a hash" + ); + + assert_eq!( + micromark("![](a?b:c)"), + "

\"\"

", + "should allow a colon in a search" + ); + + assert_eq!( + micromark("![](a/b:c)"), + "

\"\"

", + "should allow a colon in a path" + ); +} + +#[test] +fn dangerous_protocol_link() { + assert_eq!( + micromark("[](javascript:alert(1))"), + "

", + "should be safe by default" + ); + + assert_eq!( + micromark("[](http://a)"), + "

", + "should allow `http:`" + ); + + assert_eq!( + micromark("[](https://a)"), + "

", + "should allow `https:`" + ); + + assert_eq!( + micromark("[](irc:///help)"), + "

", + "should allow `irc:`" + ); + + assert_eq!( + micromark("[](mailto:a)"), + "

", + "should allow `mailto:`" + ); + + assert_eq!( + micromark("[](#a)"), + "

", + "should allow a hash" + ); + + assert_eq!( + micromark("[](?a)"), + "

", + "should allow a search" + ); + + assert_eq!( + micromark("[](/a)"), + "

", + "should allow an absolute" + ); + + assert_eq!( + micromark("[](./a)"), + "

", + "should allow an relative" + ); + + assert_eq!( + micromark("[](../a)"), + "

", + "should allow an upwards relative" + ); + + assert_eq!( + micromark("[](a#b:c)"), + "

", + "should allow a colon in a hash" + ); + + assert_eq!( + micromark("[](a?b:c)"), + "

", + "should allow a colon in a search" + ); + + assert_eq!( + micromark("[](a/b:c)"), + "

", + "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)"), - // "

x\ty

", - // "should support an initial tab in a link label" - // ); + assert_eq!( + micromark("x[\ty](z)"), + "

x\ty

", + "should support an initial tab in a link label" + ); - // To do: link (reference). - // assert_eq!( - // micromark("x[y\t](z)"), - // "

xy\t

", - // "should support a final tab in a link label" - // ); + assert_eq!( + micromark("x[y\t](z)"), + "

xy\t

", + "should support a final tab in a link label" + ); - // To do: link (reference). - // assert_eq!( - // micromark("[x\ty](z)"), - // "

x\ty

", - // "should support a tab in a link label" - // ); + assert_eq!( + micromark("[x\ty](z)"), + "

x\ty

", + "should support a tab in a link label" + ); - // To do: link (resource). // Note: CM.js bug, see: - // assert_eq!( - // micromark("[x](\ty)"), - // "

x

", - // "should support a tab starting a link resource" - // ); + assert_eq!( + micromark("[x](\ty)"), + "

x

", + "should support a tab starting a link resource" + ); - // To do: link (resource). - // assert_eq!( - // micromark("[x](y\t)"), - // "

x

", - // "should support a tab ending a link resource" - // ); + assert_eq!( + micromark("[x](y\t)"), + "

x

", + "should support a tab ending a link resource" + ); - // To do: link (resource). - // assert_eq!( - // micromark("[x](y\t\"z\")"), - // "

x

", - // "should support a tab between a link destination and title" - // ); + assert_eq!( + micromark("[x](y\t\"z\")"), + "

x

", + "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("[](<%>)"), - // "

", - // "should support incorrect percentage encoded values (1)" - // ); - - // To do: link. - // assert_eq!( - // micromark("[](<%%20>)"), - // "

", - // "should support incorrect percentage encoded values (2)" - // ); - - // To do: link. - // assert_eq!( - // micromark("[](<%a%20>)"), - // "

", - // "should support incorrect percentage encoded values (3)" - // ); + assert_eq!( + micromark("[](<%>)"), + "

", + "should support incorrect percentage encoded values (1)" + ); - // Surrogate handling not needed in Rust. + assert_eq!( + micromark("[](<%%20>)"), + "

", + "should support incorrect percentage encoded values (2)" + ); + + assert_eq!( + micromark("[](<%a%20>)"), + "

", + "should support incorrect percentage encoded values (3)" + ); + + // Note: Surrogate handling not needed in Rust. // assert_eq!( // micromark("[]()"), // "

", @@ -114,39 +111,37 @@ fn url() { // "should support a lone low surrogate at the end (highest)" // ); - // To do: link. - // assert_eq!( - // micromark("[](<🤔>)"), - // "

", - // "should support an emoji" - // ); - - // To do: link. - // let mut ascii: Vec = 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::(); - // 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!("

", ascii_out), - // "should support ascii characters" - // ); + assert_eq!( + micromark("[](<🤔>)"), + "

", + "should support an emoji" + ); + + let mut ascii: Vec = 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::(); + 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!("

", ascii_out), + "should support ascii characters" + ); } -- cgit