use super::*; // A series of tests ensuring the default HTML formatter doesn't panic on // unusual but still believable ASTs. We also assert expected panics give at // least helpful messages. #[test] fn test_paragraph_at_root_crash() { let options = Options { ..Default::default() }; let arena = Arena::new(); let para = parse_document(&arena, "para", &options) .first_child() .unwrap(); // This is the bit we don't expect: what if the paragraph doesn't have a // parent at all? Normally it should have at least a Document. para.detach(); let mut output = String::new(); html::format_document(para, &options, &mut output).unwrap(); } #[test] fn test_empty_table_crash() { let options = Options { extension: options::Extension { table: true, ..Default::default() }, ..Default::default() }; let arena = Arena::new(); let table = parse_document(&arena, "| x |\n| - |\n| z |", &options) .first_child() .unwrap(); // What if the table has been emptied of *all* children? while let Some(child) = table.first_child() { child.detach(); } let mut output = String::new(); html::format_document(table, &options, &mut output).unwrap(); } #[test] #[should_panic(expected = "rendered a table cell without a containing table")] fn test_table_cell_out_of_water_crash() { let options = Options { extension: options::Extension { table: true, ..Default::default() }, ..Default::default() }; let arena = Arena::new(); let doc = parse_document(&arena, "| x |\n| - |\n| z |", &options); let table_row = doc .first_child() // table .unwrap() .last_child() // table row .unwrap(); let table_cell = table_row .first_child() // table cell .unwrap(); // What if the table cell has no owning table? table_row.detach(); let mut output = String::new(); html::format_document(table_cell, &options, &mut output).unwrap(); } #[test] #[should_panic(expected = "rendered a table cell without a containing table row")] fn test_table_cell_out_of_school_crash() { let options = Options { extension: options::Extension { table: true, ..Default::default() }, ..Default::default() }; let arena = Arena::new(); let doc = parse_document(&arena, "| x |\n| - |\n| z |", &options); let table_row = doc .first_child() // table .unwrap() .last_child() // table row .unwrap(); let table_cell = table_row .first_child() // table cell .unwrap(); // What if the table cell has no owning table row? table_cell.detach(); let mut output = String::new(); html::format_document(table_cell, &options, &mut output).unwrap(); } // List of HTML block kinds: https://spec.commonmark.org/0.31.2/#html-blocks #[test] fn sourcepos_kind_1() { assert_ast_match!( [], "", (document (1:1-1:17) [ (html_block (1:1-1:17) "") ]) ); assert_ast_match!( [], "", (document (1:1-1:18) [ (html_block (1:1-1:18) "") ]) ); assert_ast_match!( [], "", (document (1:1-1:29) [ (html_block (1:1-1:29) "") ]) ); assert_ast_match!( [], "\n\n**parsed**", (document (1:1-5:10) [ (html_block (1:1-3:19) "\n") (paragraph (5:1-5:10) [ (strong (5:1-5:10) [ (text (5:3-5:8) "parsed") ]) ]) ]) ); assert_ast_match!( [], "", (document (1:1-1:27) [ (html_block (1:1-1:27) "") ]) ); assert_ast_match!( [], "", (document (1:1-1:33) [ (html_block (1:1-1:33) "") ]) ); assert_ast_match!( [], "
A html block
", (document (1:1-1:23) [ (html_block (1:1-1:23) "
A html block
") ]) ); assert_ast_match!( [], "
\nA html block\n
", (document (1:1-3:6) [ (html_block (1:1-3:6) "
\nA html block\n
") ]) ); assert_ast_match!( [], "
\n\nA html block\n\n
", (document (1:1-5:6) [ (html_block (1:1-5:6) "
\n\nA html block\n\n
") ]) ); assert_ast_match!( [], "Test\n\n
\nA html block\n
\n\nMore text", (document (1:1-7:9) [ (paragraph (1:1-1:4) [ (text (1:1-1:4) "Test") ]) (html_block (3:1-5:6) "
\nA html block\n
\n") (paragraph (7:1-7:9) [ (text (7:1-7:9) "More text") ]) ]) ); } #[test] fn sourcepos_kind_2() { assert_ast_match!( [], "", (document (1:1-1:7) [ (html_block (1:1-1:7) "") ]) ); assert_ast_match!( [], "", (document (1:1-1:8) [ (html_block (1:1-1:8) "") ]) ); assert_ast_match!( [], "", (document (1:1-1:21) [ (html_block (1:1-1:21) "") ]) ); assert_ast_match!( [], "", (document (1:1-3:9) [ (html_block (1:1-3:9) "") ]) ); assert_ast_match!( [], "", (document (1:1-5:9) [ (html_block (1:1-5:9) "") ]) ); assert_ast_match!( [], "Test\n\n\n\nMore text", (document (1:1-7:9) [ (paragraph (1:1-1:4) [ (text (1:1-1:4) "Test") ]) (html_block (3:1-5:9) "\n") (paragraph (7:1-7:9) [ (text (7:1-7:9) "More text") ]) ]) ); } #[test] fn sourcepos_kind_3() { assert_ast_match!( [], "", (document (1:1-1:4) [ (html_block (1:1-1:4) "") ]) ); assert_ast_match!( [], "", (document (1:1-1:5) [ (html_block (1:1-1:5) "") ]) ); assert_ast_match!( [], "", (document (1:1-1:22) [ (html_block (1:1-1:22) "") ]) ); assert_ast_match!( [], "", (document (1:1-3:8) [ (html_block (1:1-3:8) "") ]) ); assert_ast_match!( [], "", (document (1:1-5:8) [ (html_block (1:1-5:8) "") ]) ); assert_ast_match!( [], "Test\n\n\n\nMore text", (document (1:1-7:9) [ (paragraph (1:1-1:4) [ (text (1:1-1:4) "Test") ]) (html_block (3:1-5:8) "\n") (paragraph (7:1-7:9) [ (text (7:1-7:9) "More text") ]) ]) ); } #[test] fn sourcepos_kind_4() { assert_ast_match!( [], "", (document (1:1-1:4) [ (html_block (1:1-1:4) "") ]) ); assert_ast_match!( [], "", (document (1:1-1:4) [ (html_block (1:1-1:4) "") ]) ); assert_ast_match!( [], "", (document (1:1-1:15) [ (html_block (1:1-1:15) "") ]) ); assert_ast_match!( [], "", (document (1:1-2:5) [ (html_block (1:1-2:5) "") ]) ); assert_ast_match!( [], "", (document (1:1-3:5) [ (html_block (1:1-3:5) "") ]) ); assert_ast_match!( [], "Test\n\n\n\nMore text", (document (1:1-6:9) [ (paragraph (1:1-1:4) [ (text (1:1-1:4) "Test") ]) (html_block (3:1-4:5) "\n") (paragraph (6:1-6:9) [ (text (6:1-6:9) "More text") ]) ]) ); } #[test] fn sourcepos_kind_5() { assert_ast_match!( [], "", (document (1:1-1:12) [ (html_block (1:1-1:12) "") ]) ); assert_ast_match!( [], "", (document (1:1-1:24) [ (html_block (1:1-1:24) "") ]) ); assert_ast_match!( [], "", (document (1:1-3:8) [ (html_block (1:1-3:8) "") ]) ); assert_ast_match!( [], "", (document (1:1-5:8) [ (html_block (1:1-5:8) "") ]) ); assert_ast_match!( [], "Test\n\n\n\nMore text", (document (1:1-7:9) [ (paragraph (1:1-1:4) [ (text (1:1-1:4) "Test") ]) (html_block (3:1-5:8) "\n") (paragraph (7:1-7:9) [ (text (7:1-7:9) "More text") ]) ]) ); } #[test] fn sourcepos_kind_6() { assert_ast_match!( [], "
", (document (1:1-1:11) [ (html_block (1:1-1:11) "
") ]) ); assert_ast_match!( [], "
A html block
", (document (1:1-1:23) [ (html_block (1:1-1:23) "
A html block
") ]) ); assert_ast_match!( [], "
A html block\n
", (document (1:1-2:6) [ (html_block (1:1-2:6) "
A html block\n
") ]) ); assert_ast_match!( [], "
\nA html block
", (document (1:1-2:18) [ (html_block (1:1-2:18) "
\nA html block
") ]) ); assert_ast_match!( [], "
\nA html block\n
", (document (1:1-3:6) [ (html_block (1:1-3:6) "
\nA html block\n
") ]) ); assert_ast_match!( [], "
\nA html block\n\n
", (document (1:1-4:6) [ (html_block (1:1-2:12) "
\nA html block\n") (html_block (4:1-4:6) "
") ]) ); assert_ast_match!( [], "
\n\nA html block\n
", (document (1:1-4:6) [ (html_block (1:1-1:5) "
\n") (paragraph (3:1-3:12) [ (text (3:1-3:12) "A html block") ]) (html_block (4:1-4:6) "
") ]) ); assert_ast_match!( [], "
\n\nA html block\n\n
", (document (1:1-5:6) [ (html_block (1:1-1:5) "
\n") (paragraph (3:1-3:12) [ (text (3:1-3:12) "A html block") ]) (html_block (5:1-5:6) "
") ]) ); assert_ast_match!( [], "Test\n
\nA html block\n
\n\nMore text", (document (1:1-6:9) [ (paragraph (1:1-1:4) [ (text (1:1-1:4) "Test") ]) (html_block (2:1-4:6) "
\nA html block\n
\n") (paragraph (6:1-6:9) [ (text (6:1-6:9) "More text") ]) ]) ); assert_ast_match!( [], "Test\n\n
\nA html block\n
\n\nMore text", (document (1:1-7:9) [ (paragraph (1:1-1:4) [ (text (1:1-1:4) "Test") ]) (html_block (3:1-5:6) "
\nA html block\n
\n") (paragraph (7:1-7:9) [ (text (7:1-7:9) "More text") ]) ]) ); assert_ast_match!( [], "Test\n\n
\nA html block\n\n
\n\nMore text", (document (1:1-8:9) [ (paragraph (1:1-1:4) [ (text (1:1-1:4) "Test") ]) (html_block (3:1-4:12) "
\nA html block\n") (html_block (6:1-6:6) "
\n") (paragraph (8:1-8:9) [ (text (8:1-8:9) "More text") ]) ]) ); assert_ast_match!( [], "Test\n\n
\n\nA html block\n
\n\nMore text", (document (1:1-8:9) [ (paragraph (1:1-1:4) [ (text (1:1-1:4) "Test") ]) (html_block (3:1-3:5) "
\n") (paragraph (5:1-5:12) [ (text (5:1-5:12) "A html block") ]) (html_block (6:1-6:6) "
\n") (paragraph (8:1-8:9) [ (text (8:1-8:9) "More text") ]) ]) ); assert_ast_match!( [], "Test\n\n
\n\nA html block\n\n
\n\nMore text", (document (1:1-9:9) [ (paragraph (1:1-1:4) [ (text (1:1-1:4) "Test") ]) (html_block (3:1-3:5) "
\n") (paragraph (5:1-5:12) [ (text (5:1-5:12) "A html block") ]) (html_block (7:1-7:6) "
\n") (paragraph (9:1-9:9) [ (text (9:1-9:9) "More text") ]) ]) ); } #[test] fn sourcepos_kind_7() { assert_ast_match!( [], "", (document (1:1-1:15) [ (paragraph (1:1-1:15) [ (html_inline (1:1-1:7) "") (html_inline (1:8-1:15) "") ]) ]) ); assert_ast_match!( [], "\n", (document (1:1-2:8) [ (html_block (1:1-2:8) "\n") ]) ); assert_ast_match!( [], "\n\n", (document (1:1-3:8) [ (html_block (1:1-1:7) "\n") (html_block (3:1-3:8) "") ]) ); assert_ast_match!( [], "\nA html block\n", (document (1:1-3:7) [ (html_block (1:1-3:7) "\nA html block\n") ]) ); assert_ast_match!( [], "\nB html block\n\n", (document (1:1-4:7) [ (html_block (1:1-2:12) "\nB html block\n") (html_block (4:1-4:7) "") ]) ); assert_ast_match!( [], "\n\nC html block\n", (document (1:1-4:7) [ (html_block (1:1-1:6) "\n") (paragraph (3:1-4:7) [ (text (3:1-3:12) "C html block") (softbreak (3:13-3:13)) (html_inline (4:1-4:7) "") ]) ]) ); assert_ast_match!( [], "\n\nD html block\n\n", (document (1:1-5:7) [ (html_block (1:1-1:6) "\n") (paragraph (3:1-3:12) [ (text (3:1-3:12) "D html block") ]) (html_block (5:1-5:7) "") ]) ); assert_ast_match!( [], "Test\n\n\nE html block\n\n\nMore text", (document (1:1-7:9) [ (paragraph (1:1-1:4) [ (text (1:1-1:4) "Test") ]) (html_block (3:1-5:7) "\nE html block\n\n") (paragraph (7:1-7:9) [ (text (7:1-7:9) "More text") ]) ]) ); assert_ast_match!( [], "Test\n\n\n\nF html block\n\n\n\nMore text", (document (1:1-9:9) [ (paragraph (1:1-1:4) [ (text (1:1-1:4) "Test") ]) (html_block (3:1-3:6) "\n") (paragraph (5:1-5:12) [ (text (5:1-5:12) "F html block") ]) (html_block (7:1-7:7) "\n") (paragraph (9:1-9:9) [ (text (9:1-9:9) "More text") ]) ]) ); } #[test] fn sourcepos_inside_blockquote() { // Kind 1 assert_ast_match!( [], "> ", (document (1:1-1:19) [ (block_quote (1:1-1:19) [ (html_block (1:3-1:19) "") ]) ]) ); // Kind 2 assert_ast_match!( [], "> ", (document (1:1-1:9) [ (block_quote (1:1-1:9) [ (html_block (1:3-1:9) "") ]) ]) ); // Kind 3 assert_ast_match!( [], "> ", (document (1:1-1:6) [ (block_quote (1:1-1:6) [ (html_block (1:3-1:6) "") ]) ]) ); // Kind 4 assert_ast_match!( [], "> ", (document (1:1-1:6) [ (block_quote (1:1-1:6) [ (html_block (1:3-1:6) "") ]) ]) ); // Kind 5 assert_ast_match!( [], "> ", (document (1:1-1:14) [ (block_quote (1:1-1:14) [ (html_block (1:3-1:14) "") ]) ]) ); // Kind 6 assert_ast_match!( [], ">
", (document (1:1-1:13) [ (block_quote (1:1-1:13) [ (html_block (1:3-1:13) "
") ]) ]) ); // Kind 7 assert_ast_match!( [], "> ", (document (1:1-1:17) [ (block_quote (1:1-1:17) [ (paragraph (1:3-1:17) [ (html_inline (1:3-1:9) "") (html_inline (1:10-1:17) "") ]) ]) ]) ); }