inserttablecommand.js 4.5 KB

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