inserttablecommand.js 4.4 KB

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