Debian Codegraph @

tests/test_extensions/test_blocks/test_html.py

raw
1 """Test cases for Blocks (HTML)."""
2 from ... import util
3
4
5 class TestBlocksHTML(util.MdCase):
6 """Test Blocks HTML cases."""
7
8 extension = ['pymdownx.blocks.html', 'md_in_html']
9 extension_configs = {
10 'pymdownx.blocks.html': {
11 'custom': [
12 {'tag': 'custom', 'mode': 'block'}
13 ]
14 }
15 }
16
17
18 def test_raw_empty_block(self):
19 """Test that raw empty blocks are handled properly."""
20
21 self.check_markdown(
22 R'''
23 /// html | pre
24 ///
25 ''',
26 R'''
27 <pre></pre>
28 ''',
29 True
30 )
31
32
33 def test_bad_tag(self):
34 """Test bad HTML tag."""
35
36 self.check_markdown(
37 R'''
38 /// html | 3tag
39 Some *content*
40 ///
41 ''',
42 R'''
43 <p>/// html | 3tag
44 Some <em>content</em>
45 ///</p>
46 ''',
47 True
48 )
49
50 def test_required_tag(self):
51 """Test that tab is not processed if tag is omitted."""
52
53 self.check_markdown(
54 R'''
55 /// html
56 Some *content*
57 ///
58 ''',
59 r'''
60 <p>/// html
61 Some <em>content</em>
62 ///</p>
63 ''',
64 True
65 )
66
67 def test_html_block(self):
68 """Test HTML block element."""
69
70 self.check_markdown(
71 R'''
72 /// html | div
73 Some *content*
74
75 And more `content`.
76 ///
77 ''',
78 r'''
79 <div>
80 <p>Some <em>content</em></p>
81 <p>And more <code>content</code>.</p>
82 </div>
83 ''',
84 True
85 )
86
87 def test_html_span(self):
88 """Test HTML with span element."""
89
90 self.check_markdown(
91 R'''
92 /// html | span
93 Will be parsed as inline *content*
94
95 And more `content`.
96 ///
97 ''',
98 r'''
99 <span>Will be parsed as inline <em>content</em>
100
101 And more <code>content</code>.</span>
102 ''',
103 True
104 )
105
106 def test_html_raw_element(self):
107 """Test HTML raw element."""
108
109 self.check_markdown(
110 R'''
111 /// html | pre
112 Some *content*
113
114 And more `content`.
115 ///
116 ''',
117 r'''
118 <pre>Some *content*
119
120 And more `content`.</pre>
121 ''',
122 True
123 )
124
125 def test_html_forced_raw_element(self):
126 """Test HTML force raw element."""
127
128 self.check_markdown(
129 R'''
130 /// html | div
131 markdown: raw
132
133 Some *content*
134
135 And more `content`.
136 ///
137 ''',
138 r'''
139 <div>Some *content*
140
141 And more `content`.</div>
142 ''',
143 True
144 )
145
146 def test_html_force_span(self):
147 """Test HTML with force span element."""
148
149 self.check_markdown(
150 R'''
151 /// html | div
152 markdown: inline
153
154 Will be parsed as inline *content*
155
156 And more `content`.
157 ///
158 ''',
159 r'''
160 <div>Will be parsed as inline <em>content</em>
161
162 And more <code>content</code>.</div>
163 ''',
164 True
165 )
166
167 def test_html_force_block(self):
168 """Test HTML force block element."""
169
170 self.check_markdown(
171 R'''
172 /// html | span
173 markdown: block
174
175 Some *content*
176
177 And more `content`.
178 ///
179 ''',
180 r'''
181 <span><p>Some <em>content</em></p><p>And more <code>content</code>.</p></span>
182 ''',
183 True
184 )
185
186 def test_attributes(self):
187 """Test attributes."""
188
189 self.check_markdown(
190 R'''
191 /// html | div.some.classes#an-id[name1 name2=value name3="string value"]
192 Some *content*
193
194 And more `content`.
195 ///
196 ''',
197 r'''
198 <div class="classes some" id="an-id" name1="name1" name2="value" name3="string value">
199 <p>Some <em>content</em></p>
200 <p>And more <code>content</code>.</p>
201 </div>
202 ''',
203 True
204 )
205
206 def test_bad_attributes(self):
207 """Test no attributes."""
208
209 self.check_markdown(
210 R'''
211 /// html | div.+
212 content
213 ///
214 ''',
215 '''
216 <p>/// html | div.+
217 content
218 ///</p>
219 ''',
220 True
221 )
222
223 def test_multi_class(self):
224 """Test multiple classes."""
225
226 self.check_markdown(
227 R'''
228 /// html | div.a.b[class=c]
229 content
230 ///
231 ''',
232 '''
233 <div class="a b c">
234 <p>content</p>
235 </div>
236 ''',
237 True
238 )
239
240 def test_multi_class2(self):
241 """Test multiple classes."""
242
243 self.check_markdown(
244 R'''
245 /// html | div[class="a b"].c
246 content
247 ///
248 ''',
249 '''
250 <div class="a b c">
251 <p>content</p>
252 </div>
253 ''',
254 True
255 )
256
257 def test_inline_and_md_in_html(self):
258 """Test inline format and HTML content."""
259
260 self.check_markdown(
261 R'''
262 /// html | div
263 markdown: inline
264
265 <div markdown="block">
266 **content**
267 </div>
268
269 **content**
270 ///
271 ''',
272 '''
273 <div><div markdown="block">
274 **content**
275 </div>
276
277 <strong>content</strong></div>
278 ''',
279 True
280 )
281
282 def test_raw_and_md_in_html(self):
283 """Test raw format and HTML content."""
284
285 self.check_markdown(
286 R'''
287 /// html | div
288 markdown: raw
289
290 <div>
291 **content**
292 </div>
293
294 this is <span>raw</span> **content**
295 ///
296 ''',
297 '''
298 <div>&lt;div&gt;
299 **content**
300 &lt;/div&gt;
301
302 this is &lt;span&gt;raw&lt;/span&gt; **content**</div>
303 ''',
304 True
305 )
306
307 def test_html_and_html(self):
308 """Test HTML mode format with HTML code."""
309
310 self.check_markdown(
311 R'''
312 /// html | div
313 markdown: html
314
315 <div>
316 **content**
317 </div>
318
319 this is <span>raw</span> **content**
320 ///
321 ''',
322 '''
323 <div><div>
324 **content**
325 </div>
326
327 this is <span>raw</span> **content**</div>
328 ''',
329 True
330 )
331
332 def test_html_and_script(self):
333 """Test inline format with script."""
334
335 self.check_markdown(
336 R'''
337 /// html | script
338
339 const el = document.querySelector('div');
340 el.innerHTML = '<span>test</span>
341 ///
342 ''',
343 '''
344 <script>const el = document.querySelector('div');
345 el.innerHTML = '<span>test</span></script>
346 ''',
347 True
348 )
349
350 def test_custom(self):
351 """Test custom block handling."""
352
353 self.check_markdown(
354 R'''
355 /// html | custom
356 - a
357 - b
358 ///
359 ''',
360 '''
361 <custom><ul><li>a</li><li>b</li></ul></custom>
362 ''',
363 True
364 )
365
366 def test_custom_override(self):
367 """Test custom block handling but mode is overridden."""
368
369 self.check_markdown(
370 R'''
371 /// html | custom
372 markdown: inline
373
374 - a
375 - b
376 ///
377 ''',
378 '''
379 <custom>- a
380 - b</custom>
381 ''',
382 True
383 )