import { Assert, UnitTest } from '@ephox/bedrock-client'; import { Arr, Optional } from '@ephox/katamari'; import { Insert, InsertAll, Remove, SelectorFilter, SelectorFind, SugarElement } from '@ephox/sugar'; import * as TablePositions from 'ephox/snooker/api/TablePositions'; UnitTest.test('RectangularTest', () => { const body = SelectorFind.first('body').getOrDie(); const div = SugarElement.fromTag('div'); Insert.append(body, div); const table = SugarElement.fromHtml( '' + '' + '' + '' + '' + '' + // '' + '' + '' + // '' + '' + '' + '' + '' + '' + // '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
A1B1 START SELECTION
C1


D1
A2B2

C2

More


D2
A3B3
C3 END SELECTION
D3
A4B4
C4
D4
' ); const table2 = SugarElement.fromHtml('' + '' + '' + '' + '' + '' + '' + ' ' + '' + '' + '' + '' + '' + '' + '' + ' ' + '' + '' + // ''+ // ''+ // ''+ '' + '' + '' + '' + '' + '' + // ''+ // ''+ '
A0A1 A2 A3 A4 A5
B1B2 B3 B4
C1 C2 C3 C4 C5
D1 D2 D3 D4 D5 D6
'); InsertAll.append(div, [ table, table2 ] ); const check = (tableTarget: SugarElement, from: string, to: string, expected: boolean) => { Arr.each(tableTarget.dom.querySelectorAll('td'), (td) => { td.style.background = ''; }); Optional.from(document.querySelector(from) as HTMLTableCellElement).getOrDie('Missing element for "from" selector').style.background = '#cadbee'; Optional.from(document.querySelector(to) as HTMLTableCellElement).getOrDie('Missing element for "to" selector').style.background = '#5adb33'; const start = SelectorFilter.descendants(tableTarget, from)[0]; const finish = SelectorFilter.descendants(tableTarget, to)[0]; const c = TablePositions.getBox(tableTarget, start, finish); Assert.eq('', expected, c.isSome()); }; check(table, '#tableA td#B1', '#tableA td#C3', false); check(table, '#tableA td#A1', '#tableA td#B3', true); check(table, '#tableA td#C1', '#tableA td#B2', true); check(table, '#tableA td#D2', '#tableA td#A4', false); check(table, '#tableA td#C1', '#tableA td#C4', true); check(table, '#tableA td#C1', '#tableA td#D4', true); check(table, '#tableA td#C1', '#tableA td#B4', true); check(table, '#tableA td#C1', '#tableA td#A4', true); check(table, '#tableA td#D4', '#tableA td#A1', true); Remove.remove(table); check(table2, '#tableB td#TBA1', '#tableB td#TBB1', true); check(table2, '#tableB td#TBA0', '#tableB td#TBD1', true); check(table2, '#tableB td#TBA1', '#tableB td#TBC2', true); check(table2, '#tableB td#TBA4', '#tableB td#TBC2', true); check(table2, '#tableB td#TBC2', '#tableB td#TBA4', true); check(table2, '#tableB td#TBA5', '#tableB td#TBB1', false); check(table2, '#tableB td#TBB4', '#tableB td#TBC1', false); Remove.remove(div); });