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 Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import TableEditing from '../../src/tableediting';
  9. import { modelTable } from '../_utils/utils';
  10. import { findAncestor, isHeadingColumnCell } from '../../src/utils/common';
  11. describe( 'table utils', () => {
  12. let editor, model, modelRoot, tableUtils;
  13. beforeEach( () => {
  14. return ModelTestEditor
  15. .create( {
  16. plugins: [ Paragraph, TableEditing ]
  17. } )
  18. .then( newEditor => {
  19. editor = newEditor;
  20. model = editor.model;
  21. modelRoot = model.document.getRoot();
  22. tableUtils = editor.plugins.get( 'TableUtils' );
  23. } );
  24. } );
  25. afterEach( () => {
  26. return editor.destroy();
  27. } );
  28. describe( 'common', () => {
  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. } );
  72. } );