8
0

utils.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 TableSelection from '../src/tableselection';
  8. import TableEditing from '../src/tableediting';
  9. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. import { modelTable } from './_utils/utils';
  11. import { getSelectedTableCells, getSelectionAffectedTableCells, getTableCellsContainingSelection } from '../src/utils/selection';
  12. import { getHorizontallyOverlappingCells, getVerticallyOverlappingCells } from '../src/utils/structure';
  13. describe( 'table utils', () => {
  14. let editor, model, tableSelection, modelRoot;
  15. beforeEach( async () => {
  16. editor = await VirtualTestEditor.create( {
  17. plugins: [ TableEditing, TableSelection, Paragraph ]
  18. } );
  19. model = editor.model;
  20. modelRoot = model.document.getRoot();
  21. tableSelection = editor.plugins.get( TableSelection );
  22. setModelData( model, modelTable( [
  23. [ '11[]', '12', '13' ],
  24. [ '21', '22', '23' ],
  25. [ '31', '32', '33' ]
  26. ] ) );
  27. } );
  28. afterEach( async () => {
  29. await editor.destroy();
  30. } );
  31. describe( 'getSelectedTableCells()', () => {
  32. let selection;
  33. beforeEach( () => {
  34. selection = model.document.selection;
  35. } );
  36. it( 'should return an empty array when a collapsed selection is anchored in a cell', () => {
  37. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  38. model.change( writer => {
  39. writer.setSelection( writer.createRange( writer.createPositionAt( firstCell, 0 ) ) );
  40. } );
  41. expect( getSelectedTableCells( selection ) ).to.be.empty;
  42. } );
  43. it( 'should return an empty array when a non-collapsed selection is anchored in a cell', () => {
  44. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  45. model.change( writer => {
  46. writer.setSelection( writer.createRangeIn( firstCell ) );
  47. } );
  48. expect( getSelectedTableCells( selection ) ).to.be.empty;
  49. } );
  50. it( 'should return an empty array when a non-cell node is selected', () => {
  51. const paragraph = modelRoot.getNodeByPath( [ 0, 0, 0, 0 ] );
  52. expect( paragraph.is( 'paragraph' ) ).to.be.true;
  53. model.change( writer => {
  54. writer.setSelection( writer.createRangeOn( paragraph ) );
  55. } );
  56. expect( getSelectedTableCells( selection ) ).to.be.empty;
  57. } );
  58. it( 'should return an empty array when an entire table is selected', () => {
  59. const table = modelRoot.getNodeByPath( [ 0 ] );
  60. model.change( writer => {
  61. writer.setSelection( writer.createRangeOn( table ) );
  62. } );
  63. expect( getSelectedTableCells( selection ) ).to.be.empty;
  64. } );
  65. it( 'should return two table cells', () => {
  66. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  67. const lastCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  68. tableSelection.setCellSelection( firstCell, lastCell );
  69. expect( getSelectedTableCells( selection ) ).to.have.ordered.members( [
  70. firstCell, lastCell
  71. ] );
  72. } );
  73. it( 'should return four table cells for diagonal selection', () => {
  74. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  75. const lastCell = modelRoot.getNodeByPath( [ 0, 1, 1 ] );
  76. tableSelection.setCellSelection( firstCell, lastCell );
  77. expect( getSelectedTableCells( selection ) ).to.have.ordered.members( [
  78. firstCell,
  79. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  80. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  81. lastCell
  82. ] );
  83. } );
  84. it( 'should return row table cells', () => {
  85. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  86. const lastCell = modelRoot.getNodeByPath( [ 0, 0, 2 ] );
  87. tableSelection.setCellSelection( firstCell, lastCell );
  88. expect( getSelectedTableCells( selection ) ).to.have.ordered.members( [
  89. firstCell,
  90. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  91. lastCell
  92. ] );
  93. } );
  94. it( 'should return column table cells', () => {
  95. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  96. const lastCell = modelRoot.getNodeByPath( [ 0, 2, 1 ] );
  97. tableSelection.setCellSelection( firstCell, lastCell );
  98. expect( getSelectedTableCells( selection ) ).to.have.ordered.members( [
  99. firstCell,
  100. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  101. lastCell
  102. ] );
  103. } );
  104. it( 'should return cells in source order despite backward selection and forward ranges', () => {
  105. const leftCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  106. const rightCell = modelRoot.getNodeByPath( [ 0, 0, 2 ] );
  107. editor.model.change( writer => {
  108. writer.setSelection(
  109. [ writer.createRangeOn( leftCell ), writer.createRangeOn( rightCell ) ],
  110. { backward: true }
  111. );
  112. } );
  113. expect( Array.from( tableSelection.getSelectedTableCells() ) ).to.deep.equal( [
  114. leftCell, rightCell
  115. ] );
  116. } );
  117. it( 'should return cells in source order despite backward selection and backward ranges', () => {
  118. const leftCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  119. const rightCell = modelRoot.getNodeByPath( [ 0, 0, 2 ] );
  120. editor.model.change( writer => {
  121. writer.setSelection(
  122. [ writer.createRangeOn( rightCell ), writer.createRangeOn( leftCell ) ],
  123. { backward: true }
  124. );
  125. } );
  126. expect( Array.from( tableSelection.getSelectedTableCells() ) ).to.deep.equal( [
  127. leftCell, rightCell
  128. ] );
  129. } );
  130. // Backward direction does not have to equal ranges in the reversed order.
  131. it( 'should return cells in source order despite forward selection and backward ranges', () => {
  132. const leftCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  133. const rightCell = modelRoot.getNodeByPath( [ 0, 0, 2 ] );
  134. editor.model.change( writer => {
  135. writer.setSelection( [ writer.createRangeOn( rightCell ), writer.createRangeOn( leftCell ) ] );
  136. } );
  137. expect( Array.from( tableSelection.getSelectedTableCells() ) ).to.deep.equal( [
  138. leftCell, rightCell
  139. ] );
  140. } );
  141. it( 'should return cells in source order despite selection with mixed range order', () => {
  142. const leftCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  143. const midCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  144. const rightCell = modelRoot.getNodeByPath( [ 0, 0, 2 ] );
  145. editor.model.change( writer => {
  146. writer.setSelection( [
  147. writer.createRangeOn( rightCell ),
  148. writer.createRangeOn( leftCell ),
  149. writer.createRangeOn( midCell )
  150. ] );
  151. } );
  152. expect( Array.from( tableSelection.getSelectedTableCells() ) ).to.deep.equal( [
  153. leftCell, midCell, rightCell
  154. ] );
  155. } );
  156. } );
  157. describe( 'getTableCellsContainingSelection()', () => {
  158. let selection;
  159. beforeEach( () => {
  160. selection = model.document.selection;
  161. } );
  162. it( 'should return an array with a cell when a selection is anchored in it', () => {
  163. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  164. model.change( writer => {
  165. writer.setSelection( writer.createRange( writer.createPositionAt( firstCell, 0 ) ) );
  166. } );
  167. expect( getTableCellsContainingSelection( selection ) ).to.have.ordered.members( [ firstCell ] );
  168. } );
  169. it( 'should return an array with a cell when a selection range is anchored in its descendant', () => {
  170. const cell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  171. const paragraph = modelRoot.getNodeByPath( [ 0, 0, 0, 0 ] );
  172. model.change( writer => {
  173. writer.setSelection( writer.createRange(
  174. writer.createPositionAt( paragraph, 0 ),
  175. writer.createPositionAt( paragraph, 1 ),
  176. ) );
  177. } );
  178. expect( getTableCellsContainingSelection( selection ) ).to.have.ordered.members( [
  179. cell
  180. ] );
  181. } );
  182. it( 'should return an array with cells when multiple collapsed selection ranges are anchored in them', () => {
  183. const cellA = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  184. const cellB = modelRoot.getNodeByPath( [ 0, 1, 0 ] );
  185. model.change( writer => {
  186. writer.setSelection( [
  187. writer.createRange( writer.createPositionAt( cellA, 0 ) ),
  188. writer.createRange( writer.createPositionAt( cellB, 0 ) )
  189. ] );
  190. } );
  191. expect( getTableCellsContainingSelection( selection ) ).to.have.ordered.members( [
  192. cellA,
  193. cellB
  194. ] );
  195. } );
  196. it( 'should return an array with cells when multiple non–collapsed selection ranges are anchored in them', () => {
  197. const cellA = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  198. const cellB = modelRoot.getNodeByPath( [ 0, 1, 0 ] );
  199. model.change( writer => {
  200. writer.setSelection( [
  201. writer.createRangeIn( cellA ),
  202. writer.createRangeIn( cellB )
  203. ] );
  204. } );
  205. expect( getTableCellsContainingSelection( selection ) ).to.have.ordered.members( [
  206. cellA,
  207. cellB
  208. ] );
  209. } );
  210. it( 'should return an empty array when an entire cell is selected', () => {
  211. const cell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  212. model.change( writer => {
  213. writer.setSelection( writer.createRangeOn( cell ) );
  214. } );
  215. expect( getTableCellsContainingSelection( selection ) ).to.be.empty;
  216. } );
  217. it( 'should return an empty array when an entire table is selected', () => {
  218. const table = modelRoot.getNodeByPath( [ 0 ] );
  219. model.change( writer => {
  220. writer.setSelection( writer.createRangeOn( table ) );
  221. } );
  222. expect( getTableCellsContainingSelection( selection ) ).to.be.empty;
  223. } );
  224. it( 'should return an empty array when unrelated elements host selection ranges', () => {
  225. setModelData( model, '<paragraph>foo</paragraph>' );
  226. const paragraph = modelRoot.getNodeByPath( [ 0 ] );
  227. model.change( writer => {
  228. writer.setSelection( writer.createRange( writer.createPositionAt( paragraph, 1 ) ) );
  229. } );
  230. expect( getTableCellsContainingSelection( selection ) ).to.be.empty;
  231. } );
  232. } );
  233. describe( 'getSelectionAffectedTableCells()', () => {
  234. let selection;
  235. beforeEach( () => {
  236. selection = model.document.selection;
  237. } );
  238. it( 'should return completely selected cells (if there are any)', () => {
  239. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  240. const lastCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  241. tableSelection.setCellSelection( firstCell, lastCell );
  242. expect( Array.from( getSelectionAffectedTableCells( selection ) ) ).to.have.ordered.members( [
  243. firstCell, lastCell
  244. ] );
  245. } );
  246. it( 'should return cells when selection ranges are starting in them', () => {
  247. const cellA = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  248. const cellB = modelRoot.getNodeByPath( [ 0, 1, 0 ] );
  249. model.change( writer => {
  250. writer.setSelection( [
  251. writer.createRange( writer.createPositionAt( cellA, 0 ) ),
  252. writer.createRange( writer.createPositionAt( cellB, 0 ) )
  253. ] );
  254. } );
  255. expect( getSelectionAffectedTableCells( selection ) ).to.have.ordered.members( [
  256. cellA,
  257. cellB
  258. ] );
  259. } );
  260. it( 'should return an empty array if no cells are selected and no selection ranges start in any cell', () => {
  261. const table = modelRoot.getNodeByPath( [ 0 ] );
  262. model.change( writer => {
  263. writer.setSelection( writer.createRangeOn( table ) );
  264. } );
  265. expect( getSelectionAffectedTableCells( selection ) ).to.be.empty;
  266. setModelData( model, '<paragraph>foo</paragraph>' );
  267. const paragraph = modelRoot.getNodeByPath( [ 0 ] );
  268. model.change( writer => {
  269. writer.setSelection( writer.createRange( writer.createPositionAt( paragraph, 1 ) ) );
  270. } );
  271. expect( getSelectionAffectedTableCells( selection ) ).to.be.empty;
  272. } );
  273. } );
  274. describe( 'getVerticallyOverlappingCells()', () => {
  275. let table;
  276. beforeEach( () => {
  277. // +----+----+----+----+----+
  278. // | 00 | 01 | 02 | 03 | 04 |
  279. // + + +----+ +----+
  280. // | | | 12 | | 14 |
  281. // + + + +----+----+
  282. // | | | | 23 | 24 |
  283. // + +----+ + +----+
  284. // | | 31 | | | 34 |
  285. // + + +----+----+----+
  286. // | | | 42 | 43 | 44 |
  287. // +----+----+----+----+----+
  288. setModelData( model, modelTable( [
  289. [ { contents: '00', rowspan: 5 }, { contents: '01', rowspan: 3 }, '02', { contents: '03', rowspan: 2 }, '04' ],
  290. [ { contents: '12', rowspan: 3 }, '14' ],
  291. [ { contents: '23', rowspan: 2 }, '24' ],
  292. [ { contents: '31', rowspan: 2 }, '34' ],
  293. [ '42', '43', '44' ]
  294. ] ) );
  295. table = modelRoot.getChild( 0 );
  296. } );
  297. it( 'should return empty array for no overlapping cells', () => {
  298. const cellsInfo = getVerticallyOverlappingCells( table, 0 );
  299. expect( cellsInfo ).to.be.empty;
  300. } );
  301. it( 'should return overlapping cells info for given overlapRow', () => {
  302. const cellsInfo = getVerticallyOverlappingCells( table, 2 );
  303. expect( cellsInfo[ 0 ].cell ).to.equal( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) ); // Cell 00
  304. expect( cellsInfo[ 1 ].cell ).to.equal( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) ); // Cell 01
  305. expect( cellsInfo[ 2 ].cell ).to.equal( modelRoot.getNodeByPath( [ 0, 1, 0 ] ) ); // Cell 12
  306. } );
  307. it( 'should ignore rows below startRow', () => {
  308. const cellsInfo = getVerticallyOverlappingCells( table, 2, 1 );
  309. expect( cellsInfo[ 0 ].cell ).to.equal( modelRoot.getNodeByPath( [ 0, 1, 0 ] ) ); // Cell 12
  310. } );
  311. } );
  312. describe( 'getHorizontallyOverlappingCells()', () => {
  313. let table;
  314. beforeEach( () => {
  315. // +----+----+----+----+----+
  316. // | 00 |
  317. // +----+----+----+----+----+
  318. // | 10 | 13 |
  319. // +----+----+----+----+----+
  320. // | 20 | 21 | 24 |
  321. // +----+----+----+----+----+
  322. // | 30 | 32 | 34 |
  323. // +----+----+----+----+----+
  324. // | 40 | 41 | 42 | 43 | 44 |
  325. // +----+----+----+----+----+
  326. setModelData( model, modelTable( [
  327. [ { contents: '00', colspan: 5 } ],
  328. [ { contents: '10', colspan: 3 }, { contents: '13', colspan: 2 } ],
  329. [ '20', { contents: '21', colspan: 3 }, '24' ],
  330. [ { contents: '30', colspan: 2 }, { contents: '32', colspan: 2 }, '34' ],
  331. [ '40', '41', '42', '43', '44' ]
  332. ] ) );
  333. table = modelRoot.getChild( 0 );
  334. } );
  335. it( 'should return empty array for no overlapping cells', () => {
  336. const cellsInfo = getHorizontallyOverlappingCells( table, 0 );
  337. expect( cellsInfo ).to.be.empty;
  338. } );
  339. it( 'should return overlapping cells info for given overlapColumn', () => {
  340. const cellsInfo = getHorizontallyOverlappingCells( table, 2 );
  341. expect( cellsInfo[ 0 ].cell ).to.equal( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) ); // Cell 00
  342. expect( cellsInfo[ 1 ].cell ).to.equal( modelRoot.getNodeByPath( [ 0, 1, 0 ] ) ); // Cell 10
  343. expect( cellsInfo[ 2 ].cell ).to.equal( modelRoot.getNodeByPath( [ 0, 2, 1 ] ) ); // Cell 21
  344. } );
  345. } );
  346. } );