Files
genoffice/apps/docs/tests/page-break-run-rpr.test.ts
T
merrick-2002andGenOffice 69b4ce0560 Sync snapshot (2026-09-06) (#243)
- Docs: Word-style dark page, phased fast open for long documents, fonts embedded in the DOCX, spellcheck toggle, hyperlink editing, and ~80 Word fidelity fixes (CJK/JP metrics, header/footer, tables, anchored pictures)
- Sheets: hold formula recalculation while file data streams in, large-workbook scroll/load performance, combo charts, duplicate/unique conditional formatting by display text, RTL frozen-pane fix, workbooks whose ZIP entries carry a leading slash
- Slides: MTX-compressed embedded font support, CJK/Latin face selection and PowerPoint script fallbacks, theme colors kept on paragraph defaults and bullets, more reliable AI edits with a layout audit after each batch
- PDF: AI page ops, text block move/align, notes and markup editing, watermark and header/footer, form check boxes, metadata; the cloud Export as Word fallback is removed, conversion is always on-device
- AI: Gemini tool schema translation, OpenCode Zen/Go providers, markdown tables and code blocks in chat, current date in the system prompt
- Shell: Office-style ribbon collapse across all editors, native Windows ARM64 installer, BUILD_DIR override for packaging

Co-authored-by: GenOffice <merrick-2002@users.noreply.github.com>
2026-09-07 00:18:14 +08:00

63 lines
2.8 KiB
TypeScript

import { Editor } from '@tiptap/core'
import { TextSelection } from '@tiptap/pm/state'
import JSZip from 'jszip'
import { parseDocx, saveDocx } from '@genoffice/docx-engine'
import { describe, expect, it } from 'vitest'
import { buildDocx } from '../../../packages/docx-engine/tests/helpers/build-docx'
import { blocksToPmDoc, pmDocToSavePlan, type PmNode } from '../src/renderer/editor/convert'
import { editorExtensions } from '../src/renderer/editor/extensions'
// A run holding only a page break still has its own rPr (Word sizes the break
// line by it). The editor models the break as a hardBreak node: without marks
// the round trip produced a bare run, so every break-only paragraph looked
// edited and was regenerated without its formatting.
const BODY =
'<w:p><w:r><w:rPr><w:b/><w:sz w:val="32"/></w:rPr><w:br w:type="page"/></w:r></w:p>' +
'<w:p><w:r><w:rPr><w:i/></w:rPr><w:br w:type="page"/></w:r>' +
'<w:r><w:rPr><w:sz w:val="24"/></w:rPr><w:t>Chapter</w:t></w:r></w:p>' +
'<w:p><w:r><w:t>plain</w:t></w:r></w:p>'
async function openDoc() {
const bytes = await buildDocx({ bodyXml: BODY })
const parsed = await parseDocx(bytes)
const editor = new Editor({
element: document.createElement('div'),
extensions: editorExtensions,
content: blocksToPmDoc(parsed.blocks) as never,
})
return { bytes, parsed, editor }
}
describe('break-only runs keep their rPr', () => {
it('untouched page-break paragraphs stay on the original-bytes path', async () => {
const { bytes, parsed, editor } = await openDoc()
const plan = pmDocToSavePlan(editor.getJSON() as PmNode, parsed.blocks)
expect(plan.changedCount).toBe(0)
expect(await saveDocx(parsed, plan.saveBlocks)).toEqual(bytes)
editor.destroy()
})
it('an edited paragraph re-emits its break run with the original rPr', async () => {
const { parsed, editor } = await openDoc()
let at = -1
editor.state.doc.descendants((node, pos) => {
if (node.isText && node.text === 'Chapter') at = pos + node.text.length
})
editor.view.dispatch(
editor.state.tr.setSelection(TextSelection.create(editor.state.doc, at)).insertText(' 1', at),
)
const plan = pmDocToSavePlan(editor.getJSON() as PmNode, parsed.blocks)
expect(plan.changedCount).toBe(1)
const block = plan.saveBlocks[1]
if (block.kind !== 'generated') throw new Error(`expected generated, got ${block.kind}`)
expect(block.block.runs.map((r) => [r.text, !!r.italic, r.sizeHalfPoints ?? null])).toEqual([
['\f', true, null],
['Chapter 1', false, 24],
])
const zip = await JSZip.loadAsync(await saveDocx(parsed, plan.saveBlocks))
const xml = await zip.file('word/document.xml')!.async('string')
expect(xml).toContain('<w:rPr><w:i/></w:rPr><w:br w:type="page"/>')
editor.destroy()
})
})