import type { TableCaptionFormat } from '../../formats';
import type { ToolOption } from '../../utils';
import Quill from 'quill';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { tableMenuTools } from '../../modules';
import { TableUp } from '../../table-up';
import { createQuillWithTableModule, createTable, createTableHTML, expectDelta } from './utils';
const Delta = Quill.import('delta');
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
describe('test tableCaption', () => {
it('insert tableCaption', async () => {
const quill = await createTable(3, 3, { full: false });
const tableModule = quill.getModule(TableUp.moduleName) as TableUp;
await vi.runAllTimersAsync();
const table = quill.root.querySelector('table')!;
(tableMenuTools.InsertCaption as ToolOption).handle.call({ quill, table } as any, tableModule, [], null);
await vi.runAllTimersAsync();
expectDelta(
quill.getContents(),
new Delta([
{ insert: '\nTable Caption' },
{ attributes: { 'table-up-caption': { tableId: '1', side: 'top' } }, insert: '\n' },
{ insert: { 'table-up-col': { tableId: '1', colId: '1', full: false, width: 100 } } },
{ insert: { 'table-up-col': { tableId: '1', colId: '2', full: false, width: 100 } } },
{ insert: { 'table-up-col': { tableId: '1', colId: '3', full: false, width: 100 } } },
{ insert: '1' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '1', colId: '1', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '2' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '1', colId: '2', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '3' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '1', colId: '3', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '4' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '2', colId: '1', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '5' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '2', colId: '2', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '6' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '2', colId: '3', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '7' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '3', colId: '1', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '8' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '3', colId: '2', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '9' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '3', colId: '3', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '\n' },
]),
);
expect(quill.root).toEqualHTML(
`
${createTableHTML(3, 3, { full: false }, { text: 'Table Caption' })}
`,
{ ignoreAttrs: ['class', 'contenteditable'] },
);
});
it('tableCaption switch position', async () => {
const quill = await createTable(3, 3, { full: false });
const tableModule = quill.getModule(TableUp.moduleName) as TableUp;
await vi.runAllTimersAsync();
const table = quill.root.querySelector('table')!;
(tableMenuTools.InsertCaption as ToolOption).handle.call({ quill, table } as any, tableModule, [], null);
await vi.runAllTimersAsync();
const tableCaption = Quill.find(table.querySelector('caption')!)! as TableCaptionFormat;
expect(tableCaption).not.toBeNull();
tableCaption.side = 'bottom';
await vi.runAllTimersAsync();
expectDelta(
quill.getContents(),
new Delta([
{ insert: '\nTable Caption' },
{ attributes: { 'table-up-caption': { tableId: '1', side: 'bottom' } }, insert: '\n' },
{ insert: { 'table-up-col': { tableId: '1', colId: '1', full: false, width: 100 } } },
{ insert: { 'table-up-col': { tableId: '1', colId: '2', full: false, width: 100 } } },
{ insert: { 'table-up-col': { tableId: '1', colId: '3', full: false, width: 100 } } },
{ insert: '1' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '1', colId: '1', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '2' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '1', colId: '2', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '3' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '1', colId: '3', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '4' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '2', colId: '1', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '5' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '2', colId: '2', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '6' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '2', colId: '3', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '7' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '3', colId: '1', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '8' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '3', colId: '2', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '9' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '3', colId: '3', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '\n' },
]),
);
expect(quill.root).toEqualHTML(
`
${createTableHTML(3, 3, { full: false }, { text: 'Table Caption', side: 'bottom' })}
`,
{ ignoreAttrs: ['class', 'contenteditable'] },
);
});
it('tableCaption should merge', async () => {
const quill = createQuillWithTableModule(`
`);
quill.setContents([
{ insert: '\nTable Caption' },
{ attributes: { 'table-up-caption': { tableId: '1', side: 'bottom' } }, insert: '\n' },
{ insert: { 'table-up-col': { tableId: '1', colId: '1', full: false, width: 100 } } },
{ insert: { 'table-up-col': { tableId: '1', colId: '2', full: false, width: 100 } } },
{ insert: { 'table-up-col': { tableId: '1', colId: '3', full: false, width: 100 } } },
{ insert: '1' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '1', colId: '1', rowspan: 1, colspan: 1 } }, insert: '\n' },
{ insert: '2' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '1', colId: '2', rowspan: 1, colspan: 1 } }, insert: '\n' },
{ insert: '3' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '1', colId: '3', rowspan: 1, colspan: 1 } }, insert: '\n' },
{ insert: '4' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '2', colId: '1', rowspan: 1, colspan: 1 } }, insert: '\n' },
{ insert: '5' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '2', colId: '2', rowspan: 1, colspan: 1 } }, insert: '\n' },
{ insert: '6' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '2', colId: '3', rowspan: 1, colspan: 1 } }, insert: '\n' },
{ insert: '7' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '3', colId: '1', rowspan: 1, colspan: 1 } }, insert: '\n' },
{ insert: '8' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '3', colId: '2', rowspan: 1, colspan: 1 } }, insert: '\n' },
{ insert: '9' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '3', colId: '3', rowspan: 1, colspan: 1 } }, insert: '\n' },
{ insert: '\n' },
]);
await vi.runAllTimersAsync();
const table = quill.root.querySelector('table')!;
const tableModule = quill.getModule(TableUp.moduleName) as TableUp;
(tableMenuTools.InsertCaption as ToolOption).handle.call({ quill, table } as any, tableModule, [], null);
await vi.runAllTimersAsync();
expectDelta(
quill.getContents(),
new Delta([
{ insert: '\nTable CaptionTable Caption' },
{ attributes: { 'table-up-caption': { tableId: '1', side: 'top' } }, insert: '\n' },
{ insert: { 'table-up-col': { tableId: '1', colId: '1', full: false, width: 100 } } },
{ insert: { 'table-up-col': { tableId: '1', colId: '2', full: false, width: 100 } } },
{ insert: { 'table-up-col': { tableId: '1', colId: '3', full: false, width: 100 } } },
{ insert: '1' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '1', colId: '1', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '2' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '1', colId: '2', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '3' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '1', colId: '3', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '4' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '2', colId: '1', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '5' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '2', colId: '2', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '6' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '2', colId: '3', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '7' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '3', colId: '1', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '8' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '3', colId: '2', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '9' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '3', colId: '3', rowspan: 1, colspan: 1, tag: 'td', wrapTag: 'tbody' } }, insert: '\n' },
{ insert: '\n' },
]),
);
expect(quill.root).toEqualHTML(
`
${createTableHTML(3, 3, { full: false }, { text: 'Table CaptionTable Caption' })}
`,
{ ignoreAttrs: ['class', 'contenteditable'] },
);
});
it('tableCaption uiNode should not in html string', async () => {
const quill = createQuillWithTableModule(`
`);
quill.setContents([
{ insert: '\nTable Caption' },
{ attributes: { 'table-up-caption': { tableId: '1', side: 'top' } }, insert: '\n' },
{ insert: { 'table-up-col': { tableId: '1', colId: '1', full: false, width: 100 } } },
{ insert: { 'table-up-col': { tableId: '1', colId: '2', full: false, width: 100 } } },
{ insert: { 'table-up-col': { tableId: '1', colId: '3', full: false, width: 100 } } },
{ insert: '1' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '1', colId: '1', rowspan: 1, colspan: 1 } }, insert: '\n' },
{ insert: '2' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '1', colId: '2', rowspan: 1, colspan: 1 } }, insert: '\n' },
{ insert: '3' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '1', colId: '3', rowspan: 1, colspan: 1 } }, insert: '\n' },
{ insert: '4' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '2', colId: '1', rowspan: 1, colspan: 1 } }, insert: '\n' },
{ insert: '5' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '2', colId: '2', rowspan: 1, colspan: 1 } }, insert: '\n' },
{ insert: '6' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '2', colId: '3', rowspan: 1, colspan: 1 } }, insert: '\n' },
{ insert: '7' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '3', colId: '1', rowspan: 1, colspan: 1 } }, insert: '\n' },
{ insert: '8' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '3', colId: '2', rowspan: 1, colspan: 1 } }, insert: '\n' },
{ insert: '9' },
{ attributes: { 'table-up-cell-inner': { tableId: '1', rowId: '3', colId: '3', rowspan: 1, colspan: 1 } }, insert: '\n' },
{ insert: '\n' },
]);
await vi.runAllTimersAsync();
const html = quill.getSemanticHTML();
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
expect(doc.body).toEqualHTML(
`
${createTableHTML(3, 3, { full: false }, { text: 'Table Caption' })}
`,
{ ignoreAttrs: ['class', 'contenteditable'] },
);
expect(doc.body.querySelector('caption .ql-ui')).toBeNull();
});
});