8
0

utils.js 11 KB

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