utils.js 14 KB

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