common.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/utils/common';
  10. describe( 'table 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( 'common', () => {
  30. describe( 'findAncestor()', () => {
  31. it( 'should return undefined if not in table', () => {
  32. setData( model, '<paragraph>foo[]</paragraph>' );
  33. expect( findAncestor( 'table', model.document.selection.focus ) ).to.be.undefined;
  34. } );
  35. it( 'should return table if position is in tableCell', () => {
  36. setData( model, modelTable( [ [ '[]' ] ] ) );
  37. const parentTable = findAncestor( 'table', model.document.selection.focus );
  38. expect( parentTable ).to.not.be.undefined;
  39. expect( parentTable.is( 'table' ) ).to.be.true;
  40. } );
  41. } );
  42. describe( 'isHeadingColumnCell()', () => {
  43. it( 'should return "true" for a heading column cell', () => {
  44. setData( model, modelTable( [
  45. [ '00', '01', '02', '03' ]
  46. ], { headingColumns: 2 } ) );
  47. const tableCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  48. expect( isHeadingColumnCell( tableUtils, tableCell ) ).to.be.true;
  49. } );
  50. it( 'should return "true" for a heading column cell with colspan', () => {
  51. setData( model, modelTable( [
  52. [ { colspan: 2, contents: '00' }, '01', '02', '03' ]
  53. ], { headingColumns: 2 } ) );
  54. const tableCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  55. expect( isHeadingColumnCell( tableUtils, tableCell ) ).to.be.true;
  56. } );
  57. it( 'should return "false" for a regular column cell', () => {
  58. setData( model, modelTable( [
  59. [ '00', '01', '02', '03' ]
  60. ], { headingColumns: 2 } ) );
  61. const tableCell = modelRoot.getNodeByPath( [ 0, 0, 2 ] );
  62. expect( isHeadingColumnCell( tableUtils, tableCell ) ).to.be.false;
  63. } );
  64. it( 'should return "false" for a regular column cell with colspan', () => {
  65. setData( model, modelTable( [
  66. [ '00', { colspan: 2, contents: '01' }, '02', '03' ]
  67. ], { headingColumns: 1 } ) );
  68. const tableCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  69. expect( isHeadingColumnCell( tableUtils, tableCell ) ).to.be.false;
  70. } );
  71. } );
  72. } );
  73. } );