fix: a date/time format outside the catalog stays a live field

Both importers matched a file's format against the seven date and four time
formats the picker offers and kept anything else as the text the producer had
cached — a Word template's `DATE \@ "MMMM d"` then froze at the day it was
written. An unlisted format now rides its own DOCX picture as the key:
`findFormat` parses it, so display, ODF number style and DOCX picture are
derived from it as they are for a catalog entry, and it re-exports unchanged.

Claude-Session: https://claude.ai/code/session_01Wzs2qG6Y5qm7mKmJHfu44G
This commit is contained in:
Steffen Becker committed 2026-09-10 23:34:55 +02:00
1 parent dca3c21e2c
commit 825af46ba4
4 files changed
+114 -5

No files matched your search

+56
View File
@@ -0,0 +1,56 @@
// A DATE/TIME field whose picture the catalog does not list: it stays a live field,
// keyed by the picture itself, and renders and re-exports the same.
import { describe, it, expect } from 'vitest';
import { zipSync, strToU8, unzipSync, strFromU8 } from 'fflate';
import { importDocx } from '../../src/lib/import/docx';
import { buildDocx } from '../../src/lib/export/docx';
import { findFormat, renderFormat, docxPicture } from '../../src/lib/utils/dateTime';
const CT = `<?xml version="1.0"?><Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
<Default Extension="xml" ContentType="application/xml"/>
<Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
</Types>`;
const RELS = `<?xml version="1.0"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
</Relationships>`;
const W = 'xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"';
const DOCUMENT = `<?xml version="1.0"?><w:document ${W}><w:body><w:p>
<w:r><w:fldChar w:fldCharType="begin"/></w:r>
<w:r><w:instrText xml:space="preserve"> DATE \\@ "MMMM d" \\* MERGEFORMAT </w:instrText></w:r>
<w:r><w:fldChar w:fldCharType="separate"/></w:r>
<w:r><w:t>July 10</w:t></w:r>
<w:r><w:fldChar w:fldCharType="end"/></w:r>
</w:p></w:body></w:document>`;
const file = zipSync({
'[Content_Types].xml': strToU8(CT),
'_rels/.rels': strToU8(RELS),
'word/document.xml': strToU8(DOCUMENT),
});
const walk = (n: any, type: string, out: any[] = []): any[] => {
for (const c of n.content ?? []) { if (c.type === type) out.push(c); walk(c, type, out); }
return out;
};
describe('a date field format the catalog does not list', () => {
it('imports as a live field keyed by its own picture', () => {
const fields = walk(importDocx(file).content, 'dateTimeField');
expect(fields).toHaveLength(1);
expect(fields[0].attrs).toMatchObject({ kind: 'date', format: 'MMMM d', fixed: false });
});
it('renders the current date in that format', () => {
const fmt = findFormat('MMMM d')!;
expect(renderFormat(fmt, new Date(2026, 8, 10), 'en-US')).toBe('September 10');
expect(docxPicture(fmt)).toBe('MMMM d');
});
it('exports the picture back, so the field stays live in Word', async () => {
const doc = importDocx(file).content;
const xml = strFromU8(unzipSync(await buildDocx(doc as any))['word/document.xml']);
expect(xml).toMatch(/<w:fldSimple[^>]*w:instr="[^"]*DATE[^"]*MMMM d/);
});
});