utils.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 } from '../src/utils';
  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( 'getSelectedTableCells()', () => {
  31. let selection;
  32. beforeEach( () => {
  33. selection = model.document.selection;
  34. } );
  35. it( 'should return an empty array when a collapsed selection is anchored in a cell', () => {
  36. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  37. model.change( writer => {
  38. writer.setSelection( writer.createRange( writer.createPositionAt( firstCell, 0 ) ) );
  39. } );
  40. expect( getSelectedTableCells( selection ) ).to.deep.equal( [] );
  41. } );
  42. it( 'should return an empty array when a non-collapsed selection is anchored in a cell', () => {
  43. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  44. model.change( writer => {
  45. writer.setSelection( writer.createRangeIn( firstCell ) );
  46. } );
  47. expect( getSelectedTableCells( selection ) ).to.deep.equal( [] );
  48. } );
  49. it( 'should return an empty array when a non-cell node is selected', () => {
  50. const paragraph = modelRoot.getNodeByPath( [ 0, 0, 0, 0 ] );
  51. expect( paragraph.is( 'paragraph' ) ).to.be.true;
  52. model.change( writer => {
  53. writer.setSelection( writer.createRangeOn( paragraph ) );
  54. } );
  55. expect( getSelectedTableCells( selection ) ).to.deep.equal( [] );
  56. } );
  57. it( 'should return an empty array when an entire table is selected', () => {
  58. const table = modelRoot.getNodeByPath( [ 0 ] );
  59. model.change( writer => {
  60. writer.setSelection( writer.createRangeOn( table ) );
  61. } );
  62. expect( getSelectedTableCells( selection ) ).to.deep.equal( [] );
  63. } );
  64. it( 'should return two table cells', () => {
  65. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  66. const lastCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  67. tableSelection._setCellSelection( firstCell, lastCell );
  68. expect( Array.from( getSelectedTableCells( selection ) ) ).to.deep.equal( [
  69. firstCell, lastCell
  70. ] );
  71. } );
  72. it( 'should return four table cells for diagonal selection', () => {
  73. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  74. const lastCell = modelRoot.getNodeByPath( [ 0, 1, 1 ] );
  75. tableSelection._setCellSelection( firstCell, lastCell );
  76. expect( Array.from( getSelectedTableCells( selection ) ) ).to.deep.equal( [
  77. firstCell,
  78. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  79. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  80. lastCell
  81. ] );
  82. } );
  83. it( 'should return row table cells', () => {
  84. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  85. const lastCell = modelRoot.getNodeByPath( [ 0, 0, 2 ] );
  86. tableSelection._setCellSelection( firstCell, lastCell );
  87. expect( Array.from( getSelectedTableCells( selection ) ) ).to.deep.equal( [
  88. firstCell,
  89. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  90. lastCell
  91. ] );
  92. } );
  93. it( 'should return column table cells', () => {
  94. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  95. const lastCell = modelRoot.getNodeByPath( [ 0, 2, 1 ] );
  96. tableSelection._setCellSelection( firstCell, lastCell );
  97. expect( Array.from( getSelectedTableCells( selection ) ) ).to.deep.equal( [
  98. firstCell,
  99. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  100. lastCell
  101. ] );
  102. } );
  103. } );
  104. } );