Files
genoffice/apps/docs/tests/pagepinned-preview.test.ts
T
merrick-2002andGenOffice 9f971edafa Sync snapshot (2026-09-02) (#181)
- Docs: DOCX rendering and pagination fidelity (page borders, VML pictures, header/footer images, cross-page tables, anchored floats, character-unit indents, CJK typography)
- Docs: more reliable PDF export for huge documents; AI rewrites keep the replaced content's formatting
- Sheets: merge workbooks; Excel-standard keyboard shortcuts; PDF export with header/footer variants and pictures
- Sheets: rendering, conditional formatting, chart and formula fidelity fixes; right-to-left sheet support
- Slides: WordArt text warp, improved text layout and font substitution; AI table edits keep formatting

Co-authored-by: GenOffice <merrick-2002@users.noreply.github.com>
2026-09-03 00:12:17 +08:00

86 lines
3.5 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { pinnedFloatPage, type PageSlice } from '../src/renderer/pagination'
import { pinnedCloneCss } from '../src/renderer/components/PaginationPreview'
const slice = (start: number, end: number): PageSlice => ({ start, end, section: 0 })
describe('pinnedFloatPage', () => {
const slices = [slice(0, 900), slice(900, 1800), slice(1800, 2700)]
it('maps an anchor to its containing slice', () => {
expect(pinnedFloatPage(slices, 0)).toBe(0)
expect(pinnedFloatPage(slices, 899.9)).toBe(0)
expect(pinnedFloatPage(slices, 900)).toBe(1)
expect(pinnedFloatPage(slices, 2000)).toBe(2)
})
it('clamps out-of-range anchors', () => {
expect(pinnedFloatPage(slices, -5)).toBe(0)
expect(pinnedFloatPage(slices, 2700)).toBe(2)
expect(pinnedFloatPage(slices, 99999)).toBe(2)
expect(pinnedFloatPage([], 100)).toBe(0)
})
})
describe('pinnedCloneCss', () => {
const page = (index: number, html: string): HTMLElement => {
const el = document.createElement('div')
el.className = 'pv-page'
el.dataset.pvPage = String(index)
el.innerHTML = html
return el
}
const pinned = (pin?: number) =>
`<div class="doc-protected doc-protected-floating doc-protected-pagepinned"${
pin === undefined ? '' : ` data-pin-page="${pin}"`
}><div class="doc-textbox"></div></div>`
const boxes = (el: HTMLElement): HTMLElement[] =>
Array.from(el.querySelectorAll('.doc-protected-pagepinned'))
const matches = (root: HTMLElement, css: string): boolean[] => {
const selectors = css
.split('\n')
.filter(Boolean)
.map((r) => r.slice(0, r.indexOf('{')))
return boxes(root).map((b) => selectors.some((s) => b.matches(s)))
}
it('hides clones owned by other pages, keeps the owning page copy', () => {
const css = pinnedCloneCss(2)
// visibility, not display: stray-run wrappers keep their measured flow height
expect(css).toContain('visibility:hidden')
expect(css).not.toContain('display')
// full-document clone: every page carries both covers (pin 0 and pin 1)
const p0 = page(0, pinned(0) + pinned(1) + '<p>body</p>')
const p1 = page(1, pinned(0) + pinned(1) + '<p>body</p>')
expect(matches(p0, css)).toEqual([false, true])
expect(matches(p1, css)).toEqual([true, false])
})
it('never targets unstamped wrappers', () => {
const css = pinnedCloneCss(3)
const p = page(2, pinned() + pinned(2) + pinned(0))
expect(matches(p, css)).toEqual([false, false, true])
})
it('emits no rules for an empty preview', () => {
expect(pinnedCloneCss(0)).toBe('')
})
it('hides hoisted spill-float boxes on non-owning pages, box only (not the stray line)', () => {
const css = pinnedCloneCss(2)
const hoisted = (pin: number) =>
`<div class="doc-protected doc-protected-floating" data-pv-hoist="1" data-pin-page="${pin}">` +
`<div class="doc-anchor-strut"><br></div><div class="doc-textbox"></div></div>`
const p0 = page(0, hoisted(0) + hoisted(1))
const selectors = css
.split('\n')
.filter(Boolean)
.flatMap((r) => r.slice(0, r.indexOf('{')).split(','))
const hidden = (root: HTMLElement, sel: string) =>
Array.from(root.querySelectorAll(sel)).map((b) => selectors.some((s) => b.matches(s)))
// the escaped boxes hide on the foreign page's clone; the flow strut stays
expect(hidden(p0, '.doc-textbox')).toEqual([false, true])
expect(hidden(p0, '.doc-anchor-strut')).toEqual([false, false])
})
})