Debian Codegraph @

tests/test_syntax/extensions/test_md_in_html.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 unittest import TestSuite
24 from markdown.test_tools import TestCase
25 from ..blocks.test_html_blocks import TestHTMLBlocks
26 from markdown import Markdown
27 from xml.etree.ElementTree import Element
28
29
30 class TestMarkdownInHTMLPostProcessor(TestCase):
31 """ Ensure any remaining elements in HTML stash are properly serialized. """
32
33 def test_stash_to_string(self):
34 # There should be no known cases where this actually happens so we need to
35 # forcefully pass an `etree` `Element` to the method to ensure proper behavior.
36 element = Element('div')
37 element.text = 'Foo bar.'
38 md = Markdown(extensions=['md_in_html'])
39 result = md.postprocessors['raw_html'].stash_to_string(element)
40 self.assertEqual(result, '<div>Foo bar.</div>')
41
42
43 class TestDefaultwMdInHTML(TestHTMLBlocks):
44 """ Ensure the md_in_html extension does not break the default behavior. """
45
46 default_kwargs = {'extensions': ['md_in_html']}
47
48
49 class TestMdInHTML(TestCase):
50
51 default_kwargs = {'extensions': ['md_in_html']}
52
53 def test_md1_paragraph(self):
54 self.assertMarkdownRenders(
55 '<p markdown="1">*foo*</p>',
56 '<p><em>foo</em></p>'
57 )
58
59 def test_md1_p_linebreaks(self):
60 self.assertMarkdownRenders(
61 self.dedent(
62 """
63 <p markdown="1">
64 *foo*
65 </p>
66 """
67 ),
68 self.dedent(
69 """
70 <p>
71 <em>foo</em>
72 </p>
73 """
74 )
75 )
76
77 def test_md1_p_blank_lines(self):
78 self.assertMarkdownRenders(
79 self.dedent(
80 """
81 <p markdown="1">
82
83 *foo*
84
85 </p>
86 """
87 ),
88 self.dedent(
89 """
90 <p>
91
92 <em>foo</em>
93
94 </p>
95 """
96 )
97 )
98
99 def test_md1_div(self):
100 self.assertMarkdownRenders(
101 '<div markdown="1">*foo*</div>',
102 self.dedent(
103 """
104 <div>
105 <p><em>foo</em></p>
106 </div>
107 """
108 )
109 )
110
111 def test_md1_div_linebreaks(self):
112 self.assertMarkdownRenders(
113 self.dedent(
114 """
115 <div markdown="1">
116 *foo*
117 </div>
118 """
119 ),
120 self.dedent(
121 """
122 <div>
123 <p><em>foo</em></p>
124 </div>
125 """
126 )
127 )
128
129 def test_md1_code_span(self):
130 self.assertMarkdownRenders(
131 self.dedent(
132 """
133 <div markdown="1">
134 `<h1>code span</h1>`
135 </div>
136 """
137 ),
138 self.dedent(
139 """
140 <div>
141 <p><code>&lt;h1&gt;code span&lt;/h1&gt;</code></p>
142 </div>
143 """
144 )
145 )
146
147 def test_md1_code_span_oneline(self):
148 self.assertMarkdownRenders(
149 '<div markdown="1">`<h1>code span</h1>`</div>',
150 self.dedent(
151 """
152 <div>
153 <p><code>&lt;h1&gt;code span&lt;/h1&gt;</code></p>
154 </div>
155 """
156 )
157 )
158
159 def test_md1_code_span_unclosed(self):
160 self.assertMarkdownRenders(
161 self.dedent(
162 """
163 <div markdown="1">
164 `<p>`
165 </div>
166 """
167 ),
168 self.dedent(
169 """
170 <div>
171 <p><code>&lt;p&gt;</code></p>
172 </div>
173 """
174 )
175 )
176
177 def test_md1_code_span_script_tag(self):
178 self.assertMarkdownRenders(
179 self.dedent(
180 """
181 <div markdown="1">
182 `<script>`
183 </div>
184 """
185 ),
186 self.dedent(
187 """
188 <div>
189 <p><code>&lt;script&gt;</code></p>
190 </div>
191 """
192 )
193 )
194
195 def test_md1_div_blank_lines(self):
196 self.assertMarkdownRenders(
197 self.dedent(
198 """
199 <div markdown="1">
200
201 *foo*
202
203 </div>
204 """
205 ),
206 self.dedent(
207 """
208 <div>
209 <p><em>foo</em></p>
210 </div>
211 """
212 )
213 )
214
215 def test_md1_div_multi(self):
216 self.assertMarkdownRenders(
217 self.dedent(
218 """
219 <div markdown="1">
220
221 *foo*
222
223 __bar__
224
225 </div>
226 """
227 ),
228 self.dedent(
229 """
230 <div>
231 <p><em>foo</em></p>
232 <p><strong>bar</strong></p>
233 </div>
234 """
235 )
236 )
237
238 def test_md1_div_nested(self):
239 self.assertMarkdownRenders(
240 self.dedent(
241 """
242 <div markdown="1">
243
244 <div markdown="1">
245 *foo*
246 </div>
247
248 </div>
249 """
250 ),
251 self.dedent(
252 """
253 <div>
254 <div>
255 <p><em>foo</em></p>
256 </div>
257 </div>
258 """
259 )
260 )
261
262 def test_md1_div_multi_nest(self):
263 self.assertMarkdownRenders(
264 self.dedent(
265 """
266 <div markdown="1">
267
268 <div markdown="1">
269 <p markdown="1">*foo*</p>
270 </div>
271
272 </div>
273 """
274 ),
275 self.dedent(
276 """
277 <div>
278 <div>
279 <p><em>foo</em></p>
280 </div>
281 </div>
282 """
283 )
284 )
285
286 def text_md1_details(self):
287 self.assertMarkdownRenders(
288 self.dedent(
289 """
290 <details markdown="1">
291 <summary>Click to expand</summary>
292 *foo*
293 </details>
294 """
295 ),
296 self.dedent(
297 """
298 <details>
299 <summary>Click to expand</summary>
300 <p><em>foo</em></p>
301 </details>
302 """
303 )
304 )
305
306 def test_md1_mix(self):
307 self.assertMarkdownRenders(
308 self.dedent(
309 """
310 <div markdown="1">
311 A _Markdown_ paragraph before a raw child.
312
313 <p markdown="1">A *raw* child.</p>
314
315 A _Markdown_ tail to the raw child.
316 </div>
317 """
318 ),
319 self.dedent(
320 """
321 <div>
322 <p>A <em>Markdown</em> paragraph before a raw child.</p>
323 <p>A <em>raw</em> child.</p>
324 <p>A <em>Markdown</em> tail to the raw child.</p>
325 </div>
326 """
327 )
328 )
329
330 def test_md1_deep_mix(self):
331 self.assertMarkdownRenders(
332 self.dedent(
333 """
334 <div markdown="1">
335
336 A _Markdown_ paragraph before a raw child.
337
338 A second Markdown paragraph
339 with two lines.
340
341 <div markdown="1">
342
343 A *raw* child.
344
345 <p markdown="1">*foo*</p>
346
347 Raw child tail.
348
349 </div>
350
351 A _Markdown_ tail to the raw child.
352
353 A second tail item
354 with two lines.
355
356 <p markdown="1">More raw.</p>
357
358 </div>
359 """
360 ),
361 self.dedent(
362 """
363 <div>
364 <p>A <em>Markdown</em> paragraph before a raw child.</p>
365 <p>A second Markdown paragraph
366 with two lines.</p>
367 <div>
368 <p>A <em>raw</em> child.</p>
369 <p><em>foo</em></p>
370 <p>Raw child tail.</p>
371 </div>
372 <p>A <em>Markdown</em> tail to the raw child.</p>
373 <p>A second tail item
374 with two lines.</p>
375 <p>More raw.</p>
376 </div>
377 """
378 )
379 )
380
381 def test_md1_div_raw_inline(self):
382 self.assertMarkdownRenders(
383 self.dedent(
384 """
385 <div markdown="1">
386
387 <em>foo</em>
388
389 </div>
390 """
391 ),
392 self.dedent(
393 """
394 <div>
395 <p><em>foo</em></p>
396 </div>
397 """
398 )
399 )
400
401 def test_no_md1_paragraph(self):
402 self.assertMarkdownRenders(
403 '<p>*foo*</p>',
404 '<p>*foo*</p>'
405 )
406
407 def test_no_md1_nest(self):
408 self.assertMarkdownRenders(
409 self.dedent(
410 """
411 <div markdown="1">
412 A _Markdown_ paragraph before a raw child.
413
414 <p>A *raw* child.</p>
415
416 A _Markdown_ tail to the raw child.
417 </div>
418 """
419 ),
420 self.dedent(
421 """
422 <div>
423 <p>A <em>Markdown</em> paragraph before a raw child.</p>
424 <p>A *raw* child.</p>
425 <p>A <em>Markdown</em> tail to the raw child.</p>
426 </div>
427 """
428 )
429 )
430
431 def test_md1_nested_empty(self):
432 self.assertMarkdownRenders(
433 self.dedent(
434 """
435 <div markdown="1">
436 A _Markdown_ paragraph before a raw empty tag.
437
438 <img src="image.png" alt="An image" />
439
440 A _Markdown_ tail to the raw empty tag.
441 </div>
442 """
443 ),
444 self.dedent(
445 """
446 <div>
447 <p>A <em>Markdown</em> paragraph before a raw empty tag.</p>
448 <p><img src="image.png" alt="An image" /></p>
449 <p>A <em>Markdown</em> tail to the raw empty tag.</p>
450 </div>
451 """
452 )
453 )
454
455 def test_md1_nested_empty_block(self):
456 self.assertMarkdownRenders(
457 self.dedent(
458 """
459 <div markdown="1">
460 A _Markdown_ paragraph before a raw empty tag.
461
462 <hr />
463
464 A _Markdown_ tail to the raw empty tag.
465 </div>
466 """
467 ),
468 self.dedent(
469 """
470 <div>
471 <p>A <em>Markdown</em> paragraph before a raw empty tag.</p>
472 <hr />
473 <p>A <em>Markdown</em> tail to the raw empty tag.</p>
474 </div>
475 """
476 )
477 )
478
479 def test_empty_tags(self):
480 self.assertMarkdownRenders(
481 self.dedent(
482 """
483 <div markdown="1">
484 <div></div>
485 </div>
486 """
487 ),
488 self.dedent(
489 """
490 <div>
491 <div></div>
492 </div>
493 """
494 )
495 )
496
497 def test_orphan_end_tag_in_raw_html(self):
498 self.assertMarkdownRenders(
499 self.dedent(
500 """
501 <div markdown="1">
502 <div>
503 Test
504
505 </pre>
506
507 Test
508 </div>
509 </div>
510 """
511 ),
512 self.dedent(
513 """
514 <div>
515 <div>
516 Test
517
518 </pre>
519
520 Test
521 </div>
522 </div>
523 """
524 )
525 )
526
527 def test_complex_nested_case(self):
528 self.assertMarkdownRenders(
529 self.dedent(
530 """
531 <div markdown="1">
532 **test**
533 <div>
534 **test**
535 <img src=""/>
536 <code>Test</code>
537 <span>**test**</span>
538 <p>Test 2</p>
539 </div>
540 </div>
541 """
542 ),
543 self.dedent(
544 """
545 <div>
546 <p><strong>test</strong></p>
547 <div>
548 **test**
549 <img src=""/>
550 <code>Test</code>
551 <span>**test**</span>
552 <p>Test 2</p>
553 </div>
554 </div>
555 """
556 )
557 )
558
559 def test_complex_nested_case_whitespace(self):
560 self.assertMarkdownRenders(
561 self.dedent(
562 """
563 Text with space\t
564 <div markdown="1">\t
565 \t
566 <div>
567 **test**
568 <img src=""/>
569 <code>Test</code>
570 <span>**test**</span>
571 <div>With whitespace</div>
572 <p>Test 2</p>
573 </div>
574 **test**
575 </div>
576 """
577 ),
578 self.dedent(
579 """
580 <p>Text with space </p>
581 <div>
582 <div>
583 **test**
584 <img src=""/>
585 <code>Test</code>
586 <span>**test**</span>
587 <div>With whitespace</div>
588 <p>Test 2</p>
589 </div>
590 <p><strong>test</strong></p>
591 </div>
592 """
593 )
594 )
595
596 def test_md1_intail_md1(self):
597 self.assertMarkdownRenders(
598 '<div markdown="1">*foo*</div><div markdown="1">*bar*</div>',
599 self.dedent(
600 """
601 <div>
602 <p><em>foo</em></p>
603 </div>
604 <div>
605 <p><em>bar</em></p>
606 </div>
607 """
608 )
609 )
610
611 def test_md1_no_blank_line_before(self):
612 self.assertMarkdownRenders(
613 self.dedent(
614 """
615 A _Markdown_ paragraph with no blank line after.
616 <div markdown="1">
617 A _Markdown_ paragraph in an HTML block with no blank line before.
618 </div>
619 """
620 ),
621 self.dedent(
622 """
623 <p>A <em>Markdown</em> paragraph with no blank line after.</p>
624 <div>
625 <p>A <em>Markdown</em> paragraph in an HTML block with no blank line before.</p>
626 </div>
627 """
628 )
629 )
630
631 def test_md1_no_line_break(self):
632 # The div here is parsed as a span-level element. Bad input equals bad output!
633 self.assertMarkdownRenders(
634 'A _Markdown_ paragraph with <div markdown="1">no _line break_.</div>',
635 '<p>A <em>Markdown</em> paragraph with <div markdown="1">no <em>line break</em>.</div></p>'
636 )
637
638 def test_md1_in_tail(self):
639 self.assertMarkdownRenders(
640 self.dedent(
641 """
642 <div></div><div markdown="1">
643 A _Markdown_ paragraph in an HTML block in tail of previous element.
644 </div>
645 """
646 ),
647 self.dedent(
648 """
649 <div></div>
650 <div>
651 <p>A <em>Markdown</em> paragraph in an HTML block in tail of previous element.</p>
652 </div>
653 """
654 )
655 )
656
657 def test_md1_PI_oneliner(self):
658 self.assertMarkdownRenders(
659 '<div markdown="1"><?php print("foo"); ?></div>',
660 self.dedent(
661 """
662 <div>
663 <?php print("foo"); ?>
664 </div>
665 """
666 )
667 )
668
669 def test_md1_PI_multiline(self):
670 self.assertMarkdownRenders(
671 self.dedent(
672 """
673 <div markdown="1">
674 <?php print("foo"); ?>
675 </div>
676 """
677 ),
678 self.dedent(
679 """
680 <div>
681 <?php print("foo"); ?>
682 </div>
683 """
684 )
685 )
686
687 def test_md1_PI_blank_lines(self):
688 self.assertMarkdownRenders(
689 self.dedent(
690 """
691 <div markdown="1">
692
693 <?php print("foo"); ?>
694
695 </div>
696 """
697 ),
698 self.dedent(
699 """
700 <div>
701 <?php print("foo"); ?>
702 </div>
703 """
704 )
705 )
706
707 def test_md_span_paragraph(self):
708 self.assertMarkdownRenders(
709 '<p markdown="span">*foo*</p>',
710 '<p><em>foo</em></p>'
711 )
712
713 def test_md_block_paragraph(self):
714 self.assertMarkdownRenders(
715 '<p markdown="block">*foo*</p>',
716 self.dedent(
717 """
718 <p>
719 <p><em>foo</em></p>
720 </p>
721 """
722 )
723 )
724
725 def test_md_span_div(self):
726 self.assertMarkdownRenders(
727 '<div markdown="span">*foo*</div>',
728 '<div><em>foo</em></div>'
729 )
730
731 def test_md_block_div(self):
732 self.assertMarkdownRenders(
733 '<div markdown="block">*foo*</div>',
734 self.dedent(
735 """
736 <div>
737 <p><em>foo</em></p>
738 </div>
739 """
740 )
741 )
742
743 def test_md_span_nested_in_block(self):
744 self.assertMarkdownRenders(
745 self.dedent(
746 """
747 <div markdown="block">
748 <div markdown="span">*foo*</div>
749 </div>
750 """
751 ),
752 self.dedent(
753 """
754 <div>
755 <div><em>foo</em></div>
756 </div>
757 """
758 )
759 )
760
761 def test_md_block_nested_in_span(self):
762 self.assertMarkdownRenders(
763 self.dedent(
764 """
765 <div markdown="span">
766 <div markdown="block">*foo*</div>
767 </div>
768 """
769 ),
770 self.dedent(
771 """
772 <div>
773 <div><em>foo</em></div>
774 </div>
775 """
776 )
777 )
778
779 def test_md_block_after_span_nested_in_block(self):
780 self.assertMarkdownRenders(
781 self.dedent(
782 """
783 <div markdown="block">
784 <div markdown="span">*foo*</div>
785 <div markdown="block">*bar*</div>
786 </div>
787 """
788 ),
789 self.dedent(
790 """
791 <div>
792 <div><em>foo</em></div>
793 <div>
794 <p><em>bar</em></p>
795 </div>
796 </div>
797 """
798 )
799 )
800
801 def test_nomd_nested_in_md1(self):
802 self.assertMarkdownRenders(
803 self.dedent(
804 """
805 <div markdown="1">
806 *foo*
807 <div>
808 *foo*
809 <p>*bar*</p>
810 *baz*
811 </div>
812 *bar*
813 </div>
814 """
815 ),
816 self.dedent(
817 """
818 <div>
819 <p><em>foo</em></p>
820 <div>
821 *foo*
822 <p>*bar*</p>
823 *baz*
824 </div>
825 <p><em>bar</em></p>
826 </div>
827 """
828 )
829 )
830
831 def test_md1_nested_in_nomd(self):
832 self.assertMarkdownRenders(
833 self.dedent(
834 """
835 <div>
836 <div markdown="1">*foo*</div>
837 </div>
838 """
839 ),
840 self.dedent(
841 """
842 <div>
843 <div markdown="1">*foo*</div>
844 </div>
845 """
846 )
847 )
848
849 def test_md1_single_quotes(self):
850 self.assertMarkdownRenders(
851 "<p markdown='1'>*foo*</p>",
852 '<p><em>foo</em></p>'
853 )
854
855 def test_md1_no_quotes(self):
856 self.assertMarkdownRenders(
857 '<p markdown=1>*foo*</p>',
858 '<p><em>foo</em></p>'
859 )
860
861 def test_md_no_value(self):
862 self.assertMarkdownRenders(
863 '<p markdown>*foo*</p>',
864 '<p><em>foo</em></p>'
865 )
866
867 def test_md1_preserve_attrs(self):
868 self.assertMarkdownRenders(
869 self.dedent(
870 """
871 <div markdown="1" id="parent">
872
873 <div markdown="1" class="foo">
874 <p markdown="1" class="bar baz">*foo*</p>
875 </div>
876
877 </div>
878 """
879 ),
880 self.dedent(
881 """
882 <div id="parent">
883 <div class="foo">
884 <p class="bar baz"><em>foo</em></p>
885 </div>
886 </div>
887 """
888 )
889 )
890
891 def test_md1_unclosed_div(self):
892 self.assertMarkdownRenders(
893 self.dedent(
894 """
895 <div markdown="1">
896
897 _foo_
898
899 <div class="unclosed">
900
901 _bar_
902
903 </div>
904 """
905 ),
906 self.dedent(
907 """
908 <div>
909 <p><em>foo</em></p>
910 <div class="unclosed">
911
912 _bar_
913
914 </div>
915 </div>
916 """
917 )
918 )
919
920 def test_md1_orphan_endtag(self):
921 self.assertMarkdownRenders(
922 self.dedent(
923 """
924 <div markdown="1">
925
926 _foo_
927
928 </p>
929
930 _bar_
931
932 </div>
933 """
934 ),
935 self.dedent(
936 """
937 <div>
938 <p><em>foo</em></p>
939 </p>
940 <p><em>bar</em></p>
941 </div>
942 """
943 )
944 )
945
946 def test_md1_unclosed_p(self):
947 self.assertMarkdownRenders(
948 self.dedent(
949 """
950 <p markdown="1">_foo_
951 <p markdown="1">_bar_
952 """
953 ),
954 self.dedent(
955 """
956 <p><em>foo</em>
957 </p>
958 <p><em>bar</em>
959
960 </p>
961 """
962 )
963 )
964
965 def test_md1_nested_unclosed_p(self):
966 self.assertMarkdownRenders(
967 self.dedent(
968 """
969 <div markdown="1">
970 <p markdown="1">_foo_
971 <p markdown="1">_bar_
972 </div>
973 """
974 ),
975 self.dedent(
976 """
977 <div>
978 <p><em>foo</em>
979 </p>
980 <p><em>bar</em>
981 </p>
982 </div>
983 """
984 )
985 )
986
987 def test_md1_nested_comment(self):
988 self.assertMarkdownRenders(
989 self.dedent(
990 """
991 <div markdown="1">
992 A *Markdown* paragraph.
993 <!-- foobar -->
994 A *Markdown* paragraph.
995 </div>
996 """
997 ),
998 self.dedent(
999 """
1000 <div>
1001 <p>A <em>Markdown</em> paragraph.</p>
1002 <!-- foobar -->
1003 <p>A <em>Markdown</em> paragraph.</p>
1004 </div>
1005 """
1006 )
1007 )
1008
1009 def test_md1_nested_link_ref(self):
1010 self.assertMarkdownRenders(
1011 self.dedent(
1012 """
1013 <div markdown="1">
1014 [link]: http://example.com
1015 <div markdown="1">
1016 [link][link]
1017 </div>
1018 </div>
1019 """
1020 ),
1021 self.dedent(
1022 """
1023 <div>
1024 <div>
1025 <p><a href="http://example.com">link</a></p>
1026 </div>
1027 </div>
1028 """
1029 )
1030 )
1031
1032 def test_md1_hr_only_start(self):
1033 self.assertMarkdownRenders(
1034 self.dedent(
1035 """
1036 *emphasis1*
1037 <hr markdown="1">
1038 *emphasis2*
1039 """
1040 ),
1041 self.dedent(
1042 """
1043 <p><em>emphasis1</em></p>
1044 <hr>
1045 <p><em>emphasis2</em></p>
1046 """
1047 )
1048 )
1049
1050 def test_md1_hr_self_close(self):
1051 self.assertMarkdownRenders(
1052 self.dedent(
1053 """
1054 *emphasis1*
1055 <hr markdown="1" />
1056 *emphasis2*
1057 """
1058 ),
1059 self.dedent(
1060 """
1061 <p><em>emphasis1</em></p>
1062 <hr>
1063 <p><em>emphasis2</em></p>
1064 """
1065 )
1066 )
1067
1068 def test_md1_hr_start_and_end(self):
1069 # Browsers ignore ending hr tags, so we don't try to do anything to handle them special.
1070 self.assertMarkdownRenders(
1071 self.dedent(
1072 """
1073 *emphasis1*
1074 <hr markdown="1"></hr>
1075 *emphasis2*
1076 """
1077 ),
1078 self.dedent(
1079 """
1080 <p><em>emphasis1</em></p>
1081 <hr>
1082 <p></hr>
1083 <em>emphasis2</em></p>
1084 """
1085 )
1086 )
1087
1088 def test_md1_hr_only_end(self):
1089 # Browsers ignore ending hr tags, so we don't try to do anything to handle them special.
1090 self.assertMarkdownRenders(
1091 self.dedent(
1092 """
1093 *emphasis1*
1094 </hr>
1095 *emphasis2*
1096 """
1097 ),
1098 self.dedent(
1099 """
1100 <p><em>emphasis1</em>
1101 </hr>
1102 <em>emphasis2</em></p>
1103 """
1104 )
1105 )
1106
1107 def test_md1_hr_with_content(self):
1108 # Browsers ignore ending hr tags, so we don't try to do anything to handle them special.
1109 # Content is not allowed and will be treated as normal content between two hr tags
1110 self.assertMarkdownRenders(
1111 self.dedent(
1112 """
1113 *emphasis1*
1114 <hr markdown="1">
1115 **content**
1116 </hr>
1117 *emphasis2*
1118 """
1119 ),
1120 self.dedent(
1121 """
1122 <p><em>emphasis1</em></p>
1123 <hr>
1124 <p><strong>content</strong>
1125 </hr>
1126 <em>emphasis2</em></p>
1127 """
1128 )
1129 )
1130
1131 def test_no_md1_hr_with_content(self):
1132 # Browsers ignore ending hr tags, so we don't try to do anything to handle them special.
1133 # Content is not allowed and will be treated as normal content between two hr tags
1134 self.assertMarkdownRenders(
1135 self.dedent(
1136 """
1137 *emphasis1*
1138 <hr>
1139 **content**
1140 </hr>
1141 *emphasis2*
1142 """
1143 ),
1144 self.dedent(
1145 """
1146 <p><em>emphasis1</em></p>
1147 <hr>
1148 <p><strong>content</strong>
1149 </hr>
1150 <em>emphasis2</em></p>
1151 """
1152 )
1153 )
1154
1155 def test_md1_nested_abbr_ref(self):
1156 self.assertMarkdownRenders(
1157 self.dedent(
1158 """
1159 <div markdown="1">
1160 *[abbr]: Abbreviation
1161 <div markdown="1">
1162 abbr
1163 </div>
1164 </div>
1165 """
1166 ),
1167 self.dedent(
1168 """
1169 <div>
1170 <div>
1171 <p><abbr title="Abbreviation">abbr</abbr></p>
1172 </div>
1173 </div>
1174 """
1175 ),
1176 extensions=['md_in_html', 'abbr']
1177 )
1178
1179 def test_md1_nested_footnote_ref(self):
1180 self.assertMarkdownRenders(
1181 self.dedent(
1182 """
1183 <div markdown="1">
1184 [^1]: The footnote.
1185 <div markdown="1">
1186 Paragraph with a footnote.[^1]
1187 </div>
1188 </div>
1189 """
1190 ),
1191 '<div>\n'
1192 '<div>\n'
1193 '<p>Paragraph with a footnote.<sup id="fnref:1"><a class="footnote-ref" href="#fn:1">1</a></sup></p>\n'
1194 '</div>\n'
1195 '</div>\n'
1196 '<div class="footnote">\n'
1197 '<hr />\n'
1198 '<ol>\n'
1199 '<li id="fn:1">\n'
1200 '<p>The footnote.&#160;'
1201 '<a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text">&#8617;</a>'
1202 '</p>\n'
1203 '</li>\n'
1204 '</ol>\n'
1205 '</div>',
1206 extensions=['md_in_html', 'footnotes']
1207 )
1208
1209 def test_md1_code_void_tag(self):
1210
1211 # https://github.com/Python-Markdown/markdown/issues/1075
1212 self.assertMarkdownRenders(
1213 self.dedent(
1214 """
1215 <div class="outer" markdown="1">
1216
1217 Code: `<label><input/></label>`
1218
1219 </div>
1220
1221 <div class="outer" markdown="1">
1222
1223 HTML: <label><input/></label>
1224
1225 </div>
1226 """
1227 ),
1228 '<div class="outer">\n'
1229 '<p>Code: <code>&lt;label&gt;&lt;input/&gt;&lt;/label&gt;</code></p>\n'
1230 '</div>\n'
1231 '<div class="outer">\n'
1232 '<p>HTML: <label><input/></label></p>\n'
1233 '</div>',
1234 extensions=['md_in_html']
1235 )
1236
1237 def test_md1_code_void_tag_multiline(self):
1238
1239 # https://github.com/Python-Markdown/markdown/issues/1075
1240 self.assertMarkdownRenders(
1241 self.dedent(
1242 """
1243 <div class="outer" markdown="1">
1244
1245 Code: `
1246 <label>
1247 <input/>
1248 </label>
1249 `
1250
1251 </div>
1252
1253 <div class="outer" markdown="1">
1254
1255 HTML:
1256 <label>
1257 <input/>
1258 </label>
1259
1260 </div>
1261 """
1262 ),
1263 '<div class="outer">\n'
1264 '<p>Code: <code>&lt;label&gt;\n'
1265 '&lt;input/&gt;\n'
1266 '&lt;/label&gt;</code></p>\n'
1267 '</div>\n'
1268 '<div class="outer">\n'
1269 '<p>HTML:\n'
1270 '<label>\n'
1271 '<input/>\n'
1272 '</label></p>\n'
1273 '</div>',
1274 extensions=['md_in_html']
1275 )
1276
1277 def test_md1_oneliner_block(self):
1278 # https://github.com/Python-Markdown/markdown/issues/1074
1279 self.assertMarkdownRenders(
1280 self.dedent(
1281 '<div class="outer" markdown="block"><div class="inner" markdown="block">*foo*</div></div>'
1282 ),
1283 '<div class="outer">\n'
1284 '<div class="inner">\n'
1285 '<p><em>foo</em></p>\n'
1286 '</div>\n'
1287 '</div>',
1288 extensions=['md_in_html']
1289 )
1290
1291 def test_md1_oneliner_block_mixed(self):
1292 # https://github.com/Python-Markdown/markdown/issues/1074
1293 self.assertMarkdownRenders(
1294 self.dedent(
1295 """
1296 <div class="a" markdown="block"><div class="b" markdown="block">
1297
1298 <div class="c" markdown="block"><div class="d" markdown="block">
1299 *foo*
1300 </div></div>
1301
1302 </div></div>
1303 """
1304 ),
1305 '<div class="a">\n'
1306 '<div class="b">\n'
1307 '<div class="c">\n'
1308 '<div class="d">\n'
1309 '<p><em>foo</em></p>\n'
1310 '</div>\n'
1311 '</div>\n'
1312 '</div>\n'
1313 '</div>',
1314 extensions=['md_in_html']
1315 )
1316
1317 def test_md1_oneliner_block_tail(self):
1318 # https://github.com/Python-Markdown/markdown/issues/1074
1319 self.assertMarkdownRenders(
1320 self.dedent(
1321 """
1322 <div class="a" markdown="block"><div class="b" markdown="block">
1323 **foo**
1324 </div><div class="c" markdown="block"><div class="d" markdown="block">
1325 *bar*
1326 </div></div></div>
1327 """
1328 ),
1329 '<div class="a">\n'
1330 '<div class="b">\n'
1331 '<p><strong>foo</strong></p>\n'
1332 '</div>\n'
1333 '<div class="c">\n'
1334 '<div class="d">\n'
1335 '<p><em>bar</em></p>\n'
1336 '</div>\n'
1337 '</div>\n'
1338 '</div>',
1339 extensions=['md_in_html']
1340 )
1341
1342 def test_md1_oneliner_block_complex_start_tail(self):
1343 # https://github.com/Python-Markdown/markdown/issues/1074
1344 self.assertMarkdownRenders(
1345 '<div class="a" markdown><div class="b" markdown>**foo**</div>'
1346 '<div class="c" markdown>*bar*</div><div class="d">*not md*</div></div>',
1347 '<div class="a">\n'
1348 '<div class="b">\n'
1349 '<p><strong>foo</strong></p>\n'
1350 '</div>\n'
1351 '<div class="c">\n'
1352 '<p><em>bar</em></p>\n'
1353 '</div>\n'
1354 '<div class="d">*not md*</div>\n'
1355 '</div>',
1356 extensions=['md_in_html']
1357 )
1358
1359 def test_md1_oneliner_block_complex_fail(self):
1360 # https://github.com/Python-Markdown/markdown/issues/1074
1361 # Nested will fail because an inline tag is only considered at the beginning if it is not preceded by text.
1362 self.assertMarkdownRenders(
1363 '<div class="a" markdown>**strong**<div class="b" markdown>**strong**</div></div>',
1364 '<div class="a">\n'
1365 '<p><strong>strong</strong><div class="b" markdown><strong>strong</strong></p>\n'
1366 '</div>\n'
1367 '</div>',
1368 extensions=['md_in_html']
1369 )
1370
1371 def test_md1_oneliner_block_start(self):
1372 # https://github.com/Python-Markdown/markdown/issues/1074
1373 self.assertMarkdownRenders(
1374 self.dedent(
1375 """
1376 <div class="outer" markdown="block"><div class="inner" markdown="block">
1377 *foo*
1378 </div></div>
1379 """
1380 ),
1381 '<div class="outer">\n'
1382 '<div class="inner">\n'
1383 '<p><em>foo</em></p>\n'
1384 '</div>\n'
1385 '</div>',
1386 extensions=['md_in_html']
1387 )
1388
1389 def test_md1_oneliner_block_span(self):
1390 # https://github.com/Python-Markdown/markdown/issues/1074
1391 self.assertMarkdownRenders(
1392 self.dedent(
1393 '<div class="outer" markdown="block"><div class="inner" markdown="span">*foo*</div></div>'
1394 ),
1395 '<div class="outer">\n'
1396 '<div class="inner"><em>foo</em></div>\n'
1397 '</div>',
1398 extensions=['md_in_html']
1399 )
1400
1401 def test_md1_oneliner_block_span_start(self):
1402 # https://github.com/Python-Markdown/markdown/issues/1074
1403 self.assertMarkdownRenders(
1404 self.dedent(
1405 """
1406 <div class="outer" markdown="block"><div class="inner" markdown="span">
1407 *foo*
1408 </div></div>
1409 """
1410 ),
1411 '<div class="outer">\n'
1412 '<div class="inner">\n'
1413 '<em>foo</em>\n'
1414 '</div>\n'
1415 '</div>',
1416 extensions=['md_in_html']
1417 )
1418
1419 def test_md1_oneliner_span_block_start(self):
1420 # https://github.com/Python-Markdown/markdown/issues/1074
1421 self.assertMarkdownRenders(
1422 self.dedent(
1423 """
1424 <div class="outer" markdown="span"><div class="inner" markdown="block">
1425 *foo*
1426 </div>
1427 *foo*
1428 </div>
1429 """
1430 ),
1431 '<div class="outer">\n'
1432 '<div class="inner">\n'
1433 '<em>foo</em>\n'
1434 '</div>\n\n'
1435 '<em>foo</em></div>',
1436 extensions=['md_in_html']
1437 )
1438
1439 def test_md1_code_comment(self):
1440
1441 self.assertMarkdownRenders(
1442 self.dedent(
1443 """
1444 <div class="outer" markdown="1">
1445
1446 Code: `<label><!-- **comment** --></label>`
1447
1448 </div>
1449
1450 <div class="outer" markdown="1">
1451
1452 HTML: <label><!-- **comment** --></label>
1453
1454 </div>
1455 """
1456 ),
1457 '<div class="outer">\n'
1458 '<p>Code: <code>&lt;label&gt;&lt;!-- **comment** --&gt;&lt;/label&gt;</code></p>\n'
1459 '</div>\n'
1460 '<div class="outer">\n'
1461 '<p>HTML: <label><!-- **comment** --></label></p>\n'
1462 '</div>',
1463 extensions=['md_in_html']
1464 )
1465
1466 def test_md1_code_pi(self):
1467
1468 self.assertMarkdownRenders(
1469 self.dedent(
1470 """
1471 <div class="outer" markdown="1">
1472
1473 Code: `<label><?php # echo '**simple**';?></label>`
1474
1475 </div>
1476
1477 <div class="outer" markdown="1">
1478
1479 HTML: <label><?php # echo '**simple**';?></label>
1480
1481 </div>
1482 """
1483 ),
1484 '<div class="outer">\n'
1485 '<p>Code: <code>&lt;label&gt;&lt;?php # echo \'**simple**\';?&gt;&lt;/label&gt;</code></p>\n'
1486 '</div>\n'
1487 '<div class="outer">\n'
1488 '<p>HTML: <label><?php # echo \'**simple**\';?></label></p>\n'
1489 '</div>',
1490 extensions=['md_in_html']
1491 )
1492
1493 def test_md1_code_cdata(self):
1494
1495 self.assertMarkdownRenders(
1496 self.dedent(
1497 """
1498 <div class="outer" markdown="1">
1499
1500 Code: `<label><![CDATA[some stuff]]></label>`
1501
1502 </div>
1503
1504 <div class="outer" markdown="1">
1505
1506 HTML: <label><![CDATA[some stuff]]></label>
1507
1508 </div>
1509 """
1510 ),
1511 '<div class="outer">\n'
1512 '<p>Code: <code>&lt;label&gt;&lt;![CDATA[some stuff]]&gt;&lt;/label&gt;</code></p>\n'
1513 '</div>\n'
1514 '<div class="outer">\n'
1515 '<p>HTML: <label><![CDATA[some stuff]]></label></p>\n'
1516 '</div>',
1517 extensions=['md_in_html']
1518 )
1519
1520 def test_trailing_content_after_tag_in_md_block(self):
1521
1522 # It should be noted that this is not the way `md_in_html` is intended to be used.
1523 # What we are specifically testing is an edge case where content was previously lost.
1524 # Lost content should not happen.
1525 self.assertMarkdownRenders(
1526 self.dedent(
1527 """
1528 <div markdown>
1529 <div class="circle"></div>AAAAA<div class="circle"></div>
1530 </div>
1531 """
1532 ),
1533 '<div>\n'
1534 '<div class="circle"></div>\n'
1535 '<p>AAAAA<div class="circle"></p>\n'
1536 '</div>\n'
1537 '</div>',
1538 extensions=['md_in_html']
1539 )
1540
1541 def test_noname_tag(self):
1542 self.assertMarkdownRenders(
1543 self.dedent(
1544 """
1545 <div markdown>
1546 </>
1547 </div>
1548 """
1549 ),
1550 self.dedent(
1551 """
1552 <div>
1553 <p>&lt;/&gt;</p>
1554 </div>
1555 """
1556 )
1557 )
1558
1559
1560 def load_tests(loader, tests, pattern):
1561 """ Ensure `TestHTMLBlocks` doesn't get run twice by excluding it here. """
1562 suite = TestSuite()
1563 for test_class in [TestDefaultwMdInHTML, TestMdInHTML, TestMarkdownInHTMLPostProcessor]:
1564 tests = loader.loadTestsFromTestCase(test_class)
1565 suite.addTests(tests)
1566 return suite