selection.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. describe( 'table utils', () => {
  13. let editor, model, tableSelection, modelRoot;
  14. beforeEach( async () => {
  15. editor = await VirtualTestEditor.create( {
  16. plugins: [ TableEditing, TableSelection, Paragraph ]
  17. } );
  18. model = editor.model;
  19. modelRoot = model.document.getRoot();
  20. tableSelection = editor.plugins.get( TableSelection );
  21. setModelData( model, modelTable( [
  22. [ '11[]', '12', '13' ],
  23. [ '21', '22', '23' ],
  24. [ '31', '32', '33' ]
  25. ] ) );
  26. } );
  27. afterEach( async () => {
  28. await editor.destroy();
  29. } );
  30. describe( 'selection', () => {
  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. } );
  275. } );