tableediting.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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 { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  9. import TableEditing from '../src/tableediting';
  10. import { formatTable, formattedModelTable, 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 SplitCellCommand from '../src/commands/splitcellcommand';
  17. import MergeCellCommand from '../src/commands/mergecellcommand';
  18. import SetHeaderRowCommand from '../src/commands/setheaderrowcommand';
  19. import SetHeaderColumnCommand from '../src/commands/setheadercolumncommand';
  20. describe( 'TableEditing', () => {
  21. let editor, model;
  22. beforeEach( () => {
  23. return VirtualTestEditor
  24. .create( {
  25. plugins: [ TableEditing, Paragraph ]
  26. } )
  27. .then( newEditor => {
  28. editor = newEditor;
  29. model = editor.model;
  30. } );
  31. } );
  32. afterEach( () => {
  33. editor.destroy();
  34. } );
  35. it( 'should set proper schema rules', () => {
  36. // Table:
  37. expect( model.schema.isRegistered( 'table' ) ).to.be.true;
  38. expect( model.schema.isObject( 'table' ) ).to.be.true;
  39. expect( model.schema.isLimit( 'table' ) ).to.be.true;
  40. expect( model.schema.checkChild( [ '$root' ], 'table' ) ).to.be.true;
  41. expect( model.schema.checkAttribute( [ '$root', 'table' ], 'headingRows' ) ).to.be.true;
  42. expect( model.schema.checkAttribute( [ '$root', 'table' ], 'headingColumns' ) ).to.be.true;
  43. // Table row:
  44. expect( model.schema.isRegistered( 'tableRow' ) ).to.be.true;
  45. expect( model.schema.isLimit( 'tableRow' ) ).to.be.true;
  46. expect( model.schema.checkChild( [ '$root' ], 'tableRow' ) ).to.be.false;
  47. expect( model.schema.checkChild( [ 'table' ], 'tableRow' ) ).to.be.true;
  48. // Table cell:
  49. expect( model.schema.isRegistered( 'tableCell' ) ).to.be.true;
  50. expect( model.schema.isLimit( 'tableCell' ) ).to.be.true;
  51. expect( model.schema.checkChild( [ '$root' ], 'tableCell' ) ).to.be.false;
  52. expect( model.schema.checkChild( [ 'table' ], 'tableCell' ) ).to.be.false;
  53. expect( model.schema.checkChild( [ 'tableRow' ], 'tableCell' ) ).to.be.true;
  54. expect( model.schema.checkChild( [ 'tableCell' ], 'tableCell' ) ).to.be.false;
  55. expect( model.schema.checkAttribute( [ 'tableCell' ], 'colspan' ) ).to.be.true;
  56. expect( model.schema.checkAttribute( [ 'tableCell' ], 'rowspan' ) ).to.be.true;
  57. // Table cell contents:
  58. expect( model.schema.checkChild( [ '$root', 'table', 'tableRow', 'tableCell' ], '$text' ) ).to.be.true;
  59. expect( model.schema.checkChild( [ '$root', 'table', 'tableRow', 'tableCell' ], '$block' ) ).to.be.true;
  60. expect( model.schema.checkChild( [ '$root', 'table', 'tableRow', 'tableCell' ], 'table' ) ).to.be.false;
  61. } );
  62. it( 'adds insertTable command', () => {
  63. expect( editor.commands.get( 'insertTable' ) ).to.be.instanceOf( InsertTableCommand );
  64. } );
  65. it( 'adds insertRowAbove command', () => {
  66. expect( editor.commands.get( 'insertTableRowAbove' ) ).to.be.instanceOf( InsertRowCommand );
  67. } );
  68. it( 'adds insertRowBelow command', () => {
  69. expect( editor.commands.get( 'insertTableRowBelow' ) ).to.be.instanceOf( InsertRowCommand );
  70. } );
  71. it( 'adds insertColumnBefore command', () => {
  72. expect( editor.commands.get( 'insertTableColumnBefore' ) ).to.be.instanceOf( InsertColumnCommand );
  73. } );
  74. it( 'adds insertColumnAfter command', () => {
  75. expect( editor.commands.get( 'insertTableColumnAfter' ) ).to.be.instanceOf( InsertColumnCommand );
  76. } );
  77. it( 'adds removeRow command', () => {
  78. expect( editor.commands.get( 'removeTableRow' ) ).to.be.instanceOf( RemoveRowCommand );
  79. } );
  80. it( 'adds removeColumn command', () => {
  81. expect( editor.commands.get( 'removeTableColumn' ) ).to.be.instanceOf( RemoveColumnCommand );
  82. } );
  83. it( 'adds splitCellVertically command', () => {
  84. expect( editor.commands.get( 'splitTableCellVertically' ) ).to.be.instanceOf( SplitCellCommand );
  85. } );
  86. it( 'adds splitCellHorizontally command', () => {
  87. expect( editor.commands.get( 'splitTableCellHorizontally' ) ).to.be.instanceOf( SplitCellCommand );
  88. } );
  89. it( 'adds mergeCellRight command', () => {
  90. expect( editor.commands.get( 'mergeTableCellRight' ) ).to.be.instanceOf( MergeCellCommand );
  91. } );
  92. it( 'adds mergeCellLeft command', () => {
  93. expect( editor.commands.get( 'mergeTableCellLeft' ) ).to.be.instanceOf( MergeCellCommand );
  94. } );
  95. it( 'adds mergeCellDown command', () => {
  96. expect( editor.commands.get( 'mergeTableCellDown' ) ).to.be.instanceOf( MergeCellCommand );
  97. } );
  98. it( 'adds mergeCellUp command', () => {
  99. expect( editor.commands.get( 'mergeTableCellUp' ) ).to.be.instanceOf( MergeCellCommand );
  100. } );
  101. it( 'adds setColumnHeader command', () => {
  102. expect( editor.commands.get( 'setTableColumnHeader' ) ).to.be.instanceOf( SetHeaderColumnCommand );
  103. } );
  104. it( 'adds setRowHeader command', () => {
  105. expect( editor.commands.get( 'setTableRowHeader' ) ).to.be.instanceOf( SetHeaderRowCommand );
  106. } );
  107. describe( 'conversion in data pipeline', () => {
  108. describe( 'model to view', () => {
  109. it( 'should create tbody section', () => {
  110. setModelData( model, '<table><tableRow><tableCell>foo[]</tableCell></tableRow></table>' );
  111. expect( editor.getData() ).to.equal(
  112. '<figure class="table">' +
  113. '<table>' +
  114. '<tbody>' +
  115. '<tr><td>foo</td></tr>' +
  116. '</tbody>' +
  117. '</table>' +
  118. '</figure>'
  119. );
  120. } );
  121. it( 'should create thead section', () => {
  122. setModelData( model, '<table headingRows="1"><tableRow><tableCell>foo[]</tableCell></tableRow></table>' );
  123. expect( editor.getData() ).to.equal(
  124. '<figure class="table">' +
  125. '<table>' +
  126. '<thead>' +
  127. '<tr><th>foo</th></tr>' +
  128. '</thead>' +
  129. '</table>' +
  130. '</figure>'
  131. );
  132. } );
  133. } );
  134. describe( 'view to model', () => {
  135. it( 'should convert table', () => {
  136. editor.setData( '<table><tbody><tr><td>foo</td></tr></tbody></table>' );
  137. expect( getModelData( model, { withoutSelection: true } ) )
  138. .to.equal( '<table><tableRow><tableCell>foo</tableCell></tableRow></table>' );
  139. } );
  140. } );
  141. } );
  142. describe( 'caret movement', () => {
  143. let domEvtDataStub;
  144. beforeEach( () => {
  145. domEvtDataStub = {
  146. keyCode: getCode( 'Tab' ),
  147. preventDefault: sinon.spy(),
  148. stopPropagation: sinon.spy()
  149. };
  150. } );
  151. it( 'should do nothing if not tab pressed', () => {
  152. setModelData( model, modelTable( [
  153. [ '11', '12[]' ]
  154. ] ) );
  155. domEvtDataStub.keyCode = getCode( 'a' );
  156. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  157. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  158. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  159. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  160. [ '11', '12[]' ]
  161. ] ) );
  162. } );
  163. it( 'should do nothing if Ctrl+Tab is pressed', () => {
  164. setModelData( model, modelTable( [
  165. [ '11', '12[]' ]
  166. ] ) );
  167. domEvtDataStub.ctrlKey = true;
  168. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  169. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  170. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  171. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  172. [ '11', '12[]' ]
  173. ] ) );
  174. } );
  175. describe( 'on TAB', () => {
  176. it( 'should do nothing if selection is not in a table', () => {
  177. setModelData( model, '[]' + modelTable( [
  178. [ '11', '12' ]
  179. ] ) );
  180. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  181. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  182. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  183. expect( formatTable( getModelData( model ) ) ).to.equal( '[]' + formattedModelTable( [
  184. [ '11', '12' ]
  185. ] ) );
  186. } );
  187. it( 'should move to next cell', () => {
  188. setModelData( model, modelTable( [
  189. [ '11[]', '12' ]
  190. ] ) );
  191. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  192. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  193. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  194. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  195. [ '11', '[<paragraph>12</paragraph>]' ]
  196. ] ) );
  197. } );
  198. it( 'should create another row and move to first cell in new row', () => {
  199. setModelData( model, modelTable( [
  200. [ '11', '[12]' ]
  201. ] ) );
  202. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  203. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  204. [ '11', '12' ],
  205. [ '[<paragraph></paragraph>]', '' ]
  206. ] ) );
  207. } );
  208. it( 'should move to the first cell of next row if on end of a row', () => {
  209. setModelData( model, modelTable( [
  210. [ '11', '12[]' ],
  211. [ '21', '22' ]
  212. ] ) );
  213. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  214. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  215. [ '11', '12' ],
  216. [ '[<paragraph>21</paragraph>]', '22' ]
  217. ] ) );
  218. } );
  219. it( 'should move to the next table cell if part of block content is selected', () => {
  220. setModelData( model, modelTable( [
  221. [ '11', '<paragraph>12</paragraph><paragraph>[foo]</paragraph><paragraph>bar</paragraph>', '13' ],
  222. ] ) );
  223. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  224. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  225. [
  226. '11',
  227. '<paragraph>12</paragraph><paragraph>foo</paragraph><paragraph>bar</paragraph>',
  228. '[<paragraph>13</paragraph>]'
  229. ],
  230. ] ) );
  231. } );
  232. describe( 'on table widget selected', () => {
  233. beforeEach( () => {
  234. editor.model.schema.register( 'block', {
  235. allowWhere: '$block',
  236. allowContentOf: '$block',
  237. isObject: true
  238. } );
  239. editor.conversion.elementToElement( { model: 'block', view: 'block' } );
  240. } );
  241. it( 'should move caret to the first table cell on TAB', () => {
  242. const spy = sinon.spy();
  243. editor.editing.view.document.on( 'keydown', spy );
  244. setModelData( model, '[' + modelTable( [
  245. [ '11', '12' ]
  246. ] ) + ']' );
  247. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  248. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  249. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  250. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  251. [ '[<paragraph>11</paragraph>]', '12' ]
  252. ] ) );
  253. // Should cancel event - so no other tab handler is called.
  254. sinon.assert.notCalled( spy );
  255. } );
  256. it( 'shouldn\'t do anything on other blocks', () => {
  257. const spy = sinon.spy();
  258. editor.editing.view.document.on( 'keydown', spy );
  259. setModelData( model, '[<block>foo</block>]' );
  260. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  261. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  262. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  263. expect( formatTable( getModelData( model ) ) ).to.equal( '[<block>foo</block>]' );
  264. // Should not cancel event.
  265. sinon.assert.calledOnce( spy );
  266. } );
  267. } );
  268. } );
  269. describe( 'on SHIFT+TAB', () => {
  270. beforeEach( () => {
  271. domEvtDataStub.shiftKey = true;
  272. } );
  273. it( 'should do nothing if selection is not in a table', () => {
  274. setModelData( model, '[]' + modelTable( [
  275. [ '11', '12' ]
  276. ] ) );
  277. domEvtDataStub.keyCode = getCode( 'Tab' );
  278. domEvtDataStub.shiftKey = true;
  279. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  280. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  281. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  282. expect( formatTable( getModelData( model ) ) ).to.equal( '[]' + formattedModelTable( [
  283. [ '11', '12' ]
  284. ] ) );
  285. } );
  286. it( 'should move to previous cell', () => {
  287. setModelData( model, modelTable( [
  288. [ '11', '12[]' ]
  289. ] ) );
  290. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  291. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  292. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  293. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  294. [ '[<paragraph>11</paragraph>]', '12' ]
  295. ] ) );
  296. } );
  297. it( 'should not move if caret is in first table cell', () => {
  298. setModelData( model, '<paragraph>foo</paragraph>' + modelTable( [
  299. [ '[]11', '12' ]
  300. ] ) );
  301. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  302. expect( formatTable( getModelData( model ) ) ).to.equal(
  303. '<paragraph>foo</paragraph>' + formattedModelTable( [ [ '[]11', '12' ] ] )
  304. );
  305. } );
  306. it( 'should move to the last cell of previous row if on beginning of a row', () => {
  307. setModelData( model, modelTable( [
  308. [ '11', '12' ],
  309. [ '[]21', '22' ]
  310. ] ) );
  311. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  312. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  313. [ '11', '[<paragraph>12</paragraph>]' ],
  314. [ '21', '22' ]
  315. ] ) );
  316. } );
  317. it( 'should move to the previous table cell if part of block content is selected', () => {
  318. setModelData( model, modelTable( [
  319. [ '11', '<paragraph>12</paragraph><paragraph>[foo]</paragraph><paragraph>bar</paragraph>', '13' ],
  320. ] ) );
  321. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  322. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  323. [
  324. '[<paragraph>11</paragraph>]',
  325. '<paragraph>12</paragraph><paragraph>foo</paragraph><paragraph>bar</paragraph>',
  326. '13'
  327. ],
  328. ] ) );
  329. } );
  330. } );
  331. } );
  332. describe( 'enter key', () => {
  333. let evtDataStub, viewDocument;
  334. beforeEach( () => {
  335. evtDataStub = {
  336. preventDefault: sinon.spy(),
  337. stopPropagation: sinon.spy(),
  338. isSoft: false
  339. };
  340. return VirtualTestEditor
  341. .create( {
  342. plugins: [ TableEditing, Paragraph ]
  343. } )
  344. .then( newEditor => {
  345. editor = newEditor;
  346. sinon.stub( editor, 'execute' );
  347. viewDocument = editor.editing.view.document;
  348. model = editor.model;
  349. } );
  350. } );
  351. it( 'should do nothing if not in table cell', () => {
  352. setModelData( model, '<paragraph>[]foo</paragraph>' );
  353. viewDocument.fire( 'enter', evtDataStub );
  354. sinon.assert.notCalled( editor.execute );
  355. expect( formatTable( getModelData( model ) ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  356. } );
  357. it( 'should do nothing if table cell has already a block content', () => {
  358. setModelData( model, modelTable( [
  359. [ '<paragraph>[]11</paragraph>' ]
  360. ] ) );
  361. viewDocument.fire( 'enter', evtDataStub );
  362. sinon.assert.notCalled( editor.execute );
  363. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  364. [ '<paragraph>[]11</paragraph>' ]
  365. ] ) );
  366. } );
  367. it( 'should do nothing if table cell with a block content is selected as a whole', () => {
  368. setModelData( model, modelTable( [
  369. [ '<paragraph>[1</paragraph><paragraph>1]</paragraph>' ]
  370. ] ) );
  371. viewDocument.fire( 'enter', evtDataStub );
  372. sinon.assert.notCalled( editor.execute );
  373. setModelData( model, modelTable( [
  374. [ '<paragraph>[1</paragraph><paragraph>1]</paragraph>' ]
  375. ] ) );
  376. } );
  377. it( 'should allow default behavior of Shift+Enter pressed', () => {
  378. setModelData( model, modelTable( [
  379. [ '[]11' ]
  380. ] ) );
  381. evtDataStub.isSoft = true;
  382. viewDocument.fire( 'enter', evtDataStub );
  383. sinon.assert.notCalled( editor.execute );
  384. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  385. [ '[]11' ]
  386. ] ) );
  387. } );
  388. } );
  389. } );