Files
edentext/tests/unit/tab-interval.test.ts
T
Steffen Becker ea39dde58b feat: a tab lands on the file's own grid, and can draw its leader
The default tab interval was fixed at 1.25cm, so a file's own
w:defaultTabStop / style:tab-stop-distance was ignored and the DOCX
export wrote 1.25 back over Word's 1.27. It is a document setting now.

A stop can also carry the character it repeats across its gap. odf-kit
has no leader option and mints one automatic style per option set, so
the leader rides style:type behind a sentinel — matching on position
and alignment alone gave every stop at 6cm the same leader, which
LibreOffice drew back plain as day.
2026-08-08 14:40:26 +00:00

40 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// The document's default tab interval: Word's w:defaultTabStop, ODF's
// style:tab-stop-distance on the paragraph default-style.
import { describe, it, expect } from 'vitest';
import { unzipSync, strFromU8 } from 'fflate';
import { buildOdt } from '../../src/lib/export/odt';
import { importOdt } from '../../src/lib/import/odt';
import { buildDocx } from '../../src/lib/export/docx';
import { importDocx } from '../../src/lib/import/docx';
import { DEFAULT_TAB_INTERVAL_CM, clampTabInterval } from '../../src/lib/storage/tabInterval';
const doc: any = { type: 'doc', content: [{ type: 'paragraph', content: [{ type: 'text', text: 'a\tb' }] }] };
const opts = [undefined, 'portrait', undefined, undefined, 'A4', undefined] as const;
const odt = (cm?: number) => buildOdt(doc, ...(opts as any), cm);
const docx = (cm?: number) => buildDocx(doc, ...(opts as any), cm);
describe('default tab interval', () => {
it('rides an ODF default-style and comes back', async () => {
const back = await importOdt(await odt(1.27));
expect(back.tabIntervalCm).toBeCloseTo(1.27, 3);
});
it('leaves LibreOffice’s own interval undeclared', async () => {
const styles = strFromU8(unzipSync(await odt(DEFAULT_TAB_INTERVAL_CM))['styles.xml']);
expect(styles).not.toContain('style:tab-stop-distance');
expect((await importOdt(await odt(DEFAULT_TAB_INTERVAL_CM))).tabIntervalCm).toBe(null);
});
it('rides a DOCX setting and comes back', async () => {
// Word stores twips, so 1.27cm = 720tw is exact and 1.25cm rounds to 709tw.
expect(importDocx(await docx(1.27)).tabIntervalCm).toBeCloseTo(1.27, 2);
expect(importDocx(await docx(DEFAULT_TAB_INTERVAL_CM)).tabIntervalCm).toBeCloseTo(1.25, 2);
});
it('clamps a nonsense value to something renderable', () => {
expect(clampTabInterval(0)).toBe(0.05);
expect(clampTabInterval(999)).toBe(10);
expect(clampTabInterval(NaN)).toBe(DEFAULT_TAB_INTERVAL_CM);
});
});