8
0

structure.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import TableEditing from '../../src/tableediting';
  8. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. import { modelTable } from '../_utils/utils';
  10. import { getHorizontallyOverlappingCells, getVerticallyOverlappingCells } from '../../src/utils/structure';
  11. describe( 'table utils', () => {
  12. let editor, model, modelRoot;
  13. beforeEach( async () => {
  14. editor = await VirtualTestEditor.create( {
  15. plugins: [ TableEditing, Paragraph ]
  16. } );
  17. model = editor.model;
  18. modelRoot = model.document.getRoot();
  19. } );
  20. afterEach( async () => {
  21. await editor.destroy();
  22. } );
  23. describe( 'structure', () => {
  24. describe( 'getVerticallyOverlappingCells()', () => {
  25. let table;
  26. beforeEach( () => {
  27. // +----+----+----+----+----+
  28. // | 00 | 01 | 02 | 03 | 04 |
  29. // + + +----+ +----+
  30. // | | | 12 | | 14 |
  31. // + + + +----+----+
  32. // | | | | 23 | 24 |
  33. // + +----+ + +----+
  34. // | | 31 | | | 34 |
  35. // + + +----+----+----+
  36. // | | | 42 | 43 | 44 |
  37. // +----+----+----+----+----+
  38. setModelData( model, modelTable( [
  39. [ { contents: '00', rowspan: 5 }, { contents: '01', rowspan: 3 }, '02', { contents: '03', rowspan: 2 }, '04' ],
  40. [ { contents: '12', rowspan: 3 }, '14' ],
  41. [ { contents: '23', rowspan: 2 }, '24' ],
  42. [ { contents: '31', rowspan: 2 }, '34' ],
  43. [ '42', '43', '44' ]
  44. ] ) );
  45. table = modelRoot.getChild( 0 );
  46. } );
  47. it( 'should return empty array for no overlapping cells', () => {
  48. const cellsInfo = getVerticallyOverlappingCells( table, 0 );
  49. expect( cellsInfo ).to.be.empty;
  50. } );
  51. it( 'should return overlapping cells info for given overlapRow', () => {
  52. const cellsInfo = getVerticallyOverlappingCells( table, 2 );
  53. expect( cellsInfo[ 0 ].cell ).to.equal( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) ); // Cell 00
  54. expect( cellsInfo[ 1 ].cell ).to.equal( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) ); // Cell 01
  55. expect( cellsInfo[ 2 ].cell ).to.equal( modelRoot.getNodeByPath( [ 0, 1, 0 ] ) ); // Cell 12
  56. } );
  57. it( 'should ignore rows below startRow', () => {
  58. const cellsInfo = getVerticallyOverlappingCells( table, 2, 1 );
  59. expect( cellsInfo[ 0 ].cell ).to.equal( modelRoot.getNodeByPath( [ 0, 1, 0 ] ) ); // Cell 12
  60. } );
  61. } );
  62. describe( 'getHorizontallyOverlappingCells()', () => {
  63. let table;
  64. beforeEach( () => {
  65. // +----+----+----+----+----+
  66. // | 00 |
  67. // +----+----+----+----+----+
  68. // | 10 | 13 |
  69. // +----+----+----+----+----+
  70. // | 20 | 21 | 24 |
  71. // +----+----+----+----+----+
  72. // | 30 | 32 | 34 |
  73. // +----+----+----+----+----+
  74. // | 40 | 41 | 42 | 43 | 44 |
  75. // +----+----+----+----+----+
  76. setModelData( model, modelTable( [
  77. [ { contents: '00', colspan: 5 } ],
  78. [ { contents: '10', colspan: 3 }, { contents: '13', colspan: 2 } ],
  79. [ '20', { contents: '21', colspan: 3 }, '24' ],
  80. [ { contents: '30', colspan: 2 }, { contents: '32', colspan: 2 }, '34' ],
  81. [ '40', '41', '42', '43', '44' ]
  82. ] ) );
  83. table = modelRoot.getChild( 0 );
  84. } );
  85. it( 'should return empty array for no overlapping cells', () => {
  86. const cellsInfo = getHorizontallyOverlappingCells( table, 0 );
  87. expect( cellsInfo ).to.be.empty;
  88. } );
  89. it( 'should return overlapping cells info for given overlapColumn', () => {
  90. const cellsInfo = getHorizontallyOverlappingCells( table, 2 );
  91. expect( cellsInfo[ 0 ].cell ).to.equal( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) ); // Cell 00
  92. expect( cellsInfo[ 1 ].cell ).to.equal( modelRoot.getNodeByPath( [ 0, 1, 0 ] ) ); // Cell 10
  93. expect( cellsInfo[ 2 ].cell ).to.equal( modelRoot.getNodeByPath( [ 0, 2, 1 ] ) ); // Cell 21
  94. } );
  95. } );
  96. } );
  97. } );