tableediting.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import ImageEditing from '@ckeditor/ckeditor5-image/src/image/imageediting';
  9. import TableEditing from '../src/tableediting';
  10. import { modelTable } from './_utils/utils';
  11. import InsertRowCommand from '../src/commands/insertrowcommand';
  12. import InsertTableCommand from '../src/commands/inserttablecommand';
  13. import InsertColumnCommand from '../src/commands/insertcolumncommand';
  14. import RemoveRowCommand from '../src/commands/removerowcommand';
  15. import RemoveColumnCommand from '../src/commands/removecolumncommand';
  16. import SelectRowCommand from '../src/commands/selectrowcommand';
  17. import SelectColumnCommand from '../src/commands/selectcolumncommand';
  18. import SplitCellCommand from '../src/commands/splitcellcommand';
  19. import MergeCellCommand from '../src/commands/mergecellcommand';
  20. import SetHeaderRowCommand from '../src/commands/setheaderrowcommand';
  21. import SetHeaderColumnCommand from '../src/commands/setheadercolumncommand';
  22. import MediaEmbedEditing from '@ckeditor/ckeditor5-media-embed/src/mediaembedediting';
  23. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  24. describe( 'TableEditing', () => {
  25. let editor, model;
  26. beforeEach( () => {
  27. return VirtualTestEditor
  28. .create( {
  29. plugins: [ TableEditing, Paragraph, ImageEditing, MediaEmbedEditing ]
  30. } )
  31. .then( newEditor => {
  32. editor = newEditor;
  33. model = editor.model;
  34. } );
  35. } );
  36. afterEach( () => {
  37. editor.destroy();
  38. } );
  39. it( 'should have pluginName', () => {
  40. expect( TableEditing.pluginName ).to.equal( 'TableEditing' );
  41. } );
  42. it( 'should set proper schema rules', () => {
  43. // Table:
  44. expect( model.schema.isRegistered( 'table' ) ).to.be.true;
  45. expect( model.schema.isObject( 'table' ) ).to.be.true;
  46. expect( model.schema.isBlock( 'table' ) ).to.be.true;
  47. expect( model.schema.checkChild( [ '$root' ], 'table' ) ).to.be.true;
  48. expect( model.schema.checkAttribute( [ '$root', 'table' ], 'headingRows' ) ).to.be.true;
  49. expect( model.schema.checkAttribute( [ '$root', 'table' ], 'headingColumns' ) ).to.be.true;
  50. // Table row:
  51. expect( model.schema.isRegistered( 'tableRow' ) ).to.be.true;
  52. expect( model.schema.isLimit( 'tableRow' ) ).to.be.true;
  53. expect( model.schema.checkChild( [ '$root' ], 'tableRow' ) ).to.be.false;
  54. expect( model.schema.checkChild( [ 'table' ], 'tableRow' ) ).to.be.true;
  55. // Table cell:
  56. expect( model.schema.isRegistered( 'tableCell' ) ).to.be.true;
  57. expect( model.schema.isLimit( 'tableCell' ) ).to.be.true;
  58. expect( model.schema.isObject( 'tableCell' ) ).to.be.false;
  59. expect( model.schema.isSelectable( 'tableCell' ) ).to.be.true;
  60. expect( model.schema.checkChild( [ '$root' ], 'tableCell' ) ).to.be.false;
  61. expect( model.schema.checkChild( [ 'table' ], 'tableCell' ) ).to.be.false;
  62. expect( model.schema.checkChild( [ 'tableRow' ], 'tableCell' ) ).to.be.true;
  63. expect( model.schema.checkChild( [ 'tableCell' ], 'tableCell' ) ).to.be.false;
  64. expect( model.schema.checkAttribute( [ 'tableCell' ], 'colspan' ) ).to.be.true;
  65. expect( model.schema.checkAttribute( [ 'tableCell' ], 'rowspan' ) ).to.be.true;
  66. // Table cell contents:
  67. expect( model.schema.checkChild( [ '$root', 'table', 'tableRow', 'tableCell' ], '$text' ) ).to.be.false;
  68. expect( model.schema.checkChild( [ '$root', 'table', 'tableRow', 'tableCell' ], '$block' ) ).to.be.true;
  69. expect( model.schema.checkChild( [ '$root', 'table', 'tableRow', 'tableCell' ], 'table' ) ).to.be.false;
  70. expect( model.schema.checkChild( [ '$root', 'table', 'tableRow', 'tableCell' ], 'image' ) ).to.be.true;
  71. } );
  72. it( 'adds insertTable command', () => {
  73. expect( editor.commands.get( 'insertTable' ) ).to.be.instanceOf( InsertTableCommand );
  74. } );
  75. it( 'adds insertRowAbove command', () => {
  76. expect( editor.commands.get( 'insertTableRowAbove' ) ).to.be.instanceOf( InsertRowCommand );
  77. } );
  78. it( 'adds insertRowBelow command', () => {
  79. expect( editor.commands.get( 'insertTableRowBelow' ) ).to.be.instanceOf( InsertRowCommand );
  80. } );
  81. it( 'adds insertColumnLeft command', () => {
  82. expect( editor.commands.get( 'insertTableColumnLeft' ) ).to.be.instanceOf( InsertColumnCommand );
  83. } );
  84. it( 'adds insertColumnRight command', () => {
  85. expect( editor.commands.get( 'insertTableColumnRight' ) ).to.be.instanceOf( InsertColumnCommand );
  86. } );
  87. it( 'adds removeRow command', () => {
  88. expect( editor.commands.get( 'removeTableRow' ) ).to.be.instanceOf( RemoveRowCommand );
  89. } );
  90. it( 'adds removeColumn command', () => {
  91. expect( editor.commands.get( 'removeTableColumn' ) ).to.be.instanceOf( RemoveColumnCommand );
  92. } );
  93. it( 'adds selectRow command', () => {
  94. expect( editor.commands.get( 'selectTableRow' ) ).to.be.instanceOf( SelectRowCommand );
  95. } );
  96. it( 'adds selectColumn command', () => {
  97. expect( editor.commands.get( 'selectTableColumn' ) ).to.be.instanceOf( SelectColumnCommand );
  98. } );
  99. it( 'adds splitCellVertically command', () => {
  100. expect( editor.commands.get( 'splitTableCellVertically' ) ).to.be.instanceOf( SplitCellCommand );
  101. } );
  102. it( 'adds splitCellHorizontally command', () => {
  103. expect( editor.commands.get( 'splitTableCellHorizontally' ) ).to.be.instanceOf( SplitCellCommand );
  104. } );
  105. it( 'adds mergeCellRight command', () => {
  106. expect( editor.commands.get( 'mergeTableCellRight' ) ).to.be.instanceOf( MergeCellCommand );
  107. } );
  108. it( 'adds mergeCellLeft command', () => {
  109. expect( editor.commands.get( 'mergeTableCellLeft' ) ).to.be.instanceOf( MergeCellCommand );
  110. } );
  111. it( 'adds mergeCellDown command', () => {
  112. expect( editor.commands.get( 'mergeTableCellDown' ) ).to.be.instanceOf( MergeCellCommand );
  113. } );
  114. it( 'adds mergeCellUp command', () => {
  115. expect( editor.commands.get( 'mergeTableCellUp' ) ).to.be.instanceOf( MergeCellCommand );
  116. } );
  117. it( 'adds setColumnHeader command', () => {
  118. expect( editor.commands.get( 'setTableColumnHeader' ) ).to.be.instanceOf( SetHeaderColumnCommand );
  119. } );
  120. it( 'adds setRowHeader command', () => {
  121. expect( editor.commands.get( 'setTableRowHeader' ) ).to.be.instanceOf( SetHeaderRowCommand );
  122. } );
  123. describe( 'conversion in data pipeline', () => {
  124. describe( 'model to view', () => {
  125. it( 'should create tbody section', () => {
  126. setModelData( model, '<table><tableRow><tableCell><paragraph>foo[]</paragraph></tableCell></tableRow></table>' );
  127. expect( editor.getData() ).to.equal(
  128. '<figure class="table">' +
  129. '<table>' +
  130. '<tbody>' +
  131. '<tr><td>foo</td></tr>' +
  132. '</tbody>' +
  133. '</table>' +
  134. '</figure>'
  135. );
  136. } );
  137. it( 'should create thead section', () => {
  138. setModelData(
  139. model,
  140. '<table headingRows="1"><tableRow><tableCell><paragraph>foo[]</paragraph></tableCell></tableRow></table>'
  141. );
  142. expect( editor.getData() ).to.equal(
  143. '<figure class="table">' +
  144. '<table>' +
  145. '<thead>' +
  146. '<tr><th>foo</th></tr>' +
  147. '</thead>' +
  148. '</table>' +
  149. '</figure>'
  150. );
  151. } );
  152. } );
  153. describe( 'view to model', () => {
  154. it( 'should convert table', () => {
  155. editor.setData( '<table><tbody><tr><td>foo</td></tr></tbody></table>' );
  156. expect( getModelData( model, { withoutSelection: true } ) )
  157. .to.equal( '<table><tableRow><tableCell><paragraph>foo</paragraph></tableCell></tableRow></table>' );
  158. } );
  159. it( 'should convert table with image', () => {
  160. editor.setData( '<table><tbody><tr><td><img src="sample.png"></td></tr></tbody></table>' );
  161. expect( getModelData( model, { withoutSelection: true } ) )
  162. .to.equal( '<table><tableRow><tableCell><image src="sample.png"></image></tableCell></tableRow></table>' );
  163. } );
  164. it( 'should insert a paragraph when the cell content is unsupported', () => {
  165. editor.setData(
  166. '<table><tbody><tr><td><foo></foo></td></tr></tbody></table>'
  167. );
  168. expect( getModelData( model, { withoutSelection: true } ) )
  169. .to.equal( '<table><tableRow><tableCell><paragraph></paragraph></tableCell></tableRow></table>' );
  170. } );
  171. it( 'should convert a table with media', () => {
  172. editor.setData(
  173. '<table><tbody><tr><td><oembed url="https://www.youtube.com/watch?v=H08tGjXNHO4"></oembed></td></tr></tbody></table>'
  174. );
  175. expect( getModelData( model, { withoutSelection: true } ) )
  176. .to.equal( '<table><tableRow><tableCell>' +
  177. '<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>' +
  178. '</tableCell></tableRow></table>' );
  179. } );
  180. } );
  181. } );
  182. describe( 'enter key', () => {
  183. let evtDataStub, viewDocument;
  184. beforeEach( () => {
  185. evtDataStub = {
  186. preventDefault: sinon.spy(),
  187. stopPropagation: sinon.spy(),
  188. isSoft: false
  189. };
  190. return VirtualTestEditor
  191. .create( {
  192. plugins: [ TableEditing, Paragraph ]
  193. } )
  194. .then( newEditor => {
  195. editor = newEditor;
  196. sinon.stub( editor, 'execute' );
  197. viewDocument = editor.editing.view.document;
  198. model = editor.model;
  199. } );
  200. } );
  201. it( 'should do nothing if not in table cell', () => {
  202. setModelData( model, '<paragraph>[]foo</paragraph>' );
  203. viewDocument.fire( 'enter', evtDataStub );
  204. sinon.assert.notCalled( editor.execute );
  205. assertEqualMarkup( getModelData( model ), '<paragraph>[]foo</paragraph>' );
  206. } );
  207. it( 'should do nothing if table cell has already a block content', () => {
  208. setModelData( model, modelTable( [
  209. [ '<paragraph>[]11</paragraph>' ]
  210. ] ) );
  211. viewDocument.fire( 'enter', evtDataStub );
  212. sinon.assert.notCalled( editor.execute );
  213. assertEqualMarkup( getModelData( model ), modelTable( [
  214. [ '<paragraph>[]11</paragraph>' ]
  215. ] ) );
  216. } );
  217. it( 'should do nothing if table cell with a block content is selected as a whole', () => {
  218. setModelData( model, modelTable( [
  219. [ '<paragraph>[1</paragraph><paragraph>1]</paragraph>' ]
  220. ] ) );
  221. viewDocument.fire( 'enter', evtDataStub );
  222. sinon.assert.notCalled( editor.execute );
  223. setModelData( model, modelTable( [
  224. [ '<paragraph>[1</paragraph><paragraph>1]</paragraph>' ]
  225. ] ) );
  226. } );
  227. it( 'should allow default behavior of Shift+Enter pressed', () => {
  228. setModelData( model, modelTable( [
  229. [ '[]11' ]
  230. ] ) );
  231. evtDataStub.isSoft = true;
  232. viewDocument.fire( 'enter', evtDataStub );
  233. sinon.assert.notCalled( editor.execute );
  234. assertEqualMarkup( getModelData( model ), modelTable( [
  235. [ '[]11' ]
  236. ] ) );
  237. } );
  238. } );
  239. } );