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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
|
use markdown::{
mdast::{Definition, Node, Root},
to_html, to_html_with_options, to_mdast,
unist::Position,
CompileOptions, Constructs, Options, ParseOptions,
};
use pretty_assertions::assert_eq;
#[test]
fn definition() -> Result<(), String> {
let danger = Options {
compile: CompileOptions {
allow_dangerous_html: true,
allow_dangerous_protocol: true,
..Default::default()
},
..Default::default()
};
assert_eq!(
to_html("[foo]: /url \"title\"\n\n[foo]"),
"<p><a href=\"/url\" title=\"title\">foo</a></p>",
"should support link definitions"
);
assert_eq!(
to_html("[foo]:\n\n/url\n\n[foo]"),
"<p>[foo]:</p>\n<p>/url</p>\n<p>[foo]</p>",
"should not support blank lines before destination"
);
assert_eq!(
to_html(" [foo]: \n /url \n 'the title' \n\n[foo]"),
"<p><a href=\"/url\" title=\"the title\">foo</a></p>",
"should support whitespace and line endings in definitions"
);
assert_eq!(
to_html("[a]:b 'c'\n\n[a]"),
"<p><a href=\"b\" title=\"c\">a</a></p>",
"should support no whitespace after `:` in definitions"
);
assert_eq!(
to_html("[Foo*bar\\]]:my_(url) 'title (with parens)'\n\n[Foo*bar\\]]"),
"<p><a href=\"my_(url)\" title=\"title (with parens)\">Foo*bar]</a></p>",
"should support complex definitions (1)"
);
assert_eq!(
to_html("[Foo bar]:\n<my url>\n'title'\n\n[Foo bar]"),
"<p><a href=\"my%20url\" title=\"title\">Foo bar</a></p>",
"should support complex definitions (2)"
);
assert_eq!(
to_html("[foo]: /url '\ntitle\nline1\nline2\n'\n\n[foo]"),
"<p><a href=\"/url\" title=\"\ntitle\nline1\nline2\n\">foo</a></p>",
"should support line endings in titles"
);
assert_eq!(
to_html("[foo]: /url 'title\n\nwith blank line'\n\n[foo]"),
"<p>[foo]: /url 'title</p>\n<p>with blank line'</p>\n<p>[foo]</p>",
"should not support blank lines in titles"
);
assert_eq!(
to_html("[foo]:\n/url\n\n[foo]"),
"<p><a href=\"/url\">foo</a></p>",
"should support definitions w/o title"
);
assert_eq!(
to_html("[foo]:\n\n[foo]"),
"<p>[foo]:</p>\n<p>[foo]</p>",
"should not support definitions w/o destination"
);
assert_eq!(
to_html("[foo]: <>\n\n[foo]"),
"<p><a href=\"\">foo</a></p>",
"should support definitions w/ explicit empty destinations"
);
assert_eq!(
to_html_with_options("[foo]: <bar>(baz)\n\n[foo]", &danger)?,
"<p>[foo]: <bar>(baz)</p>\n<p>[foo]</p>",
"should not support definitions w/ no whitespace between destination and title"
);
assert_eq!(
to_html("[foo]: /url\\bar\\*baz \"foo\\\"bar\\baz\"\n\n[foo]"),
"<p><a href=\"/url%5Cbar*baz\" title=\"foo"bar\\baz\">foo</a></p>",
"should support character escapes in destinations and titles"
);
assert_eq!(
to_html("[foo]\n\n[foo]: url"),
"<p><a href=\"url\">foo</a></p>\n",
"should support a link before a definition"
);
assert_eq!(
to_html("[foo]: first\n[foo]: second\n\n[foo]"),
"<p><a href=\"first\">foo</a></p>",
"should match w/ the first definition"
);
assert_eq!(
to_html("[FOO]: /url\n\n[Foo]"),
"<p><a href=\"/url\">Foo</a></p>",
"should match w/ case-insensitive (1)"
);
assert_eq!(
to_html("[ΑΓΩ]: /φου\n\n[αγω]"),
"<p><a href=\"/%CF%86%CE%BF%CF%85\">αγω</a></p>",
"should match w/ case-insensitive (2)"
);
assert_eq!(
to_html("[ı]: a\n\n[I]"),
"<p><a href=\"a\">I</a></p>",
"should match w/ undotted turkish i (1)"
);
assert_eq!(
to_html("[I]: a\n\n[ı]"),
"<p><a href=\"a\">ı</a></p>",
"should match w/ undotted turkish i (2)"
);
// Ref: <https://spec.commonmark.org/dingus/?text=%5Bi%5D%3A%20a%0A%0A%5Bİ%5D>
// GFM parses the same (last checked: 2022-07-11).
assert_eq!(
to_html("[i]: a\n\n[İ]"),
"<p>[İ]</p>",
"should *not* match w/ dotted turkish i (1)"
);
// Ref: <https://spec.commonmark.org/dingus/?text=%5Bİ%5D%3A%20a%0A%0A%5Bi%5D>
// GFM parses the same (last checked: 2022-07-11).
assert_eq!(
to_html("[İ]: a\n\n[i]"),
"<p>[i]</p>",
"should *not* match w/ dotted turkish i (2)"
);
assert_eq!(
to_html("[foo]: /url"),
"",
"should not contribute anything w/o reference (1)"
);
assert_eq!(
to_html("[\nfoo\n]: /url\nbar"),
"<p>bar</p>",
"should not contribute anything w/o reference (2)"
);
assert_eq!(
to_html("[foo]: /url \"title\" \n\n[foo]"),
"<p><a href=\"/url\" title=\"title\">foo</a></p>",
"should support whitespace after title"
);
assert_eq!(
to_html("[foo]: /url\n\"title\" \n\n[foo]"),
"<p><a href=\"/url\" title=\"title\">foo</a></p>",
"should support whitespace after title on a separate line"
);
assert_eq!(
to_html("[foo]: /url \"title\" ok"),
"<p>[foo]: /url "title" ok</p>",
"should not support non-whitespace content after definitions (1)"
);
assert_eq!(
to_html("[foo]: /url\n\"title\" ok"),
"<p>"title" ok</p>",
"should not support non-whitespace content after definitions (2)"
);
assert_eq!(
to_html(" [foo]: /url \"title\"\n\n[foo]"),
"<pre><code>[foo]: /url "title"\n</code></pre>\n<p>[foo]</p>",
"should prefer indented code over definitions"
);
assert_eq!(
to_html("```\n[foo]: /url\n```\n\n[foo]"),
"<pre><code>[foo]: /url\n</code></pre>\n<p>[foo]</p>",
"should not support definitions in fenced code"
);
assert_eq!(
to_html("Foo\n[bar]: /baz\n\n[bar]"),
"<p>Foo\n[bar]: /baz</p>\n<p>[bar]</p>",
"should not support definitions in paragraphs"
);
assert_eq!(
to_html("# [Foo]\n[foo]: /url\n> bar"),
"<h1><a href=\"/url\">Foo</a></h1>\n<blockquote>\n<p>bar</p>\n</blockquote>",
"should not support definitions in headings"
);
assert_eq!(
to_html("[foo]: /url\nbar\n===\n[foo]"),
"<h1>bar</h1>\n<p><a href=\"/url\">foo</a></p>",
"should support setext headings after definitions"
);
assert_eq!(
to_html("[a]: b\n="),
"<p>=</p>",
"should not support setext heading underlines after definitions (1)"
);
assert_eq!(
to_html("[foo]: /url\n===\n[foo]"),
"<p>===\n<a href=\"/url\">foo</a></p>",
"should not support setext heading underlines after definitions (2)"
);
assert_eq!(
to_html(
"[foo]: /foo-url \"foo\"\n[bar]: /bar-url\n \"bar\"\n[baz]: /baz-url\n\n[foo],\n[bar],\n[baz]"),
"<p><a href=\"/foo-url\" title=\"foo\">foo</a>,\n<a href=\"/bar-url\" title=\"bar\">bar</a>,\n<a href=\"/baz-url\">baz</a></p>",
"should support definitions after definitions"
);
assert_eq!(
to_html("> [foo]: /url\n\n[foo]"),
"<blockquote>\n</blockquote>\n<p><a href=\"/url\">foo</a></p>",
"should support definitions in block quotes (1)"
);
assert_eq!(
to_html("> [a]: <> 'b\n> c'"),
"<blockquote>\n</blockquote>",
"should support definitions in block quotes (2)"
);
assert_eq!(
to_html("> [a]\n\n[a]: b (c\n)"),
"<blockquote>\n<p><a href=\"b\" title=\"c\n\">a</a></p>\n</blockquote>\n",
"should support definitions in block quotes (3)"
);
// Extra
assert_eq!(
to_html("[\\[\\+\\]]: example.com\n\nLink: [\\[\\+\\]]."),
"<p>Link: <a href=\"example.com\">[+]</a>.</p>",
"should match w/ character escapes"
);
assert_eq!(
to_html("[x]: \\\" \\(\\)\\\"\n\n[x]"),
"<p><a href=\"%22%20()%22\">x</a></p>",
"should support character escapes & references in unenclosed destinations"
);
assert_eq!(
to_html("[x]: <\\> \\+\\>>\n\n[x]"),
"<p><a href=\"%3E%20+%3E\">x</a></p>",
"should support character escapes & references in enclosed destinations"
);
assert_eq!(
to_html("[x]: <\n\n[x]"),
"<p>[x]: <</p>\n<p>[x]</p>",
"should not support a line ending at start of enclosed destination"
);
assert_eq!(
to_html("[x]: <x\n\n[x]"),
"<p>[x]: <x</p>\n<p>[x]</p>",
"should not support a line ending in enclosed destination"
);
assert_eq!(
to_html("[x]: \u{000b}a\n\n[x]"),
"<p>[x]: \u{000b}a</p>\n<p>[x]</p>",
"should not support ascii control characters at the start of destination"
);
assert_eq!(
to_html("[x]: a\u{000b}b\n\n[x]"),
"<p>[x]: a\u{000b}b</p>\n<p>[x]</p>",
"should not support ascii control characters in destination"
);
assert_eq!(
to_html("[x]: <\u{000b}a>\n\n[x]"),
"<p><a href=\"%0Ba\">x</a></p>",
"should support ascii control characters at the start of enclosed destination"
);
assert_eq!(
to_html("[x]: <a\u{000b}b>\n\n[x]"),
"<p><a href=\"a%0Bb\">x</a></p>",
"should support ascii control characters in enclosed destinations"
);
assert_eq!(
to_html("[x]: a \"\\\"\"\n\n[x]"),
"<p><a href=\"a\" title=\""\">x</a></p>",
"should support character escapes at the start of a title"
);
assert_eq!(
to_html("[x]: a \"'\"\n\n[x]"),
"<p><a href=\"a\" title=\"'\">x</a></p>",
"should support double quoted titles"
);
assert_eq!(
to_html("[x]: a '\"'\n\n[x]"),
"<p><a href=\"a\" title=\""\">x</a></p>",
"should support single quoted titles"
);
assert_eq!(
to_html("[x]: a (\"')\n\n[x]"),
"<p><a href=\"a\" title=\""'\">x</a></p>",
"should support paren enclosed titles"
);
assert_eq!(
to_html("[x]: a(()\n\n[x]"),
"<p>[x]: a(()</p>\n<p>[x]</p>",
"should not support more opening than closing parens in the destination"
);
assert_eq!(
to_html("[x]: a(())\n\n[x]"),
"<p><a href=\"a(())\">x</a></p>",
"should support balanced opening and closing parens in the destination"
);
assert_eq!(
to_html("[x]: a())\n\n[x]"),
"<p>[x]: a())</p>\n<p>[x]</p>",
"should not support more closing than opening parens in the destination"
);
assert_eq!(
to_html("[x]: a \t\n\n[x]"),
"<p><a href=\"a\">x</a></p>",
"should support trailing whitespace after a destination"
);
assert_eq!(
to_html("[x]: a \"X\" \t\n\n[x]"),
"<p><a href=\"a\" title=\"X\">x</a></p>",
"should support trailing whitespace after a title"
);
assert_eq!(
to_html("[&©&]: example.com/&©& \"&©&\"\n\n[&©&]"),
"<p><a href=\"example.com/&%C2%A9&\" title=\"&©&\">&©&</a></p>",
"should support character references in definitions"
);
assert_eq!(
to_html("[x]:\nexample.com\n\n[x]"),
"<p><a href=\"example.com\">x</a></p>",
"should support a line ending before a destination"
);
assert_eq!(
to_html("[x]: \t\nexample.com\n\n[x]"),
"<p><a href=\"example.com\">x</a></p>",
"should support whitespace before a destination"
);
// See: <https://github.com/commonmark/commonmark.js/issues/192>
assert_eq!(
to_html("[x]: <> \"\"\n[][x]"),
"<p><a href=\"\"></a></p>",
"should ignore an empty title"
);
assert_eq!(
to_html_with_options("[a]\n\n[a]: <b<c>", &danger)?,
"<p>[a]</p>\n<p>[a]: <b<c></p>",
"should not support a less than in an enclosed destination"
);
assert_eq!(
to_html("[a]\n\n[a]: b(c"),
"<p>[a]</p>\n<p>[a]: b(c</p>",
"should not support an extra left paren (`(`) in a raw destination"
);
assert_eq!(
to_html("[a]\n\n[a]: b)c"),
"<p>[a]</p>\n<p>[a]: b)c</p>",
"should not support an extra right paren (`)`) in a raw destination"
);
assert_eq!(
to_html("[a]\n\n[a]: b)c"),
"<p>[a]</p>\n<p>[a]: b)c</p>",
"should not support an extra right paren (`)`) in a raw destination"
);
assert_eq!(
to_html("[a]\n\n[a]: a(1(2(3(4()))))b"),
"<p><a href=\"a(1(2(3(4()))))b\">a</a></p>\n",
"should support 4 or more sets of parens in a raw destination (link resources don’t)"
);
assert_eq!(
to_html("[a]\n\n[a]: aaa)"),
"<p>[a]</p>\n<p>[a]: aaa)</p>",
"should not support a final (unbalanced) right paren in a raw destination"
);
assert_eq!(
to_html("[a]\n\n[a]: aaa) \"a\""),
"<p>[a]</p>\n<p>[a]: aaa) "a"</p>",
"should not support a final (unbalanced) right paren in a raw destination “before” a title"
);
assert_eq!(
to_html(" [a]: b \"c\"\n [d]: e\n [f]: g \"h\"\n [i]: j\n\t[k]: l (m)\n\t n [k] o"),
"<p>n <a href=\"l\" title=\"m\">k</a> o</p>",
"should support subsequent indented definitions"
);
assert_eq!(
to_html("[a\n b]: c\n\n[a\n b]"),
"<p><a href=\"c\">a\nb</a></p>",
"should support line prefixes in definition labels"
);
assert_eq!(
to_html("[a]: )\n\n[a]"),
"<p>[a]: )</p>\n<p>[a]</p>",
"should not support definitions w/ only a closing paren as a raw destination"
);
assert_eq!(
to_html("[a]: )b\n\n[a]"),
"<p>[a]: )b</p>\n<p>[a]</p>",
"should not support definitions w/ closing paren + more text as a raw destination"
);
assert_eq!(
to_html("[a]: b)\n\n[a]"),
"<p>[a]: b)</p>\n<p>[a]</p>",
"should not support definitions w/ text + a closing paren as a raw destination"
);
assert_eq!(
to_html("[\na\n=\n]: b"),
"<h1>[\na</h1>\n<p>]: b</p>",
"should prefer setext headings over definition labels"
);
assert_eq!(
to_html("[a]: b '\nc\n=\n'"),
"<h1>[a]: b '\nc</h1>\n<p>'</p>",
"should prefer setext headings over definition titles"
);
assert_eq!(
to_html("[\n***\n]: b"),
"<p>[</p>\n<hr />\n<p>]: b</p>",
"should prefer thematic breaks over definition labels"
);
assert_eq!(
to_html("[a]: b '\n***\n'"),
"<p>[a]: b '</p>\n<hr />\n<p>'</p>",
"should prefer thematic breaks over definition titles"
);
assert_eq!(
to_html("[\n```\n]: b"),
"<p>[</p>\n<pre><code>]: b\n</code></pre>\n",
"should prefer code (fenced) over definition labels"
);
assert_eq!(
to_html("[a]: b '\n```\n'"),
"<p>[a]: b '</p>\n<pre><code>'\n</code></pre>\n",
"should prefer code (fenced) over definition titles"
);
assert_eq!(
to_html_with_options(
"[foo]: /url \"title\"",
&Options {
parse: ParseOptions {
constructs: Constructs {
definition: false,
..Default::default()
},
..Default::default()
},
..Default::default()
}
)?,
"<p>[foo]: /url "title"</p>",
"should support turning off definitions"
);
assert_eq!(
to_mdast("[a]: <b> 'c'", &Default::default())?,
Node::Root(Root {
children: vec![Node::Definition(Definition {
url: "b".into(),
identifier: "a".into(),
label: Some("a".into()),
title: Some("c".into()),
position: Some(Position::new(1, 1, 0, 1, 13, 12))
})],
position: Some(Position::new(1, 1, 0, 1, 13, 12))
}),
"should support definitions as `Definition`s in mdast"
);
Ok(())
}
|