tableediting.js 17 KB

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