Debian Codegraph @

tests/test_syntax/extensions/test_tables.py

raw
1 """
2 Python Markdown
3
4 A Python implementation of John Gruber's Markdown.
5
6 Documentation: https://python-markdown.github.io/
7 GitHub: https://github.com/Python-Markdown/markdown/
8 PyPI: https://pypi.org/project/Markdown/
9
10 Started by Manfred Stienstra (http://www.dwerg.net/).
11 Maintained for a few years by Yuri Takhteyev (http://www.freewisdom.org).
12 Currently maintained by Waylan Limberg (https://github.com/waylan),
13 Dmitry Shachnev (https://github.com/mitya57) and Isaac Muse (https://github.com/facelessuser).
14
15 Copyright 2007-2023 The Python Markdown Project (v. 1.7 and later)
16 Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b)
17 Copyright 2004 Manfred Stienstra (the original version)
18
19 License: BSD (see LICENSE.md for details).
20 """
21
22 from markdown.test_tools import TestCase
23 from markdown.extensions.tables import TableExtension
24
25
26 class TestTableBlocks(TestCase):
27
28 def test_empty_cells(self):
29 """Empty cells (`nbsp`)."""
30
31 text = """
32   | Second Header
33 ------------- | -------------
34   | Content Cell
35 Content Cell |  
36 """
37
38 self.assertMarkdownRenders(
39 text,
40 self.dedent(
41 """
42 <table>
43 <thead>
44 <tr>
45 <th> </th>
46 <th>Second Header</th>
47 </tr>
48 </thead>
49 <tbody>
50 <tr>
51 <td> </td>
52 <td>Content Cell</td>
53 </tr>
54 <tr>
55 <td>Content Cell</td>
56 <td> </td>
57 </tr>
58 </tbody>
59 </table>
60 """
61 ),
62 extensions=['tables']
63 )
64
65 def test_no_sides(self):
66 self.assertMarkdownRenders(
67 self.dedent(
68 """
69 First Header | Second Header
70 ------------- | -------------
71 Content Cell | Content Cell
72 Content Cell | Content Cell
73 """
74 ),
75 self.dedent(
76 """
77 <table>
78 <thead>
79 <tr>
80 <th>First Header</th>
81 <th>Second Header</th>
82 </tr>
83 </thead>
84 <tbody>
85 <tr>
86 <td>Content Cell</td>
87 <td>Content Cell</td>
88 </tr>
89 <tr>
90 <td>Content Cell</td>
91 <td>Content Cell</td>
92 </tr>
93 </tbody>
94 </table>
95 """
96 ),
97 extensions=['tables']
98 )
99
100 def test_both_sides(self):
101 self.assertMarkdownRenders(
102 self.dedent(
103 """
104 | First Header | Second Header |
105 | ------------- | ------------- |
106 | Content Cell | Content Cell |
107 | Content Cell | Content Cell |
108 """
109 ),
110 self.dedent(
111 """
112 <table>
113 <thead>
114 <tr>
115 <th>First Header</th>
116 <th>Second Header</th>
117 </tr>
118 </thead>
119 <tbody>
120 <tr>
121 <td>Content Cell</td>
122 <td>Content Cell</td>
123 </tr>
124 <tr>
125 <td>Content Cell</td>
126 <td>Content Cell</td>
127 </tr>
128 </tbody>
129 </table>
130 """
131 ),
132 extensions=['tables']
133 )
134
135 def test_align_columns(self):
136 self.assertMarkdownRenders(
137 self.dedent(
138 """
139 | Item | Value |
140 | :-------- | -----:|
141 | Computer | $1600 |
142 | Phone | $12 |
143 | Pipe | $1 |
144 """
145 ),
146 self.dedent(
147 """
148 <table>
149 <thead>
150 <tr>
151 <th style="text-align: left;">Item</th>
152 <th style="text-align: right;">Value</th>
153 </tr>
154 </thead>
155 <tbody>
156 <tr>
157 <td style="text-align: left;">Computer</td>
158 <td style="text-align: right;">$1600</td>
159 </tr>
160 <tr>
161 <td style="text-align: left;">Phone</td>
162 <td style="text-align: right;">$12</td>
163 </tr>
164 <tr>
165 <td style="text-align: left;">Pipe</td>
166 <td style="text-align: right;">$1</td>
167 </tr>
168 </tbody>
169 </table>
170 """
171 ),
172 extensions=['tables']
173 )
174
175 def test_styles_in_tables(self):
176 self.assertMarkdownRenders(
177 self.dedent(
178 """
179 | Function name | Description |
180 | ------------- | ------------------------------ |
181 | `help()` | Display the help window. |
182 | `destroy()` | **Destroy your computer!** |
183 """
184 ),
185 self.dedent(
186 """
187 <table>
188 <thead>
189 <tr>
190 <th>Function name</th>
191 <th>Description</th>
192 </tr>
193 </thead>
194 <tbody>
195 <tr>
196 <td><code>help()</code></td>
197 <td>Display the help window.</td>
198 </tr>
199 <tr>
200 <td><code>destroy()</code></td>
201 <td><strong>Destroy your computer!</strong></td>
202 </tr>
203 </tbody>
204 </table>
205 """
206 ),
207 extensions=['tables']
208 )
209
210 def test_align_three(self):
211 self.assertMarkdownRenders(
212 self.dedent(
213 """
214 |foo|bar|baz|
215 |:--|:-:|--:|
216 | | Q | |
217 |W | | W|
218 """
219 ),
220 self.dedent(
221 """
222 <table>
223 <thead>
224 <tr>
225 <th style="text-align: left;">foo</th>
226 <th style="text-align: center;">bar</th>
227 <th style="text-align: right;">baz</th>
228 </tr>
229 </thead>
230 <tbody>
231 <tr>
232 <td style="text-align: left;"></td>
233 <td style="text-align: center;">Q</td>
234 <td style="text-align: right;"></td>
235 </tr>
236 <tr>
237 <td style="text-align: left;">W</td>
238 <td style="text-align: center;"></td>
239 <td style="text-align: right;">W</td>
240 </tr>
241 </tbody>
242 </table>
243 """
244 ),
245 extensions=['tables']
246 )
247
248 def test_three_columns(self):
249 self.assertMarkdownRenders(
250 self.dedent(
251 """
252 foo|bar|baz
253 ---|---|---
254 | Q |
255 W | | W
256 """
257 ),
258 self.dedent(
259 """
260 <table>
261 <thead>
262 <tr>
263 <th>foo</th>
264 <th>bar</th>
265 <th>baz</th>
266 </tr>
267 </thead>
268 <tbody>
269 <tr>
270 <td></td>
271 <td>Q</td>
272 <td></td>
273 </tr>
274 <tr>
275 <td>W</td>
276 <td></td>
277 <td>W</td>
278 </tr>
279 </tbody>
280 </table>
281 """
282 ),
283 extensions=['tables']
284 )
285
286 def test_three_spaces_prefix(self):
287 self.assertMarkdownRenders(
288 self.dedent(
289 """
290 Three spaces in front of a table:
291
292 First Header | Second Header
293 ------------ | -------------
294 Content Cell | Content Cell
295 Content Cell | Content Cell
296
297 | First Header | Second Header |
298 | ------------ | ------------- |
299 | Content Cell | Content Cell |
300 | Content Cell | Content Cell |
301 """),
302 self.dedent(
303 """
304 <p>Three spaces in front of a table:</p>
305 <table>
306 <thead>
307 <tr>
308 <th>First Header</th>
309 <th>Second Header</th>
310 </tr>
311 </thead>
312 <tbody>
313 <tr>
314 <td>Content Cell</td>
315 <td>Content Cell</td>
316 </tr>
317 <tr>
318 <td>Content Cell</td>
319 <td>Content Cell</td>
320 </tr>
321 </tbody>
322 </table>
323 <table>
324 <thead>
325 <tr>
326 <th>First Header</th>
327 <th>Second Header</th>
328 </tr>
329 </thead>
330 <tbody>
331 <tr>
332 <td>Content Cell</td>
333 <td>Content Cell</td>
334 </tr>
335 <tr>
336 <td>Content Cell</td>
337 <td>Content Cell</td>
338 </tr>
339 </tbody>
340 </table>
341 """
342 ),
343 extensions=['tables']
344 )
345
346 def test_code_block_table(self):
347 self.assertMarkdownRenders(
348 self.dedent(
349 """
350 Four spaces is a code block:
351
352 First Header | Second Header
353 ------------ | -------------
354 Content Cell | Content Cell
355 Content Cell | Content Cell
356
357 | First Header | Second Header |
358 | ------------ | ------------- |
359 """),
360 self.dedent(
361 """
362 <p>Four spaces is a code block:</p>
363 <pre><code>First Header | Second Header
364 ------------ | -------------
365 Content Cell | Content Cell
366 Content Cell | Content Cell
367 </code></pre>
368 <table>
369 <thead>
370 <tr>
371 <th>First Header</th>
372 <th>Second Header</th>
373 </tr>
374 </thead>
375 <tbody>
376 <tr>
377 <td></td>
378 <td></td>
379 </tr>
380 </tbody>
381 </table>
382 """
383 ),
384 extensions=['tables']
385 )
386
387 def test_inline_code_blocks(self):
388 self.assertMarkdownRenders(
389 self.dedent(
390 """
391 More inline code block tests
392
393 Column 1 | Column 2 | Column 3
394 ---------|----------|---------
395 word 1 | word 2 | word 3
396 word 1 | `word 2` | word 3
397 word 1 | \\`word 2 | word 3
398 word 1 | `word 2 | word 3
399 word 1 | `word |2` | word 3
400 words |`` some | code `` | more words
401 words |``` some | code ``` | more words
402 words |```` some | code ```` | more words
403 words |`` some ` | ` code `` | more words
404 words |``` some ` | ` code ``` | more words
405 words |```` some ` | ` code ```` | more words
406 """),
407 self.dedent(
408 """
409 <p>More inline code block tests</p>
410 <table>
411 <thead>
412 <tr>
413 <th>Column 1</th>
414 <th>Column 2</th>
415 <th>Column 3</th>
416 </tr>
417 </thead>
418 <tbody>
419 <tr>
420 <td>word 1</td>
421 <td>word 2</td>
422 <td>word 3</td>
423 </tr>
424 <tr>
425 <td>word 1</td>
426 <td><code>word 2</code></td>
427 <td>word 3</td>
428 </tr>
429 <tr>
430 <td>word 1</td>
431 <td>`word 2</td>
432 <td>word 3</td>
433 </tr>
434 <tr>
435 <td>word 1</td>
436 <td>`word 2</td>
437 <td>word 3</td>
438 </tr>
439 <tr>
440 <td>word 1</td>
441 <td><code>word |2</code></td>
442 <td>word 3</td>
443 </tr>
444 <tr>
445 <td>words</td>
446 <td><code>some | code</code></td>
447 <td>more words</td>
448 </tr>
449 <tr>
450 <td>words</td>
451 <td><code>some | code</code></td>
452 <td>more words</td>
453 </tr>
454 <tr>
455 <td>words</td>
456 <td><code>some | code</code></td>
457 <td>more words</td>
458 </tr>
459 <tr>
460 <td>words</td>
461 <td><code>some ` | ` code</code></td>
462 <td>more words</td>
463 </tr>
464 <tr>
465 <td>words</td>
466 <td><code>some ` | ` code</code></td>
467 <td>more words</td>
468 </tr>
469 <tr>
470 <td>words</td>
471 <td><code>some ` | ` code</code></td>
472 <td>more words</td>
473 </tr>
474 </tbody>
475 </table>
476 """
477 ),
478 extensions=['tables']
479 )
480
481 def test_issue_440(self):
482 self.assertMarkdownRenders(
483 self.dedent(
484 """
485 A test for issue #440:
486
487 foo | bar
488 --- | ---
489 foo | (`bar`) and `baz`.
490 """),
491 self.dedent(
492 """
493 <p>A test for issue #440:</p>
494 <table>
495 <thead>
496 <tr>
497 <th>foo</th>
498 <th>bar</th>
499 </tr>
500 </thead>
501 <tbody>
502 <tr>
503 <td>foo</td>
504 <td>(<code>bar</code>) and <code>baz</code>.</td>
505 </tr>
506 </tbody>
507 </table>
508 """
509 ),
510 extensions=['tables']
511 )
512
513 def test_lists_not_tables(self):
514 self.assertMarkdownRenders(
515 self.dedent(
516 """
517 Lists are not tables
518
519 - this | should | not
520 - be | a | table
521 """),
522 self.dedent(
523 """
524 <p>Lists are not tables</p>
525 <ul>
526 <li>this | should | not</li>
527 <li>be | a | table</li>
528 </ul>
529 """
530 ),
531 extensions=['tables']
532 )
533
534 def test_issue_449(self):
535 self.assertMarkdownRenders(
536 self.dedent(
537 r"""
538 Add tests for issue #449
539
540 Odd backticks | Even backticks
541 ------------ | -------------
542 ``[!\"\#$%&'()*+,\-./:;<=>?@\[\\\]^_`{|}~]`` | ``[!\"\#$%&'()*+,\-./:;<=>?@\[\\\]^`_`{|}~]``
543
544 Escapes | More Escapes
545 ------- | ------
546 `` `\`` | `\`
547
548 Only the first backtick can be escaped
549
550 Escaped | Bacticks
551 ------- | ------
552 \`` \` | \`\`
553
554 Test escaped pipes
555
556 Column 1 | Column 2
557 -------- | --------
558 `|` \| | Pipes are okay in code and escaped. \|
559
560 | Column 1 | Column 2 |
561 | -------- | -------- |
562 | row1 | row1 \|
563 | row2 | row2 |
564
565 Test header escapes
566
567 | `` `\`` \| | `\` \|
568 | ---------- | ---- |
569 | row1 | row1 |
570 | row2 | row2 |
571
572 Escaped pipes in format row should not be a table
573
574 | Column1 | Column2 |
575 | ------- \|| ------- |
576 | row1 | row1 |
577 | row2 | row2 |
578
579 Test escaped code in Table
580
581 Should not be code | Should be code
582 ------------------ | --------------
583 \`Not code\` | \\`code`
584 \\\`Not code\\\` | \\\\`code`
585 """),
586 self.dedent(
587 """
588 <p>Add tests for issue #449</p>
589 <table>
590 <thead>
591 <tr>
592 <th>Odd backticks</th>
593 <th>Even backticks</th>
594 </tr>
595 </thead>
596 <tbody>
597 <tr>
598 <td><code>[!\\"\\#$%&amp;'()*+,\\-./:;&lt;=&gt;?@\\[\\\\\\]^_`{|}~]</code></td>
599 <td><code>[!\\"\\#$%&amp;'()*+,\\-./:;&lt;=&gt;?@\\[\\\\\\]^`_`{|}~]</code></td>
600 </tr>
601 </tbody>
602 </table>
603 <table>
604 <thead>
605 <tr>
606 <th>Escapes</th>
607 <th>More Escapes</th>
608 </tr>
609 </thead>
610 <tbody>
611 <tr>
612 <td><code>`\\</code></td>
613 <td><code>\\</code></td>
614 </tr>
615 </tbody>
616 </table>
617 <p>Only the first backtick can be escaped</p>
618 <table>
619 <thead>
620 <tr>
621 <th>Escaped</th>
622 <th>Bacticks</th>
623 </tr>
624 </thead>
625 <tbody>
626 <tr>
627 <td>`<code>\\</code></td>
628 <td>``</td>
629 </tr>
630 </tbody>
631 </table>
632 <p>Test escaped pipes</p>
633 <table>
634 <thead>
635 <tr>
636 <th>Column 1</th>
637 <th>Column 2</th>
638 </tr>
639 </thead>
640 <tbody>
641 <tr>
642 <td><code>|</code> |</td>
643 <td>Pipes are okay in code and escaped. |</td>
644 </tr>
645 </tbody>
646 </table>
647 <table>
648 <thead>
649 <tr>
650 <th>Column 1</th>
651 <th>Column 2</th>
652 </tr>
653 </thead>
654 <tbody>
655 <tr>
656 <td>row1</td>
657 <td>row1 |</td>
658 </tr>
659 <tr>
660 <td>row2</td>
661 <td>row2</td>
662 </tr>
663 </tbody>
664 </table>
665 <p>Test header escapes</p>
666 <table>
667 <thead>
668 <tr>
669 <th><code>`\\</code> |</th>
670 <th><code>\\</code> |</th>
671 </tr>
672 </thead>
673 <tbody>
674 <tr>
675 <td>row1</td>
676 <td>row1</td>
677 </tr>
678 <tr>
679 <td>row2</td>
680 <td>row2</td>
681 </tr>
682 </tbody>
683 </table>
684 <p>Escaped pipes in format row should not be a table</p>
685 <p>| Column1 | Column2 |
686 | ------- || ------- |
687 | row1 | row1 |
688 | row2 | row2 |</p>
689 <p>Test escaped code in Table</p>
690 <table>
691 <thead>
692 <tr>
693 <th>Should not be code</th>
694 <th>Should be code</th>
695 </tr>
696 </thead>
697 <tbody>
698 <tr>
699 <td>`Not code`</td>
700 <td>\\<code>code</code></td>
701 </tr>
702 <tr>
703 <td>\\`Not code\\`</td>
704 <td>\\\\<code>code</code></td>
705 </tr>
706 </tbody>
707 </table>
708 """
709 ),
710 extensions=['tables']
711 )
712
713 def test_single_column_tables(self):
714 self.assertMarkdownRenders(
715 self.dedent(
716 """
717 Single column tables
718
719 | Is a Table |
720 | ---------- |
721
722 | Is a Table
723 | ----------
724
725 Is a Table |
726 ---------- |
727
728 | Is a Table |
729 | ---------- |
730 | row |
731
732 | Is a Table
733 | ----------
734 | row
735
736 Is a Table |
737 ---------- |
738 row |
739
740 | Is not a Table
741 --------------
742 | row
743
744 Is not a Table |
745 --------------
746 row |
747
748 | Is not a Table
749 | --------------
750 row
751
752 Is not a Table |
753 -------------- |
754 row
755 """),
756 self.dedent(
757 """
758 <p>Single column tables</p>
759 <table>
760 <thead>
761 <tr>
762 <th>Is a Table</th>
763 </tr>
764 </thead>
765 <tbody>
766 <tr>
767 <td></td>
768 </tr>
769 </tbody>
770 </table>
771 <table>
772 <thead>
773 <tr>
774 <th>Is a Table</th>
775 </tr>
776 </thead>
777 <tbody>
778 <tr>
779 <td></td>
780 </tr>
781 </tbody>
782 </table>
783 <table>
784 <thead>
785 <tr>
786 <th>Is a Table</th>
787 </tr>
788 </thead>
789 <tbody>
790 <tr>
791 <td></td>
792 </tr>
793 </tbody>
794 </table>
795 <table>
796 <thead>
797 <tr>
798 <th>Is a Table</th>
799 </tr>
800 </thead>
801 <tbody>
802 <tr>
803 <td>row</td>
804 </tr>
805 </tbody>
806 </table>
807 <table>
808 <thead>
809 <tr>
810 <th>Is a Table</th>
811 </tr>
812 </thead>
813 <tbody>
814 <tr>
815 <td>row</td>
816 </tr>
817 </tbody>
818 </table>
819 <table>
820 <thead>
821 <tr>
822 <th>Is a Table</th>
823 </tr>
824 </thead>
825 <tbody>
826 <tr>
827 <td>row</td>
828 </tr>
829 </tbody>
830 </table>
831 <h2>| Is not a Table</h2>
832 <p>| row</p>
833 <h2>Is not a Table |</h2>
834 <p>row |</p>
835 <p>| Is not a Table
836 | --------------
837 row</p>
838 <p>Is not a Table |
839 -------------- |
840 row</p>
841 """
842 ),
843 extensions=['tables']
844 )
845
846 def test_align_columns_legacy(self):
847 self.assertMarkdownRenders(
848 self.dedent(
849 """
850 | Item | Value |
851 | :-------- | -----:|
852 | Computer | $1600 |
853 | Phone | $12 |
854 | Pipe | $1 |
855 """
856 ),
857 self.dedent(
858 """
859 <table>
860 <thead>
861 <tr>
862 <th align="left">Item</th>
863 <th align="right">Value</th>
864 </tr>
865 </thead>
866 <tbody>
867 <tr>
868 <td align="left">Computer</td>
869 <td align="right">$1600</td>
870 </tr>
871 <tr>
872 <td align="left">Phone</td>
873 <td align="right">$12</td>
874 </tr>
875 <tr>
876 <td align="left">Pipe</td>
877 <td align="right">$1</td>
878 </tr>
879 </tbody>
880 </table>
881 """
882 ),
883 extensions=[TableExtension(use_align_attribute=True)]
884 )
885
886 def test_align_three_legacy(self):
887 self.assertMarkdownRenders(
888 self.dedent(
889 """
890 |foo|bar|baz|
891 |:--|:-:|--:|
892 | | Q | |
893 |W | | W|
894 """
895 ),
896 self.dedent(
897 """
898 <table>
899 <thead>
900 <tr>
901 <th align="left">foo</th>
902 <th align="center">bar</th>
903 <th align="right">baz</th>
904 </tr>
905 </thead>
906 <tbody>
907 <tr>
908 <td align="left"></td>
909 <td align="center">Q</td>
910 <td align="right"></td>
911 </tr>
912 <tr>
913 <td align="left">W</td>
914 <td align="center"></td>
915 <td align="right">W</td>
916 </tr>
917 </tbody>
918 </table>
919 """
920 ),
921 extensions=[TableExtension(use_align_attribute=True)]
922 )