feat: insert date/time

This commit is contained in:
Steffen Becker committed 2026-07-08 23:21:07 +02:00
1 parent 2cfdd7a44f
commit 1fb81c389f
15 files changed
+882 -16

No files matched your search

+29
View File
@@ -268,6 +268,35 @@ describe('DOCX export → import round trip', () => {
});
});
describe('DOCX date/time fields', () => {
const dtf = (kind: string, format: string, fixed: boolean, value: string): N =>
({ type: 'dateTimeField', attrs: { kind, format, fixed, value } });
const doc: N = { type: 'doc', content: [
para([text('Auto '), dtf('date', 'dmy_dots', false, '2026-07-08T00:00:00')]),
para([text('Fixed '), dtf('date', 'mdy_long', true, '2026-07-08T14:30:00')]),
para([text('Time '), dtf('time', 'hm24', false, '2026-07-08T14:30:00')]),
] };
it('exports an auto field as a DATE fldSimple and a fixed field as text', async () => {
const bytes = await buildDocx(doc, undefined, 'portrait', undefined, { language: 'de', country: 'DE' });
const xml = strFromU8(unzipSync(bytes)['word/document.xml']);
expect(xml).toMatch(/<w:fldSimple[^>]*w:instr="[^"]*DATE[^"]*dd\.MM\.yyyy/);
expect(xml).toMatch(/<w:fldSimple[^>]*w:instr="[^"]*TIME[^"]*HH:mm/);
// The fixed field has no fldSimple wrapper — just a plain run rendered in the
// document language (German month name here).
expect(xml).toContain('Juli 8, 2026');
expect(xml).not.toMatch(/<w:fldSimple[^>]*>[^<]*<w:r><w:t[^>]*>Juli/);
});
it('re-imports the auto DATE/TIME fields as live dateTimeField nodes', async () => {
const bytes = await buildDocx(doc, undefined, 'portrait', undefined, { language: 'de', country: 'DE' });
const res = importDocx(bytes).content as N;
const fields = walk(res, 'dateTimeField');
expect(fields.map((f: N) => f.attrs.format).sort()).toEqual(['dmy_dots', 'hm24']);
expect(fields.every((f: N) => f.attrs.fixed === false)).toBe(true);
});
});
describe('DOCX import of a foreign Word document', () => {
const W = 'xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"';
const documentXml = `<?xml version="1.0"?><w:document ${W}><w:body>
+47
View File
@@ -1048,3 +1048,50 @@ describe('Leg 5: table of contents (text:table-of-content)', () => {
blocks.some((n: N) => n.type === 'heading' && n.content?.[0]?.text === 'Introduction'));
});
});
describe('Leg 10: date/time fields (text:date / text:time)', () => {
const DTF = (kind: string, format: string, fixed: boolean, value: string, marks?: N[]): N =>
({ type: 'dateTimeField', attrs: { kind, format, fixed, value }, ...(marks ? { marks } : {}) });
const FONT = { type: 'textStyle', attrs: { fontFamily: 'Calibri' } };
const dtDoc: N = {
type: 'doc',
content: [
P(null, T('Signed on '), DTF('date', 'dmy_dots', true, '2026-07-08T14:30:45', [FONT]), T(' at '),
DTF('time', 'hms24', true, '2026-07-08T14:30:45')),
P(null, T('Printed: '), DTF('date', 'weekday_mdy', false, '2026-07-08T09:00:00')),
],
};
it('exports <text:date>/<text:time> with a minted number style and re-imports the fields', async () => {
const bytes = await buildOdt(dtDoc, margins, 'portrait', undefined, { language: 'de', country: 'DE' });
const content = strFromU8(unzipSync(bytes)['content.xml']);
check('emits <text:date>', content.includes('<text:date '), content.slice(0, 120));
check('emits <text:time>', content.includes('<text:time '));
check('fixed date carries text:fixed=true', /<text:date[^>]*text:fixed="true"/.test(content));
check('auto date carries text:fixed=false', /<text:date[^>]*text:fixed="false"/.test(content));
check('date-value present', content.includes('text:date-value="2026-07-08T14:30:45"'));
check('time-value present', content.includes('text:time-value="PT14H30M45S"'));
check('mints a number:date-style', content.includes('<number:date-style '));
check('mints a number:time-style', content.includes('<number:time-style '));
check('declares number namespace', content.includes('xmlns:number='));
check('no leftover sentinel', !content.includes(''));
const res = importOdt(bytes);
const fields: N[] = [];
(function walk(n: N) { if (n.type === 'dateTimeField') fields.push(n); for (const c of n.content ?? []) walk(c); })(res.content);
check('3 date/time fields imported', fields.length === 3, fields.map((f) => f.attrs));
const fixedDate = fields.find((f) => f.attrs.kind === 'date' && f.attrs.fixed);
check('fixed date format round-trips', fixedDate?.attrs.format === 'dmy_dots', fixedDate?.attrs);
check('fixed date value round-trips', fixedDate?.attrs.value === '2026-07-08T14:30:45', fixedDate?.attrs);
// The field carries the surrounding font so the atom doesn't fall back to the
// editor default (regression: DOCX date field imported without its font).
const font = (fixedDate?.marks ?? []).find((m: N) => m.type === 'textStyle')?.attrs?.fontFamily;
check('fixed date carries its font mark', font === 'Calibri', fixedDate?.marks);
const fixedTime = fields.find((f) => f.attrs.kind === 'time');
check('fixed time format round-trips', fixedTime?.attrs.format === 'hms24', fixedTime?.attrs);
const autoDate = fields.find((f) => f.attrs.kind === 'date' && !f.attrs.fixed);
check('auto date format round-trips', autoDate?.attrs.format === 'weekday_mdy', autoDate?.attrs);
check('surrounding text preserved',
(res.content.content ?? [])[0]?.content?.[0]?.text === 'Signed on ');
});
});