mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-21 15:33:45 +00:00
Merge 239b2ee6b5 into b2244f7b53
This commit is contained in:
commit
50434fea02
3 changed files with 605 additions and 8 deletions
|
|
@ -632,6 +632,52 @@ describe('Office HTML producers', () => {
|
|||
* on resize never measures an already-transformed box. */
|
||||
expect(html).toContain('lcNativeW');
|
||||
});
|
||||
|
||||
test('initializes pptx-preview with only a width so it never boxes the deck into a fixed-height viewport', async () => {
|
||||
/* Passing `height` bounded the librarys own render box, which
|
||||
* produced a nested scroll region for multi-slide decks instead
|
||||
* of letting the panel itself scroll to the last slide. Width
|
||||
* alone is enough — the wrap+scale step above fits each slide
|
||||
* to the panel. */
|
||||
const pptx = await buildPptx([{ title: 'A' }]);
|
||||
const html = await _internal.pptxToHtmlViaCdn(
|
||||
pptx,
|
||||
'<ol class="lc-pptx-list"><li>fb</li></ol>',
|
||||
);
|
||||
expect(html).toContain('pptxPreview.init(container, { width: SLIDE_W })');
|
||||
expect(html).not.toContain('height: SLIDE_H');
|
||||
});
|
||||
|
||||
test('wraps each rendered slide directly instead of the containers immediate children', async () => {
|
||||
/* pptx-preview nests every `.pptx-preview-slide-wrapper` inside
|
||||
* one library-owned `.pptx-preview-wrapper` box, so
|
||||
* `container.children` only ever finds that single box —
|
||||
* wrapping it as one unit jammed every slide into one shared
|
||||
* block. Querying `.pptx-preview-slide-wrapper` directly finds
|
||||
* each slide wherever the library actually nested it, and each
|
||||
* wrap is inserted next to its own slide via `slide.parentNode`
|
||||
* rather than `container`. */
|
||||
const pptx = await buildPptx([{ title: 'A' }, { title: 'B' }]);
|
||||
const html = await _internal.pptxToHtmlViaCdn(
|
||||
pptx,
|
||||
'<ol class="lc-pptx-list"><li>fb</li></ol>',
|
||||
);
|
||||
expect(html).toContain("container.querySelectorAll('.pptx-preview-slide-wrapper')");
|
||||
expect(html).not.toContain('container.children');
|
||||
expect(html).toContain('slide.parentNode.insertBefore(wrap, slide)');
|
||||
});
|
||||
|
||||
test('overrides the librarys own wrapper box to hug the stacked slides width with a transparent background', async () => {
|
||||
const pptx = await buildPptx([{ title: 'A' }]);
|
||||
const html = await _internal.pptxToHtmlViaCdn(
|
||||
pptx,
|
||||
'<ol class="lc-pptx-list"><li>fb</li></ol>',
|
||||
);
|
||||
expect(html).toMatch(/\.pptx-preview-wrapper\s*\{[^}]*width:\s*auto\s*!important/);
|
||||
expect(html).toMatch(
|
||||
/\.pptx-preview-wrapper\s*\{[^}]*background:\s*transparent\s*!important/,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('OFFICE_PREVIEW_DISABLE_CDN escape hatch', () => {
|
||||
|
|
|
|||
|
|
@ -1103,6 +1103,12 @@ function buildPptxCdnDocument(base64: string, slideListFallbackBody: string): st
|
|||
<style>
|
||||
:root { color-scheme: light dark; --bg: #ffffff; --fg: #1f2937; --muted: #6b7280; }
|
||||
@media (prefers-color-scheme: dark) { :root { --bg: #1a1a2e; --fg: #e5e7eb; --muted: #9ca3af; } }
|
||||
/* Reserves the vertical scrollbar's width up front so its appearance
|
||||
* never changes the document's content width. Without this, a deck
|
||||
* whose height sits within a few px of the iframe height flips the
|
||||
* scrollbar on and off every refit as the resulting width change
|
||||
* alternately fits and overflows the content. */
|
||||
html { scrollbar-gutter: stable; }
|
||||
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--fg); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
|
||||
#lc-render { padding: 16px; box-sizing: border-box; display: flex; flex-direction: column; align-items: center; gap: 16px; }
|
||||
/* Each rendered slide is wrapped post-hoc by the bootstrap script in
|
||||
|
|
@ -1127,6 +1133,21 @@ html, body { margin: 0; padding: 0; background: var(--bg); color: var(--fg); fon
|
|||
left: 0;
|
||||
transform-origin: top left;
|
||||
}
|
||||
/* Slide blocks now stack inside the library's own wrapper box instead of
|
||||
* as direct children of #lc-render, so #lc-render's flex gap no longer
|
||||
* separates them — restore the 16px rhythm here instead. */
|
||||
.lc-slide-wrap + .lc-slide-wrap {
|
||||
margin-top: 16px;
|
||||
}
|
||||
/* pptx-preview's own init() sets a fixed inline width (and an opaque
|
||||
* background) on the wrapper box it creates around the slides.
|
||||
* Override so it hugs the stacked .lc-slide-wrap blocks' actual width
|
||||
* instead of clipping them, and drops its own background so it
|
||||
* doesn't paint a second boxed panel behind the per-slide cards. */
|
||||
.pptx-preview-wrapper {
|
||||
width: auto !important;
|
||||
background: transparent !important;
|
||||
}
|
||||
#lc-fallback { padding: 16px; font-size: 14px; line-height: 1.5; color: var(--fg); }
|
||||
#lc-fallback-notice { font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--border); padding-bottom: 8px; margin: 0 0 16px; }
|
||||
.lc-pptx-loading { display: flex; align-items: center; justify-content: center; height: 60vh; color: var(--muted); font-size: 14px; }
|
||||
|
|
@ -1223,7 +1244,12 @@ ${PPTX_SLIDE_LIST_CSS}
|
|||
* doesnt see the unscaled 960px-wide flash. We reveal once
|
||||
* wrap+scale has settled. */
|
||||
container.style.visibility = 'hidden';
|
||||
var previewer = pptxPreview.init(container, { width: SLIDE_W, height: SLIDE_H });
|
||||
/* No height option: passing one bounds the librarys own render
|
||||
* box to that pixel height, which nests a scrollable region inside
|
||||
* the panel instead of letting the panel itself scroll to the last
|
||||
* slide. Width alone is enough — wrapSlides() below fits each
|
||||
* slide to the panel on its own. */
|
||||
var previewer = pptxPreview.init(container, { width: SLIDE_W });
|
||||
|
||||
function availableWidth() {
|
||||
/* clientWidth includes the 16px padding on each side via
|
||||
|
|
@ -1258,12 +1284,17 @@ ${PPTX_SLIDE_LIST_CSS}
|
|||
* move slides out from under the librarys references and break
|
||||
* its internal state. */
|
||||
function wrapSlides() {
|
||||
var children = Array.prototype.slice.call(container.children);
|
||||
for (var i = 0; i < children.length; i++) {
|
||||
var slide = children[i];
|
||||
if (!slide.classList || slide.classList.contains('lc-slide-wrap') || slide.classList.contains('lc-pptx-loading')) {
|
||||
continue;
|
||||
}
|
||||
/* pptx-preview nests every rendered slide inside its own
|
||||
* .pptx-preview-wrapper box rather than appending them directly
|
||||
* to the render container — walking the containers immediate
|
||||
* children only ever finds that one box, wrapping the whole deck
|
||||
* as a single unit instead of one block per slide. Querying the
|
||||
* slides directly finds each one wherever the library nested it,
|
||||
* and each wrap is inserted next to its own slide via
|
||||
* slide.parentNode. */
|
||||
var slides = Array.prototype.slice.call(container.querySelectorAll('.pptx-preview-slide-wrapper'));
|
||||
for (var i = 0; i < slides.length; i++) {
|
||||
var slide = slides[i];
|
||||
/* Cache the slides actual rendered size BEFORE applying any
|
||||
* transform — measurements after a CSS scale no longer reflect
|
||||
* native pixels and would feed back into wrong sizing on
|
||||
|
|
@ -1279,11 +1310,27 @@ ${PPTX_SLIDE_LIST_CSS}
|
|||
wrap.className = 'lc-slide-wrap';
|
||||
wrap.style.width = (nativeW * scale) + 'px';
|
||||
wrap.style.height = (nativeH * scale) + 'px';
|
||||
/* pptx-preview sets an inline auto-centering margin on every
|
||||
* slide. That inline rule beats our stylesheet, so above 960px
|
||||
* content width the auto margins push the slide right before the
|
||||
* scale is applied, shifting it out of this block and clipping
|
||||
* it against the wraps overflow. Clear it so the slide pins to
|
||||
* the blocks own left edge. */
|
||||
slide.style.margin = '0';
|
||||
slide.style.transformOrigin = 'top left';
|
||||
slide.style.transform = 'scale(' + scale + ')';
|
||||
container.insertBefore(wrap, slide);
|
||||
slide.parentNode.insertBefore(wrap, slide);
|
||||
wrap.appendChild(slide);
|
||||
}
|
||||
/* The CSS rule above already wins in a real browser (a plain inline
|
||||
* style loses to a stylesheet !important), but clear it here too so
|
||||
* the librarys own box never keeps a fixed pixel width wider than
|
||||
* the stacked slide blocks it now contains. */
|
||||
var libraryBoxes = container.querySelectorAll('.pptx-preview-wrapper');
|
||||
for (var j = 0; j < libraryBoxes.length; j++) {
|
||||
libraryBoxes[j].style.width = 'auto';
|
||||
libraryBoxes[j].style.background = 'transparent';
|
||||
}
|
||||
}
|
||||
|
||||
/* Re-fit existing wraps on iframe resize (panel drag, window
|
||||
|
|
@ -1321,6 +1368,11 @@ ${PPTX_SLIDE_LIST_CSS}
|
|||
}
|
||||
|
||||
function finalize() {
|
||||
/* previewer.preview() and the safety-net timer both call this; the
|
||||
* timer already guards on settled before calling it, but the
|
||||
* promise path does not, so guard here once for both callers
|
||||
* instead of at each call site. */
|
||||
if (settled) { return; }
|
||||
wrapSlides();
|
||||
if (!hasRenderedContent()) {
|
||||
showFallback('renderer-empty-slide-list');
|
||||
|
|
|
|||
499
packages/api/src/files/documents/layout.spec.ts
Normal file
499
packages/api/src/files/documents/layout.spec.ts
Normal file
|
|
@ -0,0 +1,499 @@
|
|||
import { JSDOM } from 'jsdom';
|
||||
import type { DOMWindow } from 'jsdom';
|
||||
import { _internal } from './html';
|
||||
|
||||
/**
|
||||
* Options `pptxPreview.init` is called with, captured by the fake below so
|
||||
* tests can assert what the bootstrap script actually passed at runtime.
|
||||
*/
|
||||
interface FakePptxPreviewInitOptions {
|
||||
width: number;
|
||||
height?: number;
|
||||
}
|
||||
|
||||
/** How the fake previewer's `preview()` call resolves/rejects. */
|
||||
type PreviewBehavior =
|
||||
| 'resolve'
|
||||
| 'reject'
|
||||
| 'resolveNoSlides'
|
||||
| 'resolveEmptyWrappers'
|
||||
| 'neverResolve';
|
||||
|
||||
interface RenderPptxLayoutOptions {
|
||||
/** height = width * ratio for every rendered slide; default 0.5625 (16:9). */
|
||||
slideAspectRatio?: number;
|
||||
/** Whether the fake `pptxPreview` global is installed at all; default true. */
|
||||
installRenderer?: boolean;
|
||||
/** How the fake previewer's `preview()` call behaves; default 'resolve'. */
|
||||
preview?: PreviewBehavior;
|
||||
/** Intercept the bootstrap's 8s safety-net timer instead of letting it
|
||||
* schedule on the real clock; default false. */
|
||||
captureSafetyNet?: boolean;
|
||||
}
|
||||
|
||||
interface RenderPptxLayoutResult {
|
||||
window: DOMWindow;
|
||||
document: Document;
|
||||
initializationOptions?: FakePptxPreviewInitOptions;
|
||||
/** Changes what `#lc-render`'s stubbed `clientWidth` reports on the next read. */
|
||||
setRenderSlotWidth: (width: number) => void;
|
||||
/** Fires the captured 8s safety-net timer (only valid when `captureSafetyNet: true`). */
|
||||
fireSafetyNet: () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the actual `<script>` `pptxToHtmlViaCdn` emits inside JSDOM, against
|
||||
* a fake `pptxPreview` global that mirrors the pinned librarys DOM shape:
|
||||
* `init` creates one `.pptx-preview-wrapper` box (with the librarys own
|
||||
* inline width/background, and — only when a `height` option is passed —
|
||||
* a fixed inline height with `overflow-y: auto`), and `preview` populates
|
||||
* it with `.pptx-preview-slide-wrapper` children at their native size.
|
||||
* Exercises the real wrapping logic end to end rather than pattern-matching
|
||||
* the source text.
|
||||
*/
|
||||
async function renderPptxLayout(
|
||||
slideCount: number,
|
||||
renderSlotWidth: number,
|
||||
options: RenderPptxLayoutOptions = {},
|
||||
): Promise<RenderPptxLayoutResult> {
|
||||
const {
|
||||
slideAspectRatio = 0.5625,
|
||||
installRenderer = true,
|
||||
preview = 'resolve',
|
||||
captureSafetyNet = false,
|
||||
} = options;
|
||||
|
||||
const html = await _internal.pptxToHtmlViaCdn(
|
||||
Buffer.from('fixture'),
|
||||
'<ol class="lc-pptx-list"><li>fallback</li></ol>',
|
||||
);
|
||||
|
||||
let initializationOptions: FakePptxPreviewInitOptions | undefined;
|
||||
let currentRenderSlotWidth = renderSlotWidth;
|
||||
let safetyNetCallback: (() => void) | undefined;
|
||||
|
||||
const { window } = new JSDOM(html, {
|
||||
runScripts: 'dangerously',
|
||||
beforeParse(parsedWindow) {
|
||||
// JSDOM never computes real layout, so `clientWidth` is stubbed
|
||||
// directly on the render slot the bootstrap reads panel width from.
|
||||
Object.defineProperty(parsedWindow.HTMLElement.prototype, 'clientWidth', {
|
||||
configurable: true,
|
||||
get(this: HTMLElement) {
|
||||
return this.id === 'lc-render' ? currentRenderSlotWidth : 0;
|
||||
},
|
||||
});
|
||||
// JSDOM has no layout engine either, so `offsetWidth`/`offsetHeight`
|
||||
// are always 0 — stub them to reflect the inline size the fake
|
||||
// `preview()` sets on each slide, which is what `wrapSlides()` reads
|
||||
// to cache a slide's native (pre-scale) dimensions.
|
||||
Object.defineProperty(parsedWindow.HTMLElement.prototype, 'offsetWidth', {
|
||||
configurable: true,
|
||||
get(this: HTMLElement) {
|
||||
return Number.parseFloat(this.style.width) || 0;
|
||||
},
|
||||
});
|
||||
Object.defineProperty(parsedWindow.HTMLElement.prototype, 'offsetHeight', {
|
||||
configurable: true,
|
||||
get(this: HTMLElement) {
|
||||
return Number.parseFloat(this.style.height) || 0;
|
||||
},
|
||||
});
|
||||
if (captureSafetyNet) {
|
||||
// Capture the 8s safety-net timer instead of letting it run on the
|
||||
// real clock, so the test can fire it deterministically.
|
||||
Object.defineProperty(parsedWindow, 'setTimeout', {
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: (handler: () => void, timeout?: number) => {
|
||||
if (timeout === 8000) {
|
||||
safetyNetCallback = handler;
|
||||
}
|
||||
return 0;
|
||||
},
|
||||
});
|
||||
}
|
||||
if (!installRenderer) {
|
||||
return;
|
||||
}
|
||||
Object.assign(parsedWindow, {
|
||||
pptxPreview: {
|
||||
init(container: HTMLElement, initOptions: FakePptxPreviewInitOptions) {
|
||||
initializationOptions = initOptions;
|
||||
const wrapper = parsedWindow.document.createElement('div');
|
||||
wrapper.className = 'pptx-preview-wrapper';
|
||||
wrapper.style.width = `${initOptions.width}px`;
|
||||
wrapper.style.background = '#000';
|
||||
wrapper.style.margin = '0 auto';
|
||||
if (initOptions.height != null) {
|
||||
wrapper.style.height = `${initOptions.height}px`;
|
||||
wrapper.style.overflowY = 'auto';
|
||||
}
|
||||
container.appendChild(wrapper);
|
||||
return {
|
||||
preview(): Promise<{ slides: unknown[] }> {
|
||||
if (preview === 'reject') {
|
||||
return Promise.reject(new Error('fake-render-error'));
|
||||
}
|
||||
if (preview === 'resolveNoSlides') {
|
||||
return Promise.resolve({ slides: [] });
|
||||
}
|
||||
if (preview === 'neverResolve') {
|
||||
return new Promise<{ slides: unknown[] }>(() => {});
|
||||
}
|
||||
const emptyWrappers = preview === 'resolveEmptyWrappers';
|
||||
for (let index = 0; index < slideCount; index += 1) {
|
||||
const slide = parsedWindow.document.createElement('div');
|
||||
slide.className = 'pptx-preview-slide-wrapper';
|
||||
slide.style.width = `${initOptions.width}px`;
|
||||
slide.style.height = `${initOptions.width * slideAspectRatio}px`;
|
||||
slide.style.position = 'relative';
|
||||
slide.style.margin = '0px auto 10px';
|
||||
if (!emptyWrappers) {
|
||||
slide.textContent = `Slide ${index + 1}`;
|
||||
}
|
||||
wrapper.appendChild(slide);
|
||||
}
|
||||
return Promise.resolve({ slides: new Array(slideCount).fill({}) });
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
// Flushes the `previewer.preview(...).then(finalize)` microtask queued
|
||||
// by the synchronous script execution above.
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
|
||||
if (installRenderer && !initializationOptions) {
|
||||
throw new Error('pptxPreview.init was not called');
|
||||
}
|
||||
|
||||
return {
|
||||
window,
|
||||
document: window.document,
|
||||
initializationOptions,
|
||||
setRenderSlotWidth: (width: number) => {
|
||||
currentRenderSlotWidth = width;
|
||||
},
|
||||
fireSafetyNet: () => {
|
||||
if (!safetyNetCallback) {
|
||||
throw new Error('safety-net setTimeout was not captured');
|
||||
}
|
||||
safetyNetCallback();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe('pptx CDN bootstrap — slide layout', () => {
|
||||
it.each([
|
||||
[3, 500],
|
||||
[2, 1200],
|
||||
])(
|
||||
'wraps every one of %i slides as its own panel-width block (panel %ipx)',
|
||||
async (slideCount, renderSlotWidth) => {
|
||||
const { document } = await renderPptxLayout(slideCount, renderSlotWidth);
|
||||
const wraps = Array.from(document.querySelectorAll('.lc-slide-wrap'));
|
||||
expect(wraps).toHaveLength(slideCount);
|
||||
const expectedWidth = `${renderSlotWidth - 32}px`;
|
||||
wraps.forEach((wrap) => {
|
||||
expect(wrap.querySelectorAll('.pptx-preview-slide-wrapper')).toHaveLength(1);
|
||||
expect((wrap as HTMLElement).style.width).toBe(expectedWidth);
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
test('each slide wrap sits directly under the library box, not nested inside one shared wrap', async () => {
|
||||
const { document } = await renderPptxLayout(4, 640);
|
||||
const libraryBox = document.querySelector('.pptx-preview-wrapper');
|
||||
expect(libraryBox).not.toBeNull();
|
||||
const directChildren = Array.from(libraryBox!.children);
|
||||
expect(directChildren).toHaveLength(4);
|
||||
directChildren.forEach((child) => {
|
||||
expect(child.classList.contains('lc-slide-wrap')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
test('initializes pptx-preview with only a width, never a fixed height', async () => {
|
||||
const { initializationOptions } = await renderPptxLayout(1, 800);
|
||||
expect(initializationOptions).toEqual({ width: 960 });
|
||||
});
|
||||
|
||||
test('does not re-wrap an already-wrapped slide when the 8s safety net fires before preview() resolves', async () => {
|
||||
/* previewer.preview() can still be pending when the safety-net timer
|
||||
* fires (its bundled deps can take a while after appending slide
|
||||
* nodes). If both paths call finalize() unguarded, the slide gets
|
||||
* wrapped twice, nesting .lc-slide-wrap inside .lc-slide-wrap. */
|
||||
const html = await _internal.pptxToHtmlViaCdn(
|
||||
Buffer.from('fixture'),
|
||||
'<ol class="lc-pptx-list"><li>fallback</li></ol>',
|
||||
);
|
||||
|
||||
let safetyNetCallback: (() => void) | undefined;
|
||||
let resolvePreview: ((value: { slides: unknown[] }) => void) | undefined;
|
||||
const { window } = new JSDOM(html, {
|
||||
runScripts: 'dangerously',
|
||||
beforeParse(parsedWindow) {
|
||||
Object.defineProperty(parsedWindow.HTMLElement.prototype, 'clientWidth', {
|
||||
configurable: true,
|
||||
get(this: HTMLElement) {
|
||||
return this.id === 'lc-render' ? 800 : 0;
|
||||
},
|
||||
});
|
||||
// Capture the 8s safety-net timer instead of letting it run on the
|
||||
// real clock, so the test can fire it deterministically.
|
||||
Object.defineProperty(parsedWindow, 'setTimeout', {
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: (handler: () => void, timeout?: number) => {
|
||||
if (timeout === 8000) {
|
||||
safetyNetCallback = handler;
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
},
|
||||
});
|
||||
Object.assign(parsedWindow, {
|
||||
pptxPreview: {
|
||||
init(container: HTMLElement, options: FakePptxPreviewInitOptions) {
|
||||
const wrapper = parsedWindow.document.createElement('div');
|
||||
wrapper.className = 'pptx-preview-wrapper';
|
||||
container.appendChild(wrapper);
|
||||
return {
|
||||
preview() {
|
||||
// Slide DOM lands synchronously, mirroring the pinned
|
||||
// library appending nodes as it renders — but the
|
||||
// outer promise is held open so the test can fire the
|
||||
// safety net BEFORE it resolves.
|
||||
const slide = parsedWindow.document.createElement('div');
|
||||
slide.className = 'pptx-preview-slide-wrapper';
|
||||
slide.style.width = `${options.width}px`;
|
||||
slide.style.height = `${options.width * 0.5625}px`;
|
||||
slide.textContent = 'Slide 1';
|
||||
wrapper.appendChild(slide);
|
||||
return new Promise((resolve) => {
|
||||
resolvePreview = resolve;
|
||||
});
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
if (!safetyNetCallback) {
|
||||
throw new Error('safety-net setTimeout was not captured');
|
||||
}
|
||||
// Safety net fires first: a slide is already in the DOM, so it calls
|
||||
// finalize() and wraps it.
|
||||
safetyNetCallback();
|
||||
|
||||
// The slow preview() promise resolves after the safety net already
|
||||
// finalized — its own .then() must not wrap the same slide again.
|
||||
if (!resolvePreview) {
|
||||
throw new Error('preview() was not called');
|
||||
}
|
||||
resolvePreview({ slides: [{}] });
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
|
||||
const wraps = Array.from(window.document.querySelectorAll('.lc-slide-wrap'));
|
||||
expect(wraps).toHaveLength(1);
|
||||
expect(window.document.querySelectorAll('.lc-slide-wrap .lc-slide-wrap')).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('pptx CDN bootstrap — fits every panel width', () => {
|
||||
it.each([360, 768, 1480])(
|
||||
'fits a %ipx panel and lets the library box hug the content instead of clipping it',
|
||||
async (renderSlotWidth) => {
|
||||
const { document } = await renderPptxLayout(3, renderSlotWidth);
|
||||
const expectedWidth = `${renderSlotWidth - 32}px`;
|
||||
const wraps = Array.from(document.querySelectorAll('.lc-slide-wrap')) as HTMLElement[];
|
||||
expect(wraps).toHaveLength(3);
|
||||
wraps.forEach((wrap) => {
|
||||
expect(wrap.style.width).toBe(expectedWidth);
|
||||
});
|
||||
|
||||
const libraryBox = document.querySelector('.pptx-preview-wrapper') as HTMLElement;
|
||||
expect(libraryBox).not.toBeNull();
|
||||
// The librarys own init() sets a fixed inline width + opaque
|
||||
// background; JSDOMs getComputedStyle doesnt apply the stylesheets
|
||||
// `!important` override over that inline style (unlike a real
|
||||
// browser), so this asserts directly on what the bootstrap script
|
||||
// must clear once slides are wrapped. A library box still carrying
|
||||
// a fixed px width here would be wider than a narrow panels content
|
||||
// box (360-32=328px and 768-32=736px are both under the librarys
|
||||
// 960px default), clipping the stacked slides.
|
||||
expect(libraryBox.style.width).toBe('auto');
|
||||
expect(libraryBox.style.background).toBe('transparent');
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe('pptx CDN bootstrap — no inner scroll box for a long deck', () => {
|
||||
test('30 slides stack in order with no nested scroll region', async () => {
|
||||
const { document, window } = await renderPptxLayout(30, 768);
|
||||
const wraps = Array.from(document.querySelectorAll('.lc-slide-wrap')) as HTMLElement[];
|
||||
expect(wraps).toHaveLength(30);
|
||||
wraps.forEach((wrap, index) => {
|
||||
const slide = wrap.querySelector('.pptx-preview-slide-wrapper');
|
||||
expect(slide?.textContent).toBe(`Slide ${index + 1}`);
|
||||
});
|
||||
|
||||
const renderRoot = document.getElementById('lc-render') as HTMLElement;
|
||||
const descendants = Array.from(renderRoot.querySelectorAll('*')) as HTMLElement[];
|
||||
descendants.forEach((element) => {
|
||||
const computed = window.getComputedStyle(element);
|
||||
const hasFixedPxHeight = /^\d/.test(element.style.height) || /^\d/.test(computed.height);
|
||||
const hasScrollOverflowY =
|
||||
['auto', 'scroll'].includes(element.style.overflowY) ||
|
||||
['auto', 'scroll'].includes(computed.overflowY);
|
||||
expect(hasFixedPxHeight && hasScrollOverflowY).toBe(false);
|
||||
});
|
||||
|
||||
const libraryBox = document.querySelector('.pptx-preview-wrapper') as HTMLElement;
|
||||
expect(libraryBox.style.height).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('pptx CDN bootstrap — refits on panel resize', () => {
|
||||
test('every slide block refits when the render slot is resized', async () => {
|
||||
const { document, window, setRenderSlotWidth } = await renderPptxLayout(2, 1480);
|
||||
let wraps = Array.from(document.querySelectorAll('.lc-slide-wrap')) as HTMLElement[];
|
||||
wraps.forEach((wrap) => {
|
||||
expect(wrap.style.width).toBe('1448px');
|
||||
});
|
||||
|
||||
setRenderSlotWidth(360);
|
||||
window.dispatchEvent(new window.Event('resize'));
|
||||
wraps = Array.from(document.querySelectorAll('.lc-slide-wrap')) as HTMLElement[];
|
||||
wraps.forEach((wrap) => {
|
||||
expect(wrap.style.width).toBe('328px');
|
||||
const height = Number.parseFloat(wrap.style.height);
|
||||
expect(Math.abs(height - 328 * 0.5625)).toBeLessThanOrEqual(0.5);
|
||||
});
|
||||
|
||||
setRenderSlotWidth(768);
|
||||
window.dispatchEvent(new window.Event('resize'));
|
||||
wraps = Array.from(document.querySelectorAll('.lc-slide-wrap')) as HTMLElement[];
|
||||
wraps.forEach((wrap) => {
|
||||
expect(wrap.style.width).toBe('736px');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('pptx CDN bootstrap — keeps each slides native aspect ratio', () => {
|
||||
it.each([
|
||||
[0.75, '4:3'],
|
||||
[0.5625, '16:9'],
|
||||
])('scales a %s deck without distorting it', async (ratio) => {
|
||||
const { document } = await renderPptxLayout(2, 768, { slideAspectRatio: ratio });
|
||||
const wraps = Array.from(document.querySelectorAll('.lc-slide-wrap')) as HTMLElement[];
|
||||
expect(wraps).toHaveLength(2);
|
||||
wraps.forEach((wrap) => {
|
||||
const width = Number.parseFloat(wrap.style.width);
|
||||
const height = Number.parseFloat(wrap.style.height);
|
||||
expect(height / width).toBeCloseTo(ratio, 2);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('pptx CDN bootstrap — falls back to the slide list', () => {
|
||||
test('the pptxPreview global is missing', async () => {
|
||||
const { document } = await renderPptxLayout(2, 768, { installRenderer: false });
|
||||
const fallback = document.getElementById('lc-fallback') as HTMLElement;
|
||||
const render = document.getElementById('lc-render') as HTMLElement;
|
||||
expect(fallback.hidden).toBe(false);
|
||||
expect(render.hidden).toBe(true);
|
||||
});
|
||||
|
||||
test('the renderer throws (preview() rejects)', async () => {
|
||||
const { document } = await renderPptxLayout(2, 768, { preview: 'reject' });
|
||||
const fallback = document.getElementById('lc-fallback') as HTMLElement;
|
||||
const render = document.getElementById('lc-render') as HTMLElement;
|
||||
expect(fallback.hidden).toBe(false);
|
||||
expect(render.hidden).toBe(true);
|
||||
});
|
||||
|
||||
test('the renderer resolves with no slides', async () => {
|
||||
const { document } = await renderPptxLayout(0, 768, { preview: 'resolveNoSlides' });
|
||||
const fallback = document.getElementById('lc-fallback') as HTMLElement;
|
||||
const render = document.getElementById('lc-render') as HTMLElement;
|
||||
expect(fallback.hidden).toBe(false);
|
||||
expect(render.hidden).toBe(true);
|
||||
});
|
||||
|
||||
test('the renderer resolves slides but every slide wrapper is empty', async () => {
|
||||
const { document } = await renderPptxLayout(2, 768, { preview: 'resolveEmptyWrappers' });
|
||||
const fallback = document.getElementById('lc-fallback') as HTMLElement;
|
||||
const render = document.getElementById('lc-render') as HTMLElement;
|
||||
expect(fallback.hidden).toBe(false);
|
||||
expect(render.hidden).toBe(true);
|
||||
});
|
||||
|
||||
test('the 8s safety net fires with nothing rendered', async () => {
|
||||
const { document, fireSafetyNet } = await renderPptxLayout(2, 768, {
|
||||
preview: 'neverResolve',
|
||||
captureSafetyNet: true,
|
||||
});
|
||||
fireSafetyNet();
|
||||
const fallback = document.getElementById('lc-fallback') as HTMLElement;
|
||||
const render = document.getElementById('lc-render') as HTMLElement;
|
||||
expect(fallback.hidden).toBe(false);
|
||||
expect(render.hidden).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('pptx CDN bootstrap — spacing between stacked slide blocks', () => {
|
||||
test('restores the 16px rhythm that #lc-render gap no longer provides once slides live inside the library box', async () => {
|
||||
const { document, window } = await renderPptxLayout(3, 768);
|
||||
const wraps = Array.from(document.querySelectorAll('.lc-slide-wrap')) as HTMLElement[];
|
||||
expect(wraps).toHaveLength(3);
|
||||
expect(window.getComputedStyle(wraps[1]).marginTop).toBe('16px');
|
||||
expect(window.getComputedStyle(wraps[2]).marginTop).toBe('16px');
|
||||
});
|
||||
});
|
||||
|
||||
describe('pptx CDN bootstrap — pins each slide to its blocks left edge', () => {
|
||||
it.each([1480, 1200])(
|
||||
'clears the librarys auto-centering margin on a %ipx panel',
|
||||
async (renderSlotWidth) => {
|
||||
const { document } = await renderPptxLayout(2, renderSlotWidth);
|
||||
const wraps = Array.from(document.querySelectorAll('.lc-slide-wrap')) as HTMLElement[];
|
||||
expect(wraps).toHaveLength(2);
|
||||
wraps.forEach((wrap) => {
|
||||
const slide = wrap.querySelector('.pptx-preview-slide-wrapper') as HTMLElement;
|
||||
expect(slide.style.marginLeft).toBe('0px');
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
test('stays pinned after a live resize from 768 to 1480', async () => {
|
||||
const { document, window, setRenderSlotWidth } = await renderPptxLayout(2, 768);
|
||||
setRenderSlotWidth(1480);
|
||||
window.dispatchEvent(new window.Event('resize'));
|
||||
const wraps = Array.from(document.querySelectorAll('.lc-slide-wrap')) as HTMLElement[];
|
||||
expect(wraps).toHaveLength(2);
|
||||
wraps.forEach((wrap) => {
|
||||
const slide = wrap.querySelector('.pptx-preview-slide-wrapper') as HTMLElement;
|
||||
expect(slide.style.marginLeft).toBe('0px');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('pptx CDN bootstrap — reserves the scrollbar gutter', () => {
|
||||
test('the root element reserves scrollbar space so its appearance never changes the content width', async () => {
|
||||
const html = await _internal.pptxToHtmlViaCdn(
|
||||
Buffer.from('fixture'),
|
||||
'<ol class="lc-pptx-list"><li>fallback</li></ol>',
|
||||
);
|
||||
const { window } = new JSDOM(html);
|
||||
const gutter = window
|
||||
.getComputedStyle(window.document.documentElement)
|
||||
.getPropertyValue('scrollbar-gutter');
|
||||
expect(gutter).toBe('stable');
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue