8
0

inserttablecommand.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 InsertTableCommand from '../../src/commands/inserttablecommand';
  9. import {
  10. downcastInsertCell,
  11. downcastInsertRow,
  12. downcastInsertTable,
  13. downcastRemoveRow,
  14. downcastTableHeadingColumnsChange,
  15. downcastTableHeadingRowsChange
  16. } from '../../src/converters/downcast';
  17. import upcastTable from '../../src/converters/upcasttable';
  18. import TableUtils from '../../src/tableutils';
  19. import { formatTable, formattedModelTable } from '../_utils/utils';
  20. describe( 'InsertTableCommand', () => {
  21. let editor, model, command;
  22. beforeEach( () => {
  23. return ModelTestEditor.create( {
  24. plugins: [ TableUtils ]
  25. } ).then( newEditor => {
  26. editor = newEditor;
  27. model = editor.model;
  28. command = new InsertTableCommand( editor );
  29. const conversion = editor.conversion;
  30. const schema = model.schema;
  31. schema.register( 'table', {
  32. allowWhere: '$block',
  33. allowAttributes: [ 'headingRows' ],
  34. isObject: true
  35. } );
  36. schema.register( 'tableRow', { allowIn: 'table' } );
  37. schema.register( 'tableCell', {
  38. allowIn: 'tableRow',
  39. allowContentOf: '$block',
  40. allowAttributes: [ 'colspan', 'rowspan' ],
  41. isLimit: true
  42. } );
  43. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  44. // Table conversion.
  45. conversion.for( 'upcast' ).add( upcastTable() );
  46. conversion.for( 'downcast' ).add( downcastInsertTable() );
  47. // Insert row conversion.
  48. conversion.for( 'downcast' ).add( downcastInsertRow() );
  49. // Remove row conversion.
  50. conversion.for( 'downcast' ).add( downcastRemoveRow() );
  51. // Table cell conversion.
  52. conversion.for( 'downcast' ).add( downcastInsertCell() );
  53. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'td' } ) );
  54. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'th' } ) );
  55. // Table attributes conversion.
  56. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  57. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  58. conversion.for( 'downcast' ).add( downcastTableHeadingColumnsChange() );
  59. conversion.for( 'downcast' ).add( downcastTableHeadingRowsChange() );
  60. } );
  61. } );
  62. afterEach( () => {
  63. return editor.destroy();
  64. } );
  65. describe( 'isEnabled', () => {
  66. describe( 'when selection is collapsed', () => {
  67. it( 'should be true if in paragraph', () => {
  68. setData( model, '<p>foo[]</p>' );
  69. expect( command.isEnabled ).to.be.true;
  70. } );
  71. it( 'should be false if in table', () => {
  72. setData( model, '<table><tableRow><tableCell>foo[]</tableCell></tableRow></table>' );
  73. expect( command.isEnabled ).to.be.false;
  74. } );
  75. } );
  76. } );
  77. describe( 'execute()', () => {
  78. describe( 'collapsed selection', () => {
  79. it( 'should insert table in empty root', () => {
  80. setData( model, '[]' );
  81. command.execute();
  82. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  83. [ '[]', '' ],
  84. [ '', '' ]
  85. ] ) );
  86. } );
  87. it( 'should insert table with two rows and two columns after non-empty paragraph', () => {
  88. setData( model, '<p>foo[]</p>' );
  89. command.execute();
  90. expect( formatTable( getData( model ) ) ).to.equal(
  91. '<p>foo</p>' +
  92. formattedModelTable( [
  93. [ '[]', '' ],
  94. [ '', '' ]
  95. ] )
  96. );
  97. } );
  98. it( 'should insert table with given rows and columns after non-empty paragraph', () => {
  99. setData( model, '<p>foo[]</p>' );
  100. command.execute( { rows: 3, columns: 4 } );
  101. expect( formatTable( getData( model ) ) ).to.equal(
  102. '<p>foo</p>' +
  103. formattedModelTable( [
  104. [ '[]', '', '', '' ],
  105. [ '', '', '', '' ],
  106. [ '', '', '', '' ]
  107. ] )
  108. );
  109. } );
  110. } );
  111. } );
  112. } );