aboutsummaryrefslogtreecommitdiffstats
path: root/tests/image.rs
diff options
context:
space:
mode:
authorLibravatar Titus Wormer <tituswormer@gmail.com>2022-06-24 17:57:10 +0200
committerLibravatar Titus Wormer <tituswormer@gmail.com>2022-06-24 17:57:10 +0200
commita3dd207e3b1ebcbcb6cec0f703a695e51ae4ece0 (patch)
tree7b4bf040da23a03f38efe92a252e187a630a14f6 /tests/image.rs
parente7b3761c8cd6f0f902dd9927e4fbf2589465ed57 (diff)
downloadmarkdown-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/image.rs')
-rw-r--r--tests/image.rs229
1 files changed, 229 insertions, 0 deletions
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 &amp; 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 &amp; 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 &amp; 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 &quot;title&quot;</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("![&amp;&copy;&](example.com/&amp;&copy;& \"&amp;&copy;&\")"),
+ "<p><img src=\"example.com/&amp;%C2%A9&amp;\" alt=\"&amp;©&amp;\" title=\"&amp;©&amp;\" /></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`"
+ // );
+}