inserttablecommand.js 4.0 KB

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