Debian Codegraph @

tests/test_syntax/inline/test_links.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-2019 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
24
25 class TestInlineLinks(TestCase):
26
27 def test_nested_square_brackets(self):
28 self.assertMarkdownRenders(
29 """[Text[[[[[[[]]]]]]][]](http://link.com) more text""",
30 """<p><a href="http://link.com">Text[[[[[[[]]]]]]][]</a> more text</p>"""
31 )
32
33 def test_nested_round_brackets(self):
34 self.assertMarkdownRenders(
35 """[Text](http://link.com/(((((((()))))))())) more text""",
36 """<p><a href="http://link.com/(((((((()))))))())">Text</a> more text</p>"""
37 )
38
39 def test_nested_escaped_brackets(self):
40 self.assertMarkdownRenders(
41 R"""[Text](/url\(test\) "title").""",
42 """<p><a href="/url(test)" title="title">Text</a>.</p>"""
43 )
44
45 def test_nested_escaped_brackets_and_angles(self):
46 self.assertMarkdownRenders(
47 R"""[Text](</url\(test\)> "title").""",
48 """<p><a href="/url(test)" title="title">Text</a>.</p>"""
49 )
50
51 def test_nested_unescaped_brackets(self):
52 self.assertMarkdownRenders(
53 R"""[Text](/url(test) "title").""",
54 """<p><a href="/url(test)" title="title">Text</a>.</p>"""
55 )
56
57 def test_nested_unescaped_brackets_and_angles(self):
58 self.assertMarkdownRenders(
59 R"""[Text](</url(test)> "title").""",
60 """<p><a href="/url(test)" title="title">Text</a>.</p>"""
61 )
62
63 def test_uneven_brackets_with_titles1(self):
64 self.assertMarkdownRenders(
65 """[Text](http://link.com/("title") more text""",
66 """<p><a href="http://link.com/(" title="title">Text</a> more text</p>"""
67 )
68
69 def test_uneven_brackets_with_titles2(self):
70 self.assertMarkdownRenders(
71 """[Text](http://link.com/('"title") more text""",
72 """<p><a href="http://link.com/('" title="title">Text</a> more text</p>"""
73 )
74
75 def test_uneven_brackets_with_titles3(self):
76 self.assertMarkdownRenders(
77 """[Text](http://link.com/("title)") more text""",
78 """<p><a href="http://link.com/(" title="title)">Text</a> more text</p>"""
79 )
80
81 def test_uneven_brackets_with_titles4(self):
82 self.assertMarkdownRenders(
83 """[Text](http://link.com/( "title") more text""",
84 """<p><a href="http://link.com/(" title="title">Text</a> more text</p>"""
85 )
86
87 def test_uneven_brackets_with_titles5(self):
88 self.assertMarkdownRenders(
89 """[Text](http://link.com/( "title)") more text""",
90 """<p><a href="http://link.com/(" title="title)">Text</a> more text</p>"""
91 )
92
93 def test_mixed_title_quotes1(self):
94 self.assertMarkdownRenders(
95 """[Text](http://link.com/'"title") more text""",
96 """<p><a href="http://link.com/'" title="title">Text</a> more text</p>"""
97 )
98
99 def test_mixed_title_quotes2(self):
100 self.assertMarkdownRenders(
101 """[Text](http://link.com/"'title') more text""",
102 """<p><a href="http://link.com/&quot;" title="title">Text</a> more text</p>"""
103 )
104
105 def test_mixed_title_quotes3(self):
106 self.assertMarkdownRenders(
107 """[Text](http://link.com/with spaces'"and quotes" 'and title') more text""",
108 """<p><a href="http://link.com/with spaces" title="&quot;and quotes&quot; 'and title">"""
109 """Text</a> more text</p>"""
110 )
111
112 def test_mixed_title_quotes4(self):
113 self.assertMarkdownRenders(
114 """[Text](http://link.com/with spaces'"and quotes" 'and title") more text""",
115 """<p><a href="http://link.com/with spaces'" title="and quotes&quot; 'and title">Text</a> more text</p>"""
116 )
117
118 def test_mixed_title_quotes5(self):
119 self.assertMarkdownRenders(
120 """[Text](http://link.com/with spaces '"and quotes" 'and title') more text""",
121 """<p><a href="http://link.com/with spaces" title="&quot;and quotes&quot; 'and title">"""
122 """Text</a> more text</p>"""
123 )
124
125 def test_mixed_title_quotes6(self):
126 self.assertMarkdownRenders(
127 """[Text](http://link.com/with spaces "and quotes" 'and title') more text""",
128 """<p><a href="http://link.com/with spaces &quot;and quotes&quot;" title="and title">"""
129 """Text</a> more text</p>"""
130 )
131
132 def test_single_quote(self):
133 self.assertMarkdownRenders(
134 """[test](link"notitle)""",
135 """<p><a href="link&quot;notitle">test</a></p>"""
136 )
137
138 def test_angle_with_mixed_title_quotes(self):
139 self.assertMarkdownRenders(
140 """[Text](<http://link.com/with spaces '"and quotes"> 'and title') more text""",
141 """<p><a href="http://link.com/with spaces '&quot;and quotes&quot;" title="and title">"""
142 """Text</a> more text</p>"""
143 )
144
145 def test_amp_in_url(self):
146 """Test amp in URLs."""
147
148 self.assertMarkdownRenders(
149 '[link](http://www.freewisdom.org/this&that)',
150 '<p><a href="http://www.freewisdom.org/this&amp;that">link</a></p>'
151 )
152 self.assertMarkdownRenders(
153 '[title](http://example.com/?a=1&amp;b=2)',
154 '<p><a href="http://example.com/?a=1&amp;b=2">title</a></p>'
155 )
156 self.assertMarkdownRenders(
157 '[title](http://example.com/?a=1&#x26;b=2)',
158 '<p><a href="http://example.com/?a=1&#x26;b=2">title</a></p>'
159 )
160
161 def test_angles_and_nonsense_url(self):
162 self.assertMarkdownRenders(
163 '[test nonsense](<?}]*+|&)>).',
164 '<p><a href="?}]*+|&amp;)">test nonsense</a>.</p>'
165 )
166
167
168 class TestReferenceLinks(TestCase):
169
170 def test_ref_link(self):
171 self.assertMarkdownRenders(
172 self.dedent(
173 """
174 [Text]
175
176 [Text]: http://example.com
177 """
178 ),
179 """<p><a href="http://example.com">Text</a></p>"""
180 )
181
182 def test_ref_link_angle_brackets(self):
183 self.assertMarkdownRenders(
184 self.dedent(
185 """
186 [Text]
187
188 [Text]: <http://example.com>
189 """
190 ),
191 """<p><a href="http://example.com">Text</a></p>"""
192 )
193
194 def test_ref_link_no_space(self):
195 self.assertMarkdownRenders(
196 self.dedent(
197 """
198 [Text]
199
200 [Text]:http://example.com
201 """
202 ),
203 """<p><a href="http://example.com">Text</a></p>"""
204 )
205
206 def test_ref_link_angle_brackets_no_space(self):
207 self.assertMarkdownRenders(
208 self.dedent(
209 """
210 [Text]
211
212 [Text]:<http://example.com>
213 """
214 ),
215 """<p><a href="http://example.com">Text</a></p>"""
216 )
217
218 def test_ref_link_angle_brackets_title(self):
219 self.assertMarkdownRenders(
220 self.dedent(
221 """
222 [Text]
223
224 [Text]: <http://example.com> "title"
225 """
226 ),
227 """<p><a href="http://example.com" title="title">Text</a></p>"""
228 )
229
230 def test_ref_link_title(self):
231 self.assertMarkdownRenders(
232 self.dedent(
233 """
234 [Text]
235
236 [Text]: http://example.com "title"
237 """
238 ),
239 """<p><a href="http://example.com" title="title">Text</a></p>"""
240 )
241
242 def test_ref_link_angle_brackets_title_no_space(self):
243 # TODO: Maybe reevaluate this?
244 self.assertMarkdownRenders(
245 self.dedent(
246 """
247 [Text]
248
249 [Text]: <http://example.com>"title"
250 """
251 ),
252 """<p><a href="http://example.com&gt;&quot;title&quot;">Text</a></p>"""
253 )
254
255 def test_ref_link_title_no_space(self):
256 self.assertMarkdownRenders(
257 self.dedent(
258 """
259 [Text]
260
261 [Text]: http://example.com"title"
262 """
263 ),
264 """<p><a href="http://example.com&quot;title&quot;">Text</a></p>"""
265 )
266
267 def test_ref_link_single_quoted_title(self):
268 self.assertMarkdownRenders(
269 self.dedent(
270 """
271 [Text]
272
273 [Text]: http://example.com 'title'
274 """
275 ),
276 """<p><a href="http://example.com" title="title">Text</a></p>"""
277 )
278
279 def test_ref_link_title_nested_quote(self):
280 self.assertMarkdownRenders(
281 self.dedent(
282 """
283 [Text]
284
285 [Text]: http://example.com "title'"
286 """
287 ),
288 """<p><a href="http://example.com" title="title'">Text</a></p>"""
289 )
290
291 def test_ref_link_single_quoted_title_nested_quote(self):
292 self.assertMarkdownRenders(
293 self.dedent(
294 """
295 [Text]
296
297 [Text]: http://example.com 'title"'
298 """
299 ),
300 """<p><a href="http://example.com" title="title&quot;">Text</a></p>"""
301 )
302
303 def test_ref_link_override(self):
304 self.assertMarkdownRenders(
305 self.dedent(
306 """
307 [Text]
308
309 [Text]: http://example.com 'ignore'
310 [Text]: https://example.com 'override'
311 """
312 ),
313 """<p><a href="https://example.com" title="override">Text</a></p>"""
314 )
315
316 def test_ref_link_title_no_blank_lines(self):
317 self.assertMarkdownRenders(
318 self.dedent(
319 """
320 [Text]
321 [Text]: http://example.com "title"
322 [Text]
323 """
324 ),
325 self.dedent(
326 """
327 <p><a href="http://example.com" title="title">Text</a></p>
328 <p><a href="http://example.com" title="title">Text</a></p>
329 """
330 )
331 )
332
333 def test_ref_link_multi_line(self):
334 self.assertMarkdownRenders(
335 self.dedent(
336 """
337 [Text]
338
339 [Text]:
340 http://example.com
341 "title"
342 """
343 ),
344 """<p><a href="http://example.com" title="title">Text</a></p>"""
345 )
346
347 def test_reference_newlines(self):
348 """Test reference id whitespace cleanup."""
349
350 self.assertMarkdownRenders(
351 self.dedent(
352 """
353 Two things:
354
355 - I would like to tell you about the [code of
356 conduct][] we are using in this project.
357 - Only one in fact.
358
359 [code of conduct]: https://github.com/Python-Markdown/markdown/blob/master/CODE_OF_CONDUCT.md
360 """
361 ),
362 '<p>Two things:</p>\n<ul>\n<li>I would like to tell you about the '
363 '<a href="https://github.com/Python-Markdown/markdown/blob/master/CODE_OF_CONDUCT.md">code of\n'
364 ' conduct</a> we are using in this project.</li>\n<li>Only one in fact.</li>\n</ul>'
365 )
366
367 def test_reference_across_blocks(self):
368 """Test references across blocks."""
369
370 self.assertMarkdownRenders(
371 self.dedent(
372 """
373 I would like to tell you about the [code of
374
375 conduct][] we are using in this project.
376
377 [code of conduct]: https://github.com/Python-Markdown/markdown/blob/master/CODE_OF_CONDUCT.md
378 """
379 ),
380 '<p>I would like to tell you about the [code of</p>\n'
381 '<p>conduct][] we are using in this project.</p>'
382 )
383
384 def test_ref_link_nested_left_bracket(self):
385 self.assertMarkdownRenders(
386 self.dedent(
387 """
388 [Text[]
389
390 [Text[]: http://example.com
391 """
392 ),
393 self.dedent(
394 """
395 <p>[Text[]</p>
396 <p>[Text[]: http://example.com</p>
397 """
398 )
399 )
400
401 def test_ref_link_nested_right_bracket(self):
402 self.assertMarkdownRenders(
403 self.dedent(
404 """
405 [Text]]
406
407 [Text]]: http://example.com
408 """
409 ),
410 self.dedent(
411 """
412 <p>[Text]]</p>
413 <p>[Text]]: http://example.com</p>
414 """
415 )
416 )
417
418 def test_ref_round_brackets(self):
419 self.assertMarkdownRenders(
420 self.dedent(
421 """
422 [Text][1].
423
424 [Text][2].
425
426 [1]: /url(test) "title"
427 [2]: </url(test)> "title"
428 """
429 ),
430 self.dedent(
431 """
432 <p><a href="/url(test)" title="title">Text</a>.</p>
433 <p><a href="/url(test)" title="title">Text</a>.</p>
434 """
435 )
436 )