-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Expand file tree
/
Copy pathdeck-thumbnail-parser.test.ts
More file actions
564 lines (502 loc) · 24.3 KB
/
Copy pathdeck-thumbnail-parser.test.ts
File metadata and controls
564 lines (502 loc) · 24.3 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
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
// @vitest-environment jsdom
import { describe, expect, it } from 'vitest';
import { parseDeckThumbnails } from '../../src/runtime/deck-thumbnail-parser';
// Canonical OD framework deck: `.deck-shell > .deck-stage#deck-stage >
// section.slide`, styles in a <head> <style>, `:root` vars, chrome outside the
// stage.
function frameworkDeck(slides: number): string {
const sections = Array.from({ length: slides }, (_, i) =>
`<section class="slide${i === 0 ? ' active' : ''}" data-screen-label="0${i + 1} Title">
<h1 class="title">Slide ${i + 1}</h1>
<img src="assets/pic-${i}.png" alt="" />
</section>`,
).join('\n');
return `<!doctype html><html><head><style>
:root { --bg: #fff; --fg: #111; }
html, body { background: var(--shell); color: var(--fg); }
.deck-stage { width: 1920px; height: 1080px; background: var(--bg); }
.slide:not(.active) { display: none !important; }
.title { background: url(bg/hero.png); }
</style></head><body>
<div class="deck-shell"><div class="deck-stage" id="deck-stage">
${sections}
</div></div>
<nav class="deck-counter"><button id="deck-prev">‹</button></nav>
<script>/* nav */</script>
</body></html>`;
}
describe('parseDeckThumbnails', () => {
it('extracts slides, styles, ancestors and design size from a framework deck', () => {
const parsed = parseDeckThumbnails(frameworkDeck(3), '/api/projects/p1/raw/');
expect(parsed.renderable).toBe(true);
expect(parsed.slides).toHaveLength(3);
expect(parsed.slides[0]).toMatch(/^<section/);
expect(parsed.slides[1]).toContain('Slide 2');
expect(parsed.designWidth).toBe(1920);
expect(parsed.designHeight).toBe(1080);
expect(parsed.ancestors.map((a) => a.tag)).toEqual(['div', 'div']);
// outer→inner: deck-shell then deck-stage
expect(parsed.ancestors[0]!.attributes).toContainEqual(['class', 'deck-shell']);
expect(parsed.ancestors[1]!.attributes).toContainEqual(['id', 'deck-stage']);
});
it('rewrites root selectors to :host and body selectors to the thumbnail canvas', () => {
const parsed = parseDeckThumbnails(frameworkDeck(1));
expect(parsed.styleText).toContain(':host { --bg: #fff');
expect(parsed.styleText).toContain(':host, .od-thumb-canvas { background: var(--shell)');
expect(parsed.styleText).not.toMatch(/:root\s*\{/);
// Compound selectors are left alone.
expect(parsed.styleText).toContain('.deck-stage {');
});
it('rewrites :root to :host even when a CSS comment precedes it', () => {
// Real decks lead their `<style>` with a banner comment right before the
// `:root` custom-property block (e.g. `/* === VIEWPORT BASE === */`). If the
// rewrite is fooled by the comment, `:root` survives, matches nothing in the
// shadow tree, and every `var(--slide-bg)` resolves to transparent — the
// slide paints nothing over the near-black thumbnail host = black thumbnail.
const html = `<!doctype html><html><head><style>
/* === VIEWPORT BASE === */
:root { --stage-bg: #0a0a0a; --slide-bg: #ffffff; }
html, body { background: var(--stage-bg); }
.deck-stage { width: 1920px; height: 1080px; background: var(--slide-bg); }
.slide { position: absolute; inset: 0; background: var(--slide-bg); }
</style></head><body>
<div class="deck-viewport"><main class="deck-stage" id="deck-stage">
<section class="slide active" data-screen-label="01">A</section>
</main></div>
</body></html>`;
const parsed = parseDeckThumbnails(html);
expect(parsed.renderable).toBe(true);
// The custom properties must land on :host so they inherit into the slide.
expect(parsed.styleText).toContain(':host { --stage-bg: #0a0a0a; --slide-bg: #ffffff; }');
expect(parsed.styleText).not.toMatch(/:root\s*\{/);
});
it('absolutizes relative asset URLs against the base href', () => {
const parsed = parseDeckThumbnails(frameworkDeck(1), '/api/projects/p1/raw/sub');
expect(parsed.slides[0]).toContain('src="/api/projects/p1/raw/sub/assets/pic-0.png"');
expect(parsed.styleText).toContain('url(/api/projects/p1/raw/sub/bg/hero.png)');
});
it('lifts @font-face out of the shadow styles into fontFaces', () => {
const html = frameworkDeck(1).replace(
'<style>',
'<style>@font-face { font-family: "X"; src: url(fonts/x.woff2); }',
);
const parsed = parseDeckThumbnails(html, '/api/projects/p1/raw/');
expect(parsed.fontFaces).toContain('@font-face');
expect(parsed.fontFaces).toContain('/api/projects/p1/raw/fonts/x.woff2');
expect(parsed.styleText).not.toContain('@font-face');
});
it('collects external font-stylesheet links and stays renderable', () => {
const html = frameworkDeck(1).replace(
'</head>',
'<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter"></head>',
);
const parsed = parseDeckThumbnails(html);
expect(parsed.renderable).toBe(true);
expect(parsed.fontLinks).toEqual(['https://fonts.googleapis.com/css2?family=Inter']);
});
it('reads design size + ancestors from a <deck-stage> template deck', () => {
const html = `<!doctype html><html><head><style>
deck-stage > section.slide { width: 1280px; height: 720px; }
</style></head><body>
<deck-stage width="1280" height="720">
<section class="s1" data-screen-label="01">A</section>
<section class="s2" data-screen-label="02">B</section>
</deck-stage>
</body></html>`;
const parsed = parseDeckThumbnails(html);
expect(parsed.renderable).toBe(true);
expect(parsed.slides).toHaveLength(2);
expect(parsed.designWidth).toBe(1280);
expect(parsed.designHeight).toBe(720);
expect(parsed.ancestors.map((a) => a.tag)).toEqual(['deck-stage']);
});
it('prefers deck-stage children over unrelated screen labels elsewhere in the document', () => {
const html = `<!doctype html><html><head><style>
deck-stage > section { width: 1280px; height: 720px; }
</style></head><body>
<aside data-screen-label="Prototype navigation">Not a slide</aside>
<deck-stage width="1280" height="720">
<section data-screen-label="01 Cover">A</section>
<section data-screen-label="02 Agenda">B</section>
</deck-stage>
</body></html>`;
const parsed = parseDeckThumbnails(html);
expect(parsed.renderable).toBe(true);
expect(parsed.slides).toHaveLength(2);
expect(parsed.slides[0]).toContain('01 Cover');
expect(parsed.slides[1]).toContain('02 Agenda');
});
it('does not treat ordinary prototype annotations as deck slides', () => {
const html = `<!doctype html><html><head><style>
h1 { color: tomato; }
</style></head><body><main>
<h1 data-screen-label="Hero title">Prototype headline</h1>
<button data-screen-label="CTA">Buy now</button>
</main></body></html>`;
const parsed = parseDeckThumbnails(html);
expect(parsed.renderable).toBe(false);
expect(parsed.reason).toBe('no-slides');
});
it('requires containerless legacy slides to be numbered direct siblings', () => {
const separated = `<!doctype html><html><head><style>
section { width: 1920px; height: 1080px; }
</style></head><body><main>
<section data-screen-label="01 Cover">A</section>
<div><section data-screen-label="02 Agenda">B</section></div>
</main></body></html>`;
expect(parseDeckThumbnails(separated).reason).toBe('no-slides');
const siblings = separated.replace(
'<div><section data-screen-label="02 Agenda">B</section></div>',
'<section data-screen-label="02 Agenda">B</section>',
);
expect(parseDeckThumbnails(siblings).slides).toHaveLength(2);
});
it('rewrites viewport units in CSS to canvas px (renderable, faithful)', () => {
// No explicit px canvas → defaults to 1920×1080; 100vw→1920px, 100vh→1080px.
const html = `<!doctype html><html><head><style>
#deck > section.slide { width: 100vw; height: 100vh; }
.title { font-size: clamp(24px, 4vh, 48px); padding: 6vw; }
</style></head><body>
<div id="deck"><section class="slide">A</section></div>
</body></html>`;
const parsed = parseDeckThumbnails(html);
expect(parsed.renderable).toBe(true);
expect(parsed.styleText).toContain('width: calc(100 * 19.2px)');
expect(parsed.styleText).toContain('height: calc(100 * 10.8px)');
expect(parsed.styleText).toContain('clamp(24px, calc(4 * 10.8px), 48px)');
expect(parsed.styleText).toContain('padding: calc(6 * 19.2px)');
expect(parsed.styleText).not.toMatch(/\d(?:vw|vh)\b/);
});
it('rewrites viewport units in slide inline styles', () => {
const html = `<!doctype html><html><head><style>
.deck-stage { width: 1920px; height: 1080px; }
</style></head><body><div class="deck-stage" id="deck-stage">
<section class="slide active"><div style="height: 12vh">bar</div></section>
</div></body></html>`;
const parsed = parseDeckThumbnails(html);
expect(parsed.renderable).toBe(true);
expect(parsed.slides[0]).toContain('calc(12 * 10.8px)');
expect(parsed.slides[0]).not.toContain('12vh');
});
it('stays renderable for a fixed px canvas with percent-sized slides', () => {
const html = `<!doctype html><html><head><style>
.deck-stage { width: 1920px; height: 1080px; }
.slide { width: 100%; height: 100%; position: absolute; }
</style></head><body><div class="deck-stage" id="deck-stage">
<section class="slide active">A</section>
</div></body></html>`;
const parsed = parseDeckThumbnails(html);
expect(parsed.renderable).toBe(true);
expect(parsed.designWidth).toBe(1920);
// Percent sizing is left untouched — it already resolves to the canvas.
expect(parsed.styleText).toContain('width: 100%');
});
it('falls back when viewport media queries would diverge from the preview iframe', () => {
const html = `<!doctype html><html><head><style>
.slide { width: 100vw; height: 100vh; display: flex; }
@media (max-width: 768px) {
.slide { padding: 24px; display: grid; }
}
</style></head><body>
<section class="slide">A</section>
<section class="slide">B</section>
</body></html>`;
const parsed = parseDeckThumbnails(html);
expect(parsed.renderable).toBe(false);
expect(parsed.reason).toBe('viewport-media-query');
});
it('keeps non-viewport media queries on the static thumbnail path', () => {
const html = `<!doctype html><html><head><style>
.slide { width: 1920px; height: 1080px; display: flex; }
@media (prefers-reduced-motion: reduce) {
.slide { animation: none; }
}
</style></head><body>
<section class="slide">A</section>
<section class="slide">B</section>
</body></html>`;
expect(parseDeckThumbnails(html).renderable).toBe(true);
});
it('does not mistake a slide descendant decoration for the design canvas', () => {
const html = `<!doctype html><html><head><style>
body { display: flex; width: 200vw; height: 100vh; }
.slide { width: 100vw; height: 100vh; flex: none; }
.slide .kicker-line { width: 72px; height: 6px; }
.slide::before { width: 40px; height: 40px; }
</style></head><body>
<section class="slide"><span class="kicker-line">A</span></section>
<section class="slide">B</section>
</body></html>`;
const parsed = parseDeckThumbnails(html);
expect(parsed.renderable).toBe(true);
expect(parsed.designWidth).toBe(1920);
expect(parsed.designHeight).toBe(1080);
});
it('reads a 4:3 canvas size from a tag-prefixed slide class selector', () => {
const html = `<!doctype html><html><head><style>
section.slide { width: 1200px; height: 900px; }
</style></head><body><main class="deck">
<section class="slide">A</section>
<section class="slide">B</section>
</main></body></html>`;
const parsed = parseDeckThumbnails(html);
expect(parsed.renderable).toBe(true);
expect(parsed.designWidth).toBe(1200);
expect(parsed.designHeight).toBe(900);
});
it('reads a portrait canvas size from a tag-prefixed screen-label selector', () => {
const html = `<!doctype html><html><head><style>
section[data-screen-label] { width: 900px; height: 1200px; }
</style></head><body><main class="deck">
<section data-screen-label="01 Cover">A</section>
<section data-screen-label="02 Detail">B</section>
</main></body></html>`;
const parsed = parseDeckThumbnails(html);
expect(parsed.renderable).toBe(true);
expect(parsed.designWidth).toBe(900);
expect(parsed.designHeight).toBe(1200);
});
it('reads the design size from the shared slide-frame marker', () => {
const html = `<!doctype html><html><head><style>
.slide-frame { width: 1280px; height: 720px; }
</style></head><body><main class="deck">
<section class="slide-frame" data-screen-label="01 Cover">A</section>
<section class="slide-frame" data-screen-label="02 Detail">B</section>
</main></body></html>`;
const parsed = parseDeckThumbnails(html);
expect(parsed.renderable).toBe(true);
expect(parsed.designWidth).toBe(1280);
expect(parsed.designHeight).toBe(720);
});
it('falls back when the deck depends on an external layout stylesheet', () => {
const html = frameworkDeck(1).replace(
'</head>',
'<link rel="stylesheet" href="/api/projects/p1/raw/deck.css"></head>',
);
const parsed = parseDeckThumbnails(html);
expect(parsed.renderable).toBe(false);
expect(parsed.reason).toBe('external-stylesheet');
});
it('falls back for documents with no slides or no styles', () => {
expect(parseDeckThumbnails('<div>not a deck</div>').reason).toBe('no-slides');
expect(parseDeckThumbnails('').reason).toBe('no-slides');
const styleless = '<body><section class="slide">A</section></body>';
expect(parseDeckThumbnails(styleless).reason).toBe('no-styles');
});
it('strips executable content from untrusted slide markup', () => {
const deck = [
'<!doctype html><html><head><style>',
' .deck-stage { width: 1920px; height: 1080px; }',
' .slide:not(.active) { display: none; }',
'</style></head><body>',
' <div class="deck-shell"><div class="deck-stage" id="deck-stage">',
' <section class="slide active">',
' <img src="x" onerror="fetch(\'//evil\')" alt="" />',
' <a href="javascript:alert(1)">link</a>',
' <a href="java\tscript:alert(3)">tabbed</a>',
' <h1 onclick="steal()">Title</h1>',
' <script>alert(2)</script>',
' <iframe src="https://evil.example"></iframe>',
' <object data="https://evil.example"></object>',
' <embed src="https://evil.example" />',
' <form action="https://evil.example"><button formaction="javascript:go()">x</button></form>',
' </section>',
' </div></div>',
'</body></html>',
].join('\n');
const parsed = parseDeckThumbnails(deck, '/api/projects/p1/raw/');
expect(parsed.renderable).toBe(true);
const slide = parsed.slides[0] ?? '';
// no inline event handlers, executable/navigable elements, or script URLs
expect(slide).not.toMatch(/onerror/i);
expect(slide).not.toMatch(/onclick/i);
expect(slide).not.toMatch(/<script/i);
expect(slide).not.toMatch(/<iframe/i);
expect(slide).not.toMatch(/<object/i);
expect(slide).not.toMatch(/<embed/i);
expect(slide).not.toMatch(/<form/i);
expect(slide).not.toMatch(/formaction/i);
expect(slide).not.toMatch(/javascript:/i);
// control-character-obfuscated scheme is neutralized too
expect(slide).not.toContain('alert(3)');
// benign slide content is preserved
expect(slide).toContain('<h1');
expect(slide).toContain('Title');
});
it('sanitizes reconstructed slide ancestors (a second injection path)', () => {
const deck = [
'<!doctype html><html><head><style>',
' .deck-stage { width: 1920px; height: 1080px; }',
'</style></head><body>',
' <div class="deck-shell" onclick="wrap()"><div class="deck-stage" id="deck-stage" onmouseover="wrap2()">',
' <section class="slide active"><h1>Title</h1></section>',
' </div></div>',
'</body></html>',
].join('\n');
const parsed = parseDeckThumbnails(deck, '/api/projects/p1/raw/');
expect(parsed.renderable).toBe(true);
const ancestorAttrNames = parsed.ancestors.flatMap((a) => a.attributes.map(([n]) => n.toLowerCase()));
// wrapper inline handlers are dropped before they are recreated in the DOM
expect(ancestorAttrNames.some((n) => n.startsWith('on'))).toBe(false);
// benign wrapper attributes (class) survive so CSS still targets the chain
expect(ancestorAttrNames).toContain('class');
});
it('neutralizes a slide whose root element is itself executable/navigable', () => {
const deck = [
'<!doctype html><html><head><style>',
' .deck-stage { width: 1920px; height: 1080px; }',
'</style></head><body>',
' <div class="deck-stage" id="deck-stage">',
' <form class="slide active" onsubmit="steal()" action="https://evil.example"><h1>Title</h1></form>',
' </div>',
'</body></html>',
].join('\n');
const parsed = parseDeckThumbnails(deck, '/api/projects/p1/raw/');
expect(parsed.renderable).toBe(true);
const slide = parsed.slides[0] ?? '';
// the navigable/submittable root element and its handlers are removed ...
expect(slide).not.toMatch(/<form/i);
expect(slide).not.toMatch(/onsubmit/i);
expect(slide).not.toMatch(/action=/i);
// ... while its inert content is preserved
expect(slide).toContain('Title');
});
it('removes SVG SMIL animation that could rewrite a sanitized attribute', () => {
const deck = [
'<!doctype html><html><head><style>',
' .deck-stage { width: 1920px; height: 1080px; }',
'</style></head><body>',
' <div class="deck-stage" id="deck-stage">',
' <section class="slide active">',
' <svg><a><animate attributeName="href" to="javascript:steal()" /></a></svg>',
' </section>',
' </div>',
'</body></html>',
].join('\n');
const parsed = parseDeckThumbnails(deck, '/api/projects/p1/raw/');
expect(parsed.renderable).toBe(true);
const slide = parsed.slides[0] ?? '';
expect(slide).not.toMatch(/<animate/i);
expect(slide).not.toMatch(/javascript:/i);
});
it('rejects a font-stylesheet link whose host is not exactly an approved CDN', () => {
const deck = [
'<!doctype html><html><head>',
' <link rel="stylesheet" href="https://evil.example/fonts.googleapis.com/inject.css">',
' <style>.deck-stage { width: 1920px; height: 1080px; }</style>',
'</head><body>',
' <div class="deck-stage" id="deck-stage"><section class="slide active"><h1>Title</h1></section></div>',
'</body></html>',
].join('\n');
const parsed = parseDeckThumbnails(deck, '/api/projects/p1/raw/');
// a substring hostname match would inject this stylesheet into the app doc;
// it must be treated as an untrusted external stylesheet instead.
expect(parsed.renderable).toBe(false);
expect(parsed.reason).toBe('external-stylesheet');
expect(parsed.fontLinks).not.toContain('https://evil.example/fonts.googleapis.com/inject.css');
});
it('still accepts a genuine approved font CDN stylesheet link', () => {
const deck = [
'<!doctype html><html><head>',
' <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter">',
' <style>.deck-stage { width: 1920px; height: 1080px; }</style>',
'</head><body>',
' <div class="deck-stage" id="deck-stage"><section class="slide active"><h1>Title</h1></section></div>',
'</body></html>',
].join('\n');
const parsed = parseDeckThumbnails(deck, '/api/projects/p1/raw/');
expect(parsed.renderable).toBe(true);
expect(parsed.fontLinks).toContain('https://fonts.googleapis.com/css2?family=Inter');
});
it('lifts an approved font @import into the host so thumbnail typography matches', () => {
const fontHref = 'https://fonts.googleapis.com/css2?family=JetBrains+Mono:ital,wght@0,300;0,700&display=swap';
const deck = [
'<!doctype html><html><head><style>',
` @import url('${fontHref}');`,
' .deck-stage { width: 1920px; height: 1080px; }',
'</style></head><body>',
' <div class="deck-stage" id="deck-stage"><section class="slide active"><h1>Title</h1></section></div>',
'</body></html>',
].join('\n');
const parsed = parseDeckThumbnails(deck, '/api/projects/p1/raw/');
expect(parsed.renderable).toBe(true);
expect(parsed.fontLinks).toContain(fontHref);
expect(parsed.styleText).not.toContain('@import');
});
it.each([
['quoted content', `.deck-stage::after { content: "@import url('https://fonts.googleapis.com/css2?family=Fake');"; }`],
['a quoted custom property', `.deck-stage { --example: "@import url('https://fonts.googleapis.com/css2?family=Fake');"; }`],
])('does not treat @import text inside %s as a stylesheet import', (_case, declaration) => {
const deck = frameworkDeck(1).replace(
'.deck-stage { width: 1920px; height: 1080px; background: var(--bg); }',
`${declaration}\n.deck-stage { width: 1920px; height: 1080px; background: var(--bg); }`,
);
const parsed = parseDeckThumbnails(deck);
expect(parsed.renderable).toBe(true);
expect(parsed.fontLinks).toEqual([]);
expect(parsed.styleText).toContain('@import url');
expect(parsed.styleText).toContain('.deck-stage { width: 1920px');
});
it('lifts a top-level import with a directly quoted URL', () => {
const fontHref = 'https://fonts.googleapis.com/css2?family=Inter';
const deck = frameworkDeck(1).replace('<style>', `<style>@import "${fontHref}";`);
const parsed = parseDeckThumbnails(deck);
expect(parsed.renderable).toBe(true);
expect(parsed.fontLinks).toContain(fontHref);
expect(parsed.styleText).not.toContain('@import');
});
it.each([
['a malformed import', '@import url("https://fonts.googleapis.com/css2?family=Inter";'],
['a non-font import', '@import url("https://cdn.example.com/layout.css");'],
])('falls back for %s', (_case, importRule) => {
const deck = frameworkDeck(1).replace('<style>', `<style>${importRule}`);
const parsed = parseDeckThumbnails(deck);
expect(parsed.renderable).toBe(false);
expect(parsed.reason).toBe('external-stylesheet');
});
it.each([
['print media', '@import url("https://fonts.googleapis.com/css2?family=Inter") print;'],
[
'a true supports condition',
'@import url("https://fonts.googleapis.com/css2?family=Inter") supports(display: grid);',
],
[
'a false supports condition',
'@import url("https://fonts.googleapis.com/css2?family=Inter") supports(display: unknown-value);',
],
['a named layer', '@import url("https://fonts.googleapis.com/css2?family=Inter") layer(deck-fonts);'],
])('falls back rather than changing the semantics of %s', (_case, importRule) => {
const deck = frameworkDeck(1).replace('<style>', `<style>${importRule}`);
const parsed = parseDeckThumbnails(deck);
expect(parsed.renderable).toBe(false);
expect(parsed.reason).toBe('external-stylesheet');
});
it('falls back for an import after a normal rule', () => {
const deck = frameworkDeck(1).replace(
'</style>',
'@import url("https://fonts.googleapis.com/css2?family=Inter");</style>',
);
const parsed = parseDeckThumbnails(deck);
expect(parsed.renderable).toBe(false);
expect(parsed.reason).toBe('external-stylesheet');
});
it('falls back when a slide-nested style imports unapproved CSS', () => {
const deck = [
'<!doctype html><html><head><style>.deck-stage { width: 1920px; height: 1080px; }</style></head><body>',
' <div class="deck-stage" id="deck-stage">',
' <section class="slide active">',
' <style>@import url("https://evil.example/nested.css");</style>',
' <h1>Title</h1>',
' </section>',
' </div>',
'</body></html>',
].join('\n');
const parsed = parseDeckThumbnails(deck, '/api/projects/p1/raw/');
// Every style block contributes to the shadow stylesheet, including one
// nested inside a slide before the markup sanitizer removes that element.
// An unapproved import therefore makes static rendering unsafe/incomplete
// and must use the isolated iframe fallback.
expect(parsed.renderable).toBe(false);
expect(parsed.reason).toBe('external-stylesheet');
});
});