inserttablecommand.js 4.1 KB

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