Debian Codegraph @

tests/test_syntax/blocks/test_html_blocks.py

raw
1 # -*- coding: utf-8 -*-
2 """
3 Python Markdown
4
5 A Python implementation of John Gruber's Markdown.
6
7 Documentation: https://python-markdown.github.io/
8 GitHub: https://github.com/Python-Markdown/markdown/
9 PyPI: https://pypi.org/project/Markdown/
10
11 Started by Manfred Stienstra (http://www.dwerg.net/).
12 Maintained for a few years by Yuri Takhteyev (http://www.freewisdom.org).
13 Currently maintained by Waylan Limberg (https://github.com/waylan),
14 Dmitry Shachnev (https://github.com/mitya57) and Isaac Muse (https://github.com/facelessuser).
15
16 Copyright 2007-2023 The Python Markdown Project (v. 1.7 and later)
17 Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b)
18 Copyright 2004 Manfred Stienstra (the original version)
19
20 License: BSD (see LICENSE.md for details).
21 """
22
23 from markdown.test_tools import TestCase
24 import markdown
25
26
27 class TestHTMLBlocks(TestCase):
28
29 def test_raw_paragraph(self):
30 self.assertMarkdownRenders(
31 '<p>A raw paragraph.</p>',
32 '<p>A raw paragraph.</p>'
33 )
34
35 def test_raw_skip_inline_markdown(self):
36 self.assertMarkdownRenders(
37 '<p>A *raw* paragraph.</p>',
38 '<p>A *raw* paragraph.</p>'
39 )
40
41 def test_raw_indent_one_space(self):
42 self.assertMarkdownRenders(
43 ' <p>A *raw* paragraph.</p>',
44 '<p>A *raw* paragraph.</p>'
45 )
46
47 def test_raw_indent_two_spaces(self):
48 self.assertMarkdownRenders(
49 ' <p>A *raw* paragraph.</p>',
50 '<p>A *raw* paragraph.</p>'
51 )
52
53 def test_raw_indent_three_spaces(self):
54 self.assertMarkdownRenders(
55 ' <p>A *raw* paragraph.</p>',
56 '<p>A *raw* paragraph.</p>'
57 )
58
59 def test_raw_indent_four_spaces(self):
60 self.assertMarkdownRenders(
61 ' <p>code block</p>',
62 self.dedent(
63 """
64 <pre><code>&lt;p&gt;code block&lt;/p&gt;
65 </code></pre>
66 """
67 )
68 )
69
70 def test_raw_span(self):
71 self.assertMarkdownRenders(
72 '<span>*inline*</span>',
73 '<p><span><em>inline</em></span></p>'
74 )
75
76 def test_code_span(self):
77 self.assertMarkdownRenders(
78 '`<p>code span</p>`',
79 '<p><code>&lt;p&gt;code span&lt;/p&gt;</code></p>'
80 )
81
82 def test_code_span_open_gt(self):
83 self.assertMarkdownRenders(
84 '*bar* `<` *foo*',
85 '<p><em>bar</em> <code>&lt;</code> <em>foo</em></p>'
86 )
87
88 def test_raw_empty(self):
89 self.assertMarkdownRenders(
90 '<p></p>',
91 '<p></p>'
92 )
93
94 def test_raw_empty_space(self):
95 self.assertMarkdownRenders(
96 '<p> </p>',
97 '<p> </p>'
98 )
99
100 def test_raw_empty_newline(self):
101 self.assertMarkdownRenders(
102 '<p>\n</p>',
103 '<p>\n</p>'
104 )
105
106 def test_raw_empty_blank_line(self):
107 self.assertMarkdownRenders(
108 '<p>\n\n</p>',
109 '<p>\n\n</p>'
110 )
111
112 def test_raw_uppercase(self):
113 self.assertMarkdownRenders(
114 '<DIV>*foo*</DIV>',
115 '<DIV>*foo*</DIV>'
116 )
117
118 def test_raw_uppercase_multiline(self):
119 self.assertMarkdownRenders(
120 self.dedent(
121 """
122 <DIV>
123 *foo*
124 </DIV>
125 """
126 ),
127 self.dedent(
128 """
129 <DIV>
130 *foo*
131 </DIV>
132 """
133 )
134 )
135
136 def test_multiple_raw_single_line(self):
137 self.assertMarkdownRenders(
138 '<p>*foo*</p><div>*bar*</div>',
139 self.dedent(
140 """
141 <p>*foo*</p>
142 <div>*bar*</div>
143 """
144 )
145 )
146
147 def test_multiple_raw_single_line_with_pi(self):
148 self.assertMarkdownRenders(
149 "<p>*foo*</p><?php echo '>'; ?>",
150 self.dedent(
151 """
152 <p>*foo*</p>
153 <?php echo '>'; ?>
154 """
155 )
156 )
157
158 def test_multiline_raw(self):
159 self.assertMarkdownRenders(
160 self.dedent(
161 """
162 <p>
163 A raw paragraph
164 with multiple lines.
165 </p>
166 """
167 ),
168 self.dedent(
169 """
170 <p>
171 A raw paragraph
172 with multiple lines.
173 </p>
174 """
175 )
176 )
177
178 def test_blank_lines_in_raw(self):
179 self.assertMarkdownRenders(
180 self.dedent(
181 """
182 <p>
183
184 A raw paragraph...
185
186 with many blank lines.
187
188 </p>
189 """
190 ),
191 self.dedent(
192 """
193 <p>
194
195 A raw paragraph...
196
197 with many blank lines.
198
199 </p>
200 """
201 )
202 )
203
204 def test_raw_surrounded_by_Markdown(self):
205 self.assertMarkdownRenders(
206 self.dedent(
207 """
208 Some *Markdown* text.
209
210 <p>*Raw* HTML.</p>
211
212 More *Markdown* text.
213 """
214 ),
215 self.dedent(
216 """
217 <p>Some <em>Markdown</em> text.</p>
218 <p>*Raw* HTML.</p>
219
220 <p>More <em>Markdown</em> text.</p>
221 """
222 )
223 )
224
225 def test_raw_surrounded_by_text_without_blank_lines(self):
226 self.assertMarkdownRenders(
227 self.dedent(
228 """
229 Some *Markdown* text.
230 <p>*Raw* HTML.</p>
231 More *Markdown* text.
232 """
233 ),
234 self.dedent(
235 """
236 <p>Some <em>Markdown</em> text.</p>
237 <p>*Raw* HTML.</p>
238 <p>More <em>Markdown</em> text.</p>
239 """
240 )
241 )
242
243 def test_multiline_markdown_with_code_span(self):
244 self.assertMarkdownRenders(
245 self.dedent(
246 """
247 A paragraph with a block-level
248 `<p>code span</p>`, which is
249 at the start of a line.
250 """
251 ),
252 self.dedent(
253 """
254 <p>A paragraph with a block-level
255 <code>&lt;p&gt;code span&lt;/p&gt;</code>, which is
256 at the start of a line.</p>
257 """
258 )
259 )
260
261 def test_raw_block_preceded_by_markdown_code_span_with_unclosed_block_tag(self):
262 self.assertMarkdownRenders(
263 self.dedent(
264 """
265 A paragraph with a block-level code span: `<div>`.
266
267 <p>*not markdown*</p>
268
269 This is *markdown*
270 """
271 ),
272 self.dedent(
273 """
274 <p>A paragraph with a block-level code span: <code>&lt;div&gt;</code>.</p>
275 <p>*not markdown*</p>
276
277 <p>This is <em>markdown</em></p>
278 """
279 )
280 )
281
282 def test_raw_one_line_followed_by_text(self):
283 self.assertMarkdownRenders(
284 '<p>*foo*</p>*bar*',
285 self.dedent(
286 """
287 <p>*foo*</p>
288 <p><em>bar</em></p>
289 """
290 )
291 )
292
293 def test_raw_one_line_followed_by_span(self):
294 self.assertMarkdownRenders(
295 "<p>*foo*</p><span>*bar*</span>",
296 self.dedent(
297 """
298 <p>*foo*</p>
299 <p><span><em>bar</em></span></p>
300 """
301 )
302 )
303
304 def test_raw_with_markdown_blocks(self):
305 self.assertMarkdownRenders(
306 self.dedent(
307 """
308 <div>
309 Not a Markdown paragraph.
310
311 * Not a list item.
312 * Another non-list item.
313
314 Another non-Markdown paragraph.
315 </div>
316 """
317 ),
318 self.dedent(
319 """
320 <div>
321 Not a Markdown paragraph.
322
323 * Not a list item.
324 * Another non-list item.
325
326 Another non-Markdown paragraph.
327 </div>
328 """
329 )
330 )
331
332 def test_adjacent_raw_blocks(self):
333 self.assertMarkdownRenders(
334 self.dedent(
335 """
336 <p>A raw paragraph.</p>
337 <p>A second raw paragraph.</p>
338 """
339 ),
340 self.dedent(
341 """
342 <p>A raw paragraph.</p>
343 <p>A second raw paragraph.</p>
344 """
345 )
346 )
347
348 def test_adjacent_raw_blocks_with_blank_lines(self):
349 self.assertMarkdownRenders(
350 self.dedent(
351 """
352 <p>A raw paragraph.</p>
353
354 <p>A second raw paragraph.</p>
355 """
356 ),
357 self.dedent(
358 """
359 <p>A raw paragraph.</p>
360
361 <p>A second raw paragraph.</p>
362 """
363 )
364 )
365
366 def test_nested_raw_one_line(self):
367 self.assertMarkdownRenders(
368 '<div><p>*foo*</p></div>',
369 '<div><p>*foo*</p></div>'
370 )
371
372 def test_nested_raw_block(self):
373 self.assertMarkdownRenders(
374 self.dedent(
375 """
376 <div>
377 <p>A raw paragraph.</p>
378 </div>
379 """
380 ),
381 self.dedent(
382 """
383 <div>
384 <p>A raw paragraph.</p>
385 </div>
386 """
387 )
388 )
389
390 def test_nested_indented_raw_block(self):
391 self.assertMarkdownRenders(
392 self.dedent(
393 """
394 <div>
395 <p>A raw paragraph.</p>
396 </div>
397 """
398 ),
399 self.dedent(
400 """
401 <div>
402 <p>A raw paragraph.</p>
403 </div>
404 """
405 )
406 )
407
408 def test_nested_raw_blocks(self):
409 self.assertMarkdownRenders(
410 self.dedent(
411 """
412 <div>
413 <p>A raw paragraph.</p>
414 <p>A second raw paragraph.</p>
415 </div>
416 """
417 ),
418 self.dedent(
419 """
420 <div>
421 <p>A raw paragraph.</p>
422 <p>A second raw paragraph.</p>
423 </div>
424 """
425 )
426 )
427
428 def test_nested_raw_blocks_with_blank_lines(self):
429 self.assertMarkdownRenders(
430 self.dedent(
431 """
432 <div>
433
434 <p>A raw paragraph.</p>
435
436 <p>A second raw paragraph.</p>
437
438 </div>
439 """
440 ),
441 self.dedent(
442 """
443 <div>
444
445 <p>A raw paragraph.</p>
446
447 <p>A second raw paragraph.</p>
448
449 </div>
450 """
451 )
452 )
453
454 def test_nested_inline_one_line(self):
455 self.assertMarkdownRenders(
456 '<p><em>foo</em><br></p>',
457 '<p><em>foo</em><br></p>'
458 )
459
460 def test_raw_nested_inline(self):
461 self.assertMarkdownRenders(
462 self.dedent(
463 """
464 <div>
465 <p>
466 <span>*text*</span>
467 </p>
468 </div>
469 """
470 ),
471 self.dedent(
472 """
473 <div>
474 <p>
475 <span>*text*</span>
476 </p>
477 </div>
478 """
479 )
480 )
481
482 def test_raw_nested_inline_with_blank_lines(self):
483 self.assertMarkdownRenders(
484 self.dedent(
485 """
486 <div>
487
488 <p>
489
490 <span>*text*</span>
491
492 </p>
493
494 </div>
495 """
496 ),
497 self.dedent(
498 """
499 <div>
500
501 <p>
502
503 <span>*text*</span>
504
505 </p>
506
507 </div>
508 """
509 )
510 )
511
512 def test_raw_html5(self):
513 self.assertMarkdownRenders(
514 self.dedent(
515 """
516 <section>
517 <header>
518 <hgroup>
519 <h1>Hello :-)</h1>
520 </hgroup>
521 </header>
522 <figure>
523 <img src="image.png" alt="" />
524 <figcaption>Caption</figcaption>
525 </figure>
526 <footer>
527 <p>Some footer</p>
528 </footer>
529 </section>
530 """
531 ),
532 self.dedent(
533 """
534 <section>
535 <header>
536 <hgroup>
537 <h1>Hello :-)</h1>
538 </hgroup>
539 </header>
540 <figure>
541 <img src="image.png" alt="" />
542 <figcaption>Caption</figcaption>
543 </figure>
544 <footer>
545 <p>Some footer</p>
546 </footer>
547 </section>
548 """
549 )
550 )
551
552 def test_raw_pre_tag(self):
553 self.assertMarkdownRenders(
554 self.dedent(
555 """
556 Preserve whitespace in raw html
557
558 <pre>
559 class Foo():
560 bar = 'bar'
561
562 @property
563 def baz(self):
564 return self.bar
565 </pre>
566 """
567 ),
568 self.dedent(
569 """
570 <p>Preserve whitespace in raw html</p>
571 <pre>
572 class Foo():
573 bar = 'bar'
574
575 @property
576 def baz(self):
577 return self.bar
578 </pre>
579 """
580 )
581 )
582
583 def test_raw_pre_tag_nested_escaped_html(self):
584 self.assertMarkdownRenders(
585 self.dedent(
586 """
587 <pre>
588 &lt;p&gt;foo&lt;/p&gt;
589 </pre>
590 """
591 ),
592 self.dedent(
593 """
594 <pre>
595 &lt;p&gt;foo&lt;/p&gt;
596 </pre>
597 """
598 )
599 )
600
601 def test_raw_p_no_end_tag(self):
602 self.assertMarkdownRenders(
603 '<p>*text*',
604 '<p>*text*'
605 )
606
607 def test_raw_multiple_p_no_end_tag(self):
608 self.assertMarkdownRenders(
609 self.dedent(
610 """
611 <p>*text*'
612
613 <p>more *text*
614 """
615 ),
616 self.dedent(
617 """
618 <p>*text*'
619
620 <p>more *text*
621 """
622 )
623 )
624
625 def test_raw_p_no_end_tag_followed_by_blank_line(self):
626 self.assertMarkdownRenders(
627 self.dedent(
628 """
629 <p>*raw text*'
630
631 Still part of *raw* text.
632 """
633 ),
634 self.dedent(
635 """
636 <p>*raw text*'
637
638 Still part of *raw* text.
639 """
640 )
641 )
642
643 def test_raw_nested_p_no_end_tag(self):
644 self.assertMarkdownRenders(
645 '<div><p>*text*</div>',
646 '<div><p>*text*</div>'
647 )
648
649 def test_raw_open_bracket_only(self):
650 self.assertMarkdownRenders(
651 '<',
652 '<p>&lt;</p>'
653 )
654
655 def test_raw_open_bracket_followed_by_space(self):
656 self.assertMarkdownRenders(
657 '< foo',
658 '<p>&lt; foo</p>'
659 )
660
661 def test_raw_missing_close_bracket(self):
662 self.assertMarkdownRenders(
663 '<foo',
664 '<p>&lt;foo</p>'
665 )
666
667 def test_raw_unclosed_tag_in_code_span(self):
668 self.assertMarkdownRenders(
669 self.dedent(
670 """
671 `<div`.
672
673 <div>
674 hello
675 </div>
676 """
677 ),
678 self.dedent(
679 """
680 <p><code>&lt;div</code>.</p>
681 <div>
682 hello
683 </div>
684 """
685 )
686 )
687
688 def test_raw_unclosed_tag_in_code_span_space(self):
689 self.assertMarkdownRenders(
690 self.dedent(
691 """
692 ` <div `.
693
694 <div>
695 hello
696 </div>
697 """
698 ),
699 self.dedent(
700 """
701 <p><code>&lt;div</code>.</p>
702 <div>
703 hello
704 </div>
705 """
706 )
707 )
708
709 def test_raw_attributes(self):
710 self.assertMarkdownRenders(
711 '<p id="foo", class="bar baz", style="margin: 15px; line-height: 1.5; text-align: center;">text</p>',
712 '<p id="foo", class="bar baz", style="margin: 15px; line-height: 1.5; text-align: center;">text</p>'
713 )
714
715 def test_raw_attributes_nested(self):
716 self.assertMarkdownRenders(
717 self.dedent(
718 """
719 <div id="foo, class="bar", style="background: #ffe7e8; border: 2px solid #e66465;">
720 <p id="baz", style="margin: 15px; line-height: 1.5; text-align: center;">
721 <img scr="../foo.jpg" title="with 'quoted' text." valueless_attr weirdness="<i>foo</i>" />
722 </p>
723 </div>
724 """
725 ),
726 self.dedent(
727 """
728 <div id="foo, class="bar", style="background: #ffe7e8; border: 2px solid #e66465;">
729 <p id="baz", style="margin: 15px; line-height: 1.5; text-align: center;">
730 <img scr="../foo.jpg" title="with 'quoted' text." valueless_attr weirdness="<i>foo</i>" />
731 </p>
732 </div>
733 """
734 )
735 )
736
737 def test_raw_comment_one_line(self):
738 self.assertMarkdownRenders(
739 '<!-- *foo* -->',
740 '<!-- *foo* -->'
741 )
742
743 def test_raw_comment_one_line_with_tag(self):
744 self.assertMarkdownRenders(
745 '<!-- <tag> -->',
746 '<!-- <tag> -->'
747 )
748
749 def test_comment_in_code_span(self):
750 self.assertMarkdownRenders(
751 '`<!-- *foo* -->`',
752 '<p><code>&lt;!-- *foo* --&gt;</code></p>'
753 )
754
755 def test_raw_comment_one_line_followed_by_text(self):
756 self.assertMarkdownRenders(
757 '<!-- *foo* -->*bar*',
758 self.dedent(
759 """
760 <!-- *foo* -->
761 <p><em>bar</em></p>
762 """
763 )
764 )
765
766 def test_raw_comment_one_line_followed_by_html(self):
767 self.assertMarkdownRenders(
768 '<!-- *foo* --><p>*bar*</p>',
769 self.dedent(
770 """
771 <!-- *foo* -->
772 <p>*bar*</p>
773 """
774 )
775 )
776
777 # Note: Trailing (insignificant) whitespace is not preserved, which does not match the
778 # reference implementation. However, it is not a change in behavior for Python-Markdown.
779 def test_raw_comment_trailing_whitespace(self):
780 self.assertMarkdownRenders(
781 '<!-- *foo* --> ',
782 '<!-- *foo* -->'
783 )
784
785 def test_bogus_comment(self):
786 self.assertMarkdownRenders(
787 '<!invalid>',
788 '<p>&lt;!invalid&gt;</p>'
789 )
790
791 def test_bogus_comment_endtag(self):
792 self.assertMarkdownRenders(
793 '</#invalid>',
794 '<p>&lt;/#invalid&gt;</p>'
795 )
796
797 def test_raw_multiline_comment(self):
798 self.assertMarkdownRenders(
799 self.dedent(
800 """
801 <!--
802 *foo*
803 -->
804 """
805 ),
806 self.dedent(
807 """
808 <!--
809 *foo*
810 -->
811 """
812 )
813 )
814
815 def test_raw_multiline_comment_with_tag(self):
816 self.assertMarkdownRenders(
817 self.dedent(
818 """
819 <!--
820 <tag>
821 -->
822 """
823 ),
824 self.dedent(
825 """
826 <!--
827 <tag>
828 -->
829 """
830 )
831 )
832
833 def test_raw_multiline_comment_first_line(self):
834 self.assertMarkdownRenders(
835 self.dedent(
836 """
837 <!-- *foo*
838 -->
839 """
840 ),
841 self.dedent(
842 """
843 <!-- *foo*
844 -->
845 """
846 )
847 )
848
849 def test_raw_multiline_comment_last_line(self):
850 self.assertMarkdownRenders(
851 self.dedent(
852 """
853 <!--
854 *foo* -->
855 """
856 ),
857 self.dedent(
858 """
859 <!--
860 *foo* -->
861 """
862 )
863 )
864
865 def test_raw_comment_with_blank_lines(self):
866 self.assertMarkdownRenders(
867 self.dedent(
868 """
869 <!--
870
871 *foo*
872
873 -->
874 """
875 ),
876 self.dedent(
877 """
878 <!--
879
880 *foo*
881
882 -->
883 """
884 )
885 )
886
887 def test_raw_comment_with_blank_lines_with_tag(self):
888 self.assertMarkdownRenders(
889 self.dedent(
890 """
891 <!--
892
893 <tag>
894
895 -->
896 """
897 ),
898 self.dedent(
899 """
900 <!--
901
902 <tag>
903
904 -->
905 """
906 )
907 )
908
909 def test_raw_comment_with_blank_lines_first_line(self):
910 self.assertMarkdownRenders(
911 self.dedent(
912 """
913 <!-- *foo*
914
915 -->
916 """
917 ),
918 self.dedent(
919 """
920 <!-- *foo*
921
922 -->
923 """
924 )
925 )
926
927 def test_raw_comment_with_blank_lines_last_line(self):
928 self.assertMarkdownRenders(
929 self.dedent(
930 """
931 <!--
932
933 *foo* -->
934 """
935 ),
936 self.dedent(
937 """
938 <!--
939
940 *foo* -->
941 """
942 )
943 )
944
945 def test_raw_comment_indented(self):
946 self.assertMarkdownRenders(
947 self.dedent(
948 """
949 <!--
950
951 *foo*
952
953 -->
954 """
955 ),
956 self.dedent(
957 """
958 <!--
959
960 *foo*
961
962 -->
963 """
964 )
965 )
966
967 def test_raw_comment_indented_with_tag(self):
968 self.assertMarkdownRenders(
969 self.dedent(
970 """
971 <!--
972
973 <tag>
974
975 -->
976 """
977 ),
978 self.dedent(
979 """
980 <!--
981
982 <tag>
983
984 -->
985 """
986 )
987 )
988
989 def test_raw_comment_nested(self):
990 self.assertMarkdownRenders(
991 self.dedent(
992 """
993 <div>
994 <!-- *foo* -->
995 </div>
996 """
997 ),
998 self.dedent(
999 """
1000 <div>
1001 <!-- *foo* -->
1002 </div>
1003 """
1004 )
1005 )
1006
1007 def test_comment_in_code_block(self):
1008 self.assertMarkdownRenders(
1009 ' <!-- *foo* -->',
1010 self.dedent(
1011 """
1012 <pre><code>&lt;!-- *foo* --&gt;
1013 </code></pre>
1014 """
1015 )
1016 )
1017
1018 # Note: This is a change in behavior. Previously, Python-Markdown interpreted this in the same manner
1019 # as browsers and all text after the opening comment tag was considered to be in a comment. However,
1020 # that did not match the reference implementation. The new behavior does.
1021 def test_unclosed_comment(self):
1022 self.assertMarkdownRenders(
1023 self.dedent(
1024 """
1025 <!-- unclosed comment
1026
1027 *not* a comment
1028 """
1029 ),
1030 self.dedent(
1031 """
1032 <p>&lt;!-- unclosed comment</p>
1033 <p><em>not</em> a comment</p>
1034 """
1035 )
1036 )
1037
1038 def test_invalid_comment_end(self):
1039 self.assertMarkdownRenders(
1040 self.dedent(
1041 """
1042 <!-- This comment is malformed and never closes -- >
1043 Some content after the bad comment.
1044 """
1045 ),
1046 self.dedent(
1047 """
1048 <p>&lt;!-- This comment is malformed and never closes -- &gt;
1049 Some content after the bad comment.</p>
1050 """
1051 )
1052 )
1053
1054 def test_raw_processing_instruction_one_line(self):
1055 self.assertMarkdownRenders(
1056 "<?php echo '>'; ?>",
1057 "<?php echo '>'; ?>"
1058 )
1059
1060 # This is a change in behavior and does not match the reference implementation.
1061 # We have no way to determine if text is on the same line, so we get this. TODO: reevaluate!
1062 def test_raw_processing_instruction_one_line_followed_by_text(self):
1063 self.assertMarkdownRenders(
1064 "<?php echo '>'; ?>*bar*",
1065 self.dedent(
1066 """
1067 <?php echo '>'; ?>
1068 <p><em>bar</em></p>
1069 """
1070 )
1071 )
1072
1073 def test_raw_multiline_processing_instruction(self):
1074 self.assertMarkdownRenders(
1075 self.dedent(
1076 """
1077 <?php
1078 echo '>';
1079 ?>
1080 """
1081 ),
1082 self.dedent(
1083 """
1084 <?php
1085 echo '>';
1086 ?>
1087 """
1088 )
1089 )
1090
1091 def test_raw_processing_instruction_with_blank_lines(self):
1092 self.assertMarkdownRenders(
1093 self.dedent(
1094 """
1095 <?php
1096
1097 echo '>';
1098
1099 ?>
1100 """
1101 ),
1102 self.dedent(
1103 """
1104 <?php
1105
1106 echo '>';
1107
1108 ?>
1109 """
1110 )
1111 )
1112
1113 def test_raw_processing_instruction_indented(self):
1114 self.assertMarkdownRenders(
1115 self.dedent(
1116 """
1117 <?php
1118
1119 echo '>';
1120
1121 ?>
1122 """
1123 ),
1124 self.dedent(
1125 """
1126 <?php
1127
1128 echo '>';
1129
1130 ?>
1131 """
1132 )
1133 )
1134
1135 def test_raw_processing_instruction_code_span(self):
1136 self.assertMarkdownRenders(
1137 self.dedent(
1138 """
1139 `<?php`
1140
1141 <div>
1142 foo
1143 </div>
1144 """
1145 ),
1146 self.dedent(
1147 """
1148 <p><code>&lt;?php</code></p>
1149 <div>
1150 foo
1151 </div>
1152 """
1153 )
1154 )
1155
1156 def test_raw_declaration_one_line(self):
1157 self.assertMarkdownRenders(
1158 '<!DOCTYPE html>',
1159 '<!DOCTYPE html>'
1160 )
1161
1162 # This is a change in behavior and does not match the reference implementation.
1163 # We have no way to determine if text is on the same line, so we get this. TODO: reevaluate!
1164 def test_raw_declaration_one_line_followed_by_text(self):
1165 self.assertMarkdownRenders(
1166 '<!DOCTYPE html>*bar*',
1167 self.dedent(
1168 """
1169 <!DOCTYPE html>
1170 <p><em>bar</em></p>
1171 """
1172 )
1173 )
1174
1175 def test_raw_multiline_declaration(self):
1176 self.assertMarkdownRenders(
1177 self.dedent(
1178 """
1179 <!DOCTYPE html PUBLIC
1180 "-//W3C//DTD XHTML 1.1//EN"
1181 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
1182 """
1183 ),
1184 self.dedent(
1185 """
1186 <!DOCTYPE html PUBLIC
1187 "-//W3C//DTD XHTML 1.1//EN"
1188 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
1189 """
1190 )
1191 )
1192
1193 def test_raw_declaration_code_span(self):
1194 self.assertMarkdownRenders(
1195 self.dedent(
1196 """
1197 `<!`
1198
1199 <div>
1200 foo
1201 </div>
1202 """
1203 ),
1204 self.dedent(
1205 """
1206 <p><code>&lt;!</code></p>
1207 <div>
1208 foo
1209 </div>
1210 """
1211 )
1212 )
1213
1214 def test_raw_cdata_one_line(self):
1215 self.assertMarkdownRenders(
1216 '<![CDATA[ document.write(">"); ]]>',
1217 '<![CDATA[ document.write(">"); ]]>'
1218 )
1219
1220 # Note: this is a change. Neither previous output nor this match reference implementation.
1221 def test_raw_cdata_one_line_followed_by_text(self):
1222 self.assertMarkdownRenders(
1223 '<![CDATA[ document.write(">"); ]]>*bar*',
1224 self.dedent(
1225 """
1226 <![CDATA[ document.write(">"); ]]>
1227 <p><em>bar</em></p>
1228 """
1229 )
1230 )
1231
1232 def test_raw_multiline_cdata(self):
1233 self.assertMarkdownRenders(
1234 self.dedent(
1235 """
1236 <![CDATA[
1237 document.write(">");
1238 ]]>
1239 """
1240 ),
1241 self.dedent(
1242 """
1243 <![CDATA[
1244 document.write(">");
1245 ]]>
1246 """
1247 )
1248 )
1249
1250 def test_raw_cdata_with_blank_lines(self):
1251 self.assertMarkdownRenders(
1252 self.dedent(
1253 """
1254 <![CDATA[
1255
1256 document.write(">");
1257
1258 ]]>
1259 """
1260 ),
1261 self.dedent(
1262 """
1263 <![CDATA[
1264
1265 document.write(">");
1266
1267 ]]>
1268 """
1269 )
1270 )
1271
1272 def test_raw_cdata_indented(self):
1273 self.assertMarkdownRenders(
1274 self.dedent(
1275 """
1276 <![CDATA[
1277
1278 document.write(">");
1279
1280 ]]>
1281 """
1282 ),
1283 self.dedent(
1284 """
1285 <![CDATA[
1286
1287 document.write(">");
1288
1289 ]]>
1290 """
1291 )
1292 )
1293
1294 def test_not_actually_cdata(self):
1295 # Ensure bug reported in #1534 is avoided.
1296 self.assertMarkdownRenders(
1297 '<![',
1298 '<p>&lt;![</p>'
1299 )
1300
1301 def test_raw_cdata_code_span(self):
1302 self.assertMarkdownRenders(
1303 self.dedent(
1304 """
1305 `<![`
1306
1307 <div>
1308 foo
1309 </div>
1310 """
1311 ),
1312 self.dedent(
1313 """
1314 <p><code>&lt;![</code></p>
1315 <div>
1316 foo
1317 </div>
1318 """
1319 )
1320 )
1321
1322 def test_charref(self):
1323 self.assertMarkdownRenders(
1324 '&sect;',
1325 '<p>&sect;</p>'
1326 )
1327
1328 def test_nested_charref(self):
1329 self.assertMarkdownRenders(
1330 '<p>&sect;</p>',
1331 '<p>&sect;</p>'
1332 )
1333
1334 def test_entityref(self):
1335 self.assertMarkdownRenders(
1336 '&#167;',
1337 '<p>&#167;</p>'
1338 )
1339
1340 def test_nested_entityref(self):
1341 self.assertMarkdownRenders(
1342 '<p>&#167;</p>',
1343 '<p>&#167;</p>'
1344 )
1345
1346 def test_amperstand(self):
1347 self.assertMarkdownRenders(
1348 'AT&T & AT&amp;T',
1349 '<p>AT&amp;T &amp; AT&amp;T</p>'
1350 )
1351
1352 def test_startendtag(self):
1353 self.assertMarkdownRenders(
1354 '<hr>',
1355 '<hr>'
1356 )
1357
1358 def test_startendtag_with_attrs(self):
1359 self.assertMarkdownRenders(
1360 '<hr id="foo" class="bar">',
1361 '<hr id="foo" class="bar">'
1362 )
1363
1364 def test_startendtag_with_space(self):
1365 self.assertMarkdownRenders(
1366 '<hr >',
1367 '<hr >'
1368 )
1369
1370 def test_closed_startendtag(self):
1371 self.assertMarkdownRenders(
1372 '<hr />',
1373 '<hr />'
1374 )
1375
1376 def test_closed_startendtag_without_space(self):
1377 self.assertMarkdownRenders(
1378 '<hr/>',
1379 '<hr/>'
1380 )
1381
1382 def test_closed_startendtag_with_attrs(self):
1383 self.assertMarkdownRenders(
1384 '<hr id="foo" class="bar" />',
1385 '<hr id="foo" class="bar" />'
1386 )
1387
1388 def test_nested_startendtag(self):
1389 self.assertMarkdownRenders(
1390 '<div><hr></div>',
1391 '<div><hr></div>'
1392 )
1393
1394 def test_nested_closed_startendtag(self):
1395 self.assertMarkdownRenders(
1396 '<div><hr /></div>',
1397 '<div><hr /></div>'
1398 )
1399
1400 def test_multiline_attributes(self):
1401 self.assertMarkdownRenders(
1402 self.dedent(
1403 """
1404 <div id="foo"
1405 class="bar">
1406 text
1407 </div>
1408
1409 <hr class="foo"
1410 id="bar" >
1411 """
1412 ),
1413 self.dedent(
1414 """
1415 <div id="foo"
1416 class="bar">
1417 text
1418 </div>
1419
1420 <hr class="foo"
1421 id="bar" >
1422 """
1423 )
1424 )
1425
1426 def test_auto_links_dont_break_parser(self):
1427 self.assertMarkdownRenders(
1428 self.dedent(
1429 """
1430 <https://example.com>
1431
1432 <[email protected]>
1433 """
1434 ),
1435 '<p><a href="https://example.com">https://example.com</a></p>\n'
1436 '<p><a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#101;&#109;'
1437 '&#97;&#105;&#108;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;'
1438 '&#46;&#99;&#111;&#109;">&#101;&#109;&#97;&#105;&#108;&#64;&#101;'
1439 '&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;</a></p>'
1440 )
1441
1442 def test_text_links_ignored(self):
1443 self.assertMarkdownRenders(
1444 self.dedent(
1445 """
1446 https://example.com
1447
1448 [email protected]
1449 """
1450 ),
1451 self.dedent(
1452 """
1453 <p>https://example.com</p>
1454 <p>[email protected]</p>
1455 """
1456 ),
1457 )
1458
1459 def text_invalid_tags(self):
1460 self.assertMarkdownRenders(
1461 self.dedent(
1462 """
1463 <some [weird](http://example.com) stuff>
1464
1465 <some>> <<unbalanced>> <<brackets>
1466 """
1467 ),
1468 self.dedent(
1469 """
1470 <p><some <a href="http://example.com">weird</a> stuff></p>
1471 <p><some>&gt; &lt;<unbalanced>&gt; &lt;<brackets></p>
1472 """
1473 )
1474 )
1475
1476 def test_script_tags(self):
1477 self.assertMarkdownRenders(
1478 self.dedent(
1479 """
1480 <script>
1481 *random stuff* <div> &amp;
1482 </script>
1483
1484 <style>
1485 **more stuff**
1486 </style>
1487 """
1488 ),
1489 self.dedent(
1490 """
1491 <script>
1492 *random stuff* <div> &amp;
1493 </script>
1494
1495 <style>
1496 **more stuff**
1497 </style>
1498 """
1499 )
1500 )
1501
1502 def test_unclosed_script_tag(self):
1503 # Ensure we have a working fix for https://bugs.python.org/issue41989
1504 self.assertMarkdownRenders(
1505 self.dedent(
1506 """
1507 <script>
1508 *random stuff* <div> &amp;
1509
1510 Still part of the *script* tag
1511 """
1512 ),
1513 self.dedent(
1514 """
1515 <script>
1516 *random stuff* <div> &amp;
1517
1518 Still part of the *script* tag
1519 """
1520 )
1521 )
1522
1523 def test_inline_script_tags(self):
1524 # Ensure inline script tags doesn't cause the parser to eat content (see #1036).
1525 self.assertMarkdownRenders(
1526 self.dedent(
1527 """
1528 Text `<script>` more *text*.
1529
1530 <div>
1531 *foo*
1532 </div>
1533
1534 <div>
1535
1536 bar
1537
1538 </div>
1539
1540 A new paragraph with a closing `</script>` tag.
1541 """
1542 ),
1543 self.dedent(
1544 """
1545 <p>Text <code>&lt;script&gt;</code> more <em>text</em>.</p>
1546 <div>
1547 *foo*
1548 </div>
1549
1550 <div>
1551
1552 bar
1553
1554 </div>
1555
1556 <p>A new paragraph with a closing <code>&lt;/script&gt;</code> tag.</p>
1557 """
1558 )
1559 )
1560
1561 def test_hr_only_start(self):
1562 self.assertMarkdownRenders(
1563 self.dedent(
1564 """
1565 *emphasis1*
1566 <hr>
1567 *emphasis2*
1568 """
1569 ),
1570 self.dedent(
1571 """
1572 <p><em>emphasis1</em></p>
1573 <hr>
1574 <p><em>emphasis2</em></p>
1575 """
1576 )
1577 )
1578
1579 def test_hr_self_close(self):
1580 self.assertMarkdownRenders(
1581 self.dedent(
1582 """
1583 *emphasis1*
1584 <hr/>
1585 *emphasis2*
1586 """
1587 ),
1588 self.dedent(
1589 """
1590 <p><em>emphasis1</em></p>
1591 <hr/>
1592 <p><em>emphasis2</em></p>
1593 """
1594 )
1595 )
1596
1597 def test_hr_start_and_end(self):
1598 # Browsers ignore ending hr tags, so we don't try to do anything to handle them special.
1599 self.assertMarkdownRenders(
1600 self.dedent(
1601 """
1602 *emphasis1*
1603 <hr></hr>
1604 *emphasis2*
1605 """
1606 ),
1607 self.dedent(
1608 """
1609 <p><em>emphasis1</em></p>
1610 <hr>
1611 <p></hr>
1612 <em>emphasis2</em></p>
1613 """
1614 )
1615 )
1616
1617 def test_hr_only_end(self):
1618 # Browsers ignore ending hr tags, so we don't try to do anything to handle them special.
1619 self.assertMarkdownRenders(
1620 self.dedent(
1621 """
1622 *emphasis1*
1623 </hr>
1624 *emphasis2*
1625 """
1626 ),
1627 self.dedent(
1628 """
1629 <p><em>emphasis1</em>
1630 </hr>
1631 <em>emphasis2</em></p>
1632 """
1633 )
1634 )
1635
1636 def test_hr_with_content(self):
1637 # Browsers ignore ending hr tags, so we don't try to do anything to handle them special.
1638 # Content is not allowed and will be treated as normal content between two hr tags.
1639 self.assertMarkdownRenders(
1640 self.dedent(
1641 """
1642 *emphasis1*
1643 <hr>
1644 **content**
1645 </hr>
1646 *emphasis2*
1647 """
1648 ),
1649 self.dedent(
1650 """
1651 <p><em>emphasis1</em></p>
1652 <hr>
1653 <p><strong>content</strong>
1654 </hr>
1655 <em>emphasis2</em></p>
1656 """
1657 )
1658 )
1659
1660 def test_placeholder_in_source(self):
1661 # This should never occur, but third party extensions could create weird edge cases.
1662 md = markdown.Markdown()
1663 # Ensure there is an `htmlstash` so relevant code (nested in `if replacements`) is run.
1664 md.htmlStash.store('foo')
1665 # Run with a placeholder which is not in the stash
1666 placeholder = md.htmlStash.get_placeholder(md.htmlStash.html_counter + 1)
1667 result = md.postprocessors['raw_html'].run(placeholder)
1668 self.assertEqual(placeholder, result)
1669
1670 def test_noname_tag(self):
1671 self.assertMarkdownRenders(
1672 self.dedent(
1673 """
1674 <div>
1675 </>
1676 </div>
1677 """
1678 ),
1679 self.dedent(
1680 """
1681 <div>
1682 </>
1683 </div>
1684 """
1685 )
1686 )
1687
1688 def test_multiple_bogus_comments_no_hang(self):
1689 """Test that multiple bogus comments (</` patterns) don't cause infinite loop."""
1690 self.assertMarkdownRenders(
1691 '`</` and `</`',
1692 '<p><code>&lt;/</code> and <code>&lt;/</code></p>'
1693 )
1694
1695 def test_multiple_unclosed_comments_no_hang(self):
1696 """Test that multiple unclosed comments don't cause infinite loop."""
1697 self.assertMarkdownRenders(
1698 '<!-- and <!--',
1699 '<p>&lt;!-- and &lt;!--</p>'
1700 )
1701
1702 def test_no_hang_issue_1586(self):
1703 """Test no hang condition for issue #1586."""
1704
1705 self.assertMarkdownRenders(
1706 'Test `<!--[if mso]>` and `<!--[if !mso]>`',
1707 '<p>Test <code>&lt;!--[if mso]&gt;</code> and <code>&lt;!--[if !mso]&gt;</code></p>'
1708 )
1709
1710 def test_issue_1590(self):
1711 """Test case with comments in table for issue #1590."""
1712
1713 self.assertMarkdownRenders(
1714 self.dedent(
1715 '''
1716 <table>
1717 <!--[if mso]>-->
1718 <td>foo</td>
1719 <!--<!endif]-->
1720 <td>bar</td>
1721 </table>
1722 '''
1723 ),
1724 self.dedent(
1725 '''
1726 <table>
1727 <!--[if mso]>-->
1728 <td>foo</td>
1729 <!--<!endif]-->
1730 <td>bar</td>
1731 </table>
1732 '''
1733 )
1734 )
1735
1736 def test_stress_comment_handling(self):
1737 """Stress test the comment handling."""
1738
1739 self.assertMarkdownRenders(
1740 self.dedent(
1741 '''
1742 `</` <!-- `<!--[if mso]>` and <!-- </> and `<!--[if mso]>`
1743
1744 <!-- and <!-- `<!--[if mso]>` and </> `</` and `<!--[if mso]>`
1745
1746 <!-- Real comment -->
1747
1748 `<!--[if mso]>` `</` `<!--[if mso]>` and </> <!-- and <!--
1749
1750 </> `<!--[if mso]>` `</` <!-- and <!-- and `<!--[if mso]>`
1751 '''
1752 ),
1753 self.dedent(
1754 '''
1755 <p><code>&lt;/</code> &lt;!-- <code>&lt;!--[if mso]&gt;</code> and &lt;!-- &lt;/&gt; and <code>&lt;!--[if mso]&gt;</code></p>
1756 <p>&lt;!-- and &lt;!-- <code>&lt;!--[if mso]&gt;</code> and &lt;/&gt; <code>&lt;/</code> and <code>&lt;!--[if mso]&gt;</code></p>
1757 <!-- Real comment -->
1758 <p><code>&lt;!--[if mso]&gt;</code> <code>&lt;/</code> <code>&lt;!--[if mso]&gt;</code> and &lt;/&gt; &lt;!-- and &lt;!--</p>
1759 <p>&lt;/&gt; <code>&lt;!--[if mso]&gt;</code> <code>&lt;/</code> &lt;!-- and &lt;!-- and <code>&lt;!--[if mso]&gt;</code></p>
1760 ''' # noqa: E501
1761 )
1762 )
1763
1764 def test_unclosed_endtag(self):
1765 """Ensure unclosed end tag does not have side effects."""
1766
1767 self.assertMarkdownRenders(
1768 self.dedent(
1769 '''
1770 `</`
1771
1772 <div>
1773 <!--[if mso]>-->
1774 <p>foo</p>
1775 <!--<!endif]-->
1776 </div>
1777 '''
1778 ),
1779 self.dedent(
1780 '''
1781 <p><code>&lt;/</code></p>
1782 <div>
1783 <!--[if mso]>-->
1784 <p>foo</p>
1785 <!--<!endif]-->
1786 </div>
1787 '''
1788 )
1789 )