| 1 | import type { IrBox, IrText, RawNode, RawSlide, RawSnapshot, RawStyle } from './ir' |
| 2 | import { describe, expect, it } from 'vitest' |
| 3 | import { parseColor } from './color' |
| 4 | import { normalize, parseLength, parseShadow, rasterReasonFor } from './normalize' |
| 5 | |
| 6 | /** |
| 7 | * One named case per rule. |
| 8 | * |
| 9 | * Every case here is a defect that reached a rendered deck. A rule without one |
| 10 | * is a rule nobody will remember the reason for. |
| 11 | */ |
| 12 | |
| 13 | const BASE_STYLE: RawStyle = { |
| 14 | display: 'block', |
| 15 | position: 'static', |
| 16 | zIndex: 'auto', |
| 17 | visibility: 'visible', |
| 18 | opacity: '1', |
| 19 | color: 'rgb(0, 0, 0)', |
| 20 | backgroundColor: 'rgba(0, 0, 0, 0)', |
| 21 | backgroundImage: 'none', |
| 22 | fontFamily: 'Inter, sans-serif', |
| 23 | fontSize: '16px', |
| 24 | fontWeight: '400', |
| 25 | fontStyle: 'normal', |
| 26 | textAlign: 'start', |
| 27 | textDecorationLine: 'none', |
| 28 | textTransform: 'none', |
| 29 | letterSpacing: 'normal', |
| 30 | lineHeight: 'normal', |
| 31 | whiteSpace: 'normal', |
| 32 | paddingLeft: '0px', |
| 33 | paddingRight: '0px', |
| 34 | borderTopWidth: '0px', |
| 35 | borderTopStyle: 'none', |
| 36 | borderTopColor: 'rgb(0, 0, 0)', |
| 37 | borderRightWidth: '0px', |
| 38 | borderRightStyle: 'none', |
| 39 | borderRightColor: 'rgb(0, 0, 0)', |
| 40 | borderBottomWidth: '0px', |
| 41 | borderBottomStyle: 'none', |
| 42 | borderBottomColor: 'rgb(0, 0, 0)', |
| 43 | borderLeftWidth: '0px', |
| 44 | borderLeftStyle: 'none', |
| 45 | borderLeftColor: 'rgb(0, 0, 0)', |
| 46 | borderTopLeftRadius: '0px', |
| 47 | boxShadow: 'none', |
| 48 | filter: 'none', |
| 49 | backdropFilter: 'none', |
| 50 | mixBlendMode: 'normal', |
| 51 | clipPath: 'none', |
| 52 | transform: 'none', |
| 53 | writingMode: 'horizontal-tb', |
| 54 | webkitBackgroundClip: 'border-box', |
| 55 | overflow: 'visible', |
| 56 | top: 'auto', |
| 57 | right: 'auto', |
| 58 | bottom: 'auto', |
| 59 | left: 'auto', |
| 60 | width: 'auto', |
| 61 | height: 'auto', |
| 62 | } |
| 63 | |
| 64 | function style(overrides: Partial<RawStyle> = {}): RawStyle { |
| 65 | return { |
| 66 | ...BASE_STYLE, |
| 67 | ...overrides, |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | const RECT = { x: 0, y: 0, w: 100, h: 20 } |
| 72 | |
| 73 | function el(id: number, parent: number, tag: string, styleIndex: number, extra: Partial<RawNode> = {}): RawNode { |
| 74 | return { |
| 75 | id, |
| 76 | parent, |
| 77 | tag, |
| 78 | style: styleIndex, |
| 79 | rect: RECT, |
| 80 | ...extra, |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | function text(id: number, parent: number, value: string, extra: Partial<RawNode> = {}): RawNode { |
| 85 | return { |
| 86 | id, |
| 87 | parent, |
| 88 | tag: '#text', |
| 89 | style: -1, |
| 90 | rect: RECT, |
| 91 | text: value, |
| 92 | glyphRects: [RECT], |
| 93 | ...extra, |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | function snapshot(nodes: RawNode[], styles: RawStyle[]): RawSnapshot { |
| 98 | const slide: RawSlide = { |
| 99 | no: 1, |
| 100 | clickIndex: 0, |
| 101 | containerId: '001-01', |
| 102 | size: { w: 980, h: 552 }, |
| 103 | nodes, |
| 104 | } |
| 105 | return { |
| 106 | slides: [slide], |
| 107 | styles, |
| 108 | fontResolution: { 'Inter, sans-serif': 'Inter' }, |
| 109 | unplaceablePseudos: [], |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | function run(nodes: RawNode[], styles: RawStyle[]) { |
| 114 | return normalize(snapshot(nodes, styles), { |
| 115 | notes: new Map(), |
| 116 | }) |
| 117 | } |
| 118 | |
| 119 | function texts(nodes: any[]): IrText[] { |
| 120 | return nodes.filter(n => n.kind === 'text') |
| 121 | } |
| 122 | |
| 123 | function boxes(nodes: any[]): IrBox[] { |
| 124 | return nodes.filter(n => n.kind === 'box') |
| 125 | } |
| 126 | |
| 127 | describe('value parsing', () => { |
| 128 | it('reads rgb and rgba', () => { |
| 129 | expect(parseColor('rgb(255, 0, 0)')).toEqual({ r: 255, g: 0, b: 0, a: 1 }) |
| 130 | expect(parseColor('rgba(0, 0, 0, 0.5)')).toEqual({ r: 0, g: 0, b: 0, a: 0.5 }) |
| 131 | expect(parseColor('transparent')).toEqual({ r: 0, g: 0, b: 0, a: 0 }) |
| 132 | expect(parseColor(undefined)).toBeUndefined() |
| 133 | }) |
| 134 | |
| 135 | it('reads the modern color syntaxes Chromium leaves in computed values', () => { |
| 136 | // A theme authored in modern syntax keeps these in the computed value. |
| 137 | // Returning undefined for them dropped every fill and text color on the |
| 138 | // slide with nothing in the log to explain it. |
| 139 | const red = parseColor('oklch(0.628 0.2577 29.23)')! |
| 140 | expect(red.r).toBeGreaterThan(248) |
| 141 | expect(red.g).toBeLessThan(8) |
| 142 | expect(red.b).toBeLessThan(8) |
| 143 | |
| 144 | expect(parseColor('hsl(120, 100%, 50%)')).toEqual({ r: 0, g: 255, b: 0, a: 1 }) |
| 145 | expect(parseColor('color(srgb 1 0 0)')).toEqual({ r: 255, g: 0, b: 0, a: 1 }) |
| 146 | }) |
| 147 | |
| 148 | it('reports a color it cannot read instead of failing silently', () => { |
| 149 | const bad = style({ backgroundColor: 'lab(50% 40 59.5)' }) |
| 150 | const { unparsedColors } = run([ |
| 151 | el(0, -1, 'DIV', 1), |
| 152 | ], [BASE_STYLE, bad]) |
| 153 | expect(unparsedColors).toContain('lab(50% 40 59.5)') |
| 154 | // Per run, not module state: a drained global leaks into the next export |
| 155 | // whenever this one throws before the caller collects it. |
| 156 | expect(run([ |
| 157 | el(0, -1, 'DIV', 0), |
| 158 | ], [BASE_STYLE]).unparsedColors).toHaveLength(0) |
| 159 | }) |
| 160 | |
| 161 | it('reads a pixel length, and a percentage against its basis', () => { |
| 162 | expect(parseLength('12.5px')).toBe(12.5) |
| 163 | expect(parseLength('normal')).toBe(0) |
| 164 | // Chromium keeps border-radius as a percentage rather than resolving it, so |
| 165 | // `parseFloat` alone turned `50%` on a 200px box into 50px. |
| 166 | expect(parseLength('50%', 200)).toBe(100) |
| 167 | }) |
| 168 | |
| 169 | it('reads a box-shadow into PowerPoint polar form', () => { |
| 170 | const shadow = parseShadow('rgba(0, 0, 0, 0.5) 0px 4px 8px 0px')! |
| 171 | expect(shadow.blur).toBe(8) |
| 172 | expect(shadow.offset).toBe(4) |
| 173 | expect(shadow.angle).toBe(90) |
| 174 | expect(shadow.color.a).toBe(0.5) |
| 175 | // No DrawingML equivalent for either of these. |
| 176 | expect(parseShadow('none')).toBeUndefined() |
| 177 | expect(parseShadow('rgb(0, 0, 0) 0px 2px 4px inset')).toBeUndefined() |
| 178 | }) |
| 179 | }) |
| 180 | |
| 181 | describe('a <br> becomes a real line break', () => { |
| 182 | it('breaks between the runs either side of it', () => { |
| 183 | const nodes = [ |
| 184 | el(0, -1, 'DIV', 0), |
| 185 | text(1, 0, 'line one'), |
| 186 | el(2, 0, 'BR', 1), |
| 187 | text(3, 0, 'line two'), |
| 188 | ] |
| 189 | const { slides } = run(nodes, [ |
| 190 | BASE_STYLE, |
| 191 | style({ display: 'inline' }), |
| 192 | ]) |
| 193 | const out = texts(slides[0].nodes)[0] |
| 194 | // Without this the two runs are concatenated and export as "line oneline two". |
| 195 | expect(out.runs.map(r => r.text)).toEqual(['line one', 'line two']) |
| 196 | expect(out.runs[1].breakBefore).toBe(true) |
| 197 | expect(out.runs[0].breakBefore).toBeUndefined() |
| 198 | }) |
| 199 | }) |
| 200 | |
| 201 | describe('code blocks keep their line structure', () => { |
| 202 | it('turns a pre newline into a real line break', () => { |
| 203 | const pre = style({ whiteSpace: 'pre' }) |
| 204 | const inline = style({ display: 'inline', whiteSpace: 'pre' }) |
| 205 | const nodes = [ |
| 206 | el(0, -1, 'PRE', 1), |
| 207 | el(1, 0, 'SPAN', 2), |
| 208 | text(2, 1, 'const a = 1'), |
| 209 | // Shiki separates its line spans with a bare newline text node, which |
| 210 | // has no client rects of its own. |
| 211 | text(3, 0, '\n', { glyphRects: [] }), |
| 212 | el(4, 0, 'SPAN', 2), |
| 213 | text(5, 4, 'const b = 2'), |
| 214 | ] |
| 215 | const { slides } = run(nodes, [BASE_STYLE, pre, inline]) |
| 216 | const out = texts(slides[0].nodes)[0] |
| 217 | // Folded to a space, every multi-line code block exported as one |
| 218 | // re-wrapped paragraph, which is core content for a developer slide tool. |
| 219 | expect(out.runs.map(r => r.text)).toEqual(['const a = 1', 'const b = 2']) |
| 220 | expect(out.runs[1].breakBefore).toBe(true) |
| 221 | }) |
| 222 | |
| 223 | it('keeps a blank line between two consecutive breaks', () => { |
| 224 | const inline = style({ display: 'inline' }) |
| 225 | const nodes = [ |
| 226 | el(0, -1, 'DIV', 0), |
| 227 | text(1, 0, 'first'), |
| 228 | el(2, 0, 'BR', 1), |
| 229 | el(3, 0, 'BR', 1), |
| 230 | text(4, 0, 'second'), |
| 231 | ] |
| 232 | const { slides } = run(nodes, [BASE_STYLE, inline]) |
| 233 | const out = texts(slides[0].nodes)[0] |
| 234 | // `softBreakBefore` is one break per run, so two in a row need an empty |
| 235 | // run between them or the blank line silently disappears. |
| 236 | expect(out.runs).toHaveLength(3) |
| 237 | expect(out.runs[1].text).toBe('') |
| 238 | expect(out.runs[1].breakBefore).toBe(true) |
| 239 | expect(out.runs[2].breakBefore).toBe(true) |
| 240 | }) |
| 241 | |
| 242 | it('does not open a box with a leading break', () => { |
| 243 | const inline = style({ display: 'inline' }) |
| 244 | const nodes = [ |
| 245 | el(0, -1, 'DIV', 0), |
| 246 | el(1, 0, 'BR', 1), |
| 247 | text(2, 0, 'text'), |
| 248 | ] |
| 249 | const { slides } = run(nodes, [BASE_STYLE, inline]) |
| 250 | expect(texts(slides[0].nodes)[0].runs[0].breakBefore).toBeUndefined() |
| 251 | }) |
| 252 | }) |
| 253 | |
| 254 | describe('an inline image survives the text walk', () => { |
| 255 | it('draws an <img> that sits inside a line of text', () => { |
| 256 | const inline = style({ display: 'inline' }) |
| 257 | const nodes = [ |
| 258 | el(0, -1, 'P', 0), |
| 259 | text(1, 0, 'before'), |
| 260 | el(2, 0, 'IMG', 1, { src: 'data:image/png;base64,AAAA', alt: 'logo' }), |
| 261 | ] |
| 262 | const { slides } = run(nodes, [BASE_STYLE, inline]) |
| 263 | // Inline children go to the text grouper, which finds no text under an |
| 264 | // IMG, so the image used to vanish from the slide entirely. |
| 265 | const images = slides[0].nodes.filter(n => n.kind === 'image') |
| 266 | expect(images).toHaveLength(1) |
| 267 | expect((images[0] as any).alt).toBe('logo') |
| 268 | }) |
| 269 | }) |
| 270 | |
| 271 | describe('the space between inline elements', () => { |
| 272 | it('keeps a whitespace-only node between two spans', () => { |
| 273 | const inline = style({ display: 'inline' }) |
| 274 | const nodes = [ |
| 275 | el(0, -1, 'DIV', 0), |
| 276 | el(1, 0, 'SPAN', 1), |
| 277 | text(2, 1, 'Hello'), |
| 278 | text(3, 0, ' '), |
| 279 | el(4, 0, 'SPAN', 1), |
| 280 | text(5, 4, 'World'), |
| 281 | ] |
| 282 | const { slides } = run(nodes, [BASE_STYLE, inline]) |
| 283 | const out = texts(slides[0].nodes)[0] |
| 284 | // Dropping it exported "HelloWorld". |
| 285 | expect(out.runs.map(r => r.text).join('')).toBe('Hello World') |
| 286 | }) |
| 287 | }) |
| 288 | |
| 289 | describe('inline-level layout containers still lay out as boxes', () => { |
| 290 | it('does not merge the cells of an inline-flex badge', () => { |
| 291 | const badge = style({ display: 'inline-flex' }) |
| 292 | const cell = style({ display: 'inline' }) |
| 293 | const nodes = [ |
| 294 | el(0, -1, 'DIV', 0), |
| 295 | el(1, 0, 'SPAN', 1), |
| 296 | el(2, 1, 'SPAN', 2), |
| 297 | text(3, 2, 'Left cell'), |
| 298 | el(4, 1, 'SPAN', 2), |
| 299 | text(5, 4, 'Right cell'), |
| 300 | ] |
| 301 | const { slides } = run(nodes, [BASE_STYLE, badge, cell]) |
| 302 | // `^inline` matched inline-flex, so the walk descended and concatenated |
| 303 | // the cells: the trap-3 bug one display value over. |
| 304 | const out = texts(slides[0].nodes) |
| 305 | expect(out).toHaveLength(2) |
| 306 | expect(out.map(t => t.runs[0].text)).toEqual(['Left cell', 'Right cell']) |
| 307 | }) |
| 308 | }) |
| 309 | |
| 310 | describe('a list item flows its children as text', () => { |
| 311 | it('keeps a bold lead-in joined inside an <li>', () => { |
| 312 | const item = style({ display: 'list-item' }) |
| 313 | const inline = style({ display: 'inline' }) |
| 314 | const nodes = [ |
| 315 | el(0, -1, 'LI', 1, { marker: '\u2022 ' }), |
| 316 | text(1, 0, 'plain '), |
| 317 | el(2, 0, 'B', 2), |
| 318 | text(3, 2, 'bold'), |
| 319 | text(4, 0, ' tail'), |
| 320 | ] |
| 321 | const { slides } = run(nodes, [BASE_STYLE, item, inline]) |
| 322 | const out = texts(slides[0].nodes) |
| 323 | // One marker box plus ONE grouped paragraph. Treating `list-item` as a |
| 324 | // layout container fragmented this into four boxes. |
| 325 | expect(out).toHaveLength(2) |
| 326 | expect(out[1].runs.map(r => r.text)).toEqual(['plain ', 'bold', ' tail']) |
| 327 | }) |
| 328 | }) |
| 329 | |
| 330 | describe('element opacity reaches the fill', () => { |
| 331 | it('folds opacity into the color alpha', () => { |
| 332 | const faded = style({ backgroundColor: 'rgb(0, 0, 0)' }) |
| 333 | // Carried on the NODE, compounded down the tree by the walker, because CSS |
| 334 | // opacity does not inherit and DrawingML has no group opacity to stand in |
| 335 | // for a half-transparent wrapper. |
| 336 | const nodes = [ |
| 337 | el(0, -1, 'DIV', 1, { opacity: 0.5 }), |
| 338 | ] |
| 339 | const { slides } = run(nodes, [BASE_STYLE, faded]) |
| 340 | // DrawingML has no element opacity, only per-fill alpha, so a half |
| 341 | // transparent overlay exported fully opaque and hid what was behind it. |
| 342 | expect(boxes(slides[0].nodes)[0].fill).toEqual({ r: 0, g: 0, b: 0, a: 0.5 }) |
| 343 | }) |
| 344 | }) |
| 345 | |
| 346 | describe('paint order follows CSS, not the document', () => { |
| 347 | it('draws a positioned element above in-flow content that comes later', () => { |
| 348 | const positioned = style({ position: 'absolute' }) |
| 349 | const backdrop = style({ backgroundImage: 'url(/img/scenery.png)' }) |
| 350 | const corner = { x: 900, y: 10, w: 60, h: 20 } |
| 351 | const nodes = [ |
| 352 | // A slide's page counter is the FIRST child of its container, and it is |
| 353 | // a positioned <footer> wrapping a plain <div>, so the layer has to come |
| 354 | // from the nearest POSITIONED ancestor rather than the nearest styled one. |
| 355 | el(0, -1, 'FOOTER', 1, { rect: corner }), |
| 356 | el(1, 0, 'DIV', 0, { rect: corner }), |
| 357 | text(2, 1, '3 / 35', { |
| 358 | rect: corner, |
| 359 | glyphRects: [corner], |
| 360 | }), |
| 361 | // The full-bleed background comes after it in the tree, so array order |
| 362 | // painted it straight over the counter and the counter vanished. |
| 363 | el(3, -1, 'DIV', 2, { |
| 364 | rect: { x: 0, y: 0, w: 980, h: 552 }, |
| 365 | }), |
| 366 | ] |
| 367 | const { slides } = run(nodes, [BASE_STYLE, positioned, backdrop]) |
| 368 | const kinds = slides[0].nodes.map(n => n.kind) |
| 369 | expect(kinds.indexOf('raster')).toBeLessThan(kinds.indexOf('text')) |
| 370 | }) |
| 371 | |
| 372 | it('keeps document order within one layer', () => { |
| 373 | const nodes = [ |
| 374 | el(0, -1, 'DIV', 1), |
| 375 | text(1, 0, 'first'), |
| 376 | el(2, -1, 'DIV', 1), |
| 377 | text(3, 2, 'second'), |
| 378 | ] |
| 379 | const { slides } = run(nodes, [ |
| 380 | BASE_STYLE, |
| 381 | style({ backgroundColor: 'rgb(1, 2, 3)' }), |
| 382 | ]) |
| 383 | const out = texts(slides[0].nodes) |
| 384 | expect(out.map(t => t.runs[0].text)).toEqual(['first', 'second']) |
| 385 | }) |
| 386 | }) |
| 387 | |
| 388 | describe('opacity reaches text, not only shapes', () => { |
| 389 | it('greys a paragraph that its ancestor dimmed', () => { |
| 390 | // Slidev's own stylesheet greys this kind of paragraph with `opacity: 0.5` |
| 391 | // rather than a color. Opacity is compounded down the tree onto elements, |
| 392 | // but a text node has no style of its own, so every run lost it and the |
| 393 | // paragraph exported solid black. |
| 394 | const nodes = [ |
| 395 | el(0, -1, 'P', 0, { opacity: 0.5 }), |
| 396 | text(1, 0, 'a dimmed paragraph', { opacity: 0.5 }), |
| 397 | ] |
| 398 | const { slides } = run(nodes, [BASE_STYLE]) |
| 399 | expect(texts(slides[0].nodes)[0].runs[0].color).toEqual({ r: 0, g: 0, b: 0, a: 0.5 }) |
| 400 | }) |
| 401 | |
| 402 | it('centers a list marker on its line rather than above it', () => { |
| 403 | const item = style({ display: 'list-item' }) |
| 404 | const { slides } = run([ |
| 405 | el(0, -1, 'LI', 1, { marker: '\u25AA ' }), |
| 406 | ], [BASE_STYLE, item]) |
| 407 | expect(texts(slides[0].nodes)[0].valign).toBe('middle') |
| 408 | }) |
| 409 | }) |
| 410 | |
| 411 | describe('the slide keeps its own background', () => { |
| 412 | it('carries the container background into the IR', () => { |
| 413 | const raw = snapshot([ |
| 414 | el(0, -1, 'DIV', 0), |
| 415 | ], [BASE_STYLE]) |
| 416 | raw.slides[0].background = 'rgb(18, 18, 18)' |
| 417 | const { slides } = normalize(raw, { |
| 418 | notes: new Map(), |
| 419 | }) |
| 420 | // The walk starts at the container's children, so without this every slide |
| 421 | // exported onto PowerPoint's white and a dark theme became unreadable. |
| 422 | expect(slides[0].background).toEqual({ r: 18, g: 18, b: 18, a: 1 }) |
| 423 | }) |
| 424 | }) |
| 425 | |
| 426 | describe('a shrink-wrapped container leaves no room to wrap', () => { |
| 427 | it('widens a centered block whose container is exactly its ink', () => { |
| 428 | const centered = style({ textAlign: 'center' }) |
| 429 | // The container fits the text exactly, which is what a centered statement |
| 430 | // block does. PowerPoint sets the same string slightly wider than |
| 431 | // Chromium, so with no headroom the longest line wraps and the block |
| 432 | // gains a line. |
| 433 | const box = { x: 155, y: 192, w: 487, h: 90 } |
| 434 | const nodes = [ |
| 435 | el(0, -1, 'DIV', 1, { rect: box }), |
| 436 | text(1, 0, 'a long centered statement', { |
| 437 | rect: box, |
| 438 | glyphRects: [ |
| 439 | { x: 155, y: 192, w: 400, h: 30 }, |
| 440 | { x: 155, y: 222, w: 487, h: 30 }, |
| 441 | { x: 155, y: 252, w: 380, h: 30 }, |
| 442 | ], |
| 443 | }), |
| 444 | ] |
| 445 | const { slides } = run(nodes, [BASE_STYLE, centered]) |
| 446 | const out = texts(slides[0].nodes)[0] |
| 447 | expect(out.rect.w).toBeGreaterThan(487) |
| 448 | // Grown about the center, so the text does not slide sideways. |
| 449 | expect(out.rect.x + out.rect.w / 2).toBeCloseTo(155 + 487 / 2, 5) |
| 450 | }) |
| 451 | |
| 452 | it('leaves a column-constrained paragraph at its container width', () => { |
| 453 | const nodes = [ |
| 454 | el(0, -1, 'DIV', 0, { |
| 455 | rect: { x: 0, y: 0, w: 400, h: 60 }, |
| 456 | }), |
| 457 | text(1, 0, 'wrapped body copy', { |
| 458 | rect: { x: 0, y: 0, w: 400, h: 60 }, |
| 459 | glyphRects: [ |
| 460 | { x: 0, y: 0, w: 380, h: 20 }, |
| 461 | { x: 0, y: 24, w: 200, h: 20 }, |
| 462 | ], |
| 463 | }), |
| 464 | ] |
| 465 | const { slides } = run(nodes, [BASE_STYLE]) |
| 466 | // 20px of headroom is already more than the 2% this needs, so widening it |
| 467 | // would push the text into whatever sits beside it. |
| 468 | expect(texts(slides[0].nodes)[0].rect.w).toBe(400) |
| 469 | }) |
| 470 | }) |
| 471 | |
| 472 | describe('--range is applied by the caller, not the page', () => { |
| 473 | it('exposes the slide number so out-of-range slides can be filtered', () => { |
| 474 | // The print route renders EVERY slide whatever the `range` query says, so |
| 475 | // out-of-range containers are fully laid out rather than hidden, and an |
| 476 | // exporter that trusts the page exports the whole deck. `no` and |
| 477 | // `containerId` are what let the caller drop them, the same way the image |
| 478 | // exporter does with `pages.includes(slideNo)`. |
| 479 | const raw = snapshot([ |
| 480 | el(0, -1, 'DIV', 0), |
| 481 | text(1, 0, 'content'), |
| 482 | ], [BASE_STYLE]) |
| 483 | raw.slides[0].no = 7 |
| 484 | raw.slides[0].containerId = '007-03' |
| 485 | const { slides } = normalize(raw, { |
| 486 | notes: new Map(), |
| 487 | }) |
| 488 | expect(slides[0].no).toBe(7) |
| 489 | expect(slides[0].containerId).toBe('007-03') |
| 490 | }) |
| 491 | }) |
| 492 | |
| 493 | describe('pseudo-element decorations are drawn', () => { |
| 494 | it('draws an ::after that paints a background image', () => { |
| 495 | const mark = style({ |
| 496 | position: 'absolute', |
| 497 | backgroundImage: 'url(/img/logo.png)', |
| 498 | width: '84px', |
| 499 | height: '33px', |
| 500 | }) |
| 501 | const rect = { x: 858, y: 493, w: 84, h: 33 } |
| 502 | const nodes = [ |
| 503 | el(0, -1, 'DIV', 0), |
| 504 | el(1, 0, '::AFTER', 1, { rect, pageRect: rect }), |
| 505 | ] |
| 506 | const { slides, rasterRequests } = run(nodes, [BASE_STYLE, mark]) |
| 507 | // A pseudo-element has no DOM node, so a walk over the tree cannot see it. |
| 508 | // A corporate logo drawn this way was silently absent from every slide. |
| 509 | const raster = slides[0].nodes.find(n => n.kind === 'raster') as any |
| 510 | expect(raster).toBeDefined() |
| 511 | expect(raster.rect).toEqual(rect) |
| 512 | // And no locator can point at it, so the capture clips the page instead. |
| 513 | expect(rasterRequests[0].clip).toEqual(rect) |
| 514 | }) |
| 515 | |
| 516 | it('draws an ::before that paints a string', () => { |
| 517 | const quote = style({ position: 'absolute', width: '20px', height: '20px' }) |
| 518 | const rect = { x: 10, y: 10, w: 20, h: 20 } |
| 519 | const nodes = [ |
| 520 | el(0, -1, 'DIV', 0), |
| 521 | el(1, 0, '::BEFORE', 1, { rect, pageRect: rect, text: '\u201C' }), |
| 522 | ] |
| 523 | const { slides } = run(nodes, [BASE_STYLE, quote]) |
| 524 | expect(texts(slides[0].nodes)[0].runs[0].text).toBe('\u201C') |
| 525 | }) |
| 526 | }) |
| 527 | |
| 528 | describe('line counting tolerates sub-pixel layout', () => { |
| 529 | it('treats fragments a fraction of a pixel apart as one line', () => { |
| 530 | const nodes = [ |
| 531 | el(0, -1, 'DIV', 0), |
| 532 | text(1, 0, 'one visual line', { |
| 533 | glyphRects: [ |
| 534 | { x: 0, y: 10.4, w: 50, h: 20 }, |
| 535 | { x: 50, y: 10.6, w: 50, h: 20 }, |
| 536 | ], |
| 537 | }), |
| 538 | ] |
| 539 | const { slides } = run(nodes, [BASE_STYLE]) |
| 540 | // Rounding put these on 10 and 11 and reported two lines, which flips |
| 541 | // `wrap` in the builder, the one decision this number exists to make. |
| 542 | expect(texts(slides[0].nodes)[0].lineCount).toBe(1) |
| 543 | }) |
| 544 | }) |
| 545 | |
| 546 | describe('layout containers are not text blocks', () => { |
| 547 | it('keeps grid cells apart instead of concatenating them', () => { |
| 548 | const grid = style({ display: 'grid' }) |
| 549 | // The cells are INLINE on purpose. CSS blockifies the children of a grid |
| 550 | // container into grid items whatever their specified display says, so they |
| 551 | // are separate boxes. An earlier fixture used block children, which fall |
| 552 | // into separate text groups anyway, so it passed even with the rule |
| 553 | // disabled and tested nothing. |
| 554 | const cell = style({ display: 'inline' }) |
| 555 | const nodes = [ |
| 556 | el(0, -1, 'DIV', 1), |
| 557 | el(1, 0, 'SPAN', 2), |
| 558 | text(2, 1, 'Left cell'), |
| 559 | el(3, 0, 'SPAN', 2), |
| 560 | text(4, 3, 'Right cell'), |
| 561 | ] |
| 562 | const { slides } = run(nodes, [BASE_STYLE, grid, cell]) |
| 563 | const out = texts(slides[0].nodes) |
| 564 | // The original bug produced one run reading "Left cellRight cell". |
| 565 | expect(out).toHaveLength(2) |
| 566 | expect(out[0].runs[0].text).toBe('Left cell') |
| 567 | expect(out[1].runs[0].text).toBe('Right cell') |
| 568 | }) |
| 569 | }) |
| 570 | |
| 571 | describe('consecutive inline nodes form one anonymous block', () => { |
| 572 | it('joins a bold lead-in with the rest of its sentence', () => { |
| 573 | const inline = style({ display: 'inline' }) |
| 574 | const bold = style({ display: 'inline', fontWeight: '700' }) |
| 575 | const nodes = [ |
| 576 | el(0, -1, 'DIV', 0), |
| 577 | el(1, 0, 'B', 2), |
| 578 | text(2, 1, 'A bold lead-in'), |
| 579 | text(3, 0, ', and the rest of the sentence'), |
| 580 | el(4, 0, 'DIV', 0), |
| 581 | text(5, 4, 'A following block.'), |
| 582 | ] |
| 583 | const { slides } = run(nodes, [BASE_STYLE, inline, bold]) |
| 584 | const out = texts(slides[0].nodes) |
| 585 | // Two boxes: the anonymous block, then the nested block. Emitting three |
| 586 | // separate shapes made the bold lead-in and the tail lay out from the same |
| 587 | // origin and print on top of each other. |
| 588 | expect(out).toHaveLength(2) |
| 589 | expect(out[0].runs.map(r => r.text)).toEqual(['A bold lead-in', ', and the rest of the sentence']) |
| 590 | expect(out[0].runs[0].bold).toBe(true) |
| 591 | expect(out[0].runs[1].bold).toBeUndefined() |
| 592 | expect(out[1].runs[0].text).toBe('A following block.') |
| 593 | }) |
| 594 | }) |
| 595 | |
| 596 | describe('inline decorations paint before their text', () => { |
| 597 | it('emits a chip background ahead of the white label on it', () => { |
| 598 | const chip = style({ display: 'inline', backgroundColor: 'rgb(0, 128, 0)', color: 'rgb(255, 255, 255)' }) |
| 599 | const nodes = [ |
| 600 | el(0, -1, 'DIV', 0), |
| 601 | el(1, 0, 'SPAN', 1), |
| 602 | text(2, 1, 'NEW'), |
| 603 | ] |
| 604 | const { slides } = run(nodes, [BASE_STYLE, chip]) |
| 605 | const kinds = slides[0].nodes.map(n => n.kind) |
| 606 | // Paint order is array order. Emitted after the text, the chip covers its |
| 607 | // own label and white-on-green becomes white-on-white. |
| 608 | expect(kinds.indexOf('box')).toBeLessThan(kinds.indexOf('text')) |
| 609 | expect(boxes(slides[0].nodes)[0].fill).toEqual({ r: 0, g: 128, b: 0, a: 1 }) |
| 610 | }) |
| 611 | |
| 612 | it('anchors a chip label in its own box so it cannot drift off the chip', () => { |
| 613 | const inline = style({ display: 'inline' }) |
| 614 | const chip = style({ display: 'inline', backgroundColor: 'rgb(0, 128, 0)' }) |
| 615 | const label = { x: 122, y: 0, w: 30, h: 20 } |
| 616 | const nodes = [ |
| 617 | el(0, -1, 'DIV', 0), |
| 618 | el(1, 0, 'SPAN', 2, { rect: label }), |
| 619 | text(2, 1, 'NEW', { |
| 620 | rect: label, |
| 621 | glyphRects: [label], |
| 622 | }), |
| 623 | ] |
| 624 | const { slides } = run(nodes, [BASE_STYLE, inline, chip]) |
| 625 | const out = texts(slides[0].nodes) |
| 626 | // The chip's background is an absolutely positioned shape while the text |
| 627 | // around it flows, so sharing one box lets PowerPoint's metrics |
| 628 | // accumulate across the earlier runs and slide the label off its chip. |
| 629 | expect(out).toHaveLength(1) |
| 630 | expect(out[0].runs[0].text).toBe('NEW') |
| 631 | // Pinned to the chip and centered in it, not to its own ink. However much |
| 632 | // wider PowerPoint sets the string than the browser did, the label stays |
| 633 | // evenly inset instead of ending up hard against one edge. |
| 634 | expect(out[0].rect).toEqual(label) |
| 635 | expect(out[0].align).toBe('center') |
| 636 | expect(out[0].valign).toBe('middle') |
| 637 | }) |
| 638 | |
| 639 | it('leaves inline code in the middle of a sentence alone', () => { |
| 640 | const inline = style({ display: 'inline' }) |
| 641 | const code = style({ display: 'inline', backgroundColor: 'rgb(240, 240, 240)' }) |
| 642 | const nodes = [ |
| 643 | el(0, -1, 'P', 0), |
| 644 | text(1, 0, 'Use '), |
| 645 | el(2, 0, 'CODE', 2), |
| 646 | text(3, 2, 'npm i'), |
| 647 | text(4, 0, ' to begin.'), |
| 648 | ] |
| 649 | const { slides } = run(nodes, [BASE_STYLE, inline, code]) |
| 650 | // Slidev gives inline code a background, so an unconditional chip split |
| 651 | // cut ordinary sentences into three boxes and centered the code span; when |
| 652 | // such a sentence wrapped, the halves overlapped. A chip is a label on its |
| 653 | // own, not a word in the middle of a line. |
| 654 | const out = texts(slides[0].nodes) |
| 655 | expect(out).toHaveLength(1) |
| 656 | expect(out[0].runs.map(r => r.text)).toEqual(['Use ', 'npm i', ' to begin.']) |
| 657 | }) |
| 658 | }) |
| 659 | |
| 660 | describe('text-transform and letter-spacing are applied', () => { |
| 661 | it('uppercases the run rather than trusting PowerPoint to do it', () => { |
| 662 | const label = style({ textTransform: 'uppercase', letterSpacing: '2px' }) |
| 663 | const nodes = [ |
| 664 | el(0, -1, 'DIV', 1), |
| 665 | text(1, 0, 'section one'), |
| 666 | ] |
| 667 | const { slides } = run(nodes, [BASE_STYLE, label]) |
| 668 | const out = texts(slides[0].nodes)[0] |
| 669 | // PowerPoint has no text-transform, so an untransformed run exports the |
| 670 | // lower-case source text and the slide silently changes. |
| 671 | expect(out.runs[0].text).toBe('SECTION ONE') |
| 672 | expect(out.runs[0].letterSpacing).toBe(2) |
| 673 | }) |
| 674 | }) |
| 675 | |
| 676 | describe('text is positioned from glyph bounds', () => { |
| 677 | it('uses the glyph rects for a single line, not the element box', () => { |
| 678 | const nodes = [ |
| 679 | el(0, -1, 'DIV', 0, { |
| 680 | rect: { x: 0, y: 0, w: 500, h: 100 }, |
| 681 | }), |
| 682 | text(1, 0, 'one line', { |
| 683 | rect: { x: 0, y: 0, w: 500, h: 100 }, |
| 684 | glyphRects: [ |
| 685 | { x: 40, y: 10, w: 120, h: 20 }, |
| 686 | ], |
| 687 | }), |
| 688 | ] |
| 689 | const { slides } = run(nodes, [BASE_STYLE]) |
| 690 | const out = texts(slides[0].nodes)[0] |
| 691 | // Positioning from the element rect offsets the text by the container's |
| 692 | // padding, and its width bears no relation to the ink. |
| 693 | expect(out.rect).toEqual({ x: 40, y: 10, w: 120, h: 20 }) |
| 694 | expect(out.lineCount).toBe(1) |
| 695 | }) |
| 696 | |
| 697 | it('wraps multi-line text against the container content box', () => { |
| 698 | const padded = style({ paddingLeft: '8px', paddingRight: '8px' }) |
| 699 | const nodes = [ |
| 700 | el(0, -1, 'DIV', 1, { |
| 701 | rect: { x: 0, y: 0, w: 200, h: 100 }, |
| 702 | }), |
| 703 | text(1, 0, 'two lines here', { |
| 704 | rect: { x: 0, y: 0, w: 200, h: 100 }, |
| 705 | glyphRects: [ |
| 706 | { x: 8, y: 10, w: 120, h: 20 }, |
| 707 | { x: 8, y: 34, w: 90, h: 20 }, |
| 708 | ], |
| 709 | }), |
| 710 | ] |
| 711 | const { slides } = run(nodes, [BASE_STYLE, padded]) |
| 712 | const out = texts(slides[0].nodes)[0] |
| 713 | // Glyph bounds for wrapped text are the width of the LONGEST LINE, so |
| 714 | // handing PowerPoint 120px guarantees a second, tighter wrap: "a short caption" came back over three lines and overflowed its box. The browser |
| 715 | // wrapped against the content box, which is 200 less 8px either side. |
| 716 | expect(out.rect).toEqual({ x: 8, y: 10, w: 184, h: 44 }) |
| 717 | // Vertical position still comes from the glyphs, not the element. |
| 718 | expect(out.rect.y).toBe(10) |
| 719 | expect(out.lineCount).toBe(2) |
| 720 | }) |
| 721 | }) |
| 722 | |
| 723 | describe('list bullets survive as text', () => { |
| 724 | it('recovers a ::marker glyph that has no text node', () => { |
| 725 | const item = style({ display: 'list-item' }) |
| 726 | const nodes = [ |
| 727 | el(0, -1, 'LI', 1, { marker: '• ' }), |
| 728 | text(1, 0, 'first point'), |
| 729 | ] |
| 730 | const { slides } = run(nodes, [BASE_STYLE, item]) |
| 731 | const out = texts(slides[0].nodes) |
| 732 | // A plain DOM walk drops every bullet in the deck, because ::marker is not |
| 733 | // in the tree. |
| 734 | expect(out.some(t => t.runs[0].text.includes('•'))).toBe(true) |
| 735 | expect(out.some(t => t.runs[0].text === 'first point')).toBe(true) |
| 736 | }) |
| 737 | }) |
| 738 | |
| 739 | describe('what has to become a picture', () => { |
| 740 | it('rasterizes a mermaid svg whose labels are foreignObject', () => { |
| 741 | expect(rasterReasonFor(el(0, -1, 'SVG', 0, { hasForeignObject: true }), BASE_STYLE)) |
| 742 | .toBe('foreign-object') |
| 743 | }) |
| 744 | |
| 745 | it('rasterizes svg, canvas and cross-origin iframes', () => { |
| 746 | expect(rasterReasonFor(el(0, -1, 'SVG', 0), BASE_STYLE)).toBe('svg') |
| 747 | expect(rasterReasonFor(el(0, -1, 'CANVAS', 0), BASE_STYLE)).toBe('canvas') |
| 748 | // demo/starter embeds <Tweet>, which is a cross-origin frame the walker |
| 749 | // cannot descend into at all. |
| 750 | expect(rasterReasonFor(el(0, -1, 'IFRAME', 0), BASE_STYLE)).toBe('iframe') |
| 751 | }) |
| 752 | |
| 753 | it('rasterizes a css background image and a gradient-filled heading', () => { |
| 754 | expect(rasterReasonFor(el(0, -1, 'DIV', 0), style({ backgroundImage: 'url(cover.jpg)' }))) |
| 755 | .toBe('background-image') |
| 756 | expect(rasterReasonFor(el(0, -1, 'H1', 0), style({ webkitBackgroundClip: 'text' }))) |
| 757 | .toBe('background-clip-text') |
| 758 | }) |
| 759 | |
| 760 | it('keeps a shadow-root diagram rather than skipping it', () => { |
| 761 | const nodes = [ |
| 762 | el(0, -1, 'DIV', 0), |
| 763 | el(1, 0, 'SVG', 0, { fromShadowRoot: true, hasForeignObject: true }), |
| 764 | ] |
| 765 | const { slides, rasterRequests } = run(nodes, [BASE_STYLE]) |
| 766 | // Mermaid renders into a shadow root, so a document-level query finds |
| 767 | // nothing and the diagram vanishes from the export entirely. |
| 768 | expect(slides[0].nodes.some(n => n.kind === 'raster')).toBe(true) |
| 769 | expect(rasterRequests).toHaveLength(1) |
| 770 | }) |
| 771 | }) |
| 772 | |
| 773 | describe('a picture with content over it is captured in isolation', () => { |
| 774 | it('isolates a leaf picture that a title is drawn on top of', () => { |
| 775 | const full = { x: 0, y: 0, w: 980, h: 552 } |
| 776 | const title = { x: 80, y: 200, w: 400, h: 60 } |
| 777 | const nodes = [ |
| 778 | el(0, -1, 'SVG', 0, { rect: full }), |
| 779 | el(1, -1, 'H1', 0, { rect: title }), |
| 780 | text(2, 1, 'A cover title', { |
| 781 | rect: title, |
| 782 | glyphRects: [title], |
| 783 | }), |
| 784 | ] |
| 785 | const { slides, rasterRequests } = run(nodes, [BASE_STYLE]) |
| 786 | // `locator.screenshot()` clips the page rather than isolating the element, |
| 787 | // so without this the title is baked into the picture AND drawn again as |
| 788 | // a shape, printing every line of the slide twice. |
| 789 | const raster = slides[0].nodes.find(n => n.kind === 'raster') as any |
| 790 | expect(raster.isolate).toBe(true) |
| 791 | expect(rasterRequests[0].isolate).toBe(true) |
| 792 | // But NOT its own children: an <svg>'s children are its artwork, and |
| 793 | // nothing redraws them, so hiding those emptied the decorative chrome out |
| 794 | // of a themed deck entirely. |
| 795 | expect(rasterRequests[0].hideDescendants).toBe(false) |
| 796 | }) |
| 797 | |
| 798 | it('leaves a picture alone when nothing is drawn over it', () => { |
| 799 | const corner = { x: 0, y: 0, w: 100, h: 100 } |
| 800 | const away = { x: 500, y: 400, w: 200, h: 20 } |
| 801 | const nodes = [ |
| 802 | el(0, -1, 'SVG', 0, { rect: corner }), |
| 803 | el(1, -1, 'DIV', 0, { rect: away }), |
| 804 | text(2, 1, 'caption', { |
| 805 | rect: away, |
| 806 | glyphRects: [away], |
| 807 | }), |
| 808 | ] |
| 809 | const { rasterRequests } = run(nodes, [BASE_STYLE]) |
| 810 | // Isolation costs two DOM mutations per capture, so it is not free. |
| 811 | expect(rasterRequests[0].isolate).toBe(false) |
| 812 | }) |
| 813 | }) |
| 814 | |
| 815 | describe('only backdrops need their descendants hidden', () => { |
| 816 | it('isolates a backdrop but not a leaf picture', () => { |
| 817 | const backdrop = run( |
| 818 | [ |
| 819 | el(0, -1, 'DIV', 1), |
| 820 | el(1, 0, 'DIV', 0), |
| 821 | text(2, 1, 'on top'), |
| 822 | ], |
| 823 | [ |
| 824 | BASE_STYLE, |
| 825 | style({ backgroundImage: 'linear-gradient(red, blue)' }), |
| 826 | ], |
| 827 | ) |
| 828 | const raster = backdrop.slides[0].nodes.find(n => n.kind === 'raster') as any |
| 829 | // Without hiding siblings, the text bakes into the backdrop picture and is |
| 830 | // then drawn again as a shape, doubling every word. |
| 831 | expect(raster.isolate).toBe(true) |
| 832 | // A backdrop's children ARE walked and redrawn, so hiding them is correct. |
| 833 | expect(raster.hideDescendants).toBe(true) |
| 834 | // Its children are still walked, so the text survives as editable text. |
| 835 | expect(texts(backdrop.slides[0].nodes)).toHaveLength(1) |
| 836 | |
| 837 | const leaf = run([ |
| 838 | el(0, -1, 'SVG', 0), |
| 839 | ], [BASE_STYLE]) |
| 840 | expect((leaf.slides[0].nodes[0] as any).isolate).toBe(false) |
| 841 | }) |
| 842 | }) |
| 843 | |
| 844 | describe('a slide that cannot be rebuilt falls back to an image', () => { |
| 845 | it('falls back when a slide with text yields none', () => { |
| 846 | // Every text node sits under an <svg>, so all of it rasterizes. |
| 847 | const nodes = [ |
| 848 | el(0, -1, 'SVG', 0), |
| 849 | text(1, 0, 'invisible to the walk'), |
| 850 | ] |
| 851 | const { slides } = run(nodes, [BASE_STYLE]) |
| 852 | expect(slides[0].fallbackReason).toMatch(/no text could be recovered/) |
| 853 | }) |
| 854 | |
| 855 | it('falls back when most of the slide is a picture with nothing over it', () => { |
| 856 | const big = { x: 0, y: 0, w: 900, h: 500 } |
| 857 | const away = { x: 905, y: 505, w: 60, h: 20 } |
| 858 | const nodes = [ |
| 859 | el(0, -1, 'SVG', 0, { rect: big }), |
| 860 | el(1, -1, 'DIV', 0, { rect: away }), |
| 861 | text(2, 1, 'a caption in the corner', { |
| 862 | rect: away, |
| 863 | glyphRects: [away], |
| 864 | }), |
| 865 | ] |
| 866 | const { slides } = run(nodes, [BASE_STYLE]) |
| 867 | // The caption sits beside the picture rather than on it, so vectorizing |
| 868 | // really did recover almost nothing. |
| 869 | expect(slides[0].fallbackReason).toMatch(/rasterized/) |
| 870 | }) |
| 871 | |
| 872 | it('keeps a slide whose text sits ON TOP of a full-bleed picture', () => { |
| 873 | const full = { x: 0, y: 0, w: 980, h: 552 } |
| 874 | const title = { x: 80, y: 200, w: 400, h: 60 } |
| 875 | const nodes = [ |
| 876 | el(0, -1, 'SVG', 0, { rect: full }), |
| 877 | el(1, -1, 'H1', 0, { rect: title }), |
| 878 | text(2, 1, 'A cover title', { |
| 879 | rect: title, |
| 880 | glyphRects: [title], |
| 881 | }), |
| 882 | ] |
| 883 | const { slides } = run(nodes, [BASE_STYLE]) |
| 884 | // A cover photo or decorative graphic covers the slide by definition. The |
| 885 | // title over it vectorizes perfectly, so counting the picture against the |
| 886 | // budget threw away the text on every cover slide in the deck. |
| 887 | expect(slides[0].fallbackReason).toBeUndefined() |
| 888 | expect(texts(slides[0].nodes)[0].runs[0].text).toBe('A cover title') |
| 889 | }) |
| 890 | |
| 891 | it('does not request captures for a slide that is falling back anyway', () => { |
| 892 | const nodes = [ |
| 893 | el(0, -1, 'SVG', 0), |
| 894 | text(1, 0, 'invisible to the walk'), |
| 895 | ] |
| 896 | const { rasterRequests } = run(nodes, [BASE_STYLE]) |
| 897 | // The whole slide becomes one picture, so per-element captures would be |
| 898 | // paid for and thrown away. |
| 899 | expect(rasterRequests).toHaveLength(0) |
| 900 | }) |
| 901 | |
| 902 | it('leaves an ordinary slide alone', () => { |
| 903 | const nodes = [ |
| 904 | el(0, -1, 'DIV', 0), |
| 905 | text(1, 0, 'ordinary content'), |
| 906 | ] |
| 907 | const { slides } = run(nodes, [BASE_STYLE]) |
| 908 | expect(slides[0].fallbackReason).toBeUndefined() |
| 909 | }) |
| 910 | }) |
| 911 | |
| 912 | describe('content outside the slide is clipped', () => { |
| 913 | it('clips a shape that overflows the slide', () => { |
| 914 | const wide = style({ backgroundColor: 'rgb(1, 2, 3)' }) |
| 915 | const nodes = [ |
| 916 | el(0, -1, 'DIV', 1, { |
| 917 | rect: { x: 900, y: 0, w: 500, h: 100 }, |
| 918 | }), |
| 919 | ] |
| 920 | const { slides } = run(nodes, [BASE_STYLE, wide]) |
| 921 | // A slide container is overflow:hidden, but PowerPoint has no clipping, so |
| 922 | // an unclamped shape sits off the canvas where it cannot even be selected. |
| 923 | expect(boxes(slides[0].nodes)[0].rect).toEqual({ x: 900, y: 0, w: 80, h: 100 }) |
| 924 | }) |
| 925 | |
| 926 | it('drops a shape that is entirely off the slide', () => { |
| 927 | const off = style({ backgroundColor: 'rgb(1, 2, 3)' }) |
| 928 | const nodes = [ |
| 929 | el(0, -1, 'DIV', 1, { |
| 930 | rect: { x: 2000, y: 0, w: 100, h: 100 }, |
| 931 | }), |
| 932 | ] |
| 933 | const { slides } = run(nodes, [BASE_STYLE, off]) |
| 934 | expect(boxes(slides[0].nodes)).toHaveLength(0) |
| 935 | }) |
| 936 | |
| 937 | it('captures a runaway element as a clipped region, not as itself', () => { |
| 938 | // Slidev's own starter deck carries an element measuring tens of millions |
| 939 | // of pixels. Screenshotting it whole asks Chromium for a bitmap it cannot |
| 940 | // allocate, and the renderer dies mid-export; the failure then surfaces |
| 941 | // from an unrelated call as "Target page, context or browser has been |
| 942 | // closed", which is a miserable thing to debug. |
| 943 | // Only the left edge of it is on the slide, so this does not also trip the |
| 944 | // whole-slide fallback, which withholds captures by design. |
| 945 | const huge = { x: 900, y: 0, w: 24_000_000, h: 300 } |
| 946 | const page = { |
| 947 | x: 1000, |
| 948 | y: 200, |
| 949 | w: 24_000_000, |
| 950 | h: 300, |
| 951 | } |
| 952 | const nodes = [ |
| 953 | el(0, -1, 'CANVAS', 0, { rect: huge, pageRect: page }), |
| 954 | ] |
| 955 | const { slides, rasterRequests } = run(nodes, [BASE_STYLE]) |
| 956 | const raster = slides[0].nodes[0] as any |
| 957 | // Placed at the clipped rectangle, and captured at the same one, so the |
| 958 | // picture is neither squashed nor enormous. |
| 959 | expect(raster.rect).toEqual({ x: 900, y: 0, w: 80, h: 300 }) |
| 960 | expect(rasterRequests[0].clip).toEqual({ x: 1000, y: 200, w: 80, h: 300 }) |
| 961 | }) |
| 962 | |
| 963 | it('does not let a runaway rect blow up the fallback maths', () => { |
| 964 | // Slidev's own starter deck has an element measuring tens of millions of |
| 965 | // pixels, which reported "104064986954% of the slide had to be rasterized". |
| 966 | // No text here, so the area rule is the one under test. |
| 967 | const huge = { |
| 968 | x: 0, |
| 969 | y: 0, |
| 970 | w: 24_000_000, |
| 971 | h: 24_000_000, |
| 972 | } |
| 973 | const { slides } = run([ |
| 974 | el(0, -1, 'CANVAS', 0, { rect: huge }), |
| 975 | ], [BASE_STYLE]) |
| 976 | expect(slides[0].fallbackReason).toMatch(/^100% of the slide/) |
| 977 | }) |
| 978 | }) |
| 979 | |
| 980 | describe('a full-bleed backdrop does not trigger the fallback', () => { |
| 981 | it('keeps the text on a cover slide editable', () => { |
| 982 | const cover = style({ backgroundImage: 'url(https://cover.sli.dev)' }) |
| 983 | const nodes = [ |
| 984 | el(0, -1, 'DIV', 1, { |
| 985 | rect: { x: 0, y: 0, w: 980, h: 552 }, |
| 986 | }), |
| 987 | el(1, 0, 'H1', 0), |
| 988 | text(2, 1, 'Welcome to Slidev'), |
| 989 | ] |
| 990 | const { slides } = run(nodes, [BASE_STYLE, cover]) |
| 991 | // A cover photo covers 100% of the slide by definition, but the title on |
| 992 | // top of it is still perfectly vectorizable. Counting isolated backdrops |
| 993 | // toward the raster budget sent every deck with a cover straight to the |
| 994 | // whole-slide fallback. |
| 995 | expect(slides[0].fallbackReason).toBeUndefined() |
| 996 | expect(texts(slides[0].nodes)[0].runs[0].text).toBe('Welcome to Slidev') |
| 997 | }) |
| 998 | }) |
| 999 | |
| 1000 | describe('fonts', () => { |
| 1001 | it('names the family the browser actually resolved', () => { |
| 1002 | const nodes = [ |
| 1003 | el(0, -1, 'DIV', 0), |
| 1004 | text(1, 0, 'hello'), |
| 1005 | ] |
| 1006 | const { slides } = run(nodes, [BASE_STYLE]) |
| 1007 | expect(texts(slides[0].nodes)[0].runs[0].fontFamily).toBe('Inter') |
| 1008 | }) |
| 1009 | |
| 1010 | it('strips the @fontsource Variable suffix when nothing resolved', () => { |
| 1011 | const nodes = [ |
| 1012 | el(0, -1, 'DIV', 1), |
| 1013 | text(1, 0, 'hello'), |
| 1014 | ] |
| 1015 | const styles = [ |
| 1016 | BASE_STYLE, |
| 1017 | style({ fontFamily: '"Inter Variable", sans-serif' }), |
| 1018 | ] |
| 1019 | const { slides } = run(nodes, styles) |
| 1020 | // Nobody has a font installed under the name "Inter Variable"; that is a |
| 1021 | // packaging artifact of @fontsource-variable. |
| 1022 | expect(texts(slides[0].nodes)[0].runs[0].fontFamily).toBe('Inter') |
| 1023 | }) |
| 1024 | }) |
| 1025 | |
| 1026 | describe('boxes', () => { |
| 1027 | it('ignores a fully transparent background', () => { |
| 1028 | const nodes = [ |
| 1029 | el(0, -1, 'DIV', 0), |
| 1030 | ] |
| 1031 | const { slides } = run(nodes, [BASE_STYLE]) |
| 1032 | expect(boxes(slides[0].nodes)).toHaveLength(0) |
| 1033 | }) |
| 1034 | |
| 1035 | it('keeps one side of a border', () => { |
| 1036 | const accent = style({ borderLeftWidth: '4px', borderLeftStyle: 'solid', borderLeftColor: 'rgb(0, 128, 0)' }) |
| 1037 | const nodes = [ |
| 1038 | el(0, -1, 'DIV', 1), |
| 1039 | ] |
| 1040 | const { slides } = run(nodes, [BASE_STYLE, accent]) |
| 1041 | const box = boxes(slides[0].nodes)[0] |
| 1042 | expect(box.borders?.[3]).toEqual({ |
| 1043 | width: 4, |
| 1044 | color: { r: 0, g: 128, b: 0, a: 1 }, |
| 1045 | style: 'solid', |
| 1046 | }) |
| 1047 | expect(box.borders?.[0]).toBeUndefined() |
| 1048 | }) |
| 1049 | }) |
| 1050 | |
| 1051 | describe('lines are grouped by overlap, not by matching tops', () => { |
| 1052 | it('counts one line when a heading mixes run sizes', () => { |
| 1053 | // A browser aligns runs of different sizes on a shared BASELINE, so their |
| 1054 | // rect tops differ while they sit on the same line. Grouping by top |
| 1055 | // counted `Needed a **Pros-Cons comparison** in now?` as four lines where |
| 1056 | // it has two, and the spacing that came out of that count set the two real |
| 1057 | // lines on top of each other. |
| 1058 | const small = { x: 0, y: 130, w: 120, h: 40 } |
| 1059 | const large = { x: 120, y: 100, w: 400, h: 100 } |
| 1060 | const nodes = [ |
| 1061 | el(0, -1, 'H1', 0, { |
| 1062 | rect: { x: 0, y: 100, w: 520, h: 220 }, |
| 1063 | }), |
| 1064 | text(1, 0, 'Needed a ', { |
| 1065 | glyphRects: [small], |
| 1066 | }), |
| 1067 | text(2, 0, 'Pros-Cons', { |
| 1068 | glyphRects: [large], |
| 1069 | }), |
| 1070 | text(3, 0, 'comparison', { |
| 1071 | glyphRects: [ |
| 1072 | { ...large, y: 220 }, |
| 1073 | ], |
| 1074 | }), |
| 1075 | text(4, 0, ' in now?', { |
| 1076 | glyphRects: [ |
| 1077 | { ...small, y: 250 }, |
| 1078 | ], |
| 1079 | }), |
| 1080 | ] |
| 1081 | const [line] = texts(run(nodes, [BASE_STYLE]).slides[0].nodes) |
| 1082 | expect(line.lineCount).toBe(2) |
| 1083 | }) |
| 1084 | |
| 1085 | it('takes the line spacing from the measured line boxes', () => { |
| 1086 | // The computed `line-height` belongs to the element while a line box is as |
| 1087 | // tall as its largest run, so a heading mixing sizes reported far too |
| 1088 | // little leading and PowerPoint overlapped the lines. |
| 1089 | const nodes = [ |
| 1090 | el(0, -1, 'H1', 0, { |
| 1091 | rect: { x: 0, y: 0, w: 400, h: 240 }, |
| 1092 | }), |
| 1093 | text(1, 0, 'first', { |
| 1094 | glyphRects: [ |
| 1095 | { x: 0, y: 0, w: 300, h: 100 }, |
| 1096 | ], |
| 1097 | }), |
| 1098 | text(2, 0, 'second', { |
| 1099 | glyphRects: [ |
| 1100 | { x: 0, y: 120, w: 300, h: 100 }, |
| 1101 | ], |
| 1102 | }), |
| 1103 | ] |
| 1104 | const [line] = texts(run(nodes, [BASE_STYLE]).slides[0].nodes) |
| 1105 | // 120, the distance between the line tops. `line-height: normal` on a |
| 1106 | // 16px font would have said 19.2. |
| 1107 | expect(line.lineHeight).toBe(120) |
| 1108 | }) |
| 1109 | |
| 1110 | it('still separates lines whose ink very nearly touches', () => { |
| 1111 | const nodes = [ |
| 1112 | el(0, -1, 'P', 0, { |
| 1113 | rect: { x: 0, y: 0, w: 400, h: 40 }, |
| 1114 | }), |
| 1115 | text(1, 0, 'one', { |
| 1116 | glyphRects: [ |
| 1117 | { x: 0, y: 0, w: 300, h: 20 }, |
| 1118 | ], |
| 1119 | }), |
| 1120 | text(2, 0, 'two', { |
| 1121 | glyphRects: [ |
| 1122 | { x: 0, y: 19, w: 300, h: 20 }, |
| 1123 | ], |
| 1124 | }), |
| 1125 | ] |
| 1126 | const [line] = texts(run(nodes, [BASE_STYLE]).slides[0].nodes) |
| 1127 | expect(line.lineCount).toBe(2) |
| 1128 | }) |
| 1129 | }) |
| 1130 | |
| 1131 | describe('a wrapped inline box is painted once per line', () => { |
| 1132 | it('emits one shape per line fragment', () => { |
| 1133 | // CSS paints an inline element's background once per line FRAGMENT, while |
| 1134 | // `getBoundingClientRect` reports the union of them. Drawn as that union, |
| 1135 | // an inline `<code>` running across three lines filled the ragged space at |
| 1136 | // the end of every line with its own dark background. |
| 1137 | const inline = style({ display: 'inline', backgroundColor: 'rgb(0, 0, 0)' }) |
| 1138 | const fragments = [ |
| 1139 | { x: 100, y: 0, w: 200, h: 20 }, |
| 1140 | { x: 0, y: 20, w: 300, h: 20 }, |
| 1141 | { x: 0, y: 40, w: 80, h: 20 }, |
| 1142 | ] |
| 1143 | const nodes = [ |
| 1144 | el(0, -1, 'CODE', 1, { |
| 1145 | rect: { x: 0, y: 0, w: 300, h: 60 }, |
| 1146 | fragments, |
| 1147 | }), |
| 1148 | ] |
| 1149 | const painted = boxes(run(nodes, [BASE_STYLE, inline]).slides[0].nodes) |
| 1150 | expect(painted.map(box => box.rect)).toEqual(fragments) |
| 1151 | }) |
| 1152 | |
| 1153 | it('leaves an unwrapped element as one shape', () => { |
| 1154 | const filled = style({ backgroundColor: 'rgb(0, 0, 0)' }) |
| 1155 | const nodes = [ |
| 1156 | el(0, -1, 'DIV', 1, { |
| 1157 | rect: { x: 0, y: 0, w: 300, h: 60 }, |
| 1158 | }), |
| 1159 | ] |
| 1160 | const painted = boxes(run(nodes, [BASE_STYLE, filled]).slides[0].nodes) |
| 1161 | expect(painted).toHaveLength(1) |
| 1162 | expect(painted[0].rect).toEqual({ x: 0, y: 0, w: 300, h: 60 }) |
| 1163 | }) |
| 1164 | }) |
| 1165 | |
| 1166 | describe('a raster that cannot be placed must not swallow its subtree', () => { |
| 1167 | it('walks into a rotated wrapper that has no box of its own', () => { |
| 1168 | // A theme rotating a corner decoration puts the transform on a wrapper |
| 1169 | // with no size, while the artwork inside is absolutely positioned and does |
| 1170 | // have one. Treating the wrapper as rasterized dropped the whole |
| 1171 | // decoration: the picture had no area, so nothing was emitted for it |
| 1172 | // either, and the entire corner vanished from every slide. |
| 1173 | const rotated = style({ transform: 'matrix(0, 1, -1, 0, 0, 0)', position: 'absolute' }) |
| 1174 | const nodes = [ |
| 1175 | el(0, -1, 'DIV', 1, { |
| 1176 | rect: { x: 0, y: 0, w: 0, h: 0 }, |
| 1177 | }), |
| 1178 | el(1, 0, 'SVG', 0, { |
| 1179 | rect: { x: 10, y: 20, w: 279, h: 322 }, |
| 1180 | }), |
| 1181 | ] |
| 1182 | const { slides, rasterRequests } = run(nodes, [BASE_STYLE, rotated]) |
| 1183 | expect(slides[0].nodes.map(node => node.sourceId)).toEqual([1]) |
| 1184 | expect(rasterRequests.map(request => request.sourceId)).toEqual([1]) |
| 1185 | }) |
| 1186 | }) |
| 1187 | |
| 1188 | describe('an image cut off by the slide edge is cropped, not squashed', () => { |
| 1189 | it('keeps the full display size and takes a window out of it', () => { |
| 1190 | // The slide container has `overflow: hidden`, so a browser shows the top |
| 1191 | // of an oversized image and cuts the rest off. Clipping the BOX alone |
| 1192 | // scaled the whole image into it instead, which came out vertically |
| 1193 | // compressed and showed content the audience never saw. |
| 1194 | const nodes = [ |
| 1195 | el(0, -1, 'IMG', 0, { |
| 1196 | rect: { x: 56, y: 40, w: 434, h: 772 }, |
| 1197 | src: 'a.png', |
| 1198 | }), |
| 1199 | ] |
| 1200 | const [image] = run(nodes, [BASE_STYLE]).slides[0].nodes as any[] |
| 1201 | expect(image.rect).toEqual({ x: 56, y: 40, w: 434, h: 512 }) |
| 1202 | expect(image.crop).toEqual({ x: 0, y: 0, w: 434, h: 772 }) |
| 1203 | }) |
| 1204 | |
| 1205 | it('leaves an image that fits uncropped', () => { |
| 1206 | const nodes = [ |
| 1207 | el(0, -1, 'IMG', 0, { |
| 1208 | rect: { x: 10, y: 10, w: 100, h: 50 }, |
| 1209 | src: 'a.png', |
| 1210 | }), |
| 1211 | ] |
| 1212 | const [image] = run(nodes, [BASE_STYLE]).slides[0].nodes as any[] |
| 1213 | expect(image.crop).toBeUndefined() |
| 1214 | }) |
| 1215 | }) |
| 1216 | |
| 1217 | describe('a rendered formula is a picture', () => { |
| 1218 | it('rasterizes a KaTeX root instead of walking its spans', () => { |
| 1219 | // KaTeX sets a formula as dozens of separately positioned spans in its own |
| 1220 | // metric fonts, with radicals, braces and fraction rules drawn as bare |
| 1221 | // boxes. Walked as text it comes apart: glyphs land off their baselines |
| 1222 | // and every rule disappears. |
| 1223 | const nodes = [ |
| 1224 | el(0, -1, 'DIV', 0), |
| 1225 | el(1, 0, 'SPAN', 0, { |
| 1226 | isMath: true, |
| 1227 | rect: { x: 10, y: 10, w: 200, h: 40 }, |
| 1228 | }), |
| 1229 | el(2, 1, 'SPAN', 0, { |
| 1230 | rect: { x: 10, y: 10, w: 20, h: 40 }, |
| 1231 | }), |
| 1232 | text(3, 2, '3x'), |
| 1233 | el(4, 0, 'P', 0, { |
| 1234 | rect: { x: 10, y: 80, w: 200, h: 20 }, |
| 1235 | }), |
| 1236 | text(5, 4, 'Block', { |
| 1237 | glyphRects: [ |
| 1238 | { x: 10, y: 80, w: 60, h: 20 }, |
| 1239 | ], |
| 1240 | }), |
| 1241 | ] |
| 1242 | const { slides, rasterRequests } = run(nodes, [BASE_STYLE]) |
| 1243 | const math = slides[0].nodes.filter(node => node.kind === 'raster') |
| 1244 | expect(math).toHaveLength(1) |
| 1245 | expect((math[0] as any).reason).toBe('math') |
| 1246 | // Its own spans are not ALSO drawn as text. |
| 1247 | expect(texts(slides[0].nodes).map(t => t.runs.map(r => r.text).join(''))).toEqual(['Block']) |
| 1248 | expect(rasterRequests.map(request => request.sourceId)).toEqual([1]) |
| 1249 | }) |
| 1250 | |
| 1251 | it('leaves an ordinary span alone', () => { |
| 1252 | const nodes = [ |
| 1253 | el(0, -1, 'SPAN', 0), |
| 1254 | text(1, 0, 'plain'), |
| 1255 | ] |
| 1256 | const { slides } = run(nodes, [BASE_STYLE]) |
| 1257 | expect(slides[0].nodes.map(node => node.kind)).toEqual(['text']) |
| 1258 | }) |
| 1259 | }) |
| 1260 | |
| 1261 | describe('gradient text is the picture, not a backdrop for it', () => { |
| 1262 | it('reports background-clip before background-image', () => { |
| 1263 | // Gradient text is both at once: a linear-gradient clipped to the glyphs, |
| 1264 | // with `color` left as a flat fallback for browsers without the clip. |
| 1265 | // Called a background image it counts as a backdrop, so its own text is |
| 1266 | // drawn again on top and the fallback color hides the gradient. |
| 1267 | const gradient = style({ |
| 1268 | backgroundImage: 'linear-gradient(45deg, rgb(78, 197, 212) 10%, rgb(20, 107, 140) 20%)', |
| 1269 | webkitBackgroundClip: 'text', |
| 1270 | color: 'rgb(93, 131, 146)', |
| 1271 | }) |
| 1272 | expect(rasterReasonFor(el(0, -1, 'H1', 1), gradient)).toBe('background-clip-text') |
| 1273 | }) |
| 1274 | |
| 1275 | it('leaves the text of a real backdrop editable', () => { |
| 1276 | const backdrop = style({ backgroundImage: 'url(cover.png)' }) |
| 1277 | expect(rasterReasonFor(el(0, -1, 'DIV', 1), backdrop)).toBe('background-image') |
| 1278 | }) |
| 1279 | |
| 1280 | it('does not draw the heading twice', () => { |
| 1281 | const gradient = style({ |
| 1282 | backgroundImage: 'linear-gradient(45deg, rgb(78, 197, 212) 10%, rgb(20, 107, 140) 20%)', |
| 1283 | webkitBackgroundClip: 'text', |
| 1284 | }) |
| 1285 | const nodes = [ |
| 1286 | el(0, -1, 'H1', 1, { |
| 1287 | rect: { x: 54, y: 40, w: 870, h: 40 }, |
| 1288 | }), |
| 1289 | text(1, 0, 'What is Slidev?', { |
| 1290 | glyphRects: [ |
| 1291 | { x: 54, y: 36, w: 256, h: 47 }, |
| 1292 | ], |
| 1293 | }), |
| 1294 | el(2, -1, 'P', 0, { |
| 1295 | rect: { x: 54, y: 88, w: 700, h: 23 }, |
| 1296 | }), |
| 1297 | text(3, 2, 'Body copy', { |
| 1298 | glyphRects: [ |
| 1299 | { x: 54, y: 88, w: 200, h: 23 }, |
| 1300 | ], |
| 1301 | }), |
| 1302 | ] |
| 1303 | const { slides } = run(nodes, [BASE_STYLE, gradient]) |
| 1304 | expect(slides[0].nodes.filter(n => n.kind === 'raster')).toHaveLength(1) |
| 1305 | // The body copy survives; only the heading became a picture. |
| 1306 | expect(texts(slides[0].nodes).map(t => t.runs.map(r => r.text).join(''))).toEqual(['Body copy']) |
| 1307 | }) |
| 1308 | }) |
| 1309 | |
| 1310 | describe('an inline rule is an underline, not a shape', () => { |
| 1311 | const linkStyle = style({ |
| 1312 | display: 'inline', |
| 1313 | borderBottomWidth: '1px', |
| 1314 | borderBottomStyle: 'dashed', |
| 1315 | borderBottomColor: 'rgb(0, 0, 0)', |
| 1316 | }) |
| 1317 | |
| 1318 | it('draws a dashed border-bottom as a dashed underline', () => { |
| 1319 | // Slidev rules its links this way. As a separate line it has to be placed |
| 1320 | // against text PowerPoint may re-lay, cannot follow an edit, and is a |
| 1321 | // second thing that can draw a line under a link PowerPoint may underline |
| 1322 | // itself. DrawingML dashes an underline, so none of that is needed. |
| 1323 | const nodes = [ |
| 1324 | el(0, -1, 'P', 0), |
| 1325 | el(1, 0, 'A', 1, { href: 'https://sli.dev' }), |
| 1326 | text(2, 1, 'Why Slidev?'), |
| 1327 | ] |
| 1328 | const { slides } = run(nodes, [BASE_STYLE, linkStyle]) |
| 1329 | expect(boxes(slides[0].nodes)).toHaveLength(0) |
| 1330 | const [line] = texts(slides[0].nodes) |
| 1331 | expect(line.runs[0].underline).toBe(true) |
| 1332 | expect(line.runs[0].underlineStyle).toBe('dash') |
| 1333 | }) |
| 1334 | |
| 1335 | it('leaves a bordered box a box', () => { |
| 1336 | // A bottom border on a BLOCK is a rule under a section, not an underline. |
| 1337 | const blockRule = style({ borderBottomWidth: '1px', borderBottomStyle: 'solid', borderBottomColor: 'rgb(0, 0, 0)' }) |
| 1338 | const nodes = [ |
| 1339 | el(0, -1, 'DIV', 1), |
| 1340 | text(1, 0, 'Section'), |
| 1341 | ] |
| 1342 | const { slides } = run(nodes, [BASE_STYLE, blockRule]) |
| 1343 | expect(boxes(slides[0].nodes)).toHaveLength(1) |
| 1344 | expect(texts(slides[0].nodes)[0].runs[0].underline).toBeUndefined() |
| 1345 | }) |
| 1346 | |
| 1347 | it('leaves a chip alone, because it is more than a rule', () => { |
| 1348 | const chip = style({ |
| 1349 | display: 'inline', |
| 1350 | backgroundColor: 'rgb(200, 200, 200)', |
| 1351 | borderBottomWidth: '1px', |
| 1352 | borderBottomStyle: 'dashed', |
| 1353 | borderBottomColor: 'rgb(0, 0, 0)', |
| 1354 | }) |
| 1355 | const nodes = [ |
| 1356 | el(0, -1, 'P', 0), |
| 1357 | el(1, 0, 'CODE', 1), |
| 1358 | text(2, 1, 'npm i'), |
| 1359 | ] |
| 1360 | const { slides } = run(nodes, [BASE_STYLE, chip]) |
| 1361 | expect(boxes(slides[0].nodes).length).toBeGreaterThan(0) |
| 1362 | }) |
| 1363 | }) |
| 1364 | |
| 1365 | describe('a table cell flows its children, it does not lay them out', () => { |
| 1366 | it('places each key of a shortcut row on its own', () => { |
| 1367 | // A chip is padded and spaced by its own box, which flowed text cannot |
| 1368 | // reproduce: the labels run together while the keys keep their gaps. |
| 1369 | const cell = style({ display: 'table-cell' }) |
| 1370 | const chip = style({ display: 'inline-block', backgroundColor: 'rgb(240, 240, 240)' }) |
| 1371 | const nodes = [ |
| 1372 | el(0, -1, 'TD', 1), |
| 1373 | el(1, 0, 'KBD', 2, { |
| 1374 | rect: { x: 69, y: 226, w: 48, h: 27 }, |
| 1375 | }), |
| 1376 | text(2, 1, 'left', { |
| 1377 | rect: { x: 73, y: 233, w: 40, h: 20 }, |
| 1378 | glyphRects: [ |
| 1379 | { x: 73, y: 233, w: 40, h: 20 }, |
| 1380 | ], |
| 1381 | }), |
| 1382 | text(3, 0, ' / ', { |
| 1383 | rect: { x: 117, y: 233, w: 13, h: 20 }, |
| 1384 | glyphRects: [ |
| 1385 | { x: 117, y: 233, w: 13, h: 20 }, |
| 1386 | ], |
| 1387 | }), |
| 1388 | el(4, 0, 'KBD', 2, { |
| 1389 | rect: { x: 130, y: 226, w: 48, h: 27 }, |
| 1390 | }), |
| 1391 | text(5, 4, 'shift', { |
| 1392 | rect: { x: 134, y: 233, w: 40, h: 20 }, |
| 1393 | glyphRects: [ |
| 1394 | { x: 134, y: 233, w: 40, h: 20 }, |
| 1395 | ], |
| 1396 | }), |
| 1397 | el(6, 0, 'KBD', 2, { |
| 1398 | rect: { x: 186, y: 226, w: 48, h: 27 }, |
| 1399 | }), |
| 1400 | text(7, 6, 'space', { |
| 1401 | rect: { x: 190, y: 233, w: 40, h: 20 }, |
| 1402 | glyphRects: [ |
| 1403 | { x: 190, y: 233, w: 40, h: 20 }, |
| 1404 | ], |
| 1405 | }), |
| 1406 | ] |
| 1407 | const lines = texts(run(nodes, [BASE_STYLE, cell, chip]).slides[0].nodes) |
| 1408 | expect(lines.map(l => l.runs.map(r => r.text).join(''))).toEqual(['left', ' / ', 'shift', 'space']) |
| 1409 | // The separator keeps the spaces its glyph bounds were measured across: |
| 1410 | // trimmed, it drew where the leading space had been, on the key beside it. |
| 1411 | expect(lines[1].runs[0].text).toBe(' / ') |
| 1412 | }) |
| 1413 | |
| 1414 | it('keeps a cell of inline content in one text box', () => { |
| 1415 | // A table lays out its rows and a row its cells, but the contents of a |
| 1416 | // cell flow like any block. Treated as a layout container, |
| 1417 | // `<kbd>right</kbd> / <kbd>space</kbd>` became three boxes, and the |
| 1418 | // separator was positioned from glyph bounds that included the spaces |
| 1419 | // around it while its own text had them trimmed, so it sat on the key. |
| 1420 | const cell = style({ display: 'table-cell' }) |
| 1421 | const chip = style({ display: 'inline-block', backgroundColor: 'rgb(240, 240, 240)' }) |
| 1422 | const nodes = [ |
| 1423 | el(0, -1, 'TD', 1), |
| 1424 | el(1, 0, 'SPAN', 3, { |
| 1425 | rect: { x: 69, y: 233, w: 48, h: 20 }, |
| 1426 | }), |
| 1427 | text(2, 1, 'right', { |
| 1428 | rect: { x: 73, y: 233, w: 40, h: 20 }, |
| 1429 | glyphRects: [ |
| 1430 | { x: 73, y: 233, w: 40, h: 20 }, |
| 1431 | ], |
| 1432 | }), |
| 1433 | text(3, 0, ' / ', { |
| 1434 | rect: { x: 117, y: 233, w: 13, h: 20 }, |
| 1435 | glyphRects: [ |
| 1436 | { x: 117, y: 233, w: 13, h: 20 }, |
| 1437 | ], |
| 1438 | }), |
| 1439 | el(4, 0, 'SPAN', 3, { |
| 1440 | rect: { x: 130, y: 233, w: 48, h: 20 }, |
| 1441 | }), |
| 1442 | text(5, 4, 'space', { |
| 1443 | rect: { x: 134, y: 233, w: 40, h: 20 }, |
| 1444 | glyphRects: [ |
| 1445 | { x: 134, y: 233, w: 40, h: 20 }, |
| 1446 | ], |
| 1447 | }), |
| 1448 | ] |
| 1449 | const plain = style({ display: 'inline' }) |
| 1450 | const lines = texts(run(nodes, [BASE_STYLE, cell, chip, plain]).slides[0].nodes) |
| 1451 | expect(lines).toHaveLength(1) |
| 1452 | expect(lines[0].runs.map(r => r.text)).toEqual(['right', ' / ', 'space']) |
| 1453 | }) |
| 1454 | |
| 1455 | it('still lays out the rows of a table', () => { |
| 1456 | const table = style({ display: 'table' }) |
| 1457 | const nodes = [ |
| 1458 | el(0, -1, 'TABLE', 1), |
| 1459 | el(1, 0, 'TR', 0, { |
| 1460 | rect: { x: 0, y: 0, w: 200, h: 20 }, |
| 1461 | }), |
| 1462 | text(2, 1, 'one', { |
| 1463 | glyphRects: [ |
| 1464 | { x: 0, y: 0, w: 60, h: 20 }, |
| 1465 | ], |
| 1466 | }), |
| 1467 | el(3, 0, 'TR', 0, { |
| 1468 | rect: { x: 0, y: 30, w: 200, h: 20 }, |
| 1469 | }), |
| 1470 | text(4, 3, 'two', { |
| 1471 | glyphRects: [ |
| 1472 | { x: 0, y: 30, w: 60, h: 20 }, |
| 1473 | ], |
| 1474 | }), |
| 1475 | ] |
| 1476 | const lines = texts(run(nodes, [BASE_STYLE, table]).slides[0].nodes) |
| 1477 | expect(lines.map(l => l.runs.map(r => r.text).join(''))).toEqual(['one', 'two']) |
| 1478 | }) |
| 1479 | }) |
| 1480 | |
| 1481 | describe('a block box inside an inline run is not folded into the paragraph', () => { |
| 1482 | // Shiki's `<code>` is `display: inline`, and TwoSlash renders a diagnostic as |
| 1483 | // a `<div>` inside it. Collecting text through the inline subtree without |
| 1484 | // testing display ran the message onto the end of the code line it belongs |
| 1485 | // to: "doubled.value = 2Cannot assign to 'value' because it is a read-only |
| 1486 | // property." on `demo/starter`. |
| 1487 | const styles = [ |
| 1488 | style({ display: 'block', whiteSpace: 'pre' }), // 0: <pre> |
| 1489 | style({ display: 'inline', whiteSpace: 'pre' }), // 1: <code> |
| 1490 | style({ display: 'block', whiteSpace: 'pre' }), // 2: the diagnostic |
| 1491 | ] |
| 1492 | const nodes = [ |
| 1493 | el(1, -1, 'PRE', 0), |
| 1494 | el(2, 1, 'CODE', 1), |
| 1495 | text(3, 2, 'doubled.value = 2'), |
| 1496 | el(4, 2, 'DIV', 2, { |
| 1497 | rect: { x: 0, y: 30, w: 200, h: 20 }, |
| 1498 | }), |
| 1499 | text(5, 4, 'Cannot assign to \'value\'.', { |
| 1500 | rect: { x: 0, y: 30, w: 200, h: 20 }, |
| 1501 | glyphRects: [ |
| 1502 | { x: 0, y: 30, w: 200, h: 20 }, |
| 1503 | ], |
| 1504 | }), |
| 1505 | ] |
| 1506 | |
| 1507 | it('gives the diagnostic its own text box', () => { |
| 1508 | const found = texts(run(nodes, styles).slides[0].nodes) |
| 1509 | const joined = found.map(t => t.runs.map(r => r.text).join('')).join('|') |
| 1510 | expect(joined).not.toContain('2Cannot assign') |
| 1511 | expect(found).toHaveLength(2) |
| 1512 | }) |
| 1513 | }) |
| 1514 |