table-layout-post-fixer.js 13 KB

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