-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathembed.test.js
More file actions
373 lines (323 loc) Β· 11.5 KB
/
Copy pathembed.test.js
File metadata and controls
373 lines (323 loc) Β· 11.5 KB
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
import { waitForFunction, waitForText } from '../helpers/wait-for.js';
import docsifyInit from '../helpers/docsify-init.js';
describe('Embed', function () {
test('embed file code fragment renders', async () => {
await docsifyInit({
markdown: {
homepage: `
# Embed Test
[filename](_media/example1.js ':include :type=code :fragment=demo')
`,
},
routes: {
'_media/example1.js': `
let myURL = 'https://api.example.com/data';
/// [demo]
const result = fetch(myURL)
.then(response => {
return response.json();
})
.then(myJson => {
console.log(JSON.stringify(myJson));
});
/// [demo]
result.then(console.log).catch(console.error);
`,
},
});
// Wait for the embedded fragment to be fetched and rendered into #main
expect(
await waitForText('#main', 'console.log(JSON.stringify(myJson));'),
).toBeTruthy();
const mainText = document.querySelector('#main').textContent;
expect(mainText).not.toContain('https://api.example.com/data');
expect(mainText).not.toContain(
'result.then(console.log).catch(console.error);',
);
});
test('embed file full line fragment identifier', async () => {
await docsifyInit({
markdown: {
homepage: `
# Embed Test
[filename](_media/example1.html ':include :type=code :fragment=demo :omitFragmentLine')
`,
},
routes: {
'_media/example1.html': `
<script>
let myURL = 'https://api.example.com/data';
/// [demo] Full line fragment identifier (all of these words here should not be included in fragment)
const result = fetch(myURL)
.then(response => {
return response.json();
})
.then(myJson => {
console.log(JSON.stringify(myJson));
});
<!-- /// [demo] -->
result.then(console.log).catch(console.error);
</script>
`,
},
});
// Wait for the embedded fragment to be fetched and rendered into #main
expect(
await waitForText('#main', 'console.log(JSON.stringify(myJson));'),
).toBeTruthy();
const mainText = document.querySelector('#main').textContent;
expect(mainText).not.toContain('https://api.example.com/data');
expect(mainText).not.toContain('Full line fragment identifier');
expect(mainText).not.toContain('-->');
expect(mainText).not.toContain(
'result.then(console.log).catch(console.error);',
);
});
test('embed multiple file code fragments', async () => {
await docsifyInit({
markdown: {
homepage: `
# Embed Test
[filename](_media/example1.js ':include :type=code :fragment=demo')
[filename](_media/example2.js ":include :type=code :fragment=something")
# Text between
[filename](_media/example3.js ':include :fragment=something_else_not_code')
[filename](_media/example4.js ':include :fragment=demo')
# Text after
`,
},
routes: {
'_media/example1.js': `
let example1 = 1;
/// [demo]
example1 += 10;
/// [demo]
console.log(example1);`,
'_media/example2.js': `
let example1 = 1;
### [something]
example2 += 10;
### [something]
console.log(example2);`,
'_media/example3.js': `
let example3 = 1;
### [something_else_not_code]
example3 += 10;
/// [something_else_not_code]
console.log(example3);`,
'_media/example4.js': `
let example4 = 1;
### No fragment here
example4 += 10;
/// No fragment here
console.log(example4);`,
},
});
expect(await waitForText('#main', 'example1 += 10;')).toBeTruthy();
expect(await waitForText('#main', 'example2 += 10;')).toBeTruthy();
expect(await waitForText('#main', 'example3 += 10;')).toBeTruthy();
const mainText = document.querySelector('#main').textContent;
expect(mainText).toContain('Text between');
expect(mainText).toContain('Text after');
expect(mainText).not.toContain('let example1 = 1;');
expect(mainText).not.toContain('let example2 = 1;');
expect(mainText).not.toContain('let example3 = 1;');
expect(mainText).not.toContain('console.log(example1);');
expect(mainText).not.toContain('console.log(example2);');
expect(mainText).not.toContain('console.log(example3);');
expect(mainText).not.toContain('console.log(example4);');
expect(mainText).not.toContain('example4 += 10;');
expect(mainText).not.toContain('No fragment here');
});
test('embed multiple includes in same paragraph', async () => {
await docsifyInit({
markdown: {
homepage: `
# Embed Test
[first](_media/first.md ':include') middle paragraph text [second](_media/second.md ':include')
`,
},
routes: {
'_media/first.md': 'first include content',
'_media/second.md': 'second include content',
},
});
expect(await waitForText('#main', 'first include content')).toBeTruthy();
expect(await waitForText('#main', 'second include content')).toBeTruthy();
const mainText = document.querySelector('#main').textContent;
const firstIndex = mainText.indexOf('first include content');
const middleIndex = mainText.indexOf('middle paragraph text');
const secondIndex = mainText.indexOf('second include content');
expect(firstIndex).toBeGreaterThan(-1);
expect(middleIndex).toBeGreaterThan(-1);
expect(secondIndex).toBeGreaterThan(-1);
expect(firstIndex).toBeLessThan(middleIndex);
expect(middleIndex).toBeLessThan(secondIndex);
expect(mainText).not.toContain("_media/first.md ':include'");
expect(mainText).not.toContain("_media/second.md ':include'");
});
test('embed markdown file strips front matter when plugin is installed', async () => {
await docsifyInit({
markdown: {
homepage: `
---
title: Homepage
---
# Embed Test
[front matter include](_media/content.md ':include')
`,
},
routes: {
'_media/content.md': `
---
title: Include
---
included front matter content
`,
},
scriptURLs: ['/dist/plugins/front-matter.js'],
});
expect(
await waitForText('#main', 'included front matter content'),
).toBeTruthy();
const mainText = document.querySelector('#main').textContent;
expect(mainText).not.toContain('title: Homepage');
expect(mainText).not.toContain('title: Include');
});
test('embed multiple include code fragments in same paragraph', async () => {
await docsifyInit({
markdown: {
homepage: `
# Embed Test
[first](_media/first.js ':include :type=code :fragment=demo') [second](_media/second.js ':include :type=code :fragment=demo')
`,
},
routes: {
'_media/first.js': `
const first = 1;
/// [demo]
console.log('first demo line');
/// [demo]
console.log('first outside');
`,
'_media/second.js': `
const second = 1;
/// [demo]
console.log('second demo line');
/// [demo]
console.log('second outside');
`,
},
});
expect(
await waitForText('#main', "console.log('first demo line');"),
).toBeTruthy();
expect(
await waitForText('#main', "console.log('second demo line');"),
).toBeTruthy();
const mainText = document.querySelector('#main').textContent;
const firstIndex = mainText.indexOf("console.log('first demo line');");
const secondIndex = mainText.indexOf("console.log('second demo line');");
expect(firstIndex).toBeGreaterThan(-1);
expect(secondIndex).toBeGreaterThan(-1);
expect(firstIndex).toBeLessThan(secondIndex);
expect(mainText).not.toContain('first outside');
expect(mainText).not.toContain('second outside');
});
test('embed multiple includes in same table cell', async () => {
await docsifyInit({
markdown: {
homepage: `
# Embed Test
Command | Description | Parameters
---: | --- | ---
\`do-something\` | Does something. | [first include](_media/first.md ':include') middle table text [second include](_media/second.md ':include')
`,
},
routes: {
'_media/first.md': 'first table include content',
'_media/second.md': 'second table include content',
},
});
expect(
await waitForText('#main', 'first table include content'),
).toBeTruthy();
expect(
await waitForText('#main', 'second table include content'),
).toBeTruthy();
const mainText = document.querySelector('#main').textContent;
const firstIndex = mainText.indexOf('first table include content');
const middleIndex = mainText.indexOf('middle table text');
const secondIndex = mainText.indexOf('second table include content');
expect(firstIndex).toBeGreaterThan(-1);
expect(middleIndex).toBeGreaterThan(-1);
expect(secondIndex).toBeGreaterThan(-1);
expect(firstIndex).toBeLessThan(middleIndex);
expect(middleIndex).toBeLessThan(secondIndex);
expect(mainText).not.toContain("_media/first.md ':include'");
expect(mainText).not.toContain("_media/second.md ':include'");
});
test('failed embed URL does not block page render', async () => {
await docsifyInit({
markdown: {
homepage: `
# Embed Test
[missing](_media/missing.md ':include')
Text after missing embed
[ok](_media/ok.md ':include')
`,
},
routes: {
'_media/missing.md': {
status: 404,
body: 'Not Found',
contentType: 'text/markdown',
},
'_media/ok.md': 'reachable include content',
},
});
expect(await waitForText('#main', 'Text after missing embed')).toBeTruthy();
expect(
await waitForText('#main', 'reachable include content'),
).toBeTruthy();
});
test('embed file table cell', async () => {
await docsifyInit({
markdown: {
homepage: `
# Embed Test
Command | Description | Parameters
---: | --- | ---
**Something** | |
\`do-something\` | Does something. | [include content](_media/content.md ':include')
**Something else** | |
\`etc.\` | Etc. | |
`,
},
routes: {
'_media/content.md': `this is include content`,
},
});
const mainText = document.querySelector('#main').textContent;
expect(mainText).toContain('Something');
expect(mainText).toContain('this is include content');
});
test.each([
{ type: 'iframe', selector: 'iframe' },
{ type: 'video', selector: 'video' },
{ type: 'audio', selector: 'audio' },
])('embed %s escapes URL for XSS safety', async ({ type, selector }) => {
const dangerousUrl = 'https://example.com/?q="><svg/onload=alert(1)>';
await docsifyInit({
markdown: {
homepage: `[media](${dangerousUrl} ':include :type=${type}')`,
},
});
expect(
await waitForFunction(() => !!document.querySelector(selector)),
).toBe(true);
const mediaElm = document.querySelector(selector);
expect(mediaElm.getAttribute('src')).toBe(dangerousUrl);
expect(mediaElm.hasAttribute('onload')).toBe(false);
});
});