import { Assert, UnitTest } from '@ephox/bedrock-client'; import { Arr } from '@ephox/katamari'; import { Hierarchy, Html, Insert, Remove, SugarBody, SugarElement } from '@ephox/sugar'; import { copyRows } from 'ephox/snooker/api/CopyRows'; import * as Bridge from 'ephox/snooker/test/Bridge'; UnitTest.test('CopyRowsTest', () => { const check = ( label: string, inputHtml: string, expectedHtml: string, section: number, row: number, column: number ) => { const table = SugarElement.fromHtml(inputHtml); Insert.append(SugarBody.body(), table); const rowsOpt = copyRows(table, { selection: [ Hierarchy.follow(table, [ section, row, column, 0 ]).getOrDie(label + ': could not follow path') ] }, Bridge.generators); const copiedHtml = rowsOpt.map((rows) => Arr.map(rows, Html.getOuter).join('')).getOr(''); Assert.eq(label + ': Copied HTML should match', expectedHtml, copiedHtml); Remove.remove(table); }; const defaultTable = (hasColgroup: boolean = false, lockedColumns: number[] = []) => ` 0 ? `data-snooker-locked-cols="${lockedColumns.join(',')}"` : ''}>` + `${hasColgroup ? '' : ''}` + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
H1H2
A2B2
A3B3
F1F2
'; const colspanTable = (hasColgroup: boolean = false, lockedColumns: number[] = []) => ` 0 ? `data-snooker-locked-cols="${lockedColumns.join(',')}"` : ''}>` + `${hasColgroup ? '' : ''}` + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
H1H2H3
A2B2C2
A3C3
A4
F1F2F3
'; const rowspanTable = (hasColgroup: boolean = false, lockedColumns: number[] = []) => ` 0 ? `data-snooker-locked-cols="${lockedColumns.join(',')}"` : ''}>` + `${hasColgroup ? '' : ''}` + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
H1H2
A2B2
B3
F1F2
'; check( 'Test copying row from basic table (0)', defaultTable(), 'H1H2', 0, 0, 0 ); check( 'Test copying row from basic table (1)', defaultTable(), 'F1F2', 2, 0, 1 ); check( 'Test copying row from colspan table (0)', colspanTable(), 'A3C3', 1, 1, 0 ); check( 'Test copying row from colspan table (1)', colspanTable(), 'A2B2C2', 1, 0, 1 ); check( 'Test copying row from rowspan table where rowspan cell is selected', rowspanTable(), ( 'A2B2' + 'B3' ), 1, 0, 0 ); check( 'Test copying row from rowspan table where rowspan cell is not selected', rowspanTable(), 'A2B3', 1, 1, 0 ); check( 'Test copying row from colgroup table - header', defaultTable(true), 'H1H2', 1, 0, 0 ); check( 'Test copying row from colgroup table - body', defaultTable(true), 'A2B2', 2, 0, 0 ); check( 'Test copying row from colgroup table - footer', defaultTable(true), 'F1F2', 3, 0, 0 ); check( 'Test copying row from colgroup table with locked colum - should not include locked column cells - header', defaultTable(true, [ 0 ]), 'H2', 1, 0, 0 ); check( 'Test copying row from colgroup table with locked colum - should not include locked column cells - body', defaultTable(true, [ 0 ]), 'B2', 2, 0, 0 ); check( 'Test copying row from colgroup table with locked colum - should not include locked column cells - body', defaultTable(true, [ 1 ]), 'A2', 2, 0, 0 ); check( 'Test copying row from colgroup table with locked colum - should not include locked column cells - body', defaultTable(true, [ 0, 1 ]), '', 2, 0, 0 ); check( 'Test copying row from colgroup table with locked colum - should not include locked column cells - footer', defaultTable(true, [ 0 ]), 'F2', 3, 0, 0 ); check( 'Test copying row from colgroup colspan table with locked column - should not include locked column cells (0)', colspanTable(true, [ 0 ]), 'C3', 2, 1, 0 ); check( 'Test copying row from colgroup colspan table with locked column - should not include locked column cells (1)', colspanTable(true, [ 1 ]), 'A3C3', 2, 1, 0 ); check( 'Test copying row from colgroup colspan table with locked column - should not include locked column cells (2)', colspanTable(true, [ 2 ]), 'A3', 2, 1, 0 ); check( 'Test copying row from colgroup colspan table with locked column - should not include locked column cells (3)', colspanTable(true, [ 0, 1, 2 ]), '', 2, 1, 0 ); check( 'Test copying row from colgroup colspan table with locked column - should not include locked column cells (4)', colspanTable(true, [ 0 ]), '', 2, 2, 0 ); check( 'Test copying row from colgroup rowspan table with locked column - should not include locked column cells (0)', rowspanTable(true, [ 0 ]), ( 'B2' + 'B3' ), 2, 0, 0 ); check( 'Test copying row from colgroup rowspan table with locked column - should not include locked column cells (0)', rowspanTable(true, [ 1 ]), 'A2', 2, 1, 0 ); check( 'Test copying row from colgroup rowspan table with locked column - should not include locked column cells (0)', rowspanTable(true, [ 0, 1 ]), '', 2, 1, 0 ); });