8
0

tableediting.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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.isLimit( 'table' ) ).to.be.true;
  48. expect( model.schema.checkChild( [ '$root' ], 'table' ) ).to.be.true;
  49. expect( model.schema.checkAttribute( [ '$root', 'table' ], 'headingRows' ) ).to.be.true;
  50. expect( model.schema.checkAttribute( [ '$root', 'table' ], 'headingColumns' ) ).to.be.true;
  51. // Table row:
  52. expect( model.schema.isRegistered( 'tableRow' ) ).to.be.true;
  53. expect( model.schema.isLimit( 'tableRow' ) ).to.be.true;
  54. expect( model.schema.checkChild( [ '$root' ], 'tableRow' ) ).to.be.false;
  55. expect( model.schema.checkChild( [ 'table' ], 'tableRow' ) ).to.be.true;
  56. // Table cell:
  57. expect( model.schema.isRegistered( 'tableCell' ) ).to.be.true;
  58. expect( model.schema.isLimit( 'tableCell' ) ).to.be.true;
  59. expect( model.schema.checkChild( [ '$root' ], 'tableCell' ) ).to.be.false;
  60. expect( model.schema.checkChild( [ 'table' ], 'tableCell' ) ).to.be.false;
  61. expect( model.schema.checkChild( [ 'tableRow' ], 'tableCell' ) ).to.be.true;
  62. expect( model.schema.checkChild( [ 'tableCell' ], 'tableCell' ) ).to.be.false;
  63. expect( model.schema.checkAttribute( [ 'tableCell' ], 'colspan' ) ).to.be.true;
  64. expect( model.schema.checkAttribute( [ 'tableCell' ], 'rowspan' ) ).to.be.true;
  65. // Table cell contents:
  66. expect( model.schema.checkChild( [ '$root', 'table', 'tableRow', 'tableCell' ], '$text' ) ).to.be.false;
  67. expect( model.schema.checkChild( [ '$root', 'table', 'tableRow', 'tableCell' ], '$block' ) ).to.be.true;
  68. expect( model.schema.checkChild( [ '$root', 'table', 'tableRow', 'tableCell' ], 'table' ) ).to.be.false;
  69. expect( model.schema.checkChild( [ '$root', 'table', 'tableRow', 'tableCell' ], 'image' ) ).to.be.true;
  70. } );
  71. it( 'adds insertTable command', () => {
  72. expect( editor.commands.get( 'insertTable' ) ).to.be.instanceOf( InsertTableCommand );
  73. } );
  74. it( 'adds insertRowAbove command', () => {
  75. expect( editor.commands.get( 'insertTableRowAbove' ) ).to.be.instanceOf( InsertRowCommand );
  76. } );
  77. it( 'adds insertRowBelow command', () => {
  78. expect( editor.commands.get( 'insertTableRowBelow' ) ).to.be.instanceOf( InsertRowCommand );
  79. } );
  80. it( 'adds insertColumnLeft command', () => {
  81. expect( editor.commands.get( 'insertTableColumnLeft' ) ).to.be.instanceOf( InsertColumnCommand );
  82. } );
  83. it( 'adds insertColumnRight command', () => {
  84. expect( editor.commands.get( 'insertTableColumnRight' ) ).to.be.instanceOf( InsertColumnCommand );
  85. } );
  86. it( 'adds removeRow command', () => {
  87. expect( editor.commands.get( 'removeTableRow' ) ).to.be.instanceOf( RemoveRowCommand );
  88. } );
  89. it( 'adds removeColumn command', () => {
  90. expect( editor.commands.get( 'removeTableColumn' ) ).to.be.instanceOf( RemoveColumnCommand );
  91. } );
  92. it( 'adds selectRow command', () => {
  93. expect( editor.commands.get( 'selectTableRow' ) ).to.be.instanceOf( SelectRowCommand );
  94. } );
  95. it( 'adds selectColumn command', () => {
  96. expect( editor.commands.get( 'selectTableColumn' ) ).to.be.instanceOf( SelectColumnCommand );
  97. } );
  98. it( 'adds splitCellVertically command', () => {
  99. expect( editor.commands.get( 'splitTableCellVertically' ) ).to.be.instanceOf( SplitCellCommand );
  100. } );
  101. it( 'adds splitCellHorizontally command', () => {
  102. expect( editor.commands.get( 'splitTableCellHorizontally' ) ).to.be.instanceOf( SplitCellCommand );
  103. } );
  104. it( 'adds mergeCellRight command', () => {
  105. expect( editor.commands.get( 'mergeTableCellRight' ) ).to.be.instanceOf( MergeCellCommand );
  106. } );
  107. it( 'adds mergeCellLeft command', () => {
  108. expect( editor.commands.get( 'mergeTableCellLeft' ) ).to.be.instanceOf( MergeCellCommand );
  109. } );
  110. it( 'adds mergeCellDown command', () => {
  111. expect( editor.commands.get( 'mergeTableCellDown' ) ).to.be.instanceOf( MergeCellCommand );
  112. } );
  113. it( 'adds mergeCellUp command', () => {
  114. expect( editor.commands.get( 'mergeTableCellUp' ) ).to.be.instanceOf( MergeCellCommand );
  115. } );
  116. it( 'adds setColumnHeader command', () => {
  117. expect( editor.commands.get( 'setTableColumnHeader' ) ).to.be.instanceOf( SetHeaderColumnCommand );
  118. } );
  119. it( 'adds setRowHeader command', () => {
  120. expect( editor.commands.get( 'setTableRowHeader' ) ).to.be.instanceOf( SetHeaderRowCommand );
  121. } );
  122. describe( 'conversion in data pipeline', () => {
  123. describe( 'model to view', () => {
  124. it( 'should create tbody section', () => {
  125. setModelData( model, '<table><tableRow><tableCell><paragraph>foo[]</paragraph></tableCell></tableRow></table>' );
  126. expect( editor.getData() ).to.equal(
  127. '<figure class="table">' +
  128. '<table>' +
  129. '<tbody>' +
  130. '<tr><td>foo</td></tr>' +
  131. '</tbody>' +
  132. '</table>' +
  133. '</figure>'
  134. );
  135. } );
  136. it( 'should create thead section', () => {
  137. setModelData(
  138. model,
  139. '<table headingRows="1"><tableRow><tableCell><paragraph>foo[]</paragraph></tableCell></tableRow></table>'
  140. );
  141. expect( editor.getData() ).to.equal(
  142. '<figure class="table">' +
  143. '<table>' +
  144. '<thead>' +
  145. '<tr><th>foo</th></tr>' +
  146. '</thead>' +
  147. '</table>' +
  148. '</figure>'
  149. );
  150. } );
  151. } );
  152. describe( 'view to model', () => {
  153. it( 'should convert table', () => {
  154. editor.setData( '<table><tbody><tr><td>foo</td></tr></tbody></table>' );
  155. expect( getModelData( model, { withoutSelection: true } ) )
  156. .to.equal( '<table><tableRow><tableCell><paragraph>foo</paragraph></tableCell></tableRow></table>' );
  157. } );
  158. it( 'should convert table with image', () => {
  159. editor.setData( '<table><tbody><tr><td><img src="sample.png"></td></tr></tbody></table>' );
  160. expect( getModelData( model, { withoutSelection: true } ) )
  161. .to.equal( '<table><tableRow><tableCell><image src="sample.png"></image></tableCell></tableRow></table>' );
  162. } );
  163. it( 'should insert a paragraph when the cell content is unsupported', () => {
  164. editor.setData(
  165. '<table><tbody><tr><td><foo></foo></td></tr></tbody></table>'
  166. );
  167. expect( getModelData( model, { withoutSelection: true } ) )
  168. .to.equal( '<table><tableRow><tableCell><paragraph></paragraph></tableCell></tableRow></table>' );
  169. } );
  170. it( 'should convert a table with media', () => {
  171. editor.setData(
  172. '<table><tbody><tr><td><oembed url="https://www.youtube.com/watch?v=H08tGjXNHO4"></oembed></td></tr></tbody></table>'
  173. );
  174. expect( getModelData( model, { withoutSelection: true } ) )
  175. .to.equal( '<table><tableRow><tableCell>' +
  176. '<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>' +
  177. '</tableCell></tableRow></table>' );
  178. } );
  179. } );
  180. } );
  181. describe( 'enter key', () => {
  182. let evtDataStub, viewDocument;
  183. beforeEach( () => {
  184. evtDataStub = {
  185. preventDefault: sinon.spy(),
  186. stopPropagation: sinon.spy(),
  187. isSoft: false
  188. };
  189. return VirtualTestEditor
  190. .create( {
  191. plugins: [ TableEditing, Paragraph ]
  192. } )
  193. .then( newEditor => {
  194. editor = newEditor;
  195. sinon.stub( editor, 'execute' );
  196. viewDocument = editor.editing.view.document;
  197. model = editor.model;
  198. } );
  199. } );
  200. it( 'should do nothing if not in table cell', () => {
  201. setModelData( model, '<paragraph>[]foo</paragraph>' );
  202. viewDocument.fire( 'enter', evtDataStub );
  203. sinon.assert.notCalled( editor.execute );
  204. assertEqualMarkup( getModelData( model ), '<paragraph>[]foo</paragraph>' );
  205. } );
  206. it( 'should do nothing if table cell has already a block content', () => {
  207. setModelData( model, modelTable( [
  208. [ '<paragraph>[]11</paragraph>' ]
  209. ] ) );
  210. viewDocument.fire( 'enter', evtDataStub );
  211. sinon.assert.notCalled( editor.execute );
  212. assertEqualMarkup( getModelData( model ), modelTable( [
  213. [ '<paragraph>[]11</paragraph>' ]
  214. ] ) );
  215. } );
  216. it( 'should do nothing if table cell with a block content is selected as a whole', () => {
  217. setModelData( model, modelTable( [
  218. [ '<paragraph>[1</paragraph><paragraph>1]</paragraph>' ]
  219. ] ) );
  220. viewDocument.fire( 'enter', evtDataStub );
  221. sinon.assert.notCalled( editor.execute );
  222. setModelData( model, modelTable( [
  223. [ '<paragraph>[1</paragraph><paragraph>1]</paragraph>' ]
  224. ] ) );
  225. } );
  226. it( 'should allow default behavior of Shift+Enter pressed', () => {
  227. setModelData( model, modelTable( [
  228. [ '[]11' ]
  229. ] ) );
  230. evtDataStub.isSoft = true;
  231. viewDocument.fire( 'enter', evtDataStub );
  232. sinon.assert.notCalled( editor.execute );
  233. assertEqualMarkup( getModelData( model ), modelTable( [
  234. [ '[]11' ]
  235. ] ) );
  236. } );
  237. } );
  238. } );