table-post-fixer.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 TableEditing from '../../src/tableediting';
  10. import { formatTable, formattedModelTable, modelTable } from './../_utils/utils';
  11. import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
  12. import TableSelection from '../../src/tableselection';
  13. describe( 'Table post-fixer', () => {
  14. let editor, model, root;
  15. beforeEach( () => {
  16. return VirtualTestEditor
  17. .create( {
  18. plugins: [ TableEditing, TableSelection, Paragraph, UndoEditing ]
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. model = editor.model;
  23. root = model.document.getRoot();
  24. } );
  25. } );
  26. afterEach( () => {
  27. editor.destroy();
  28. } );
  29. describe( 'on insert table', () => {
  30. it( 'should add missing columns to tableRows that are shorter then the longest table row', () => {
  31. const parsed = parse( modelTable( [
  32. [ '00' ],
  33. [ '10', '11', '12' ],
  34. [ '20', '21' ]
  35. ] ), model.schema );
  36. model.change( writer => {
  37. writer.remove( Range.createIn( root ) );
  38. writer.insert( parsed, root );
  39. } );
  40. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formattedModelTable( [
  41. [ '00', '', '' ],
  42. [ '10', '11', '12' ],
  43. [ '20', '21', '' ]
  44. ] ) );
  45. } );
  46. it( 'should add missing columns to tableRows that are shorter then the longest table row (complex 1)', () => {
  47. const parsed = parse( modelTable( [
  48. [ '00', { rowspan: 2, contents: '10' } ],
  49. [ '10', { colspan: 2, contents: '12' } ],
  50. [ '20', '21' ]
  51. ] ), model.schema );
  52. model.change( writer => {
  53. writer.remove( Range.createIn( root ) );
  54. writer.insert( parsed, root );
  55. } );
  56. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formattedModelTable( [
  57. [ '00', { rowspan: 2, contents: '10' }, '', '' ],
  58. [ '10', { colspan: 2, contents: '12' } ],
  59. [ '20', '21', '', '' ]
  60. ] ) );
  61. } );
  62. it( 'should add missing columns to tableRows that are shorter then the longest table row (complex 2)', () => {
  63. const parsed = parse( modelTable( [
  64. [ { colspan: 6, contents: '00' } ],
  65. [ { rowspan: 2, contents: '10' }, '11', { colspan: 3, contents: '12' } ],
  66. [ '21', '22' ]
  67. ] ), model.schema );
  68. model.change( writer => {
  69. writer.remove( Range.createIn( root ) );
  70. writer.insert( parsed, root );
  71. } );
  72. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formattedModelTable( [
  73. [ { colspan: 6, contents: '00' } ],
  74. [ { rowspan: 2, contents: '10' }, '11', { colspan: 3, contents: '12' }, '' ],
  75. [ '21', '22', '', '', '' ]
  76. ] ) );
  77. } );
  78. it( 'should fix the wrong rowspan attribute of a table cell inside the header', () => {
  79. const parsed = parse( modelTable( [
  80. [ { rowspan: 2, contents: '00' }, { rowspan: 3, contents: '01' }, '02' ],
  81. [ { rowspan: 8, contents: '12' } ],
  82. [ '20', '21', '22' ]
  83. ], { headingRows: 2 } ), model.schema );
  84. model.change( writer => {
  85. writer.remove( Range.createIn( root ) );
  86. writer.insert( parsed, root );
  87. } );
  88. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formattedModelTable( [
  89. [ { rowspan: 2, contents: '00' }, { rowspan: 2, contents: '01' }, '02' ],
  90. [ '12' ],
  91. [ '20', '21', '22' ]
  92. ], { headingRows: 2 } ) );
  93. } );
  94. it( 'should fix the wrong rowspan attribute of a table cell inside the body', () => {
  95. const parsed = parse( modelTable( [
  96. [ '00', '01', '02' ],
  97. [ { rowspan: 2, contents: '10' }, { rowspan: 3, contents: '11' }, '12' ],
  98. [ { rowspan: 8, contents: '22' } ]
  99. ], { headingRows: 1 } ), model.schema );
  100. model.change( writer => {
  101. writer.remove( Range.createIn( root ) );
  102. writer.insert( parsed, root );
  103. } );
  104. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formattedModelTable( [
  105. [ '00', '01', '02' ],
  106. [ { rowspan: 2, contents: '10' }, { rowspan: 2, contents: '11' }, '12' ],
  107. [ '22' ]
  108. ], { headingRows: 1 } ) );
  109. } );
  110. it( 'should fix multiple tables', () => {
  111. const tableA = modelTable( [
  112. [ '11' ],
  113. [ '21', '22' ]
  114. ] );
  115. const tableB = modelTable( [
  116. [ 'aa', 'ab' ],
  117. [ 'ba', 'bb' ]
  118. ] );
  119. const tableC = modelTable( [
  120. [ 'xx' ],
  121. [ 'yy', 'yy' ]
  122. ] );
  123. const parsed = parse( tableA + tableB + tableC, model.schema );
  124. model.change( writer => {
  125. writer.remove( Range.createIn( root ) );
  126. writer.insert( parsed, root );
  127. } );
  128. const expectedTableA = formattedModelTable( [
  129. [ '11', '' ],
  130. [ '21', '22' ]
  131. ] );
  132. const expectedTableB = formattedModelTable( [
  133. [ 'aa', 'ab' ],
  134. [ 'ba', 'bb' ]
  135. ] );
  136. const expectedTableC = formattedModelTable( [
  137. [ 'xx', '' ],
  138. [ 'yy', 'yy' ]
  139. ] );
  140. const expectedTables = expectedTableA + expectedTableB + expectedTableC;
  141. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( expectedTables );
  142. } );
  143. it( 'should not crash on table remove', () => {
  144. setModelData( model, modelTable( [
  145. [ '11', '12' ]
  146. ] ) );
  147. expect( () => {
  148. model.change( writer => {
  149. writer.remove( Range.createIn( root ) );
  150. } );
  151. } ).to.not.throw();
  152. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<paragraph></paragraph>' );
  153. } );
  154. } );
  155. describe( 'on collaboration', () => {
  156. it( 'should add missing cells to columns (remove column vs insert row)', () => {
  157. _testExternal(
  158. modelTable( [
  159. [ '00[]', '01' ],
  160. [ '10', '11' ]
  161. ] ),
  162. writer => _removeColumn( writer, 1, [ 0, 1 ] ),
  163. writer => _insertRow( writer, 1, [ 'a', 'b' ] ),
  164. // Table should have added empty cells.
  165. formattedModelTable( [
  166. [ '00', '' ],
  167. [ 'a', 'b' ],
  168. [ '10', '' ]
  169. ] ),
  170. // Table will have empty column after undo.
  171. formattedModelTable( [
  172. [ '00', '01', '' ],
  173. [ 'a', 'b', '' ],
  174. [ '10', '11', '' ]
  175. ] ) );
  176. } );
  177. it( 'should add missing cells to columns (insert row vs remove column)', () => {
  178. _testExternal(
  179. modelTable( [
  180. [ '00[]', '01' ],
  181. [ '10', '11' ]
  182. ] ),
  183. writer => _insertRow( writer, 1, [ 'a', 'b' ] ),
  184. writer => _removeColumn( writer, 1, [ 0, 2 ] ),
  185. // There should be empty cells added.
  186. formattedModelTable( [
  187. [ '00', '' ],
  188. [ 'a', 'b' ],
  189. [ '10', '' ]
  190. ] ),
  191. // Table will have empty column after undo.
  192. formattedModelTable( [
  193. [ '00', '' ],
  194. [ '10', '' ]
  195. ] ) );
  196. } );
  197. it( 'should add empty cell to an added row (insert row vs insert column)', () => {
  198. _testExternal(
  199. modelTable( [
  200. [ '00[]', '01' ],
  201. [ '10', '11' ]
  202. ] ),
  203. writer => _insertRow( writer, 1, [ 'a', 'b' ] ),
  204. writer => _insertColumn( writer, 1, [ 0, 2 ] ),
  205. // There should be empty cells added.
  206. formattedModelTable( [
  207. [ '00', '', '01' ],
  208. [ 'a', 'b', '' ],
  209. [ '10', '', '11' ]
  210. ] ),
  211. // Table will have empty column after undo.
  212. formattedModelTable( [
  213. [ '00', '', '01' ],
  214. [ '10', '', '11' ]
  215. ] ) );
  216. } );
  217. it( 'should add empty cell to an added row (insert column vs insert row)', () => {
  218. _testExternal(
  219. modelTable( [
  220. [ '00[]', '01' ],
  221. [ '10', '11' ]
  222. ] ),
  223. writer => _insertColumn( writer, 1, [ 0, 1 ] ),
  224. writer => _insertRow( writer, 1, [ 'a', 'b' ] ),
  225. // There should be empty cells added.
  226. formattedModelTable( [
  227. [ '00', '', '01' ],
  228. [ 'a', 'b', '' ],
  229. [ '10', '', '11' ]
  230. ] ),
  231. // Table will have empty column after undo.
  232. formattedModelTable( [
  233. [ '00', '01', '' ],
  234. [ 'a', 'b', '' ],
  235. [ '10', '11', '' ]
  236. ] ) );
  237. } );
  238. it( 'should add empty cell when inserting column over a colspanned cell (insert column vs insert column)', () => {
  239. _testExternal(
  240. modelTable( [
  241. [ { colspan: 3, contents: '00' } ],
  242. [ '10', '11', '12' ]
  243. ] ),
  244. writer => {
  245. _setAttribute( writer, 'colspan', 4, [ 0, 0, 0 ] );
  246. _insertColumn( writer, 2, [ 1 ] );
  247. },
  248. writer => {
  249. _setAttribute( writer, 'colspan', 4, [ 0, 0, 0 ] );
  250. _insertColumn( writer, 1, [ 1 ] );
  251. },
  252. // There should be empty cells added.
  253. formattedModelTable( [
  254. [ { colspan: 4, contents: '00' }, '' ],
  255. [ '10', '', '11', '', '12' ]
  256. ] ),
  257. // Table will have empty column after undo.
  258. formattedModelTable( [
  259. [ { colspan: 3, contents: '00' }, '' ],
  260. [ '10', '', '11', '12' ]
  261. ] ) );
  262. } );
  263. it( 'should add empty cell when inserting column over a colspanned cell (insert column vs insert column) - inverted', () => {
  264. _testExternal(
  265. modelTable( [
  266. [ { colspan: 3, contents: '00' } ],
  267. [ '10', '11', '12' ]
  268. ] ),
  269. writer => {
  270. _setAttribute( writer, 'colspan', 4, [ 0, 0, 0 ] );
  271. _insertColumn( writer, 1, [ 1 ] );
  272. },
  273. writer => {
  274. _setAttribute( writer, 'colspan', 4, [ 0, 0, 0 ] );
  275. _insertColumn( writer, 3, [ 1 ] );
  276. },
  277. // There should be empty cells added.
  278. formattedModelTable( [
  279. [ { colspan: 4, contents: '00' }, '' ],
  280. [ '10', '', '11', '', '12' ]
  281. ] ),
  282. // Table will have empty column after undo.
  283. formattedModelTable( [
  284. [ { colspan: 3, contents: '00' }, '' ],
  285. [ '10', '11', '', '12' ]
  286. ] ) );
  287. } );
  288. it( 'should insert table cell on undo (change table headers on row with rowspanned cell vs remove row)', () => {
  289. _testExternal(
  290. modelTable( [
  291. [ '11', { rowspan: 2, contents: '12' }, '13' ],
  292. [ '21', '23' ],
  293. [ '31', '32', '33' ]
  294. ] ),
  295. writer => {
  296. _setAttribute( writer, 'headingRows', 1, [ 0 ] );
  297. _removeAttribute( writer, 'rowspan', [ 0, 0, 1 ] );
  298. _insertCell( writer, 1, 1 );
  299. },
  300. writer => {
  301. _removeRow( writer, 1 );
  302. },
  303. formattedModelTable( [
  304. [ '11', '12', '13' ],
  305. [ '31', '32', '33' ]
  306. ], { headingRows: 1 } ),
  307. formattedModelTable( [
  308. [ '11', { rowspan: 2, contents: '12' }, '13', '' ],
  309. [ '31', '32', '33' ]
  310. ] ) );
  311. } );
  312. it( 'should insert empty table cell (remove row vs change table headers on row with rowspanned cell)', () => {
  313. _testExternal(
  314. modelTable( [
  315. [ '11', { rowspan: 2, contents: '12' }, '13' ],
  316. [ '21', '23' ],
  317. [ '31', '32', '33' ]
  318. ] ),
  319. writer => {
  320. _removeRow( writer, 1 );
  321. },
  322. writer => {
  323. _setAttribute( writer, 'headingRows', 1, [ 0 ] );
  324. _removeAttribute( writer, 'rowspan', [ 0, 0, 1 ] );
  325. },
  326. formattedModelTable( [
  327. [ '11', '12', '13', '' ],
  328. [ '31', '32', '33', '' ]
  329. ], { headingRows: 1 } ),
  330. formattedModelTable( [
  331. [ '11', '12', '13', '' ],
  332. [ '21', '23', '', '' ],
  333. [ '31', '32', '33', '' ]
  334. ], { headingRows: 1 } ) );
  335. } );
  336. function _testExternal( initialData, localCallback, externalCallback, modelAfter, modelAfterUndo ) {
  337. setModelData( model, initialData );
  338. model.change( localCallback );
  339. model.enqueueChange( 'transparent', externalCallback );
  340. expect( formatTable( getModelData( model, { withoutSelection: true } ) ), 'after operations' ).to.equal( modelAfter );
  341. editor.execute( 'undo' );
  342. expect( formatTable( getModelData( model, { withoutSelection: true } ) ), 'after undo' ).to.equal( modelAfterUndo );
  343. editor.execute( 'redo' );
  344. expect( formatTable( getModelData( model, { withoutSelection: true } ) ), 'after redo' ).to.equal( modelAfter );
  345. }
  346. function _removeColumn( writer, columnIndex, rows ) {
  347. const table = root.getChild( 0 );
  348. for ( const index of rows ) {
  349. const tableRow = table.getChild( index );
  350. const tableCell = tableRow.getChild( columnIndex );
  351. writer.remove( tableCell );
  352. }
  353. }
  354. function _removeRow( writer, rowIndex ) {
  355. const table = root.getChild( 0 );
  356. const tableRow = table.getChild( rowIndex );
  357. writer.remove( tableRow );
  358. }
  359. function _insertRow( writer, rowIndex, rowData ) {
  360. const table = root.getChild( 0 );
  361. const parsedTable = parse(
  362. modelTable( [ rowData ] ),
  363. model.schema
  364. );
  365. writer.insert( parsedTable.getChild( 0 ), table, rowIndex );
  366. }
  367. function _insertCell( writer, rowIndex, index ) {
  368. const table = root.getChild( 0 );
  369. const tableRow = table.getChild( rowIndex );
  370. const tableCell = writer.createElement( 'tableCell' );
  371. writer.insert( tableCell, tableRow, index );
  372. writer.insertElement( 'paragraph', tableCell );
  373. }
  374. function _setAttribute( writer, attributeKey, attributeValue, path ) {
  375. const node = root.getNodeByPath( path );
  376. writer.setAttribute( attributeKey, attributeValue, node );
  377. }
  378. function _removeAttribute( writer, attributeKey, path ) {
  379. const node = root.getNodeByPath( path );
  380. writer.removeAttribute( attributeKey, node );
  381. }
  382. function _insertColumn( writer, columnIndex, rows ) {
  383. const table = root.getChild( 0 );
  384. for ( const index of rows ) {
  385. const tableRow = table.getChild( index );
  386. const tableCell = writer.createElement( 'tableCell' );
  387. writer.insert( tableCell, tableRow, columnIndex );
  388. writer.insertElement( 'paragraph', tableCell );
  389. }
  390. }
  391. } );
  392. } );