1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
extern crate micromark;
use micromark::{micromark, micromark_with_options, Options};
const DANGER: &Options = &Options {
allow_dangerous_html: true,
allow_dangerous_protocol: true,
default_line_ending: None,
};
#[test]
fn link_resource() {
assert_eq!(
micromark("[link](/uri \"title\")"),
"<p><a href=\"/uri\" title=\"title\">link</a></p>",
"should support links"
);
assert_eq!(
micromark("[link](/uri)"),
"<p><a href=\"/uri\">link</a></p>",
"should support links w/o title"
);
assert_eq!(
micromark("[link]()"),
"<p><a href=\"\">link</a></p>",
"should support links w/o destination"
);
assert_eq!(
micromark("[link](<>)"),
"<p><a href=\"\">link</a></p>",
"should support links w/ empty enclosed destination"
);
assert_eq!(
micromark("[link](/my uri)"),
"<p>[link](/my uri)</p>",
"should not support links w/ spaces in destination"
);
assert_eq!(
micromark("[link](</my uri>)"),
"<p><a href=\"/my%20uri\">link</a></p>",
"should support links w/ spaces in enclosed destination"
);
assert_eq!(
micromark("[link](foo\nbar)"),
"<p>[link](foo\nbar)</p>",
"should not support links w/ line endings in destination"
);
assert_eq!(
micromark_with_options("[link](<foo\nbar>)", DANGER),
"<p>[link](<foo\nbar>)</p>",
"should not support links w/ line endings in enclosed destination"
);
assert_eq!(
micromark("[a](<b)c>)"),
"<p><a href=\"b)c\">a</a></p>",
"should support links w/ closing parens in destination"
);
assert_eq!(
micromark("[link](<foo\\>)"),
"<p>[link](<foo>)</p>",
"should not support links w/ enclosed destinations w/o end"
);
assert_eq!(
micromark_with_options("[a](<b)c\n[a](<b)c>\n[a](<b>c)", DANGER),
"<p>[a](<b)c\n[a](<b)c>\n[a](<b>c)</p>",
"should not support links w/ unmatched enclosed destinations"
);
assert_eq!(
micromark("[link](\\(foo\\))"),
"<p><a href=\"(foo)\">link</a></p>",
"should support links w/ destinations w/ escaped parens"
);
assert_eq!(
micromark("[link](foo(and(bar)))"),
"<p><a href=\"foo(and(bar))\">link</a></p>",
"should support links w/ destinations w/ balanced parens"
);
assert_eq!(
micromark("[link](foo\\(and\\(bar\\))"),
"<p><a href=\"foo(and(bar)\">link</a></p>",
"should support links w/ destinations w/ escaped parens"
);
assert_eq!(
micromark("[link](<foo(and(bar)>)"),
"<p><a href=\"foo(and(bar)\">link</a></p>",
"should support links w/ enclosed destinations w/ parens"
);
assert_eq!(
micromark_with_options("[link](foo\\)\\:)", DANGER),
"<p><a href=\"foo):\">link</a></p>",
"should support links w/ escapes in destinations"
);
assert_eq!(
micromark("[link](#fragment)"),
"<p><a href=\"#fragment\">link</a></p>",
"should support links w/ destinations to fragments"
);
assert_eq!(
micromark("[link](http://example.com#fragment)"),
"<p><a href=\"http://example.com#fragment\">link</a></p>",
"should support links w/ destinations to URLs w/ fragments"
);
assert_eq!(
micromark("[link](http://example.com?foo=3#frag)"),
"<p><a href=\"http://example.com?foo=3#frag\">link</a></p>",
"should support links w/ destinations to URLs w/ search and fragments"
);
assert_eq!(
micromark("[link](foo\\bar)"),
"<p><a href=\"foo%5Cbar\">link</a></p>",
"should not support non-punctuation character escapes in links"
);
assert_eq!(
micromark("[link](foo%20bä)"),
"<p><a href=\"foo%20b%C3%A4\">link</a></p>",
"should support character references in links"
);
assert_eq!(
micromark("[link](\"title\")"),
"<p><a href=\"%22title%22\">link</a></p>",
"should not support links w/ only a title"
);
assert_eq!(
micromark("[link](/url \"title\")"),
"<p><a href=\"/url\" title=\"title\">link</a></p>",
"should support titles w/ double quotes"
);
assert_eq!(
micromark("[link](/url 'title')"),
"<p><a href=\"/url\" title=\"title\">link</a></p>",
"should support titles w/ single quotes"
);
assert_eq!(
micromark("[link](/url (title))"),
"<p><a href=\"/url\" title=\"title\">link</a></p>",
"should support titles w/ parens"
);
assert_eq!(
micromark("[link](/url \"title \\\""\")"),
"<p><a href=\"/url\" title=\"title ""\">link</a></p>",
"should support character references and escapes in titles"
);
assert_eq!(
micromark("[link](/url \"title\")"),
"<p><a href=\"/url%C2%A0%22title%22\">link</a></p>",
"should not support unicode whitespace between destination and title"
);
assert_eq!(
micromark("[link](/url \"title \"and\" title\")"),
"<p>[link](/url "title "and" title")</p>",
"should not support nested balanced quotes in title"
);
assert_eq!(
micromark("[link](/url 'title \"and\" title')"),
"<p><a href=\"/url\" title=\"title "and" title\">link</a></p>",
"should support the other quotes in titles"
);
assert_eq!(
micromark("[link]( /uri\n \"title\" )"),
"<p><a href=\"/uri\" title=\"title\">link</a></p>",
"should support whitespace around destination and title (1)"
);
assert_eq!(
micromark("[link](\t\n/uri \"title\")"),
"<p><a href=\"/uri\" title=\"title\">link</a></p>",
"should support whitespace around destination and title (2)"
);
assert_eq!(
micromark("[link](/uri \"title\"\t\n)"),
"<p><a href=\"/uri\" title=\"title\">link</a></p>",
"should support whitespace around destination and title (3)"
);
assert_eq!(
micromark("[link] (/uri)"),
"<p>[link] (/uri)</p>",
"should not support whitespace between label and resource"
);
assert_eq!(
micromark("[link [foo [bar]]](/uri)"),
"<p><a href=\"/uri\">link [foo [bar]]</a></p>",
"should support balanced brackets"
);
assert_eq!(
micromark("[link] bar](/uri)"),
"<p>[link] bar](/uri)</p>",
"should not support unbalanced brackets (1)"
);
assert_eq!(
micromark("[link [bar](/uri)"),
"<p>[link <a href=\"/uri\">bar</a></p>",
"should not support unbalanced brackets (2)"
);
assert_eq!(
micromark("[link \\[bar](/uri)"),
"<p><a href=\"/uri\">link [bar</a></p>",
"should support characer escapes"
);
// To do: link/attention interplay.
// assert_eq!(
// micromark("[link *foo **bar** `#`*](/uri)"),
// "<p><a href=\"/uri\">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>",
// "should support content"
// );
assert_eq!(
micromark("[![moon](moon.jpg)](/uri)"),
"<p><a href=\"/uri\"><img src=\"moon.jpg\" alt=\"moon\" /></a></p>",
"should support an image as content"
);
assert_eq!(
micromark("[foo [bar](/uri)](/uri)"),
"<p>[foo <a href=\"/uri\">bar</a>](/uri)</p>",
"should not support links in links (1)"
);
assert_eq!(
micromark("[foo *[bar [baz](/uri)](/uri)*](/uri)"),
"<p>[foo <em>[bar <a href=\"/uri\">baz</a>](/uri)</em>](/uri)</p>",
"should not support links in links (2)"
);
assert_eq!(
micromark("![[[foo](uri1)](uri2)](uri3)"),
"<p><img src=\"uri3\" alt=\"[foo](uri2)\" /></p>",
"should not support links in links (3)"
);
// To do: link/attention interplay.
// assert_eq!(
// micromark("*[foo*](/uri)"),
// "<p>*<a href=\"/uri\">foo*</a></p>",
// "should prefer links over emphasis (1)"
// );
assert_eq!(
micromark("[foo *bar](baz*)"),
"<p><a href=\"baz*\">foo *bar</a></p>",
"should prefer links over emphasis (2)"
);
assert_eq!(
micromark_with_options("[foo <bar attr=\"](baz)\">", DANGER),
"<p>[foo <bar attr=\"](baz)\"></p>",
"should prefer HTML over links"
);
assert_eq!(
micromark("[foo`](/uri)`"),
"<p>[foo<code>](/uri)</code></p>",
"should prefer code over links"
);
assert_eq!(
micromark("[foo<http://example.com/?search=](uri)>"),
"<p>[foo<a href=\"http://example.com/?search=%5D(uri)\">http://example.com/?search=](uri)</a></p>",
"should prefer autolinks over links"
);
assert_eq!(
micromark("[foo<http://example.com/?search=](uri)>"),
"<p>[foo<a href=\"http://example.com/?search=%5D(uri)\">http://example.com/?search=](uri)</a></p>",
"should prefer autolinks over links"
);
// Extra
assert_eq!(
micromark("[]()"),
"<p><a href=\"\"></a></p>",
"should support an empty link"
);
// See: <https://github.com/commonmark/commonmark.js/issues/192>
assert_eq!(
micromark("[](<> \"\")"),
"<p><a href=\"\"></a></p>",
"should ignore an empty title"
);
assert_eq!(
micromark_with_options("[a](<b>\"c\")", DANGER),
"<p>[a](<b>"c")</p>",
"should require whitespace between enclosed destination and title"
);
assert_eq!(
micromark("[](<"),
"<p>[](<</p>",
"should not support an unclosed enclosed destination"
);
assert_eq!(
micromark("[]("),
"<p>[](</p>",
"should not support an unclosed destination"
);
assert_eq!(
micromark("[](\\<)"),
"<p><a href=\"%3C\"></a></p>",
"should support unenclosed link destination starting w/ escapes"
);
assert_eq!(
micromark("[](<\\<>)"),
"<p><a href=\"%3C\"></a></p>",
"should support enclosed link destination starting w/ escapes"
);
assert_eq!(
micromark("[](\\"),
"<p>[](\\</p>",
"should not support unenclosed link destination starting w/ an incorrect escape"
);
assert_eq!(
micromark("[](<\\"),
"<p>[](<\\</p>",
"should not support enclosed link destination starting w/ an incorrect escape"
);
assert_eq!(
micromark("[](a \""),
"<p>[](a "</p>",
"should not support an eof in a link title (1)"
);
assert_eq!(
micromark("[](a '"),
"<p>[](a '</p>",
"should not support an eof in a link title (2)"
);
assert_eq!(
micromark("[](a ("),
"<p>[](a (</p>",
"should not support an eof in a link title (3)"
);
assert_eq!(
micromark("[](a \"\\"),
"<p>[](a "\\</p>",
"should not support an eof in a link title escape (1)"
);
assert_eq!(
micromark("[](a '\\"),
"<p>[](a '\\</p>",
"should not support an eof in a link title escape (2)"
);
assert_eq!(
micromark("[](a (\\"),
"<p>[](a (\\</p>",
"should not support an eof in a link title escape (3)"
);
assert_eq!(
micromark("[](a \"\\\"\")"),
"<p><a href=\"a\" title=\""\"></a></p>",
"should support a character escape to start a link title (1)"
);
assert_eq!(
micromark("[](a '\\'')"),
"<p><a href=\"a\" title=\"\'\"></a></p>",
"should support a character escape to start a link title (2)"
);
assert_eq!(
micromark("[](a (\\)))"),
"<p><a href=\"a\" title=\")\"></a></p>",
"should support a character escape to start a link title (3)"
);
assert_eq!(
micromark("[&©&](example.com/&©& \"&©&\")"),
"<p><a href=\"example.com/&%C2%A9&\" title=\"&©&\">&©&</a></p>",
"should support character references in links"
);
assert_eq!(
micromark("[a](1())"),
"<p><a href=\"1()\">a</a></p>",
"should support 1 set of parens"
);
assert_eq!(
micromark("[a](1(2()))"),
"<p><a href=\"1(2())\">a</a></p>",
"should support 2 sets of parens"
);
assert_eq!(
micromark(
"[a](1(2(3(4(5(6(7(8(9(10(11(12(13(14(15(16(17(18(19(20(21(22(23(24(25(26(27(28(29(30(31(32()))))))))))))))))))))))))))))))))"
),
"<p><a href=\"1(2(3(4(5(6(7(8(9(10(11(12(13(14(15(16(17(18(19(20(21(22(23(24(25(26(27(28(29(30(31(32())))))))))))))))))))))))))))))))\">a</a></p>",
"should support 32 sets of parens"
);
assert_eq!(
micromark(
"[a](1(2(3(4(5(6(7(8(9(10(11(12(13(14(15(16(17(18(19(20(21(22(23(24(25(26(27(28(29(30(31(32(33())))))))))))))))))))))))))))))))))"
),
"<p>[a](1(2(3(4(5(6(7(8(9(10(11(12(13(14(15(16(17(18(19(20(21(22(23(24(25(26(27(28(29(30(31(32(33())))))))))))))))))))))))))))))))))</p>",
"should not support 33 or more sets of parens"
);
assert_eq!(
micromark("[a](b \"\n c\")"),
"<p><a href=\"b\" title=\"\nc\">a</a></p>",
"should support an eol at the start of a title"
);
assert_eq!(
micromark("[a](b( \"c\")"),
"<p>[a](b( "c")</p>",
"should not support whitespace when unbalanced in a raw destination"
);
assert_eq!(
micromark("[a](\0)"),
"<p><a href=\"%EF%BF%BD\">a</a></p>",
"should support a single NUL character as a link resource"
);
}
|