table-post-fixer.js 13 KB

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