import { describe, expect, it } from 'vitest';
import { buildDocxFromBody } from './fixtures/build-docx';
import { buildXlsx } from './fixtures/build-xlsx';
import { createConverter } from '@/core/converter/facade';
import { Ream } from '@/core/converter/ream';
import { ConversionLossError, pt } from '@/core/ir';
import { htmlWriter, writeHtml } from '@/html/html-writer';
import { readDocx } from '@/word/docx-reader';
const decode = (b: Uint8Array) => new TextDecoder().decode(b);
describe('html writer (FlowDoc adapter)', () => {
it('converts docx → flowed HTML with no fonts and no I/O', async () => {
const docx = buildDocxFromBody(
'Title' +
'' +
'styled run',
);
// No fonts anywhere in the options — html conversion must not need them.
const html = decode(await Ream.parse(docx).convert('html'));
expect(html.startsWith('')).toBe(true);
expect(html).toContain('
');
expect(html).toContain('colspan="2"');
expect(html).toContain('rowspan="2"');
expect(html).toContain('background-color:#FFCC00');
expect(html).toContain('border-top:');
// The vMerge continuation cell must not be emitted as its own
{
// Thirty 20-character columns are some 4000pt: printed, the sheet is cut
// into column bands a page wide, and the page breaks between them and
// between the sheets. On a screen it is one table, and every tab shows —
// the empty one too.
const wide = Array.from({ length: 30 }, (_, i) => `c${String(i + 1)}`);
const xlsx = buildXlsx({
sheets: [
{ name: 'Wide', rows: [wide], columns: [{ min: 1, max: 30, widthChars: 20 }] },
{ name: 'Empty', rows: [] },
{ name: 'R&D', rows: [['x']] },
],
});
const html = decode(await Ream.parse(xlsx).convert('html'));
expect(
[...html.matchAll(//gu)].map((m) => m[1]),
).toEqual(['Wide', 'Empty', 'R&D']);
const wideSheet = html.slice(
html.indexOf('data-sheet="Wide"'),
html.indexOf('data-sheet="Empty"'),
);
expect(wideSheet.match(/
R&D');
// No page, so no page-wide column for the sheet to be squeezed into.
expect(html).toContain('');
});
it("draws the window's gridlines, but not over a fill nor where the sheet hides them", async () => {
const STYLES = `
`;
const xlsx = buildXlsx({
stylesXml: STYLES,
sheets: [
{ name: 'Grid', rows: [['plain', { value: 'filled', styleIndex: 1 }]] },
{ name: 'Clean', rows: [['plain']], hideGridLines: true },
],
});
const html = decode(await Ream.parse(xlsx).convert('html'));
const sheet = (name: string): string =>
html.slice(html.indexOf(`data-sheet="${name}"`)).split('')[0]!;
// Three edges round the plain cell: the fourth is the yellow one's, and a
// fill covers the gridlines on every side of it.
expect(sheet('Grid').match(/0\.5pt solid #D4D4D4/gu)).toHaveLength(3);
expect(
/
\s*
]*>]*>filled/u.exec(sheet('Grid'))?.[1],
).not.toContain('D4D4D4');
expect(sheet('Clean')).not.toContain('D4D4D4');
});
it('flags a noted cell in its corner, and shows the note on hover', async () => {
const xlsx = buildXlsx({
rows: [['noted', 'plain']],
comments: [{ ref: 'A1', author: 'Ada', text: 'look here' }],
});
const html = decode(await Ream.parse(xlsx).convert('html'));
expect(html).toMatch(/
{
// A gridline and a thin rule come out the same pixel wide, and collapsed
// borders then go to the cell above or to the left: a cell ruled all
// round lost its top and left sides to the grey grid next to them.
const STYLES = `
`;
const xlsx = buildXlsx({
stylesXml: STYLES,
rows: [
['', 'above'],
['beside', { value: 'ruled', styleIndex: 1 }],
],
});
const html = decode(await Ream.parse(xlsx).convert('html'));
const styleOf = (word: string): string =>
new RegExp(`
\\s*
]*>]*>${word}<`, 'u').exec(html)?.[1] ?? '';
expect(styleOf('ruled')).toContain('border-top:0.75pt solid #000000');
expect(styleOf('above')).not.toContain('border-bottom');
expect(styleOf('beside')).not.toContain('border-right');
// The edges nobody rules keep their gridline.
expect(styleOf('above')).toContain('border-top:0.5pt solid #D4D4D4');
});
it('sets a grid row at its height and a cell at the bottom of it', async () => {
const xlsx = buildXlsx({
rows: [['tall'], ['plain']],
rowHeights: [{ row: 0, heightPt: 30, customHeight: true }],
columns: [{ min: 1, max: 1, widthChars: 10 }],
});
const html = decode(await Ream.parse(xlsx).convert('html'));
expect(html).toContain('
');
expect(html).toContain('
');
expect(html).toMatch(/
/u);
});
it("places a sheet's drawing on its surface where it is anchored", () => {
const { doc } = readDocx(buildDocxFromBody('x'));
const html = decode(
writeHtml({
...doc,
sections: [
{ properties: { headers: [], footers: [] }, endIndex: 2, sheet: { name: 'Plot' } },
],
body: [
{
kind: 'shape' as const,
shape: {
float: {
wrap: 'none' as const,
posH: { relativeFrom: 'margin' as const, offsetPt: pt(100) },
posV: { relativeFrom: 'margin' as const, offsetPt: pt(50) },
},
width: pt(80),
height: pt(40),
geometry: { kind: 'preset' as const, preset: 'rect' },
fill: { kind: 'solid' as const, colorHex: 'FF0000' },
paragraphProperties: {},
},
},
{
kind: 'paragraph' as const,
paragraph: { properties: {}, runs: [{ text: 'under it', properties: {} }] },
},
],
}).bytes,
);
expect(html).toContain(
'
',
);
// The surface reaches the drawing's far corner, so the next sheet starts below it.
expect(html).toContain('