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
|
extern crate markdown;
use markdown::{
mdast::{Definition, Image, ImageReference, Node, Paragraph, ReferenceKind, Root, Text},
to_html, to_html_with_options, to_mdast,
unist::Position,
CompileOptions, Constructs, Options, ParseOptions,
};
use pretty_assertions::assert_eq;
#[test]
fn image() -> Result<(), String> {
assert_eq!(
to_html("[link](/uri \"title\")"),
"<p><a href=\"/uri\" title=\"title\">link</a></p>",
"should support links"
);
assert_eq!(
to_html("![foo](/url \"title\")"),
"<p><img src=\"/url\" alt=\"foo\" title=\"title\" /></p>",
"should support image w/ resource"
);
assert_eq!(
to_html("[foo *bar*]: train.jpg \"train & tracks\"\n\n![foo *bar*]"),
"<p><img src=\"train.jpg\" alt=\"foo bar\" title=\"train & tracks\" /></p>",
"should support image as shortcut reference"
);
assert_eq!(
to_html("![foo ![bar](/url)](/url2)"),
"<p><img src=\"/url2\" alt=\"foo bar\" /></p>",
"should “support” images in images"
);
assert_eq!(
to_html("![foo [bar](/url)](/url2)"),
"<p><img src=\"/url2\" alt=\"foo bar\" /></p>",
"should “support” links in images"
);
assert_eq!(
to_html("[foo *bar*]: train.jpg \"train & tracks\"\n\n![foo *bar*][]"),
"<p><img src=\"train.jpg\" alt=\"foo bar\" title=\"train & tracks\" /></p>",
"should support “content” in images"
);
assert_eq!(
to_html("[FOOBAR]: train.jpg \"train & tracks\"\n\n![foo *bar*][foobar]"),
"<p><img src=\"train.jpg\" alt=\"foo bar\" title=\"train & tracks\" /></p>",
"should support “content” in images"
);
assert_eq!(
to_html("![foo](train.jpg)"),
"<p><img src=\"train.jpg\" alt=\"foo\" /></p>",
"should support images w/o title"
);
assert_eq!(
to_html("My ![foo bar](/path/to/train.jpg \"title\" )"),
"<p>My <img src=\"/path/to/train.jpg\" alt=\"foo bar\" title=\"title\" /></p>",
"should support images w/ lots of whitespace"
);
assert_eq!(
to_html("![foo](<url>)"),
"<p><img src=\"url\" alt=\"foo\" /></p>",
"should support images w/ enclosed destinations"
);
assert_eq!(
to_html("![](/url)"),
"<p><img src=\"/url\" alt=\"\" /></p>",
"should support images w/ empty labels"
);
assert_eq!(
to_html("[bar]: /url\n\n![foo][bar]"),
"<p><img src=\"/url\" alt=\"foo\" /></p>",
"should support full references (1)"
);
assert_eq!(
to_html("[BAR]: /url\n\n![foo][bar]"),
"<p><img src=\"/url\" alt=\"foo\" /></p>",
"should support full references (2)"
);
assert_eq!(
to_html("[foo]: /url \"title\"\n\n![foo][]"),
"<p><img src=\"/url\" alt=\"foo\" title=\"title\" /></p>",
"should support collapsed references (1)"
);
assert_eq!(
to_html("[*foo* bar]: /url \"title\"\n\n![*foo* bar][]"),
"<p><img src=\"/url\" alt=\"foo bar\" title=\"title\" /></p>",
"should support collapsed references (2)"
);
assert_eq!(
to_html("[foo]: /url \"title\"\n\n![Foo][]"),
"<p><img src=\"/url\" alt=\"Foo\" title=\"title\" /></p>",
"should support case-insensitive labels"
);
assert_eq!(
to_html("[foo]: /url \"title\"\n\n![foo] \n[]"),
"<p><img src=\"/url\" alt=\"foo\" title=\"title\" />\n[]</p>",
"should not support whitespace between sets of brackets"
);
assert_eq!(
to_html("[foo]: /url \"title\"\n\n![foo]"),
"<p><img src=\"/url\" alt=\"foo\" title=\"title\" /></p>",
"should support shortcut references (1)"
);
assert_eq!(
to_html("[*foo* bar]: /url \"title\"\n\n![*foo* bar]"),
"<p><img src=\"/url\" alt=\"foo bar\" title=\"title\" /></p>",
"should support shortcut references (2)"
);
assert_eq!(
to_html("[[foo]]: /url \"title\"\n\n![[foo]]"),
"<p>[[foo]]: /url "title"</p>\n<p>![[foo]]</p>",
"should not support link labels w/ unescaped brackets"
);
assert_eq!(
to_html("[foo]: /url \"title\"\n\n![Foo]"),
"<p><img src=\"/url\" alt=\"Foo\" title=\"title\" /></p>",
"should support case-insensitive label matching"
);
assert_eq!(
to_html("[foo]: /url \"title\"\n\n!\\[foo]"),
"<p>![foo]</p>",
"should “support” an escaped bracket instead of an image"
);
assert_eq!(
to_html("[foo]: /url \"title\"\n\n\\![foo]"),
"<p>!<a href=\"/url\" title=\"title\">foo</a></p>",
"should support an escaped bang instead of an image, but still have a link"
);
// Extra
assert_eq!(
to_html("![foo]()"),
"<p><img src=\"\" alt=\"foo\" /></p>",
"should support images w/o destination"
);
assert_eq!(
to_html("![foo](<>)"),
"<p><img src=\"\" alt=\"foo\" /></p>",
"should support images w/ explicit empty destination"
);
assert_eq!(
to_html("![](example.png)"),
"<p><img src=\"example.png\" alt=\"\" /></p>",
"should support images w/o alt"
);
assert_eq!(
to_html("![alpha](bravo.png \"\")"),
"<p><img src=\"bravo.png\" alt=\"alpha\" /></p>",
"should support images w/ empty title (1)"
);
assert_eq!(
to_html("![alpha](bravo.png '')"),
"<p><img src=\"bravo.png\" alt=\"alpha\" /></p>",
"should support images w/ empty title (2)"
);
assert_eq!(
to_html("![alpha](bravo.png ())"),
"<p><img src=\"bravo.png\" alt=\"alpha\" /></p>",
"should support images w/ empty title (3)"
);
assert_eq!(
to_html("![&©&](example.com/&©& \"&©&\")"),
"<p><img src=\"example.com/&%C2%A9&\" alt=\"&©&\" title=\"&©&\" /></p>",
"should support character references in images"
);
// Extra
// See: <https://github.com/commonmark/commonmark.js/issues/192>
assert_eq!(
to_html("![](<> \"\")"),
"<p><img src=\"\" alt=\"\" /></p>",
"should ignore an empty title"
);
assert_eq!(
to_html_with_options(
"![x]()",
&Options {
parse: ParseOptions {
constructs: Constructs {
label_start_image: false,
..Constructs::default()
},
..ParseOptions::default()
},
..Options::default()
}
)?,
"<p>!<a href=\"\">x</a></p>",
"should support turning off label start (image)"
);
assert_eq!(
to_html("![](javascript:alert(1))"),
"<p><img src=\"\" alt=\"\" /></p>",
"should ignore non-http protocols by default"
);
assert_eq!(
to_html_with_options(
"![](javascript:alert(1))",
&Options {
compile: CompileOptions {
allow_dangerous_protocol: true,
..CompileOptions::default()
},
..Options::default()
}
)?,
"<p><img src=\"javascript:alert(1)\" alt=\"\" /></p>",
"should allow non-http protocols w/ `allowDangerousProtocol`"
);
assert_eq!(
to_mdast(
"a ![alpha]() b ![bravo](charlie 'delta') c.",
&ParseOptions::default()
)?,
Node::Root(Root {
children: vec![Node::Paragraph(Paragraph {
children: vec![
Node::Text(Text {
value: "a ".into(),
position: Some(Position::new(1, 1, 0, 1, 3, 2))
}),
Node::Image(Image {
alt: "alpha".into(),
url: String::new(),
title: None,
position: Some(Position::new(1, 3, 2, 1, 13, 12))
}),
Node::Text(Text {
value: " b ".into(),
position: Some(Position::new(1, 13, 12, 1, 16, 15))
}),
Node::Image(Image {
alt: "bravo".into(),
url: "charlie".into(),
title: Some("delta".into()),
position: Some(Position::new(1, 16, 15, 1, 41, 40))
}),
Node::Text(Text {
value: " c.".into(),
position: Some(Position::new(1, 41, 40, 1, 44, 43))
})
],
position: Some(Position::new(1, 1, 0, 1, 44, 43))
})],
position: Some(Position::new(1, 1, 0, 1, 44, 43))
}),
"should support image (resource) as `Image`s in mdast"
);
assert_eq!(
to_mdast(
"[x]: y\n\na ![x] b ![x][] c ![d][x] e.",
&ParseOptions::default()
)?,
Node::Root(Root {
children: vec![
Node::Definition(Definition {
identifier: "x".into(),
label: Some("x".into()),
url: "y".into(),
title: None,
position: Some(Position::new(1, 1, 0, 1, 7, 6))
}),
Node::Paragraph(Paragraph {
children: vec![
Node::Text(Text {
value: "a ".into(),
position: Some(Position::new(3, 1, 8, 3, 3, 10))
}),
Node::ImageReference(ImageReference {
reference_kind: ReferenceKind::Shortcut,
identifier: "x".into(),
label: Some("x".into()),
alt: "x".into(),
position: Some(Position::new(3, 3, 10, 3, 7, 14))
}),
Node::Text(Text {
value: " b ".into(),
position: Some(Position::new(3, 7, 14, 3, 10, 17))
}),
Node::ImageReference(ImageReference {
reference_kind: ReferenceKind::Collapsed,
identifier: "x".into(),
label: Some("x".into()),
alt: "x".into(),
position: Some(Position::new(3, 10, 17, 3, 16, 23))
}),
Node::Text(Text {
value: " c ".into(),
position: Some(Position::new(3, 16, 23, 3, 19, 26))
}),
Node::ImageReference(ImageReference {
reference_kind: ReferenceKind::Full,
identifier: "x".into(),
label: Some("x".into()),
alt: "d".into(),
position: Some(Position::new(3, 19, 26, 3, 26, 33))
}),
Node::Text(Text {
value: " e.".into(),
position: Some(Position::new(3, 26, 33, 3, 29, 36))
}),
],
position: Some(Position::new(3, 1, 8, 3, 29, 36))
}),
],
position: Some(Position::new(1, 1, 0, 3, 29, 36))
}),
"should support image (reference) as `ImageReference`s in mdast"
);
Ok(())
}
|