tableediting.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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, parse, setData as setModelData } 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. import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
  22. describe( 'TableEditing', () => {
  23. let editor, model;
  24. beforeEach( () => {
  25. return VirtualTestEditor
  26. .create( {
  27. plugins: [ TableEditing, Paragraph, UndoEditing ]
  28. } )
  29. .then( newEditor => {
  30. editor = newEditor;
  31. model = editor.model;
  32. } );
  33. } );
  34. afterEach( () => {
  35. editor.destroy();
  36. } );
  37. it( 'should set proper schema rules', () => {
  38. } );
  39. it( 'adds insertTable command', () => {
  40. expect( editor.commands.get( 'insertTable' ) ).to.be.instanceOf( InsertTableCommand );
  41. } );
  42. it( 'adds insertRowAbove command', () => {
  43. expect( editor.commands.get( 'insertTableRowAbove' ) ).to.be.instanceOf( InsertRowCommand );
  44. } );
  45. it( 'adds insertRowBelow command', () => {
  46. expect( editor.commands.get( 'insertTableRowBelow' ) ).to.be.instanceOf( InsertRowCommand );
  47. } );
  48. it( 'adds insertColumnBefore command', () => {
  49. expect( editor.commands.get( 'insertTableColumnBefore' ) ).to.be.instanceOf( InsertColumnCommand );
  50. } );
  51. it( 'adds insertColumnAfter command', () => {
  52. expect( editor.commands.get( 'insertTableColumnAfter' ) ).to.be.instanceOf( InsertColumnCommand );
  53. } );
  54. it( 'adds removeRow command', () => {
  55. expect( editor.commands.get( 'removeTableRow' ) ).to.be.instanceOf( RemoveRowCommand );
  56. } );
  57. it( 'adds removeColumn command', () => {
  58. expect( editor.commands.get( 'removeTableColumn' ) ).to.be.instanceOf( RemoveColumnCommand );
  59. } );
  60. it( 'adds splitCellVertically command', () => {
  61. expect( editor.commands.get( 'splitTableCellVertically' ) ).to.be.instanceOf( SplitCellCommand );
  62. } );
  63. it( 'adds splitCellHorizontally command', () => {
  64. expect( editor.commands.get( 'splitTableCellHorizontally' ) ).to.be.instanceOf( SplitCellCommand );
  65. } );
  66. it( 'adds mergeCellRight command', () => {
  67. expect( editor.commands.get( 'mergeTableCellRight' ) ).to.be.instanceOf( MergeCellCommand );
  68. } );
  69. it( 'adds mergeCellLeft command', () => {
  70. expect( editor.commands.get( 'mergeTableCellLeft' ) ).to.be.instanceOf( MergeCellCommand );
  71. } );
  72. it( 'adds mergeCellDown command', () => {
  73. expect( editor.commands.get( 'mergeTableCellDown' ) ).to.be.instanceOf( MergeCellCommand );
  74. } );
  75. it( 'adds mergeCellUp command', () => {
  76. expect( editor.commands.get( 'mergeTableCellUp' ) ).to.be.instanceOf( MergeCellCommand );
  77. } );
  78. it( 'adds setColumnHeader command', () => {
  79. expect( editor.commands.get( 'setTableColumnHeader' ) ).to.be.instanceOf( SetHeaderColumnCommand );
  80. } );
  81. it( 'adds setRowHeader command', () => {
  82. expect( editor.commands.get( 'setTableRowHeader' ) ).to.be.instanceOf( SetHeaderRowCommand );
  83. } );
  84. describe( 'conversion in data pipeline', () => {
  85. describe( 'model to view', () => {
  86. it( 'should create tbody section', () => {
  87. setModelData( model, '<table><tableRow><tableCell>foo[]</tableCell></tableRow></table>' );
  88. expect( editor.getData() ).to.equal(
  89. '<figure class="table">' +
  90. '<table>' +
  91. '<tbody>' +
  92. '<tr><td>foo</td></tr>' +
  93. '</tbody>' +
  94. '</table>' +
  95. '</figure>'
  96. );
  97. } );
  98. it( 'should create thead section', () => {
  99. setModelData( model, '<table headingRows="1"><tableRow><tableCell>foo[]</tableCell></tableRow></table>' );
  100. expect( editor.getData() ).to.equal(
  101. '<figure class="table">' +
  102. '<table>' +
  103. '<thead>' +
  104. '<tr><th>foo</th></tr>' +
  105. '</thead>' +
  106. '</table>' +
  107. '</figure>'
  108. );
  109. } );
  110. } );
  111. describe( 'view to model', () => {
  112. it( 'should convert table', () => {
  113. editor.setData( '<table><tbody><tr><td>foo</td></tr></tbody></table>' );
  114. expect( getModelData( model, { withoutSelection: true } ) )
  115. .to.equal( '<table><tableRow><tableCell>foo</tableCell></tableRow></table>' );
  116. } );
  117. } );
  118. } );
  119. describe( 'caret movement', () => {
  120. let domEvtDataStub;
  121. beforeEach( () => {
  122. domEvtDataStub = {
  123. keyCode: getCode( 'Tab' ),
  124. preventDefault: sinon.spy(),
  125. stopPropagation: sinon.spy()
  126. };
  127. } );
  128. it( 'should do nothing if not tab pressed', () => {
  129. setModelData( model, modelTable( [
  130. [ '11', '12[]' ]
  131. ] ) );
  132. domEvtDataStub.keyCode = getCode( 'a' );
  133. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  134. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  135. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  136. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  137. [ '11', '12[]' ]
  138. ] ) );
  139. } );
  140. it( 'should do nothing if Ctrl+Tab is pressed', () => {
  141. setModelData( model, modelTable( [
  142. [ '11', '12[]' ]
  143. ] ) );
  144. domEvtDataStub.ctrlKey = true;
  145. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  146. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  147. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  148. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  149. [ '11', '12[]' ]
  150. ] ) );
  151. } );
  152. describe( 'on TAB', () => {
  153. it( 'should do nothing if selection is not in a table', () => {
  154. setModelData( model, '[]' + modelTable( [
  155. [ '11', '12' ]
  156. ] ) );
  157. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  158. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  159. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  160. expect( formatTable( getModelData( model ) ) ).to.equal( '[]' + formattedModelTable( [
  161. [ '11', '12' ]
  162. ] ) );
  163. } );
  164. it( 'should move to next cell', () => {
  165. setModelData( model, modelTable( [
  166. [ '11[]', '12' ]
  167. ] ) );
  168. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  169. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  170. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  171. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  172. [ '11', '[12]' ]
  173. ] ) );
  174. } );
  175. it( 'should create another row and move to first cell in new row', () => {
  176. setModelData( model, modelTable( [
  177. [ '11', '[12]' ]
  178. ] ) );
  179. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  180. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  181. [ '11', '12' ],
  182. [ '[]', '' ]
  183. ] ) );
  184. } );
  185. it( 'should move to the first cell of next row if on end of a row', () => {
  186. setModelData( model, modelTable( [
  187. [ '11', '12[]' ],
  188. [ '21', '22' ]
  189. ] ) );
  190. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  191. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  192. [ '11', '12' ],
  193. [ '[21]', '22' ]
  194. ] ) );
  195. } );
  196. describe( 'on table widget selected', () => {
  197. beforeEach( () => {
  198. editor.model.schema.register( 'block', {
  199. allowWhere: '$block',
  200. allowContentOf: '$block',
  201. isObject: true
  202. } );
  203. editor.conversion.elementToElement( { model: 'block', view: 'block' } );
  204. } );
  205. it( 'should move caret to the first table cell on TAB', () => {
  206. const spy = sinon.spy();
  207. editor.editing.view.document.on( 'keydown', spy );
  208. setModelData( model, '[' + modelTable( [
  209. [ '11', '12' ]
  210. ] ) + ']' );
  211. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  212. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  213. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  214. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  215. [ '[11]', '12' ]
  216. ] ) );
  217. // Should cancel event - so no other tab handler is called.
  218. sinon.assert.notCalled( spy );
  219. } );
  220. it( 'shouldn\'t do anything on other blocks', () => {
  221. const spy = sinon.spy();
  222. editor.editing.view.document.on( 'keydown', spy );
  223. setModelData( model, '[<block>foo</block>]' );
  224. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  225. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  226. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  227. expect( formatTable( getModelData( model ) ) ).to.equal( '[<block>foo</block>]' );
  228. // Should not cancel event.
  229. sinon.assert.calledOnce( spy );
  230. } );
  231. } );
  232. } );
  233. describe( 'on SHIFT+TAB', () => {
  234. beforeEach( () => {
  235. domEvtDataStub.shiftKey = true;
  236. } );
  237. it( 'should do nothing if selection is not in a table', () => {
  238. setModelData( model, '[]' + modelTable( [
  239. [ '11', '12' ]
  240. ] ) );
  241. domEvtDataStub.keyCode = getCode( 'Tab' );
  242. domEvtDataStub.shiftKey = true;
  243. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  244. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  245. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  246. expect( formatTable( getModelData( model ) ) ).to.equal( '[]' + formattedModelTable( [
  247. [ '11', '12' ]
  248. ] ) );
  249. } );
  250. it( 'should move to previous cell', () => {
  251. setModelData( model, modelTable( [
  252. [ '11', '12[]' ]
  253. ] ) );
  254. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  255. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  256. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  257. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  258. [ '[11]', '12' ]
  259. ] ) );
  260. } );
  261. it( 'should not move if caret is in first table cell', () => {
  262. setModelData( model, '<paragraph>foo</paragraph>' + modelTable( [
  263. [ '[]11', '12' ]
  264. ] ) );
  265. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  266. expect( formatTable( getModelData( model ) ) ).to.equal(
  267. '<paragraph>foo</paragraph>' + formattedModelTable( [ [ '[]11', '12' ] ] )
  268. );
  269. } );
  270. it( 'should move to the last cell of previous row if on beginning of a row', () => {
  271. setModelData( model, modelTable( [
  272. [ '11', '12' ],
  273. [ '[]21', '22' ]
  274. ] ) );
  275. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  276. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  277. [ '11', '[12]' ],
  278. [ '21', '22' ]
  279. ] ) );
  280. } );
  281. } );
  282. } );
  283. describe( 'post-fixer', () => {
  284. let root;
  285. beforeEach( () => {
  286. root = model.document.getRoot();
  287. } );
  288. it( 'should add missing columns to a tableRows that are shorter then longest table row', () => {
  289. const parsed = parse( modelTable( [
  290. [ '00' ],
  291. [ '10', '11', '12' ],
  292. [ '20', '21' ]
  293. ] ), model.schema );
  294. model.change( writer => {
  295. writer.remove( Range.createIn( root ) );
  296. writer.insert( parsed, root );
  297. } );
  298. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formattedModelTable( [
  299. [ '00', '', '' ],
  300. [ '10', '11', '12' ],
  301. [ '20', '21', '' ]
  302. ] ) );
  303. } );
  304. it( 'should add missing columns to a tableRows that are shorter then longest table row (complex 1)', () => {
  305. const parsed = parse( modelTable( [
  306. [ '00', { rowspan: 2, contents: '10' } ],
  307. [ '10', { colspan: 2, contents: '12' } ],
  308. [ '20', '21' ]
  309. ] ), model.schema );
  310. model.change( writer => {
  311. writer.remove( Range.createIn( root ) );
  312. writer.insert( parsed, root );
  313. } );
  314. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formattedModelTable( [
  315. [ '00', { rowspan: 2, contents: '10' }, '', '' ],
  316. [ '10', { colspan: 2, contents: '12' } ],
  317. [ '20', '21', '', '' ]
  318. ] ) );
  319. } );
  320. it( 'should add missing columns to a tableRows that are shorter then longest table row (complex 2)', () => {
  321. const parsed = parse( modelTable( [
  322. [ { colspan: 6, contents: '00' } ],
  323. [ { rowspan: 2, contents: '10' }, '11', { colspan: 3, contents: '12' } ],
  324. [ '21', '22' ]
  325. ] ), model.schema );
  326. model.change( writer => {
  327. writer.remove( Range.createIn( root ) );
  328. writer.insert( parsed, root );
  329. } );
  330. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formattedModelTable( [
  331. [ { colspan: 6, contents: '00' } ],
  332. [ { rowspan: 2, contents: '10' }, '11', { colspan: 3, contents: '12' }, '' ],
  333. [ '21', '22', '', '', '' ]
  334. ] ) );
  335. } );
  336. it( 'should fix wrong rowspan attribute on table header', () => {
  337. const parsed = parse( modelTable( [
  338. [ { rowspan: 2, contents: '00' }, { rowspan: 3, contents: '01' }, '02' ],
  339. [ { rowspan: 8, contents: '12' } ],
  340. [ '20', '21', '22' ]
  341. ], { headingRows: 2 } ), model.schema );
  342. model.change( writer => {
  343. writer.remove( Range.createIn( root ) );
  344. writer.insert( parsed, root );
  345. } );
  346. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formattedModelTable( [
  347. [ { rowspan: 2, contents: '00' }, { rowspan: 2, contents: '01' }, '02' ],
  348. [ '12' ],
  349. [ '20', '21', '22' ]
  350. ], { headingRows: 2 } ) );
  351. } );
  352. it( 'should fix wrong rowspan attribute on table body', () => {
  353. const parsed = parse( modelTable( [
  354. [ '00', '01', '02' ],
  355. [ { rowspan: 2, contents: '10' }, { rowspan: 3, contents: '11' }, '12' ],
  356. [ { rowspan: 8, contents: '22' } ]
  357. ], { headingRows: 1 } ), model.schema );
  358. model.change( writer => {
  359. writer.remove( Range.createIn( root ) );
  360. writer.insert( parsed, root );
  361. } );
  362. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formattedModelTable( [
  363. [ '00', '01', '02' ],
  364. [ { rowspan: 2, contents: '10' }, { rowspan: 2, contents: '11' }, '12' ],
  365. [ '22' ]
  366. ], { headingRows: 1 } ) );
  367. } );
  368. it( 'collab remove column vs insert row', () => {
  369. _testExternal(
  370. modelTable( [
  371. [ '00[]', '01' ],
  372. [ '10', '11' ]
  373. ] ),
  374. writer => _removeColumn( writer, 1, [ 0, 1 ] ),
  375. writer => _insertRow( writer, 1, [ 'a', 'b' ] ),
  376. formattedModelTable( [
  377. [ '00', '' ],
  378. [ 'a', 'b' ],
  379. [ '10', '' ]
  380. ] ),
  381. formattedModelTable( [
  382. [ '00', '01', '' ],
  383. [ 'a', 'b', '' ],
  384. [ '10', '11', '' ]
  385. ] ) );
  386. } );
  387. it( 'collab insert row vs remove column', () => {
  388. _testExternal(
  389. modelTable( [
  390. [ '00[]', '01' ],
  391. [ '10', '11' ]
  392. ] ),
  393. writer => _insertRow( writer, 1, [ 'a', 'b' ] ),
  394. writer => _removeColumn( writer, 1, [ 0, 2 ] ),
  395. formattedModelTable( [
  396. [ '00', '' ],
  397. [ 'a', 'b' ],
  398. [ '10', '' ]
  399. ] ),
  400. formattedModelTable( [
  401. [ '00', '' ],
  402. [ '10', '' ]
  403. ] ) );
  404. } );
  405. it( 'collab insert row vs insert column', () => {
  406. _testExternal(
  407. modelTable( [
  408. [ '00[]', '01' ],
  409. [ '10', '11' ]
  410. ] ),
  411. writer => _insertRow( writer, 1, [ 'a', 'b' ] ),
  412. writer => _insertColumn( writer, 1, [ 0, 2 ] ),
  413. formattedModelTable( [
  414. [ '00', '', '01' ],
  415. [ 'a', 'b', '' ],
  416. [ '10', '', '11' ]
  417. ] ),
  418. formattedModelTable( [
  419. [ '00', '', '01' ],
  420. [ '10', '', '11' ]
  421. ] ) );
  422. } );
  423. it( 'collab insert column vs insert row', () => {
  424. _testExternal(
  425. modelTable( [
  426. [ '00[]', '01' ],
  427. [ '10', '11' ]
  428. ] ),
  429. writer => _insertColumn( writer, 1, [ 0, 1 ] ),
  430. writer => _insertRow( writer, 1, [ 'a', 'b' ] ),
  431. formattedModelTable( [
  432. [ '00', '', '01' ],
  433. [ 'a', 'b', '' ],
  434. [ '10', '', '11' ]
  435. ] ),
  436. formattedModelTable( [
  437. [ '00', '01', '' ],
  438. [ 'a', 'b', '' ],
  439. [ '10', '11', '' ]
  440. ] ) );
  441. } );
  442. it( 'collab insert column vs insert column - other row has spanned cell', () => {
  443. _testExternal(
  444. modelTable( [
  445. [ { colspan: 3, contents: '00' } ],
  446. [ '10', '11', '12' ]
  447. ] ),
  448. writer => {
  449. _setAttribute( writer, 'colspan', 4, [ 0, 0, 0 ] );
  450. _insertColumn( writer, 2, [ 1 ] );
  451. },
  452. writer => {
  453. _setAttribute( writer, 'colspan', 4, [ 0, 0, 0 ] );
  454. _insertColumn( writer, 1, [ 1 ] );
  455. },
  456. formattedModelTable( [
  457. [ { colspan: 4, contents: '00' }, '' ],
  458. [ '10', '', '11', '', '12' ]
  459. ] ),
  460. formattedModelTable( [
  461. [ { colspan: 3, contents: '00' }, '' ],
  462. [ '10', '', '11', '12' ]
  463. ] ) );
  464. } );
  465. it( 'collab insert column vs insert column - other row has spanned cell (inverted)', () => {
  466. _testExternal(
  467. modelTable( [
  468. [ { colspan: 3, contents: '00' } ],
  469. [ '10', '11', '12' ]
  470. ] ),
  471. writer => {
  472. _setAttribute( writer, 'colspan', 4, [ 0, 0, 0 ] );
  473. _insertColumn( writer, 1, [ 1 ] );
  474. },
  475. writer => {
  476. _setAttribute( writer, 'colspan', 4, [ 0, 0, 0 ] );
  477. _insertColumn( writer, 3, [ 1 ] );
  478. },
  479. formattedModelTable( [
  480. [ { colspan: 4, contents: '00' }, '' ],
  481. [ '10', '', '11', '', '12' ]
  482. ] ),
  483. formattedModelTable( [
  484. [ { colspan: 3, contents: '00' }, '' ],
  485. [ '10', '11', '', '12' ]
  486. ] ) );
  487. } );
  488. it( 'collab change table header rows vs remove row', () => {
  489. _testExternal(
  490. modelTable( [
  491. [ '11', { rowspan: 2, contents: '12' }, '13' ],
  492. [ '21', '23' ],
  493. [ '31', '32', '33' ]
  494. ] ),
  495. writer => {
  496. _setAttribute( writer, 'headingRows', 1, [ 0 ] );
  497. _setAttribute( writer, 'rowspan', 1, [ 0, 0, 1 ] );
  498. _insertCell( writer, 1, 1 );
  499. },
  500. writer => {
  501. _removeRow( writer, 1 );
  502. },
  503. formattedModelTable( [
  504. [ '11', { rowspan: 1, contents: '12' }, '13' ],
  505. [ '31', '32', '33' ]
  506. ], { headingRows: 1 } ),
  507. formattedModelTable( [
  508. [ '11', { rowspan: 2, contents: '12' }, '13', '' ],
  509. [ '31', '32', '33' ]
  510. ] ) );
  511. } );
  512. it( 'collab remove row vs change table header rows', () => {
  513. _testExternal(
  514. modelTable( [
  515. [ '11', { rowspan: 2, contents: '12' }, '13' ],
  516. [ '21', '23' ],
  517. [ '31', '32', '33' ]
  518. ] ),
  519. writer => {
  520. _removeRow( writer, 1 );
  521. },
  522. writer => {
  523. _setAttribute( writer, 'headingRows', 1, [ 0 ] );
  524. _setAttribute( writer, 'rowspan', 1, [ 0, 0, 1 ] );
  525. },
  526. formattedModelTable( [
  527. [ '11', { rowspan: 1, contents: '12' }, '13', '' ],
  528. [ '31', '32', '33', '' ]
  529. ], { headingRows: 1 } ),
  530. formattedModelTable( [
  531. [ '11', { rowspan: 1, contents: '12' }, '13', '' ],
  532. [ '21', '23', '', '' ],
  533. [ '31', '32', '33', '' ]
  534. ], { headingRows: 1 } ) );
  535. } );
  536. it( 'should not crash on table remove', () => {
  537. setModelData( model, modelTable( [
  538. [ '11', '12' ]
  539. ] ) );
  540. expect( () => {
  541. model.change( writer => {
  542. writer.remove( Range.createIn( root ) );
  543. } );
  544. } ).to.not.throw();
  545. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<paragraph></paragraph>' );
  546. } );
  547. // Case: remove same column (undo does nothing on one client - NOOP in batch).
  548. // Case: remove same row (undo does nothing on one client - NOOP in batch).
  549. // Case: Typing over user selecting - typing in marker...
  550. function _testExternal( initialData, localCallback, externalCallback, modelAfter, modelAfterUndo ) {
  551. setModelData( model, initialData );
  552. model.change( localCallback );
  553. model.enqueueChange( 'transparent', externalCallback );
  554. expect( formatTable( getModelData( model, { withoutSelection: true } ) ), 'after operations' ).to.equal( modelAfter );
  555. editor.execute( 'undo' );
  556. expect( formatTable( getModelData( model, { withoutSelection: true } ) ), 'after undo' ).to.equal( modelAfterUndo );
  557. editor.execute( 'redo' );
  558. expect( formatTable( getModelData( model, { withoutSelection: true } ) ), 'after redo' ).to.equal( modelAfter );
  559. }
  560. function _removeColumn( writer, columnIndex, rows ) {
  561. const table = root.getChild( 0 );
  562. for ( const index of rows ) {
  563. const tableRow = table.getChild( index );
  564. const tableCell = tableRow.getChild( columnIndex );
  565. writer.remove( tableCell );
  566. }
  567. }
  568. function _removeRow( writer, rowIndex ) {
  569. const table = root.getChild( 0 );
  570. const tableRow = table.getChild( rowIndex );
  571. writer.remove( tableRow );
  572. }
  573. function _insertRow( writer, rowIndex, rowData ) {
  574. const table = root.getChild( 0 );
  575. const parsedTable = parse(
  576. modelTable( [ rowData ] ),
  577. model.schema
  578. );
  579. writer.insert( parsedTable.getChild( 0 ), table, rowIndex );
  580. }
  581. function _insertCell( writer, rowIndex, index ) {
  582. const table = root.getChild( 0 );
  583. const tableRow = table.getChild( rowIndex );
  584. writer.insertElement( 'tableCell', tableRow, index );
  585. }
  586. function _setAttribute( writer, attributeKey, attributeValue, path ) {
  587. const node = root.getNodeByPath( path );
  588. writer.setAttribute( attributeKey, attributeValue, node );
  589. }
  590. function _insertColumn( writer, columnIndex, rows ) {
  591. const table = root.getChild( 0 );
  592. for ( const index of rows ) {
  593. const tableRow = table.getChild( index );
  594. const tableCell = writer.createElement( 'tableCell' );
  595. writer.insert( tableCell, tableRow, columnIndex );
  596. }
  597. }
  598. } );
  599. } );