8
0

table-post-fixer.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 a tableRows that are shorter then 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 a tableRows that are shorter then 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 a tableRows that are shorter then 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 wrong rowspan attribute on table 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 wrong rowspan attribute on table 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( 'collaboration', () => {
  155. it( 'collab 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. formattedModelTable( [
  164. [ '00', '' ],
  165. [ 'a', 'b' ],
  166. [ '10', '' ]
  167. ] ),
  168. formattedModelTable( [
  169. [ '00', '01', '' ],
  170. [ 'a', 'b', '' ],
  171. [ '10', '11', '' ]
  172. ] ) );
  173. } );
  174. it( 'collab insert row vs remove column', () => {
  175. _testExternal(
  176. modelTable( [
  177. [ '00[]', '01' ],
  178. [ '10', '11' ]
  179. ] ),
  180. writer => _insertRow( writer, 1, [ 'a', 'b' ] ),
  181. writer => _removeColumn( writer, 1, [ 0, 2 ] ),
  182. formattedModelTable( [
  183. [ '00', '' ],
  184. [ 'a', 'b' ],
  185. [ '10', '' ]
  186. ] ),
  187. formattedModelTable( [
  188. [ '00', '' ],
  189. [ '10', '' ]
  190. ] ) );
  191. } );
  192. it( 'collab insert row vs insert column', () => {
  193. _testExternal(
  194. modelTable( [
  195. [ '00[]', '01' ],
  196. [ '10', '11' ]
  197. ] ),
  198. writer => _insertRow( writer, 1, [ 'a', 'b' ] ),
  199. writer => _insertColumn( writer, 1, [ 0, 2 ] ),
  200. formattedModelTable( [
  201. [ '00', '', '01' ],
  202. [ 'a', 'b', '' ],
  203. [ '10', '', '11' ]
  204. ] ),
  205. formattedModelTable( [
  206. [ '00', '', '01' ],
  207. [ '10', '', '11' ]
  208. ] ) );
  209. } );
  210. it( 'collab insert column vs insert row', () => {
  211. _testExternal(
  212. modelTable( [
  213. [ '00[]', '01' ],
  214. [ '10', '11' ]
  215. ] ),
  216. writer => _insertColumn( writer, 1, [ 0, 1 ] ),
  217. writer => _insertRow( writer, 1, [ 'a', 'b' ] ),
  218. formattedModelTable( [
  219. [ '00', '', '01' ],
  220. [ 'a', 'b', '' ],
  221. [ '10', '', '11' ]
  222. ] ),
  223. formattedModelTable( [
  224. [ '00', '01', '' ],
  225. [ 'a', 'b', '' ],
  226. [ '10', '11', '' ]
  227. ] ) );
  228. } );
  229. it( 'collab insert column vs insert column - other row has spanned cell', () => {
  230. _testExternal(
  231. modelTable( [
  232. [ { colspan: 3, contents: '00' } ],
  233. [ '10', '11', '12' ]
  234. ] ),
  235. writer => {
  236. _setAttribute( writer, 'colspan', 4, [ 0, 0, 0 ] );
  237. _insertColumn( writer, 2, [ 1 ] );
  238. },
  239. writer => {
  240. _setAttribute( writer, 'colspan', 4, [ 0, 0, 0 ] );
  241. _insertColumn( writer, 1, [ 1 ] );
  242. },
  243. formattedModelTable( [
  244. [ { colspan: 4, contents: '00' }, '' ],
  245. [ '10', '', '11', '', '12' ]
  246. ] ),
  247. formattedModelTable( [
  248. [ { colspan: 3, contents: '00' }, '' ],
  249. [ '10', '', '11', '12' ]
  250. ] ) );
  251. } );
  252. it( 'collab insert column vs insert column - other row has spanned cell (inverted)', () => {
  253. _testExternal(
  254. modelTable( [
  255. [ { colspan: 3, contents: '00' } ],
  256. [ '10', '11', '12' ]
  257. ] ),
  258. writer => {
  259. _setAttribute( writer, 'colspan', 4, [ 0, 0, 0 ] );
  260. _insertColumn( writer, 1, [ 1 ] );
  261. },
  262. writer => {
  263. _setAttribute( writer, 'colspan', 4, [ 0, 0, 0 ] );
  264. _insertColumn( writer, 3, [ 1 ] );
  265. },
  266. formattedModelTable( [
  267. [ { colspan: 4, contents: '00' }, '' ],
  268. [ '10', '', '11', '', '12' ]
  269. ] ),
  270. formattedModelTable( [
  271. [ { colspan: 3, contents: '00' }, '' ],
  272. [ '10', '11', '', '12' ]
  273. ] ) );
  274. } );
  275. it( 'collab change table header rows vs remove row', () => {
  276. _testExternal(
  277. modelTable( [
  278. [ '11', { rowspan: 2, contents: '12' }, '13' ],
  279. [ '21', '23' ],
  280. [ '31', '32', '33' ]
  281. ] ),
  282. writer => {
  283. _setAttribute( writer, 'headingRows', 1, [ 0 ] );
  284. _setAttribute( writer, 'rowspan', 1, [ 0, 0, 1 ] );
  285. _insertCell( writer, 1, 1 );
  286. },
  287. writer => {
  288. _removeRow( writer, 1 );
  289. },
  290. formattedModelTable( [
  291. [ '11', { rowspan: 1, contents: '12' }, '13' ],
  292. [ '31', '32', '33' ]
  293. ], { headingRows: 1 } ),
  294. formattedModelTable( [
  295. [ '11', { rowspan: 2, contents: '12' }, '13', '' ],
  296. [ '31', '32', '33' ]
  297. ] ) );
  298. } );
  299. it( 'collab remove row vs change table header rows', () => {
  300. _testExternal(
  301. modelTable( [
  302. [ '11', { rowspan: 2, contents: '12' }, '13' ],
  303. [ '21', '23' ],
  304. [ '31', '32', '33' ]
  305. ] ),
  306. writer => {
  307. _removeRow( writer, 1 );
  308. },
  309. writer => {
  310. _setAttribute( writer, 'headingRows', 1, [ 0 ] );
  311. _setAttribute( writer, 'rowspan', 1, [ 0, 0, 1 ] );
  312. },
  313. formattedModelTable( [
  314. [ '11', { rowspan: 1, contents: '12' }, '13', '' ],
  315. [ '31', '32', '33', '' ]
  316. ], { headingRows: 1 } ),
  317. formattedModelTable( [
  318. [ '11', { rowspan: 1, contents: '12' }, '13', '' ],
  319. [ '21', '23', '', '' ],
  320. [ '31', '32', '33', '' ]
  321. ], { headingRows: 1 } ) );
  322. } );
  323. // Case: remove same column (undo does nothing on one client - NOOP in batch).
  324. // Case: remove same row (undo does nothing on one client - NOOP in batch).
  325. // Case: Typing over user selecting - typing in marker...
  326. function _testExternal( initialData, localCallback, externalCallback, modelAfter, modelAfterUndo ) {
  327. setModelData( model, initialData );
  328. model.change( localCallback );
  329. model.enqueueChange( 'transparent', externalCallback );
  330. expect( formatTable( getModelData( model, { withoutSelection: true } ) ), 'after operations' ).to.equal( modelAfter );
  331. editor.execute( 'undo' );
  332. expect( formatTable( getModelData( model, { withoutSelection: true } ) ), 'after undo' ).to.equal( modelAfterUndo );
  333. editor.execute( 'redo' );
  334. expect( formatTable( getModelData( model, { withoutSelection: true } ) ), 'after redo' ).to.equal( modelAfter );
  335. }
  336. function _removeColumn( writer, columnIndex, rows ) {
  337. const table = root.getChild( 0 );
  338. for ( const index of rows ) {
  339. const tableRow = table.getChild( index );
  340. const tableCell = tableRow.getChild( columnIndex );
  341. writer.remove( tableCell );
  342. }
  343. }
  344. function _removeRow( writer, rowIndex ) {
  345. const table = root.getChild( 0 );
  346. const tableRow = table.getChild( rowIndex );
  347. writer.remove( tableRow );
  348. }
  349. function _insertRow( writer, rowIndex, rowData ) {
  350. const table = root.getChild( 0 );
  351. const parsedTable = parse(
  352. modelTable( [ rowData ] ),
  353. model.schema
  354. );
  355. writer.insert( parsedTable.getChild( 0 ), table, rowIndex );
  356. }
  357. function _insertCell( writer, rowIndex, index ) {
  358. const table = root.getChild( 0 );
  359. const tableRow = table.getChild( rowIndex );
  360. writer.insertElement( 'tableCell', tableRow, index );
  361. }
  362. function _setAttribute( writer, attributeKey, attributeValue, path ) {
  363. const node = root.getNodeByPath( path );
  364. writer.setAttribute( attributeKey, attributeValue, node );
  365. }
  366. function _insertColumn( writer, columnIndex, rows ) {
  367. const table = root.getChild( 0 );
  368. for ( const index of rows ) {
  369. const tableRow = table.getChild( index );
  370. const tableCell = writer.createElement( 'tableCell' );
  371. writer.insert( tableCell, tableRow, columnIndex );
  372. }
  373. }
  374. } );
  375. } );