8
0

utils.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import TableUtils from '../../src/tableutils';
  7. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import { defaultConversion, defaultSchema, modelTable } from '../_utils/utils';
  9. import { findAncestor, isHeadingColumnCell } from '../../src/commands/utils';
  10. describe( 'commands utils', () => {
  11. let editor, model, modelRoot, tableUtils;
  12. beforeEach( () => {
  13. return ModelTestEditor
  14. .create( {
  15. plugins: [ TableUtils ]
  16. } )
  17. .then( newEditor => {
  18. editor = newEditor;
  19. model = editor.model;
  20. modelRoot = model.document.getRoot();
  21. tableUtils = editor.plugins.get( TableUtils );
  22. defaultSchema( model.schema );
  23. defaultConversion( editor.conversion );
  24. } );
  25. } );
  26. afterEach( () => {
  27. return editor.destroy();
  28. } );
  29. describe( 'findAncestor()', () => {
  30. it( 'should return undefined if not in table', () => {
  31. setData( model, '<paragraph>foo[]</paragraph>' );
  32. expect( findAncestor( 'table', model.document.selection.focus ) ).to.be.undefined;
  33. } );
  34. it( 'should return table if position is in tableCell', () => {
  35. setData( model, modelTable( [ [ '[]' ] ] ) );
  36. const parentTable = findAncestor( 'table', model.document.selection.focus );
  37. expect( parentTable ).to.not.be.undefined;
  38. expect( parentTable.is( 'table' ) ).to.be.true;
  39. } );
  40. } );
  41. describe( 'isHeadingColumnCell()', () => {
  42. it( 'should return "true" for a heading column cell', () => {
  43. setData( model, modelTable( [
  44. [ '00', '01', '02', '03' ]
  45. ], { headingColumns: 2 } ) );
  46. const tableCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  47. expect( isHeadingColumnCell( tableUtils, tableCell ) ).to.be.true;
  48. } );
  49. it( 'should return "true" for a heading column cell with colspan', () => {
  50. setData( model, modelTable( [
  51. [ { colspan: 2, contents: '00' }, '01', '02', '03' ]
  52. ], { headingColumns: 2 } ) );
  53. const tableCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  54. expect( isHeadingColumnCell( tableUtils, tableCell ) ).to.be.true;
  55. } );
  56. it( 'should return "false" for a regular column cell', () => {
  57. setData( model, modelTable( [
  58. [ '00', '01', '02', '03' ]
  59. ], { headingColumns: 2 } ) );
  60. const tableCell = modelRoot.getNodeByPath( [ 0, 0, 2 ] );
  61. expect( isHeadingColumnCell( tableUtils, tableCell ) ).to.be.false;
  62. } );
  63. it( 'should return "false" for a regular column cell with colspan', () => {
  64. setData( model, modelTable( [
  65. [ '00', { colspan: 2, contents: '01' }, '02', '03' ]
  66. ], { headingColumns: 1 } ) );
  67. const tableCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  68. expect( isHeadingColumnCell( tableUtils, tableCell ) ).to.be.false;
  69. } );
  70. } );
  71. } );