8
0

table-post-fixer.js 12 KB

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