table-layout-post-fixer.js 13 KB

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