8
0

inserttablecommand.js 4.1 KB

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