insertcolumncommand.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
  8. import InsertColumnCommand from '../src/insertcolumncommand';
  9. import downcastTable from '../src/converters/downcasttable';
  10. import upcastTable from '../src/converters/upcasttable';
  11. import { formatModelTable, formattedModelTable, modelTable } from './_utils/utils';
  12. describe( 'InsertColumnCommand', () => {
  13. let editor, model, command;
  14. beforeEach( () => {
  15. return ModelTestEditor.create()
  16. .then( newEditor => {
  17. editor = newEditor;
  18. model = editor.model;
  19. command = new InsertColumnCommand( editor );
  20. const conversion = editor.conversion;
  21. const schema = model.schema;
  22. schema.register( 'table', {
  23. allowWhere: '$block',
  24. allowAttributes: [ 'headingRows' ],
  25. isBlock: true,
  26. isObject: true
  27. } );
  28. schema.register( 'tableRow', {
  29. allowIn: 'table',
  30. allowAttributes: [],
  31. isBlock: true,
  32. isLimit: true
  33. } );
  34. schema.register( 'tableCell', {
  35. allowIn: 'tableRow',
  36. allowContentOf: '$block',
  37. allowAttributes: [ 'colspan', 'rowspan' ],
  38. isBlock: true,
  39. isLimit: true
  40. } );
  41. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  42. // Table conversion.
  43. conversion.for( 'upcast' ).add( upcastTable() );
  44. conversion.for( 'downcast' ).add( downcastTable() );
  45. // Table row upcast only since downcast conversion is done in `downcastTable()`.
  46. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableRow', view: 'tr' } ) );
  47. // Table cell conversion.
  48. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'td' } ) );
  49. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'th' } ) );
  50. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  51. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  52. } );
  53. } );
  54. afterEach( () => {
  55. return editor.destroy();
  56. } );
  57. describe( 'isEnabled', () => {
  58. describe( 'when selection is collapsed', () => {
  59. it( 'should be false if wrong node', () => {
  60. setData( model, '<p>foo[]</p>' );
  61. expect( command.isEnabled ).to.be.false;
  62. } );
  63. it( 'should be true if in table', () => {
  64. setData( model, modelTable( 1, [ '[]' ] ) );
  65. expect( command.isEnabled ).to.be.true;
  66. } );
  67. } );
  68. } );
  69. describe( 'execute()', () => {
  70. it( 'should insert column in given table at given index', () => {
  71. setData( model, modelTable( 2, [
  72. '11[]', '12',
  73. '21', '22'
  74. ] ) );
  75. command.execute( { at: 1 } );
  76. expect( formatModelTable( getData( model ) ) ).to.equal( formattedModelTable( 3, [
  77. '11[]', '', '12',
  78. '21', '', '22'
  79. ] ) );
  80. } );
  81. it( 'should insert column in given table at default index', () => {
  82. setData( model, modelTable( 2, [
  83. '11[]', '12',
  84. '21', '22'
  85. ] ) );
  86. command.execute();
  87. expect( formatModelTable( getData( model ) ) ).to.equal( formattedModelTable( 3, [
  88. '', '11[]', '12',
  89. '', '21', '22'
  90. ] ) );
  91. } );
  92. it( 'should update table heading columns attribute when inserting column in headings section', () => {
  93. setData( model, modelTable( 2, [
  94. '11[]', '12',
  95. '21', '22',
  96. '31', '32'
  97. ], { headingColumns: 2 } ) );
  98. command.execute( { at: 1 } );
  99. expect( formatModelTable( getData( model ) ) ).to.equal( formattedModelTable( 3, [
  100. '11[]', '', '12',
  101. '21', '', '22',
  102. '31', '', '32'
  103. ], { headingColumns: 3 } ) );
  104. } );
  105. it( 'should not update table heading columns attribute when inserting column after headings section', () => {
  106. setData( model, modelTable( 2, [
  107. '11[]', '12',
  108. '21', '22',
  109. '31', '32'
  110. ], { headingColumns: 2 } ) );
  111. command.execute( { at: 2 } );
  112. expect( formatModelTable( getData( model ) ) ).to.equal( formattedModelTable( 3, [
  113. '11[]', '12', '',
  114. '21', '22', '',
  115. '31', '32', ''
  116. ], { headingColumns: 2 } ) );
  117. } );
  118. } );
  119. } );