8
0

utils.js 2.9 KB

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