tableediting.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  6. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  8. import { getData as getModelData, setData as setModelData, parse } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  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 ]
  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. } );
  38. it( 'adds insertTable command', () => {
  39. expect( editor.commands.get( 'insertTable' ) ).to.be.instanceOf( InsertTableCommand );
  40. } );
  41. it( 'adds insertRowAbove command', () => {
  42. expect( editor.commands.get( 'insertTableRowAbove' ) ).to.be.instanceOf( InsertRowCommand );
  43. } );
  44. it( 'adds insertRowBelow command', () => {
  45. expect( editor.commands.get( 'insertTableRowBelow' ) ).to.be.instanceOf( InsertRowCommand );
  46. } );
  47. it( 'adds insertColumnBefore command', () => {
  48. expect( editor.commands.get( 'insertTableColumnBefore' ) ).to.be.instanceOf( InsertColumnCommand );
  49. } );
  50. it( 'adds insertColumnAfter command', () => {
  51. expect( editor.commands.get( 'insertTableColumnAfter' ) ).to.be.instanceOf( InsertColumnCommand );
  52. } );
  53. it( 'adds removeRow command', () => {
  54. expect( editor.commands.get( 'removeTableRow' ) ).to.be.instanceOf( RemoveRowCommand );
  55. } );
  56. it( 'adds removeColumn command', () => {
  57. expect( editor.commands.get( 'removeTableColumn' ) ).to.be.instanceOf( RemoveColumnCommand );
  58. } );
  59. it( 'adds splitCellVertically command', () => {
  60. expect( editor.commands.get( 'splitTableCellVertically' ) ).to.be.instanceOf( SplitCellCommand );
  61. } );
  62. it( 'adds splitCellHorizontally command', () => {
  63. expect( editor.commands.get( 'splitTableCellHorizontally' ) ).to.be.instanceOf( SplitCellCommand );
  64. } );
  65. it( 'adds mergeCellRight command', () => {
  66. expect( editor.commands.get( 'mergeTableCellRight' ) ).to.be.instanceOf( MergeCellCommand );
  67. } );
  68. it( 'adds mergeCellLeft command', () => {
  69. expect( editor.commands.get( 'mergeTableCellLeft' ) ).to.be.instanceOf( MergeCellCommand );
  70. } );
  71. it( 'adds mergeCellDown command', () => {
  72. expect( editor.commands.get( 'mergeTableCellDown' ) ).to.be.instanceOf( MergeCellCommand );
  73. } );
  74. it( 'adds mergeCellUp command', () => {
  75. expect( editor.commands.get( 'mergeTableCellUp' ) ).to.be.instanceOf( MergeCellCommand );
  76. } );
  77. it( 'adds setColumnHeader command', () => {
  78. expect( editor.commands.get( 'setTableColumnHeader' ) ).to.be.instanceOf( SetHeaderColumnCommand );
  79. } );
  80. it( 'adds setRowHeader command', () => {
  81. expect( editor.commands.get( 'setTableRowHeader' ) ).to.be.instanceOf( SetHeaderRowCommand );
  82. } );
  83. describe( 'conversion in data pipeline', () => {
  84. describe( 'model to view', () => {
  85. it( 'should create tbody section', () => {
  86. setModelData( model, '<table><tableRow><tableCell>foo[]</tableCell></tableRow></table>' );
  87. expect( editor.getData() ).to.equal(
  88. '<figure class="table">' +
  89. '<table>' +
  90. '<tbody>' +
  91. '<tr><td>foo</td></tr>' +
  92. '</tbody>' +
  93. '</table>' +
  94. '</figure>'
  95. );
  96. } );
  97. it( 'should create thead section', () => {
  98. setModelData( model, '<table headingRows="1"><tableRow><tableCell>foo[]</tableCell></tableRow></table>' );
  99. expect( editor.getData() ).to.equal(
  100. '<figure class="table">' +
  101. '<table>' +
  102. '<thead>' +
  103. '<tr><th>foo</th></tr>' +
  104. '</thead>' +
  105. '</table>' +
  106. '</figure>'
  107. );
  108. } );
  109. } );
  110. describe( 'view to model', () => {
  111. it( 'should convert table', () => {
  112. editor.setData( '<table><tbody><tr><td>foo</td></tr></tbody></table>' );
  113. expect( getModelData( model, { withoutSelection: true } ) )
  114. .to.equal( '<table><tableRow><tableCell>foo</tableCell></tableRow></table>' );
  115. } );
  116. } );
  117. } );
  118. describe( 'caret movement', () => {
  119. let domEvtDataStub;
  120. beforeEach( () => {
  121. domEvtDataStub = {
  122. keyCode: getCode( 'Tab' ),
  123. preventDefault: sinon.spy(),
  124. stopPropagation: sinon.spy()
  125. };
  126. } );
  127. it( 'should do nothing if not tab pressed', () => {
  128. setModelData( model, modelTable( [
  129. [ '11', '12[]' ]
  130. ] ) );
  131. domEvtDataStub.keyCode = getCode( 'a' );
  132. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  133. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  134. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  135. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  136. [ '11', '12[]' ]
  137. ] ) );
  138. } );
  139. it( 'should do nothing if Ctrl+Tab is pressed', () => {
  140. setModelData( model, modelTable( [
  141. [ '11', '12[]' ]
  142. ] ) );
  143. domEvtDataStub.ctrlKey = true;
  144. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  145. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  146. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  147. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  148. [ '11', '12[]' ]
  149. ] ) );
  150. } );
  151. describe( 'on TAB', () => {
  152. it( 'should do nothing if selection is not in a table', () => {
  153. setModelData( model, '[]' + modelTable( [
  154. [ '11', '12' ]
  155. ] ) );
  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 move to next cell', () => {
  164. setModelData( model, modelTable( [
  165. [ '11[]', '12' ]
  166. ] ) );
  167. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  168. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  169. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  170. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  171. [ '11', '[12]' ]
  172. ] ) );
  173. } );
  174. it( 'should create another row and move to first cell in new row', () => {
  175. setModelData( model, modelTable( [
  176. [ '11', '[12]' ]
  177. ] ) );
  178. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  179. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  180. [ '11', '12' ],
  181. [ '[]', '' ]
  182. ] ) );
  183. } );
  184. it( 'should move to the first cell of next row if on end of a row', () => {
  185. setModelData( model, modelTable( [
  186. [ '11', '12[]' ],
  187. [ '21', '22' ]
  188. ] ) );
  189. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  190. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  191. [ '11', '12' ],
  192. [ '[21]', '22' ]
  193. ] ) );
  194. } );
  195. describe( 'on table widget selected', () => {
  196. beforeEach( () => {
  197. editor.model.schema.register( 'block', {
  198. allowWhere: '$block',
  199. allowContentOf: '$block',
  200. isObject: true
  201. } );
  202. editor.conversion.elementToElement( { model: 'block', view: 'block' } );
  203. } );
  204. it( 'should move caret to the first table cell on TAB', () => {
  205. const spy = sinon.spy();
  206. editor.editing.view.document.on( 'keydown', spy );
  207. setModelData( model, '[' + modelTable( [
  208. [ '11', '12' ]
  209. ] ) + ']' );
  210. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  211. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  212. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  213. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  214. [ '[11]', '12' ]
  215. ] ) );
  216. // Should cancel event - so no other tab handler is called.
  217. sinon.assert.notCalled( spy );
  218. } );
  219. it( 'shouldn\'t do anything on other blocks', () => {
  220. const spy = sinon.spy();
  221. editor.editing.view.document.on( 'keydown', spy );
  222. setModelData( model, '[<block>foo</block>]' );
  223. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  224. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  225. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  226. expect( formatTable( getModelData( model ) ) ).to.equal( '[<block>foo</block>]' );
  227. // Should not cancel event.
  228. sinon.assert.calledOnce( spy );
  229. } );
  230. } );
  231. } );
  232. describe( 'on SHIFT+TAB', () => {
  233. beforeEach( () => {
  234. domEvtDataStub.shiftKey = true;
  235. } );
  236. it( 'should do nothing if selection is not in a table', () => {
  237. setModelData( model, '[]' + modelTable( [
  238. [ '11', '12' ]
  239. ] ) );
  240. domEvtDataStub.keyCode = getCode( 'Tab' );
  241. domEvtDataStub.shiftKey = true;
  242. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  243. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  244. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  245. expect( formatTable( getModelData( model ) ) ).to.equal( '[]' + formattedModelTable( [
  246. [ '11', '12' ]
  247. ] ) );
  248. } );
  249. it( 'should move to previous cell', () => {
  250. setModelData( model, modelTable( [
  251. [ '11', '12[]' ]
  252. ] ) );
  253. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  254. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  255. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  256. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  257. [ '[11]', '12' ]
  258. ] ) );
  259. } );
  260. it( 'should not move if caret is in first table cell', () => {
  261. setModelData( model, '<paragraph>foo</paragraph>' + modelTable( [
  262. [ '[]11', '12' ]
  263. ] ) );
  264. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  265. expect( formatTable( getModelData( model ) ) ).to.equal(
  266. '<paragraph>foo</paragraph>' + formattedModelTable( [ [ '[]11', '12' ] ] )
  267. );
  268. } );
  269. it( 'should move to the last cell of previous row if on beginning of a row', () => {
  270. setModelData( model, modelTable( [
  271. [ '11', '12' ],
  272. [ '[]21', '22' ]
  273. ] ) );
  274. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  275. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  276. [ '11', '[12]' ],
  277. [ '21', '22' ]
  278. ] ) );
  279. } );
  280. } );
  281. } );
  282. describe( 'post-fixer', () => {
  283. let root;
  284. beforeEach( () => {
  285. root = model.document.getRoot();
  286. } );
  287. it( 'should add missing columns to a tableRows that are shorter then longest table row', () => {
  288. const parsed = parse( modelTable( [
  289. [ '00' ],
  290. [ '10', '11', '12' ],
  291. [ '20', '21' ]
  292. ] ), model.schema );
  293. model.change( writer => {
  294. writer.remove( Range.createIn( root ) );
  295. writer.insert( parsed, root );
  296. } );
  297. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formattedModelTable( [
  298. [ '00', '', '' ],
  299. [ '10', '11', '12' ],
  300. [ '20', '21', '' ]
  301. ] ) );
  302. } );
  303. it( 'should add missing columns to a tableRows that are shorter then longest table row (complex)', () => {
  304. const parsed = parse( modelTable( [
  305. [ { colspan: 6, contents: '00' } ],
  306. [ { rowspan: 2, contents: '10' }, '11', { colspan: 3, contents: '12' } ],
  307. [ '21', '22' ]
  308. ] ), model.schema );
  309. model.change( writer => {
  310. writer.remove( Range.createIn( root ) );
  311. writer.insert( parsed, root );
  312. } );
  313. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formattedModelTable( [
  314. [ { colspan: 6, contents: '00' } ],
  315. [ { rowspan: 2, contents: '10' }, '11', { colspan: 3, contents: '12' }, '' ],
  316. [ '21', '22', '', '', '' ]
  317. ] ) );
  318. } );
  319. it( 'should fix wrong rowspan attribute on table header', () => {
  320. const parsed = parse( modelTable( [
  321. [ { rowspan: 2, contents: '00' }, { rowspan: 3, contents: '01' }, '02' ],
  322. [ { rowspan: 8, contents: '12' } ],
  323. [ '20', '21', '22' ]
  324. ], { headingRows: 2 } ), model.schema );
  325. model.change( writer => {
  326. writer.remove( Range.createIn( root ) );
  327. writer.insert( parsed, root );
  328. } );
  329. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formattedModelTable( [
  330. [ { rowspan: 2, contents: '00' }, { rowspan: 2, contents: '01' }, '02' ],
  331. [ '12' ],
  332. [ '20', '21', '22' ]
  333. ], { headingRows: 2 } ) );
  334. } );
  335. it( 'should fix wrong rowspan attribute on table body', () => {
  336. const parsed = parse( modelTable( [
  337. [ '00', '01', '02' ],
  338. [ { rowspan: 2, contents: '10' }, { rowspan: 3, contents: '11' }, '12' ],
  339. [ { rowspan: 8, contents: '22' } ]
  340. ], { headingRows: 1 } ), model.schema );
  341. model.change( writer => {
  342. writer.remove( Range.createIn( root ) );
  343. writer.insert( parsed, root );
  344. } );
  345. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formattedModelTable( [
  346. [ '00', '01', '02' ],
  347. [ { rowspan: 2, contents: '10' }, { rowspan: 2, contents: '11' }, '12' ],
  348. [ '22' ]
  349. ], { headingRows: 1 } ) );
  350. } );
  351. // Client A: insert row. Client B: insert column.
  352. it( 'should fix missing insert column before insert row', () => {
  353. setModelData( model, modelTable( [
  354. [ '00[]', '01', '02' ],
  355. [ '10', '11', '12' ],
  356. [ '20', '21', '22' ]
  357. ] ) );
  358. const table = root.getChild( 0 );
  359. // Insert column:
  360. model.change( writer => {
  361. for ( const tableRow of table.getChildren() ) {
  362. const tableCell = writer.createElement( 'tableCell' );
  363. writer.insert( tableCell, tableRow, 1 );
  364. writer.insertText( 'x', tableCell );
  365. }
  366. } );
  367. // Insert table row
  368. model.change( writer => {
  369. const parsedTable = parse(
  370. modelTable( [ [ 'a', 'b', 'c' ] ] ),
  371. model.schema
  372. );
  373. writer.insert( parsedTable.getChild( 0 ), table, 2 );
  374. } );
  375. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formattedModelTable( [
  376. [ '00', 'x', '01', '02' ],
  377. [ '10', 'x', '11', '12' ],
  378. [ 'a', 'b', 'c', '' ],
  379. [ '20', 'x', '21', '22' ]
  380. ] ) );
  381. } );
  382. // Client A: insert row. Client B: insert column.
  383. it( 'should fix (undo of add tableRow & add tableCell)', () => {
  384. setModelData( model, modelTable( [
  385. [ '00', 'x', '01', '02' ],
  386. [ '10', 'x', '11', '12' ],
  387. [ 'a', 'b', 'c', '' ],
  388. [ '20', 'x', '21', '22' ]
  389. ] ) );
  390. const table = root.getChild( 0 );
  391. // Insert column:
  392. model.change( writer => {
  393. [ 0, 1, 3 ].map( index => writer.remove( table.getChild( index ).getChild( 1 ) ) );
  394. } );
  395. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formattedModelTable( [
  396. [ '00', '01', '02' ],
  397. [ '10', '11', '12' ],
  398. [ 'a', 'b', 'c' ],
  399. [ '20', '21', '22' ]
  400. ] ) );
  401. } );
  402. // Client A: insert row. Client B: insert column.
  403. it( 'should fix (undo of add tableRow & add tableCell) fff', () => {
  404. setModelData( model, modelTable( [
  405. [ '00', 'x', '01', '02' ],
  406. [ '10', 'x', '11', '12' ],
  407. [ 'a', { colspan: 3, contents: 'b' } ],
  408. [ '20', 'x', '21', '22' ]
  409. ] ) );
  410. const table = root.getChild( 0 );
  411. // Insert column:
  412. model.change( writer => {
  413. [ 0, 1, 3 ].map( index => writer.remove( table.getChild( index ).getChild( 1 ) ) );
  414. } );
  415. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formattedModelTable( [
  416. [ '00', '01', '02' ],
  417. [ '10', '11', '12' ],
  418. [ 'a', { colspan: 2, contents: 'b' } ],
  419. [ '20', '21', '22' ]
  420. ] ) );
  421. } );
  422. // Client A: insert row. Client B: insert column.
  423. it( 'should not fix (undo of add tableRow & add tableCell) - OK', () => {
  424. setModelData( model, modelTable( [
  425. [ '00', 'x', '01', '02' ],
  426. [ '10', 'x', '11', '12' ],
  427. [ 'a', 'b', 'c', '' ],
  428. [ '20', 'x', '21', '22' ]
  429. ] ) );
  430. const table = root.getChild( 0 );
  431. // Insert column:
  432. model.change( writer => {
  433. [ 0, 1, 3 ].map( index => writer.remove( table.getChild( index ).getChild( 1 ) ) );
  434. // Remove one more cell... (shouldn't occur??)
  435. writer.remove( table.getChild( 0 ).getChild( 1 ) );
  436. } );
  437. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formattedModelTable( [
  438. [ '00', '02' ],
  439. [ '10', '11', '12' ],
  440. [ 'a', 'b', 'c', '' ],
  441. [ '20', '21', '22' ]
  442. ] ) );
  443. } );
  444. } );
  445. } );