import { Assertions } from '@ephox/agar'; import { context, describe, it } from '@ephox/bedrock-client'; import { Hierarchy, Html, SugarElement } from '@ephox/sugar'; import { assert } from 'chai'; import * as SimpleTableModel from 'tinymce/core/selection/SimpleTableModel'; describe('browser.tinymce.core.selection.SimpleTableModel', () => { const fromDom = (html: string) => SimpleTableModel.fromDom(SugarElement.fromHtml(html)); const fromDomSubSection = (html: string, startPath: number[], endPath: number[]) => { const tableElm = SugarElement.fromHtml(html); const startElm = Hierarchy.follow(tableElm, startPath).getOrDie(); const endElm = Hierarchy.follow(tableElm, endPath).getOrDie(); return SimpleTableModel.subsection(SimpleTableModel.fromDom(tableElm), startElm, endElm).getOrDie('Failed to get the subsection'); }; const assertWidth = (tableModel: SimpleTableModel.TableModel, expectedWidth: number) => { assert.equal(tableModel.width, expectedWidth, 'Should be expected width'); }; const assertHeight = (tableModel: SimpleTableModel.TableModel, expectedHeight: number) => { assert.equal(tableModel.rows.length, expectedHeight, 'Should be expected height'); }; const assertModelAsHtml = (tableModel: SimpleTableModel.TableModel, expectedHtml: string) => { const actualHtml = Html.getOuter(SimpleTableModel.toDom(tableModel)); Assertions.assertHtml('Should be expected table html', expectedHtml, actualHtml); }; context('fromDom/toDom', () => { it('Table 1x1', () => { const model = fromDom('
A
'); assertWidth(model, 1); assertHeight(model, 1); assertModelAsHtml(model, '
A
'); }); it('Table 1x1 with classes', () => { const model = fromDom('
A
'); assertWidth(model, 1); assertHeight(model, 1); assertModelAsHtml(model, '
A
'); }); it('Table 2x1', () => { const model = fromDom('
AB
'); assertWidth(model, 2); assertHeight(model, 1); assertModelAsHtml(model, '
AB
'); }); it('Table 2x2', () => { const model = fromDom('
AB
CD
'); assertWidth(model, 2); assertHeight(model, 2); assertModelAsHtml(model, '
AB
CD
'); }); it('Table 2x2 with colspan', () => { const model = fromDom('
A
CD
'); assertWidth(model, 2); assertHeight(model, 2); assertModelAsHtml(model, '
A
CD
'); }); it('Table 2x2 with rowspan', () => { const model = fromDom('
AB
D
'); assertWidth(model, 2); assertHeight(model, 2); assertModelAsHtml(model, '
AB
D
'); }); it('Table 3x3 with colspan & rowspan', () => { const model = fromDom('
AB
C
DEF
'); assertWidth(model, 3); assertHeight(model, 3); assertModelAsHtml(model, '
AB
C
DEF
'); }); }); context('subsection', () => { it('Table 1x1 subsection (1,1)-(1,1)', () => { const model = fromDomSubSection('
A
', [ 0, 0, 0 ], [ 0, 0, 0 ]); assertWidth(model, 1); assertHeight(model, 1); assertModelAsHtml(model, '
A
'); }); it('Table 2x2 subsection (1,1)-(2,1)', () => { const model = fromDomSubSection('
AB
CD
', [ 0, 0, 0 ], [ 0, 0, 1 ]); assertWidth(model, 2); assertHeight(model, 1); assertModelAsHtml(model, '
AB
'); }); it('Table 2x2 subsection (2,1)-(1,1)', () => { const model = fromDomSubSection('
AB
CD
', [ 0, 0, 1 ], [ 0, 0, 0 ]); assertWidth(model, 2); assertHeight(model, 1); assertModelAsHtml(model, '
AB
'); }); it('Table 2x2 subsection (1,1)-(1,2)', () => { const model = fromDomSubSection('
AB
CD
', [ 0, 0, 0 ], [ 0, 1, 0 ]); assertWidth(model, 1); assertHeight(model, 2); assertModelAsHtml(model, '
A
C
'); }); it('Table 2x2 subsection (1,2)-(1,1)', () => { const model = fromDomSubSection('
AB
CD
', [ 0, 1, 0 ], [ 0, 0, 0 ]); assertWidth(model, 1); assertHeight(model, 2); assertModelAsHtml(model, '
A
C
'); }); it('Table 3x3 subsection (2,2)-(3,3)', () => { const model = fromDomSubSection('
ABC
DEF
GHI
', [ 0, 1, 1 ], [ 0, 2, 2 ]); assertWidth(model, 2); assertHeight(model, 2); assertModelAsHtml(model, '
EF
HI
'); }); it('Table 3x3 subsection (3,3)-(2,2)', () => { const model = fromDomSubSection('
ABC
DEF
GHI
', [ 0, 2, 2 ], [ 0, 1, 1 ]); assertWidth(model, 2); assertHeight(model, 2); assertModelAsHtml(model, '
EF
HI
'); }); }); });