8
0

table-layout-post-fixer.js 13 KB

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