Debian Codegraph @

src/tests/html.rs

raw
1 use super::*;
2
3 // A series of tests ensuring the default HTML formatter doesn't panic on
4 // unusual but still believable ASTs. We also assert expected panics give at
5 // least helpful messages.
6
7 #[test]
8 fn test_paragraph_at_root_crash() {
9 let options = Options {
10 ..Default::default()
11 };
12 let arena = Arena::new();
13
14 let para = parse_document(&arena, "para", &options)
15 .first_child()
16 .unwrap();
17
18 // This is the bit we don't expect: what if the paragraph doesn't have a
19 // parent at all? Normally it should have at least a Document.
20 para.detach();
21
22 let mut output = String::new();
23 html::format_document(para, &options, &mut output).unwrap();
24 }
25
26 #[test]
27 fn test_empty_table_crash() {
28 let options = Options {
29 extension: options::Extension {
30 table: true,
31 ..Default::default()
32 },
33 ..Default::default()
34 };
35 let arena = Arena::new();
36
37 let table = parse_document(&arena, "| x |\n| - |\n| z |", &options)
38 .first_child()
39 .unwrap();
40
41 // What if the table has been emptied of *all* children?
42 while let Some(child) = table.first_child() {
43 child.detach();
44 }
45
46 let mut output = String::new();
47 html::format_document(table, &options, &mut output).unwrap();
48 }
49
50 #[test]
51 #[should_panic(expected = "rendered a table cell without a containing table")]
52 fn test_table_cell_out_of_water_crash() {
53 let options = Options {
54 extension: options::Extension {
55 table: true,
56 ..Default::default()
57 },
58 ..Default::default()
59 };
60 let arena = Arena::new();
61
62 let doc = parse_document(&arena, "| x |\n| - |\n| z |", &options);
63
64 let table_row = doc
65 .first_child() // table
66 .unwrap()
67 .last_child() // table row
68 .unwrap();
69
70 let table_cell = table_row
71 .first_child() // table cell
72 .unwrap();
73
74 // What if the table cell has no owning table?
75 table_row.detach();
76
77 let mut output = String::new();
78 html::format_document(table_cell, &options, &mut output).unwrap();
79 }
80
81 #[test]
82 #[should_panic(expected = "rendered a table cell without a containing table row")]
83 fn test_table_cell_out_of_school_crash() {
84 let options = Options {
85 extension: options::Extension {
86 table: true,
87 ..Default::default()
88 },
89 ..Default::default()
90 };
91 let arena = Arena::new();
92
93 let doc = parse_document(&arena, "| x |\n| - |\n| z |", &options);
94
95 let table_row = doc
96 .first_child() // table
97 .unwrap()
98 .last_child() // table row
99 .unwrap();
100
101 let table_cell = table_row
102 .first_child() // table cell
103 .unwrap();
104
105 // What if the table cell has no owning table row?
106 table_cell.detach();
107
108 let mut output = String::new();
109 html::format_document(table_cell, &options, &mut output).unwrap();
110 }
111
112 // List of HTML block kinds: https://spec.commonmark.org/0.31.2/#html-blocks
113 #[test]
114 fn sourcepos_kind_1() {
115 assert_ast_match!(
116 [],
117 "<script></script>",
118 (document (1:1-1:17) [
119 (html_block (1:1-1:17) "<script></script>")
120 ])
121 );
122
123 assert_ast_match!(
124 [],
125 "<script> </script>",
126 (document (1:1-1:18) [
127 (html_block (1:1-1:18) "<script> </script>")
128 ])
129 );
130
131 assert_ast_match!(
132 [],
133 "<script>A html block</script>",
134 (document (1:1-1:29) [
135 (html_block (1:1-1:29) "<script>A html block</script>")
136 ])
137 );
138
139 assert_ast_match!(
140 [],
141 "<SCRIPT>A html\n\n**block** </Script>\n\n**parsed**",
142 (document (1:1-5:10) [
143 (html_block (1:1-3:19) "<SCRIPT>A html\n\n**block** </Script>\n")
144 (paragraph (5:1-5:10) [
145 (strong (5:1-5:10) [
146 (text (5:3-5:8) "parsed")
147 ])
148 ])
149 ])
150 );
151
152 assert_ast_match!(
153 [],
154 "<style>A html block</style>",
155 (document (1:1-1:27) [
156 (html_block (1:1-1:27) "<style>A html block</style>")
157 ])
158 );
159
160 assert_ast_match!(
161 [],
162 "<textarea>A html block</textarea>",
163 (document (1:1-1:33) [
164 (html_block (1:1-1:33) "<textarea>A html block</textarea>")
165 ])
166 );
167
168 assert_ast_match!(
169 [],
170 "<pre>A html block</pre>",
171 (document (1:1-1:23) [
172 (html_block (1:1-1:23) "<pre>A html block</pre>")
173 ])
174 );
175
176 assert_ast_match!(
177 [],
178 "<pre>\nA html block\n</pre>",
179 (document (1:1-3:6) [
180 (html_block (1:1-3:6) "<pre>\nA html block\n</pre>")
181 ])
182 );
183
184 assert_ast_match!(
185 [],
186 "<pre>\n\nA html block\n\n</pre>",
187 (document (1:1-5:6) [
188 (html_block (1:1-5:6) "<pre>\n\nA html block\n\n</pre>")
189 ])
190 );
191
192 assert_ast_match!(
193 [],
194 "Test\n\n<pre>\nA html block\n</pre>\n\nMore text",
195 (document (1:1-7:9) [
196 (paragraph (1:1-1:4) [
197 (text (1:1-1:4) "Test")
198 ])
199 (html_block (3:1-5:6) "<pre>\nA html block\n</pre>\n")
200 (paragraph (7:1-7:9) [
201 (text (7:1-7:9) "More text")
202 ])
203 ])
204 );
205 }
206
207 #[test]
208 fn sourcepos_kind_2() {
209 assert_ast_match!(
210 [],
211 "<!---->",
212 (document (1:1-1:7) [
213 (html_block (1:1-1:7) "<!---->")
214 ])
215 );
216
217 assert_ast_match!(
218 [],
219 "<!-- -->",
220 (document (1:1-1:8) [
221 (html_block (1:1-1:8) "<!-- -->")
222 ])
223 );
224
225 assert_ast_match!(
226 [],
227 "<!-- A html block -->",
228 (document (1:1-1:21) [
229 (html_block (1:1-1:21) "<!-- A html block -->")
230 ])
231 );
232
233 assert_ast_match!(
234 [],
235 "<!-- A\nhtml\nblock -->",
236 (document (1:1-3:9) [
237 (html_block (1:1-3:9) "<!-- A\nhtml\nblock -->")
238 ])
239 );
240
241 assert_ast_match!(
242 [],
243 "<!-- A\n\nhtml\n\nblock -->",
244 (document (1:1-5:9) [
245 (html_block (1:1-5:9) "<!-- A\n\nhtml\n\nblock -->")
246 ])
247 );
248
249 assert_ast_match!(
250 [],
251 "Test\n\n<!-- A\nhtml\nblock -->\n\nMore text",
252 (document (1:1-7:9) [
253 (paragraph (1:1-1:4) [
254 (text (1:1-1:4) "Test")
255 ])
256 (html_block (3:1-5:9) "<!-- A\nhtml\nblock -->\n")
257 (paragraph (7:1-7:9) [
258 (text (7:1-7:9) "More text")
259 ])
260 ])
261 );
262 }
263
264 #[test]
265 fn sourcepos_kind_3() {
266 assert_ast_match!(
267 [],
268 "<??>",
269 (document (1:1-1:4) [
270 (html_block (1:1-1:4) "<??>")
271 ])
272 );
273
274 assert_ast_match!(
275 [],
276 "<? ?>",
277 (document (1:1-1:5) [
278 (html_block (1:1-1:5) "<? ?>")
279 ])
280 );
281
282 assert_ast_match!(
283 [],
284 "<?html a html block ?>",
285 (document (1:1-1:22) [
286 (html_block (1:1-1:22) "<?html a html block ?>")
287 ])
288 );
289
290 assert_ast_match!(
291 [],
292 "<?html\na html\nblock ?>",
293 (document (1:1-3:8) [
294 (html_block (1:1-3:8) "<?html\na html\nblock ?>")
295 ])
296 );
297
298 assert_ast_match!(
299 [],
300 "<?html\n\na html\n\nblock ?>",
301 (document (1:1-5:8) [
302 (html_block (1:1-5:8) "<?html\n\na html\n\nblock ?>")
303 ])
304 );
305
306 assert_ast_match!(
307 [],
308 "Test\n\n<?html\na html\nblock ?>\n\nMore text",
309 (document (1:1-7:9) [
310 (paragraph (1:1-1:4) [
311 (text (1:1-1:4) "Test")
312 ])
313 (html_block (3:1-5:8) "<?html\na html\nblock ?>\n")
314 (paragraph (7:1-7:9) [
315 (text (7:1-7:9) "More text")
316 ])
317 ])
318 );
319 }
320
321 #[test]
322 fn sourcepos_kind_4() {
323 assert_ast_match!(
324 [],
325 "<!a>",
326 (document (1:1-1:4) [
327 (html_block (1:1-1:4) "<!a>")
328 ])
329 );
330
331 assert_ast_match!(
332 [],
333 "<!A>",
334 (document (1:1-1:4) [
335 (html_block (1:1-1:4) "<!A>")
336 ])
337 );
338
339 assert_ast_match!(
340 [],
341 "<!DOCTYPE html>",
342 (document (1:1-1:15) [
343 (html_block (1:1-1:15) "<!DOCTYPE html>")
344 ])
345 );
346
347 assert_ast_match!(
348 [],
349 "<!DOCTYPE\nhtml>",
350 (document (1:1-2:5) [
351 (html_block (1:1-2:5) "<!DOCTYPE\nhtml>")
352 ])
353 );
354
355 assert_ast_match!(
356 [],
357 "<!DOCTYPE\n\nhtml>",
358 (document (1:1-3:5) [
359 (html_block (1:1-3:5) "<!DOCTYPE\n\nhtml>")
360 ])
361 );
362
363 assert_ast_match!(
364 [],
365 "Test\n\n<!DOCTYPE\nhtml>\n\nMore text",
366 (document (1:1-6:9) [
367 (paragraph (1:1-1:4) [
368 (text (1:1-1:4) "Test")
369 ])
370 (html_block (3:1-4:5) "<!DOCTYPE\nhtml>\n")
371 (paragraph (6:1-6:9) [
372 (text (6:1-6:9) "More text")
373 ])
374 ])
375 );
376 }
377
378 #[test]
379 fn sourcepos_kind_5() {
380 assert_ast_match!(
381 [],
382 "<![CDATA[]]>",
383 (document (1:1-1:12) [
384 (html_block (1:1-1:12) "<![CDATA[]]>")
385 ])
386 );
387
388 assert_ast_match!(
389 [],
390 "<![CDATA[A html block]]>",
391 (document (1:1-1:24) [
392 (html_block (1:1-1:24) "<![CDATA[A html block]]>")
393 ])
394 );
395
396 assert_ast_match!(
397 [],
398 "<![CDATA[\nA html\nblock]]>",
399 (document (1:1-3:8) [
400 (html_block (1:1-3:8) "<![CDATA[\nA html\nblock]]>")
401 ])
402 );
403
404 assert_ast_match!(
405 [],
406 "<![CDATA[\n\nA html\n\nblock]]>",
407 (document (1:1-5:8) [
408 (html_block (1:1-5:8) "<![CDATA[\n\nA html\n\nblock]]>")
409 ])
410 );
411
412 assert_ast_match!(
413 [],
414 "Test\n\n<![CDATA[\nA html\nblock]]>\n\nMore text",
415 (document (1:1-7:9) [
416 (paragraph (1:1-1:4) [
417 (text (1:1-1:4) "Test")
418 ])
419 (html_block (3:1-5:8) "<![CDATA[\nA html\nblock]]>\n")
420 (paragraph (7:1-7:9) [
421 (text (7:1-7:9) "More text")
422 ])
423 ])
424 );
425 }
426
427 #[test]
428 fn sourcepos_kind_6() {
429 assert_ast_match!(
430 [],
431 "<div></div>",
432 (document (1:1-1:11) [
433 (html_block (1:1-1:11) "<div></div>")
434 ])
435 );
436
437 assert_ast_match!(
438 [],
439 "<div>A html block</div>",
440 (document (1:1-1:23) [
441 (html_block (1:1-1:23) "<div>A html block</div>")
442 ])
443 );
444
445 assert_ast_match!(
446 [],
447 "<div>A html block\n</div>",
448 (document (1:1-2:6) [
449 (html_block (1:1-2:6) "<div>A html block\n</div>")
450 ])
451 );
452
453 assert_ast_match!(
454 [],
455 "<div>\nA html block</div>",
456 (document (1:1-2:18) [
457 (html_block (1:1-2:18) "<div>\nA html block</div>")
458 ])
459 );
460
461 assert_ast_match!(
462 [],
463 "<div>\nA html block\n</div>",
464 (document (1:1-3:6) [
465 (html_block (1:1-3:6) "<div>\nA html block\n</div>")
466 ])
467 );
468
469 assert_ast_match!(
470 [],
471 "<div>\nA html block\n\n</div>",
472 (document (1:1-4:6) [
473 (html_block (1:1-2:12) "<div>\nA html block\n")
474 (html_block (4:1-4:6) "</div>")
475 ])
476 );
477
478 assert_ast_match!(
479 [],
480 "<div>\n\nA html block\n</div>",
481 (document (1:1-4:6) [
482 (html_block (1:1-1:5) "<div>\n")
483 (paragraph (3:1-3:12) [
484 (text (3:1-3:12) "A html block")
485 ])
486 (html_block (4:1-4:6) "</div>")
487 ])
488 );
489
490 assert_ast_match!(
491 [],
492 "<div>\n\nA html block\n\n</div>",
493 (document (1:1-5:6) [
494 (html_block (1:1-1:5) "<div>\n")
495 (paragraph (3:1-3:12) [
496 (text (3:1-3:12) "A html block")
497 ])
498 (html_block (5:1-5:6) "</div>")
499 ])
500 );
501
502 assert_ast_match!(
503 [],
504 "Test\n<div>\nA html block\n</div>\n\nMore text",
505 (document (1:1-6:9) [
506 (paragraph (1:1-1:4) [
507 (text (1:1-1:4) "Test")
508 ])
509 (html_block (2:1-4:6) "<div>\nA html block\n</div>\n")
510 (paragraph (6:1-6:9) [
511 (text (6:1-6:9) "More text")
512 ])
513 ])
514 );
515
516 assert_ast_match!(
517 [],
518 "Test\n\n<div>\nA html block\n</div>\n\nMore text",
519 (document (1:1-7:9) [
520 (paragraph (1:1-1:4) [
521 (text (1:1-1:4) "Test")
522 ])
523 (html_block (3:1-5:6) "<div>\nA html block\n</div>\n")
524 (paragraph (7:1-7:9) [
525 (text (7:1-7:9) "More text")
526 ])
527 ])
528 );
529
530 assert_ast_match!(
531 [],
532 "Test\n\n<div>\nA html block\n\n</div>\n\nMore text",
533 (document (1:1-8:9) [
534 (paragraph (1:1-1:4) [
535 (text (1:1-1:4) "Test")
536 ])
537 (html_block (3:1-4:12) "<div>\nA html block\n")
538 (html_block (6:1-6:6) "</div>\n")
539 (paragraph (8:1-8:9) [
540 (text (8:1-8:9) "More text")
541 ])
542 ])
543 );
544
545 assert_ast_match!(
546 [],
547 "Test\n\n<div>\n\nA html block\n</div>\n\nMore text",
548 (document (1:1-8:9) [
549 (paragraph (1:1-1:4) [
550 (text (1:1-1:4) "Test")
551 ])
552 (html_block (3:1-3:5) "<div>\n")
553 (paragraph (5:1-5:12) [
554 (text (5:1-5:12) "A html block")
555 ])
556 (html_block (6:1-6:6) "</div>\n")
557 (paragraph (8:1-8:9) [
558 (text (8:1-8:9) "More text")
559 ])
560 ])
561 );
562
563 assert_ast_match!(
564 [],
565 "Test\n\n<div>\n\nA html block\n\n</div>\n\nMore text",
566 (document (1:1-9:9) [
567 (paragraph (1:1-1:4) [
568 (text (1:1-1:4) "Test")
569 ])
570 (html_block (3:1-3:5) "<div>\n")
571 (paragraph (5:1-5:12) [
572 (text (5:1-5:12) "A html block")
573 ])
574 (html_block (7:1-7:6) "</div>\n")
575 (paragraph (9:1-9:9) [
576 (text (9:1-9:9) "More text")
577 ])
578 ])
579 );
580 }
581
582 #[test]
583 fn sourcepos_kind_7() {
584 assert_ast_match!(
585 [],
586 "<testa></testa>",
587 (document (1:1-1:15) [
588 (paragraph (1:1-1:15) [
589 (html_inline (1:1-1:7) "<testa>")
590 (html_inline (1:8-1:15) "</testa>")
591 ])
592 ])
593 );
594
595 assert_ast_match!(
596 [],
597 "<testb>\n</testb>",
598 (document (1:1-2:8) [
599 (html_block (1:1-2:8) "<testb>\n</testb>")
600 ])
601 );
602
603 assert_ast_match!(
604 [],
605 "<testc>\n\n</testc>",
606 (document (1:1-3:8) [
607 (html_block (1:1-1:7) "<testc>\n")
608 (html_block (3:1-3:8) "</testc>")
609 ])
610 );
611
612 assert_ast_match!(
613 [],
614 "<test>\nA html block\n</test>",
615 (document (1:1-3:7) [
616 (html_block (1:1-3:7) "<test>\nA html block\n</test>")
617 ])
618 );
619
620 assert_ast_match!(
621 [],
622 "<test>\nB html block\n\n</test>",
623 (document (1:1-4:7) [
624 (html_block (1:1-2:12) "<test>\nB html block\n")
625 (html_block (4:1-4:7) "</test>")
626 ])
627 );
628
629 assert_ast_match!(
630 [],
631 "<test>\n\nC html block\n</test>",
632 (document (1:1-4:7) [
633 (html_block (1:1-1:6) "<test>\n")
634 (paragraph (3:1-4:7) [
635 (text (3:1-3:12) "C html block")
636 (softbreak (3:13-3:13))
637 (html_inline (4:1-4:7) "</test>")
638 ])
639 ])
640 );
641
642 assert_ast_match!(
643 [],
644 "<test>\n\nD html block\n\n</test>",
645 (document (1:1-5:7) [
646 (html_block (1:1-1:6) "<test>\n")
647 (paragraph (3:1-3:12) [
648 (text (3:1-3:12) "D html block")
649 ])
650 (html_block (5:1-5:7) "</test>")
651 ])
652 );
653
654 assert_ast_match!(
655 [],
656 "Test\n\n<test>\nE html block\n</test>\n\nMore text",
657 (document (1:1-7:9) [
658 (paragraph (1:1-1:4) [
659 (text (1:1-1:4) "Test")
660 ])
661 (html_block (3:1-5:7) "<test>\nE html block\n</test>\n")
662 (paragraph (7:1-7:9) [
663 (text (7:1-7:9) "More text")
664 ])
665 ])
666 );
667
668 assert_ast_match!(
669 [],
670 "Test\n\n<test>\n\nF html block\n\n</test>\n\nMore text",
671 (document (1:1-9:9) [
672 (paragraph (1:1-1:4) [
673 (text (1:1-1:4) "Test")
674 ])
675 (html_block (3:1-3:6) "<test>\n")
676 (paragraph (5:1-5:12) [
677 (text (5:1-5:12) "F html block")
678 ])
679 (html_block (7:1-7:7) "</test>\n")
680 (paragraph (9:1-9:9) [
681 (text (9:1-9:9) "More text")
682 ])
683 ])
684 );
685 }
686
687 #[test]
688 fn sourcepos_inside_blockquote() {
689 // Kind 1
690 assert_ast_match!(
691 [],
692 "> <script></script>",
693 (document (1:1-1:19) [
694 (block_quote (1:1-1:19) [
695 (html_block (1:3-1:19) "<script></script>")
696 ])
697 ])
698 );
699
700 // Kind 2
701 assert_ast_match!(
702 [],
703 "> <!---->",
704 (document (1:1-1:9) [
705 (block_quote (1:1-1:9) [
706 (html_block (1:3-1:9) "<!---->")
707 ])
708 ])
709 );
710
711 // Kind 3
712 assert_ast_match!(
713 [],
714 "> <??>",
715 (document (1:1-1:6) [
716 (block_quote (1:1-1:6) [
717 (html_block (1:3-1:6) "<??>")
718 ])
719 ])
720 );
721
722 // Kind 4
723 assert_ast_match!(
724 [],
725 "> <!a>",
726 (document (1:1-1:6) [
727 (block_quote (1:1-1:6) [
728 (html_block (1:3-1:6) "<!a>")
729 ])
730 ])
731 );
732
733 // Kind 5
734 assert_ast_match!(
735 [],
736 "> <![CDATA[]]>",
737 (document (1:1-1:14) [
738 (block_quote (1:1-1:14) [
739 (html_block (1:3-1:14) "<![CDATA[]]>")
740 ])
741 ])
742 );
743
744 // Kind 6
745 assert_ast_match!(
746 [],
747 "> <div></div>",
748 (document (1:1-1:13) [
749 (block_quote (1:1-1:13) [
750 (html_block (1:3-1:13) "<div></div>")
751 ])
752 ])
753 );
754
755 // Kind 7
756 assert_ast_match!(
757 [],
758 "> <testa></testa>",
759 (document (1:1-1:17) [
760 (block_quote (1:1-1:17) [
761 (paragraph (1:3-1:17) [
762 (html_inline (1:3-1:9) "<testa>")
763 (html_inline (1:10-1:17) "</testa>")
764 ])
765 ])
766 ])
767 );
768 }