use crate::nodes::{NodeCode, NodeValue}; use super::*; #[test] fn basic() { html( concat!( "My **document**.\n", "\n", "It's mine.\n", "\n", "> Yes.\n", "\n", "## Hi!\n", "\n", "Okay.\n" ), concat!( "
My document.
\n", "It's mine.
\n", "\n", "\n", "Yes.
\n", "
Okay.
\n" ), ); } #[test] fn codefence() { html( concat!("``` rust yum\n", "fn main<'a>();\n", "```\n"), concat!( "fn main<'a>();\n",
"\n"
),
);
}
#[test]
fn lists() {
html(
concat!("2. Hello.\n", "3. Hi.\n"),
concat!(
"foo
\n", "####### foo
\n" ), ); } #[test] fn atx_heading_sourcepos() { assert_ast_match!( [], "# h1\n" "foo\n" "## h2\n" // https://spec.commonmark.org/0.31.2/#example-63 "####### foo\n", (document (1:1-4:11) [ (heading (1:1-1:4) [ (text (1:3-1:4) "h1") ]) (paragraph (2:1-2:3) [ (text (2:1-2:3) "foo") ]) (heading (3:1-3:5) [ (text (3:4-3:5) "h2") ]) (paragraph (4:1-4:11) [ (text (4:1-4:11) "####### foo") ]) ]) ); } #[test] fn atx_heading_closed() { let input = concat!( // https://spec.commonmark.org/0.31.2/#example-71 "## Heading ##\n", " ## Heading ##\n", // https://spec.commonmark.org/0.31.2/#example-72 "## Heading #########\n", "#### Heading ##\n", // https://spec.commonmark.org/0.31.2/#example-73 "## Heading ### \n", ); let arena = Arena::new(); let options = Options::default(); let root = parse_document(&arena, input, &options); let headings = root .descendants() .filter(|n| matches!(n.data().value, NodeValue::Heading(_))); for heading in headings { if let NodeValue::Heading(ref nh) = heading.data().value { assert!( nh.closed, "Expected all ATX headings to be closed: {:?}", heading ); } } } #[test] fn atx_heading_not_closed() { let input = concat!( // https://spec.commonmark.org/0.31.2/#example-62 "# Heading\n", // https://spec.commonmark.org/0.31.2/#example-74 "### Heading ### b\n", // https://spec.commonmark.org/0.31.2/#example-75 "# Heading#\n", // https://spec.commonmark.org/0.31.2/#example-76 "### Heading \\###\n", "## Heading #\\##\n", "# Heading \\#\n", ); let arena = Arena::new(); let options = Options::default(); let root = parse_document(&arena, input, &options); let headings = root .descendants() .filter(|n| matches!(n.data().value, NodeValue::Heading(_))); for heading in headings { if let NodeValue::Heading(ref nh) = heading.data().value { assert!( !nh.closed, "Expected all ATX headings to be not closed: {:?}", heading ); } } } #[test] fn atx_heading_closed_sourcepos() { assert_ast_match!( [parse.escaped_char_spans], // https://spec.commonmark.org/0.31.2/#example-71 "## Heading ##\n" " ## Heading ##\n" // https://spec.commonmark.org/0.31.2/#example-72 "## Heading #########\n" "#### Heading ##\n" // Not part of the spec, but for completeness "######### Not heading ##\n" // https://spec.commonmark.org/0.31.2/#example-73 "## Heading ### \n" // https://spec.commonmark.org/0.31.2/#example-74 "### Heading ### b\n" // https://spec.commonmark.org/0.31.2/#example-75 "# Heading#\n" // https://spec.commonmark.org/0.31.2/#example-76 "### Heading \\###\n" "## Heading #\\##\n" "# Heading \\#\n", (document (1:1-11:12) [ (heading (1:1-1:13) [ (text (1:4-1:10) "Heading") ]) (heading (2:4-2:16) [ (text (2:7-2:13) "Heading") ]) (heading (3:1-3:20) [ (text (3:4-3:10) "Heading") ]) (heading (4:1-4:15) [ (text (4:6-4:12) "Heading") ]) (paragraph (5:1-5:24) [ (text (5:1-5:24) "######### Not heading ##") ]) (heading (6:1-6:17) [ (text (6:4-6:10) "Heading") ]) (heading (7:1-7:17) [ (text (7:5-7:17) "Heading ### b") ]) (heading (8:1-8:10) [ (text (8:3-8:10) "Heading#") ]) (heading (9:1-9:16) [ (text (9:5-9:12) "Heading ") (escaped (9:13-9:14) [ (text (9:14-9:14) "#") ]) (text (9:15-9:16) "##") ]) (heading (10:1-10:15) [ (text (10:4-10:12) "Heading #") (escaped (10:13-10:14) [ (text (10:14-10:14) "#") ]) (text (10:15-10:15) "#") ]) (heading (11:1-11:12) [ (text (11:3-11:10) "Heading ") (escaped (11:11-11:12) [ (text (11:12-11:12) "#") ]) ]) ]) ); } #[test] fn setext_heading() { html( concat!("Hi\n", "==\n", "\n", "Ok\n", "-----\n"), concat!("text text
\n

\n",
"*ok*\n",
"\n",
"*ok*\n",
"\n",
"\n",
"*ok*\n"
),
concat!(
" *ok*\n",
"ok
\n",
"ok
\n",
"\n",
"*ok*\n",
"\n",
"ok
\n",
"\n",
"ok
\n"
),
);
}
#[test]
fn html_block_2() {
html_opts!(
[render.r#unsafe],
concat!(" *hi*\n", "*hi*\n"),
concat!(
" *hi*\n",
"hi
\n"
),
);
}
#[test]
fn html_block_3() {
html_opts!(
[render.r#unsafe],
concat!(" o\n", "k ?> *a*\n", "*a*\n"),
concat!(" o\n", "k ?> *a*\n", "a
\n"),
);
}
#[test]
fn html_block_4() {
html_opts!(
[render.r#unsafe],
concat!("\n", "ok\n", " h\n", "ok\n"),
concat!("\n", "ok
\n", " h\n", "ok
\n"),
);
}
#[test]
fn html_block_5() {
html_opts!(
[render.r#unsafe],
concat!(
"\n",
"*ok*\n",
"]]> *ok*\n",
"*ok*\n"
),
concat!(
"\n",
"*ok*\n",
"]]> *ok*\n",
"ok
\n"
),
);
}
#[test]
fn html_block_6() {
html_opts!(
[render.r#unsafe],
concat!(" \n", "*x*\n", "\n", "ok\n", "\n", "ok
\n", "<a b=>\n", "ok
\n", "<a b\n", " c\n", "ok
\n" ), ); html_opts!( [render.r#unsafe], concat!("\n", "ok\n", "\n", "ok\n"), concat!("\n", "ok\n", "ok
\n"), ); } #[test] fn backticks() { html( "Some `code\\` yep.\n", "Some code\\ yep.
\n\n"); } #[test] fn blockquote_hard_linebreak_nonlazy_space() { html( "> A\\\n> B", "\n
\nA
\n\n", ); } #[test] fn backticks_num() { let input = "Some `code1`. More ``` code2 ```.\n"; let arena = Arena::new(); let options = Options::default(); let root = parse_document(&arena, input, &options); let code1 = NodeValue::Code(NodeCode { num_backticks: 1, literal: "code1".to_string(), }); assert_node_eq(root, &[0, 1], &code1); let code2 = NodeValue::Code(NodeCode { num_backticks: 3, literal: "code2".to_string(), }); assert_node_eq(root, &[0, 3], &code2); } #[test] fn backslashes() { html( concat!( "Some \\`fake code\\`.\n", "\n", "Some fake linebreaks:\\\n", "Yes.\\\n", "See?\n", "\n", "Ga\\rbage.\n" ), concat!( "A
\n
\nB
Some `fake code`.
\n", "Some fake linebreaks:
\n",
"Yes.
\n",
"See?
Ga\\rbage.
\n" ), ); } #[test] fn entities() { html( concat!( "This is &, ©, ™, \\™, &xyz;, ≂̸.\n", "\n", "∞ ∞\n" ), concat!( "This is &, ©, ™, ™, &xyz;, \u{2242}\u{338}.
\n", "∞ ∞
\n" ), ); } #[test] fn links() { html( concat!( "Where are you [going](https://microsoft.com (today))?\n", "\n", "[Where am I?](/here)\n" ), concat!( "Where are you going?
\n", "\n" ), ); html( concat!( r"Where are you [going](#1\.-link (today))?", "\n", "\n", "[Where am I?](/here)\n" ), concat!( "Where are you going?
\n", "\n" ), ); html( r"[Link Text](\\\\)", concat!(r##""##, '\n'), ); html( r"[Link Text](\\\\\\\\\\)", concat!(r##""##, '\n'), ); html( r"[Link Text](\\\\ (title))", concat!( r##""##, '\n' ), ); html( r"[Link Text](\#)", concat!(r##""##, '\n'), ); } #[test] fn images() { html( "I am ](http://i.imgur.com/QqK1vq7.png).\n", "I am
.
[a](\\ b)
\n"); } #[test] fn nul_replacement_1() { html("a\0b", "a\u{fffd}b
\n"); } #[test] fn nul_replacement_2() { html("a\0b\0c", "a\u{fffd}b\u{fffd}c
\n"); } #[test] fn nul_replacement_3() { html("a\0\nb", "a\u{fffd}\nb
\n"); } #[test] fn nul_replacement_4() { html("a\0\r\nb", "a\u{fffd}\nb
\n"); } #[test] fn nul_replacement_5() { html("a\r\n\0b", "a\n\u{fffd}b
\n"); } #[test] fn case_insensitive_safety() { html( "[a](javascript:a) [b](Javascript:b) [c](jaVascript:c) [d](data:xyz) [e](Data:xyz) [f](vbscripT:f) [g](FILE:g)\n", "\n", ); } #[test] fn link_sourcepos_baseline() { assert_ast_match!( [], "[ABCD](/)\n", (document (1:1-1:9) [ (paragraph (1:1-1:9) [ (link (1:1-1:9) "/" [ (text (1:2-1:5) "ABCD") ]) ]) ]) ); } // https://github.com/kivikakk/comrak/issues/301 #[test] fn link_sourcepos_newline() { assert_ast_match!( [], "[AB\nCD](/)\n", (document (1:1-2:6) [ (paragraph (1:1-2:6) [ (link (1:1-2:6) "/" [ (text (1:2-1:3) "AB") (softbreak (1:4-1:4)) (text (2:1-2:2) "CD") ]) ]) ]) ); } // https://github.com/kivikakk/comrak/issues/572 #[test] fn link_sourcepos_newline_n_between_dest_and_title() { assert_ast_match!( [], "[link]( /uri\n \"title\" )\n", (document (1:1-2:12) [ (paragraph (1:1-2:12) [ (link (1:1-1:14) "/uri" [ (text (1:2-1:5) "link") ]) ]) ]) ); } #[test] fn link_sourcepos_newline_r_between_dest_and_title() { assert_ast_match!( [], "[link]( /uri\r \"title\" )\r", (document (1:1-2:12) [ (paragraph (1:1-2:12) [ (link (1:1-1:14) "/uri" [ (text (1:2-1:5) "link") ]) ]) ]) ); } #[test] fn link_sourcepos_newline_rn_between_dest_and_title() { assert_ast_match!( [], "[link]( /uri\r\n \"title\" )\r\n", (document (1:1-2:12) [ (paragraph (1:1-2:12) [ (link (1:1-1:14) "/uri" [ (text (1:2-1:5) "link") ]) ]) ]) ); } #[test] fn link_sourcepos_truffle() { assert_ast_match!( [], "- A\n[](/B)\n", (document (1:1-2:18) [ (list (1:1-2:18) [ (item (1:1-2:18) [ (paragraph (1:3-2:18) [ (text (1:3-1:3) "A") (softbreak (1:4-1:4)) (link (2:1-2:18) "/B" [ (image (2:2-2:13) "/B.png" [ (text (2:4-2:4) "B") ]) ]) ]) ]) ]) ]) ); } #[test] fn link_sourcepos_truffle_twist() { assert_ast_match!( [], "- A\n [](/B)\n", (document (1:1-2:20) [ (list (1:1-2:20) [ (item (1:1-2:20) [ (paragraph (1:3-2:20) [ (text (1:3-1:3) "A") (softbreak (1:4-1:4)) (link (2:3-2:20) "/B" [ (image (2:4-2:15) "/B.png" [ (text (2:6-2:6) "B") ]) ]) ]) ]) ]) ]) ); } #[test] fn link_sourcepos_truffle_bergamot() { assert_ast_match!( [], "- A\n [](/B)\n", (document (1:1-2:21) [ (list (1:1-2:21) [ (item (1:1-2:21) [ (paragraph (1:3-2:21) [ (text (1:3-1:3) "A") (softbreak (1:4-1:4)) (link (2:4-2:21) "/B" [ (image (2:5-2:16) "/B.png" [ (text (2:7-2:7) "B") ]) ]) ]) ]) ]) ]) ); } #[test] fn paragraph_sourcepos_multiline() { assert_ast_match!( [], " A\n" " B\n", (document (1:1-2:4) [ (paragraph (1:3-2:4) [ (text (1:3-1:3) "A") (softbreak (1:4-1:4)) (text (2:4-2:4) "B") ]) ]) ); } #[test] fn listitem_sourcepos_multiline() { assert_ast_match!( [], "- A\n" "B\n", (document (1:1-2:1) [ (list (1:1-2:1) [ (item (1:1-2:1) [ (paragraph (1:3-2:1) [ (text (1:3-1:3) "A") (softbreak (1:4-1:4)) (text (2:1-2:1) "B") ]) ]) ]) ]) ); } #[test] fn listitem_sourcepos_multiline_2() { assert_ast_match!( [], "- A\n" " B\n" "- C\n" " D", (document (1:1-4:2) [ (list (1:1-4:2) [ (item (1:1-2:4) [ (paragraph (1:3-2:4) [ (text (1:3-1:3) "A") (softbreak (1:4-1:4)) (text (2:4-2:4) "B") ]) ]) (item (3:1-4:2) [ (paragraph (3:4-4:2) [ (text (3:4-3:4) "C") (softbreak (3:5-3:5)) (text (4:2-4:2) "D") ]) ]) ]) ]) ); } #[test] fn emphasis_sourcepos_double_1() { assert_ast_match!( [], "_**this**_\n", (document (1:1-1:10) [ (paragraph (1:1-1:10) [ (emph (1:1-1:10) [ (strong (1:2-1:9) [ (text (1:4-1:7) "this") ]) ]) ]) ]) ); } #[test] fn emphasis_sourcepos_double_2() { assert_ast_match!( [], "**_this_**\n", (document (1:1-1:10) [ (paragraph (1:1-1:10) [ (strong (1:1-1:10) [ (emph (1:3-1:8) [ (text (1:4-1:7) "this") ]) ]) ]) ]) ); } #[test] fn emphasis_sourcepos_double_3() { assert_ast_match!( [], "___this___\n", (document (1:1-1:10) [ (paragraph (1:1-1:10) [ (emph (1:1-1:10) [ (strong (1:2-1:9) [ (text (1:4-1:7) "this") ]) ]) ]) ]) ); } #[test] fn emphasis_sourcepos_double_4() { assert_ast_match!( [], "***this***\n", (document (1:1-1:10) [ (paragraph (1:1-1:10) [ (emph (1:1-1:10) [ (strong (1:2-1:9) [ (text (1:4-1:7) "this") ]) ]) ]) ]) ); } #[test] fn ipv6_host_unescaped() { html( "testtest http://[319:3cf0:dd1d:47b9:20c:29ff:fe2c:39be]/test
\n", ); html( "[henwo](https://[2402:1f00:89aa:300::5%25eth0]:9443?target=[foo]:
\n[foo]
\n"); }