8
0

inserttablecommand.js 4.3 KB

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