8
0

tableutils.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  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 ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import { defaultConversion, defaultSchema, modelTable } from './_utils/utils';
  8. import TableEditing from '../src/tableediting';
  9. import TableUtils from '../src/tableutils';
  10. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  11. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  12. describe( 'TableUtils', () => {
  13. let editor, model, root, tableUtils;
  14. afterEach( () => {
  15. return editor.destroy();
  16. } );
  17. describe( '#pluginName', () => {
  18. beforeEach( () => {
  19. return ModelTestEditor.create( {
  20. plugins: [ TableUtils ]
  21. } ).then( newEditor => {
  22. editor = newEditor;
  23. model = editor.model;
  24. root = model.document.getRoot( 'main' );
  25. tableUtils = editor.plugins.get( TableUtils );
  26. defaultSchema( model.schema );
  27. defaultConversion( editor.conversion );
  28. } );
  29. } );
  30. it( 'should provide plugin name', () => {
  31. expect( TableUtils.pluginName ).to.equal( 'TableUtils' );
  32. } );
  33. } );
  34. describe( 'getCellLocation()', () => {
  35. beforeEach( () => {
  36. return ModelTestEditor.create( {
  37. plugins: [ TableUtils ]
  38. } ).then( newEditor => {
  39. editor = newEditor;
  40. model = editor.model;
  41. root = model.document.getRoot( 'main' );
  42. tableUtils = editor.plugins.get( TableUtils );
  43. defaultSchema( model.schema );
  44. defaultConversion( editor.conversion );
  45. } );
  46. } );
  47. it( 'should return proper table cell location', () => {
  48. setData( model, modelTable( [
  49. [ { rowspan: 2, colspan: 2, contents: '00[]' }, '02' ],
  50. [ '12' ]
  51. ] ) );
  52. expect( tableUtils.getCellLocation( root.getNodeByPath( [ 0, 0, 0 ] ) ) ).to.deep.equal( { row: 0, column: 0 } );
  53. expect( tableUtils.getCellLocation( root.getNodeByPath( [ 0, 0, 1 ] ) ) ).to.deep.equal( { row: 0, column: 2 } );
  54. expect( tableUtils.getCellLocation( root.getNodeByPath( [ 0, 1, 0 ] ) ) ).to.deep.equal( { row: 1, column: 2 } );
  55. } );
  56. } );
  57. describe( 'insertRows()', () => {
  58. beforeEach( () => {
  59. return ModelTestEditor.create( {
  60. plugins: [ TableUtils ]
  61. } ).then( newEditor => {
  62. editor = newEditor;
  63. model = editor.model;
  64. root = model.document.getRoot( 'main' );
  65. tableUtils = editor.plugins.get( TableUtils );
  66. defaultSchema( model.schema );
  67. defaultConversion( editor.conversion );
  68. } );
  69. } );
  70. it( 'should insert row in given table at given index', () => {
  71. setData( model, modelTable( [
  72. [ '11[]', '12' ],
  73. [ '21', '22' ]
  74. ] ) );
  75. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  76. assertEqualMarkup( getData( model ), modelTable( [
  77. [ '11[]', '12' ],
  78. [ '', '' ],
  79. [ '21', '22' ]
  80. ] ) );
  81. } );
  82. it( 'should insert row in given table at default index', () => {
  83. setData( model, modelTable( [
  84. [ '11[]', '12' ],
  85. [ '21', '22' ]
  86. ] ) );
  87. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ) );
  88. assertEqualMarkup( getData( model ), modelTable( [
  89. [ '', '' ],
  90. [ '11[]', '12' ],
  91. [ '21', '22' ]
  92. ] ) );
  93. } );
  94. it( 'should update table heading rows attribute when inserting row in headings section', () => {
  95. setData( model, modelTable( [
  96. [ '11[]', '12' ],
  97. [ '21', '22' ],
  98. [ '31', '32' ]
  99. ], { headingRows: 2 } ) );
  100. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  101. assertEqualMarkup( getData( model ), modelTable( [
  102. [ '11[]', '12' ],
  103. [ '', '' ],
  104. [ '21', '22' ],
  105. [ '31', '32' ]
  106. ], { headingRows: 3 } ) );
  107. } );
  108. it( 'should not update table heading rows attribute when inserting row after headings section', () => {
  109. setData( model, modelTable( [
  110. [ '11[]', '12' ],
  111. [ '21', '22' ],
  112. [ '31', '32' ]
  113. ], { headingRows: 2 } ) );
  114. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 2 } );
  115. assertEqualMarkup( getData( model ), modelTable( [
  116. [ '11[]', '12' ],
  117. [ '21', '22' ],
  118. [ '', '' ],
  119. [ '31', '32' ]
  120. ], { headingRows: 2 } ) );
  121. } );
  122. it( 'should expand rowspan of a cell that overlaps inserted rows', () => {
  123. setData( model, modelTable( [
  124. [ { colspan: 2, contents: '11[]' }, '13', '14' ],
  125. [ { colspan: 2, rowspan: 4, contents: '21' }, '23', '24' ],
  126. [ '33', '34' ]
  127. ], { headingColumns: 3, headingRows: 1 } ) );
  128. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 2, rows: 3 } );
  129. assertEqualMarkup( getData( model ), modelTable( [
  130. [ { colspan: 2, contents: '11[]' }, '13', '14' ],
  131. [ { colspan: 2, rowspan: 7, contents: '21' }, '23', '24' ],
  132. [ '', '' ],
  133. [ '', '' ],
  134. [ '', '' ],
  135. [ '33', '34' ]
  136. ], { headingColumns: 3, headingRows: 1 } ) );
  137. } );
  138. it( 'should not expand rowspan of a cell that does not overlaps inserted rows', () => {
  139. setData( model, modelTable( [
  140. [ { rowspan: 2, contents: '11[]' }, '12', '13' ],
  141. [ '22', '23' ],
  142. [ '31', '32', '33' ]
  143. ], { headingColumns: 3, headingRows: 1 } ) );
  144. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 2, rows: 3 } );
  145. assertEqualMarkup( getData( model ), modelTable( [
  146. [ { rowspan: 2, contents: '11[]' }, '12', '13' ],
  147. [ '22', '23' ],
  148. [ '', '', '' ],
  149. [ '', '', '' ],
  150. [ '', '', '' ],
  151. [ '31', '32', '33' ]
  152. ], { headingColumns: 3, headingRows: 1 } ) );
  153. } );
  154. it( 'should properly calculate columns if next row has colspans', () => {
  155. setData( model, modelTable( [
  156. [ { rowspan: 2, contents: '11[]' }, '12', '13' ],
  157. [ '22', '23' ],
  158. [ { colspan: 3, contents: '31' } ]
  159. ], { headingColumns: 3, headingRows: 1 } ) );
  160. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 2, rows: 3 } );
  161. assertEqualMarkup( getData( model ), modelTable( [
  162. [ { rowspan: 2, contents: '11[]' }, '12', '13' ],
  163. [ '22', '23' ],
  164. [ '', '', '' ],
  165. [ '', '', '' ],
  166. [ '', '', '' ],
  167. [ { colspan: 3, contents: '31' } ]
  168. ], { headingColumns: 3, headingRows: 1 } ) );
  169. } );
  170. it( 'should insert rows at the end of a table', () => {
  171. setData( model, modelTable( [
  172. [ '11[]', '12' ],
  173. [ '21', '22' ]
  174. ] ) );
  175. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 2, rows: 3 } );
  176. assertEqualMarkup( getData( model ), modelTable( [
  177. [ '11[]', '12' ],
  178. [ '21', '22' ],
  179. [ '', '' ],
  180. [ '', '' ],
  181. [ '', '' ]
  182. ] ) );
  183. } );
  184. } );
  185. describe( 'insertColumns()', () => {
  186. beforeEach( () => {
  187. return ModelTestEditor.create( {
  188. plugins: [ TableUtils ]
  189. } ).then( newEditor => {
  190. editor = newEditor;
  191. model = editor.model;
  192. root = model.document.getRoot( 'main' );
  193. tableUtils = editor.plugins.get( TableUtils );
  194. defaultSchema( model.schema );
  195. defaultConversion( editor.conversion );
  196. } );
  197. } );
  198. it( 'should insert column in given table at given index', () => {
  199. setData( model, modelTable( [
  200. [ '11[]', '12' ],
  201. [ '21', '22' ]
  202. ] ) );
  203. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  204. assertEqualMarkup( getData( model ), modelTable( [
  205. [ '11[]', '', '12' ],
  206. [ '21', '', '22' ]
  207. ] ) );
  208. } );
  209. it( 'should insert column in given table with default values', () => {
  210. setData( model, modelTable( [
  211. [ '11[]', '12' ],
  212. [ '21', '22' ]
  213. ] ) );
  214. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ) );
  215. assertEqualMarkup( getData( model ), modelTable( [
  216. [ '', '11[]', '12' ],
  217. [ '', '21', '22' ]
  218. ] ) );
  219. } );
  220. it( 'should insert column in given table at default index', () => {
  221. setData( model, modelTable( [
  222. [ '11[]', '12' ],
  223. [ '21', '22' ]
  224. ] ) );
  225. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ) );
  226. assertEqualMarkup( getData( model ), modelTable( [
  227. [ '', '11[]', '12' ],
  228. [ '', '21', '22' ]
  229. ] ) );
  230. } );
  231. it( 'should insert columns at the end of a row', () => {
  232. setData( model, modelTable( [
  233. [ '00[]', '01' ],
  234. [ { colspan: 2, contents: '10' } ],
  235. [ '20', { rowspan: 2, contents: '21' } ],
  236. [ '30' ]
  237. ] ) );
  238. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 2, columns: 2 } );
  239. assertEqualMarkup( getData( model ), modelTable( [
  240. [ '00[]', '01', '', '' ],
  241. [ { colspan: 2, contents: '10' }, '', '' ],
  242. [ '20', { rowspan: 2, contents: '21' }, '', '' ],
  243. [ '30', '', '' ]
  244. ] ) );
  245. } );
  246. it( 'should insert columns at the beginning of a row', () => {
  247. setData( model, modelTable( [
  248. [ '00[]', '01' ],
  249. [ { colspan: 2, contents: '10' } ],
  250. [ '20', { rowspan: 2, contents: '21' } ],
  251. [ { rowspan: 2, contents: '30' } ],
  252. [ '41' ]
  253. ] ) );
  254. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 0, columns: 2 } );
  255. assertEqualMarkup( getData( model ), modelTable( [
  256. [ '', '', '00[]', '01' ],
  257. [ '', '', { colspan: 2, contents: '10' } ],
  258. [ '', '', '20', { rowspan: 2, contents: '21' } ],
  259. [ '', '', { rowspan: 2, contents: '30' } ],
  260. [ '', '', '41' ]
  261. ] ) );
  262. } );
  263. it( 'should properly insert column at beginning of row-col-spanned cell', () => {
  264. setData( model, modelTable( [
  265. [ '11', '12', '13' ],
  266. [ '21', { colspan: 2, rowspan: 2, contents: '22[]' } ],
  267. [ '31' ],
  268. [ '41', '42', '43' ]
  269. ] ) );
  270. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 1, columns: 1 } );
  271. assertEqualMarkup( getData( model ), modelTable( [
  272. [ '11', '', '12', '13' ],
  273. [ '21', '', { colspan: 2, rowspan: 2, contents: '22[]' } ],
  274. [ '31', '' ],
  275. [ '41', '', '42', '43' ]
  276. ] ) );
  277. } );
  278. it( 'should update table heading columns attribute when inserting column in headings section', () => {
  279. setData( model, modelTable( [
  280. [ '11[]', '12' ],
  281. [ '21', '22' ],
  282. [ '31', '32' ]
  283. ], { headingColumns: 2 } ) );
  284. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  285. assertEqualMarkup( getData( model ), modelTable( [
  286. [ '11[]', '', '12' ],
  287. [ '21', '', '22' ],
  288. [ '31', '', '32' ]
  289. ], { headingColumns: 3 } ) );
  290. } );
  291. it( 'should not update table heading columns attribute when inserting column after headings section', () => {
  292. setData( model, modelTable( [
  293. [ '11[]', '12', '13' ],
  294. [ '21', '22', '23' ],
  295. [ '31', '32', '33' ]
  296. ], { headingColumns: 2 } ) );
  297. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 2 } );
  298. assertEqualMarkup( getData( model ), modelTable( [
  299. [ '11[]', '12', '', '13' ],
  300. [ '21', '22', '', '23' ],
  301. [ '31', '32', '', '33' ]
  302. ], { headingColumns: 2 } ) );
  303. } );
  304. it( 'should expand spanned columns', () => {
  305. setData( model, modelTable( [
  306. [ '00[]', '01' ],
  307. [ { colspan: 2, contents: '10' } ],
  308. [ '20', '21' ]
  309. ], { headingColumns: 2 } ) );
  310. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  311. assertEqualMarkup( getData( model ), modelTable( [
  312. [ '00[]', '', '01' ],
  313. [ { colspan: 3, contents: '10' } ],
  314. [ '20', '', '21' ]
  315. ], { headingColumns: 3 } ) );
  316. } );
  317. it( 'should skip wide spanned columns', () => {
  318. setData( model, modelTable( [
  319. [ '11[]', '12', '13', '14', '15' ],
  320. [ '21', '22', { colspan: 2, contents: '23' }, '25' ],
  321. [ { colspan: 4, contents: '31' }, { colspan: 2, contents: '34' } ]
  322. ], { headingColumns: 4 } ) );
  323. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 2, columns: 2 } );
  324. assertEqualMarkup( getData( model ), modelTable( [
  325. [ '11[]', '12', '', '', '13', '14', '15' ],
  326. [ '21', '22', '', '', { colspan: 2, contents: '23' }, '25' ],
  327. [ { colspan: 6, contents: '31' }, { colspan: 2, contents: '34' } ]
  328. ], { headingColumns: 6 } ) );
  329. } );
  330. it( 'should skip row & column spanned cells', () => {
  331. setData( model, modelTable( [
  332. [ { colspan: 2, rowspan: 2, contents: '00[]' }, '02' ],
  333. [ '12' ],
  334. [ '20', '21', '22' ]
  335. ], { headingColumns: 2 } ) );
  336. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 1, columns: 2 } );
  337. assertEqualMarkup( getData( model ), modelTable( [
  338. [ { colspan: 4, rowspan: 2, contents: '00[]' }, '02' ],
  339. [ '12' ],
  340. [ '20', '', '', '21', '22' ]
  341. ], { headingColumns: 4 } ) );
  342. } );
  343. it( 'should properly insert column while table has rowspanned cells', () => {
  344. setData( model, modelTable( [
  345. [ { rowspan: 4, contents: '00[]' }, { rowspan: 2, contents: '01' }, '02' ],
  346. [ '12' ],
  347. [ { rowspan: 2, contents: '21' }, '22' ],
  348. [ '32' ]
  349. ], { headingColumns: 2 } ) );
  350. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 1, columns: 1 } );
  351. assertEqualMarkup( getData( model ), modelTable( [
  352. [ { rowspan: 4, contents: '00[]' }, '', { rowspan: 2, contents: '01' }, '02' ],
  353. [ '', '12' ],
  354. [ '', { rowspan: 2, contents: '21' }, '22' ],
  355. [ '', '32' ]
  356. ], { headingColumns: 3 } ) );
  357. } );
  358. } );
  359. describe( 'splitCellVertically()', () => {
  360. beforeEach( () => {
  361. return ModelTestEditor.create( {
  362. plugins: [ TableUtils ]
  363. } ).then( newEditor => {
  364. editor = newEditor;
  365. model = editor.model;
  366. root = model.document.getRoot( 'main' );
  367. tableUtils = editor.plugins.get( TableUtils );
  368. defaultSchema( model.schema );
  369. defaultConversion( editor.conversion );
  370. } );
  371. } );
  372. it( 'should split table cell to given table cells number', () => {
  373. setData( model, modelTable( [
  374. [ '00', '01', '02' ],
  375. [ '10', '[]11', '12' ],
  376. [ '20', { colspan: 2, contents: '21' } ],
  377. [ { colspan: 2, contents: '30' }, '32' ]
  378. ] ) );
  379. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 1 ] ), 3 );
  380. assertEqualMarkup( getData( model ), modelTable( [
  381. [ '00', { colspan: 3, contents: '01' }, '02' ],
  382. [ '10', '[]11', '', '', '12' ],
  383. [ '20', { colspan: 4, contents: '21' } ],
  384. [ { colspan: 4, contents: '30' }, '32' ]
  385. ] ) );
  386. } );
  387. it( 'should split table cell for two table cells as a default', () => {
  388. setData( model, modelTable( [
  389. [ '00', '01', '02' ],
  390. [ '10', '[]11', '12' ],
  391. [ '20', { colspan: 2, contents: '21' } ],
  392. [ { colspan: 2, contents: '30' }, '32' ]
  393. ] ) );
  394. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 1 ] ) );
  395. assertEqualMarkup( getData( model ), modelTable( [
  396. [ '00', { colspan: 2, contents: '01' }, '02' ],
  397. [ '10', '[]11', '', '12' ],
  398. [ '20', { colspan: 3, contents: '21' } ],
  399. [ { colspan: 3, contents: '30' }, '32' ]
  400. ] ) );
  401. } );
  402. it( 'should split table cell if split is equal to colspan', () => {
  403. setData( model, modelTable( [
  404. [ '00', '01', '02' ],
  405. [ '10', '11', '12' ],
  406. [ '20', { colspan: 2, contents: '21[]' } ],
  407. [ { colspan: 2, contents: '30' }, '32' ]
  408. ] ) );
  409. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 2, 1 ] ), 2 );
  410. assertEqualMarkup( getData( model ), modelTable( [
  411. [ '00', '01', '02' ],
  412. [ '10', '11', '12' ],
  413. [ '20', '21[]', '' ],
  414. [ { colspan: 2, contents: '30' }, '32' ]
  415. ] ) );
  416. } );
  417. it( 'should properly split table cell if split is uneven', () => {
  418. setData( model, modelTable( [
  419. [ '00', '01', '02' ],
  420. [ { colspan: 3, contents: '10[]' } ]
  421. ] ) );
  422. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 0 ] ), 2 );
  423. assertEqualMarkup( getData( model ), modelTable( [
  424. [ '00', '01', '02' ],
  425. [ { colspan: 2, contents: '10[]' }, '' ]
  426. ] ) );
  427. } );
  428. it( 'should properly set colspan of inserted cells', () => {
  429. setData( model, modelTable( [
  430. [ '00', '01', '02', '03' ],
  431. [ { colspan: 4, contents: '10[]' } ]
  432. ] ) );
  433. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 0 ] ), 2 );
  434. assertEqualMarkup( getData( model ), modelTable( [
  435. [ '00', '01', '02', '03' ],
  436. [ { colspan: 2, contents: '10[]' }, { colspan: 2, contents: '' } ]
  437. ] ) );
  438. } );
  439. it( 'should keep rowspan attribute for newly inserted cells', () => {
  440. setData( model, modelTable( [
  441. [ '00', '01', '02', '03', '04', '05' ],
  442. [ { colspan: 5, rowspan: 2, contents: '10[]' }, '15' ],
  443. [ '25' ]
  444. ] ) );
  445. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 0 ] ), 2 );
  446. assertEqualMarkup( getData( model ), modelTable( [
  447. [ '00', '01', '02', '03', '04', '05' ],
  448. [ { colspan: 3, rowspan: 2, contents: '10[]' }, { colspan: 2, rowspan: 2, contents: '' }, '15' ],
  449. [ '25' ]
  450. ] ) );
  451. } );
  452. it( 'should keep rowspan attribute of for newly inserted cells if number of cells is bigger then curren colspan', () => {
  453. setData( model, modelTable( [
  454. [ '00', '01', '02' ],
  455. [ { colspan: 2, rowspan: 2, contents: '10[]' }, '12' ],
  456. [ '22' ]
  457. ] ) );
  458. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 0 ] ), 3 );
  459. assertEqualMarkup( getData( model ), modelTable( [
  460. [ { colspan: 2, contents: '00' }, '01', '02' ],
  461. [ { rowspan: 2, contents: '10[]' }, { rowspan: 2, contents: '' }, { rowspan: 2, contents: '' }, '12' ],
  462. [ '22' ]
  463. ] ) );
  464. } );
  465. it( 'should properly break a cell if it has colspan and number of created cells is bigger then colspan', () => {
  466. setData( model, modelTable( [
  467. [ '00', '01', '02', '03' ],
  468. [ { colspan: 4, contents: '10[]' } ]
  469. ] ) );
  470. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 0 ] ), 6 );
  471. assertEqualMarkup( getData( model ), modelTable( [
  472. [ { colspan: 3, contents: '00' }, '01', '02', '03' ],
  473. [ '10[]', '', '', '', '', '' ]
  474. ] ) );
  475. } );
  476. it( 'should update heading columns is split cell is in heading section', () => {
  477. setData( model, modelTable( [
  478. [ '00', '01' ],
  479. [ '10[]', '11' ]
  480. ], { headingColumns: 1 } ) );
  481. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 0 ] ), 3 );
  482. assertEqualMarkup( getData( model ), modelTable( [
  483. [ { colspan: 3, contents: '00' }, '01' ],
  484. [ '10[]', '', '', '11' ]
  485. ], { headingColumns: 3 } ) );
  486. } );
  487. } );
  488. describe( 'splitCellHorizontally()', () => {
  489. beforeEach( () => {
  490. return ModelTestEditor.create( {
  491. plugins: [ TableUtils ]
  492. } ).then( newEditor => {
  493. editor = newEditor;
  494. model = editor.model;
  495. root = model.document.getRoot( 'main' );
  496. tableUtils = editor.plugins.get( TableUtils );
  497. defaultSchema( model.schema );
  498. defaultConversion( editor.conversion );
  499. } );
  500. } );
  501. it( 'should split table cell to default table cells number', () => {
  502. setData( model, modelTable( [
  503. [ '00', '01', '02' ],
  504. [ '10', '[]11', '12' ],
  505. [ '20', '21', '22' ]
  506. ] ) );
  507. tableUtils.splitCellHorizontally( root.getNodeByPath( [ 0, 1, 1 ] ) );
  508. assertEqualMarkup( getData( model ), modelTable( [
  509. [ '00', '01', '02' ],
  510. [ { rowspan: 2, contents: '10' }, '[]11', { rowspan: 2, contents: '12' } ],
  511. [ '' ],
  512. [ '20', '21', '22' ]
  513. ] ) );
  514. } );
  515. it( 'should split table cell to given table cells number', () => {
  516. setData( model, modelTable( [
  517. [ '00', '01', '02' ],
  518. [ '10', '[]11', '12' ],
  519. [ '20', '21', '22' ]
  520. ] ) );
  521. tableUtils.splitCellHorizontally( root.getNodeByPath( [ 0, 1, 1 ] ), 4 );
  522. assertEqualMarkup( getData( model ), modelTable( [
  523. [ '00', '01', '02' ],
  524. [ { rowspan: 4, contents: '10' }, '[]11', { rowspan: 4, contents: '12' } ],
  525. [ '' ],
  526. [ '' ],
  527. [ '' ],
  528. [ '20', '21', '22' ]
  529. ] ) );
  530. } );
  531. it( 'should properly update rowspanned cells overlapping selected cell', () => {
  532. setData( model, modelTable( [
  533. [ { rowspan: 2, contents: '00' }, '01', { rowspan: 3, contents: '02' } ],
  534. [ '[]11' ],
  535. [ '20', '21' ]
  536. ] ) );
  537. tableUtils.splitCellHorizontally( root.getNodeByPath( [ 0, 1, 0 ] ), 3 );
  538. assertEqualMarkup( getData( model ), modelTable( [
  539. [ { rowspan: 4, contents: '00' }, '01', { rowspan: 5, contents: '02' } ],
  540. [ '[]11' ],
  541. [ '' ],
  542. [ '' ],
  543. [ '20', '21' ]
  544. ] ) );
  545. } );
  546. it( 'should split rowspanned cell', () => {
  547. setData( model, modelTable( [
  548. [ '00', { rowspan: 2, contents: '01[]' } ],
  549. [ '10' ],
  550. [ '20', '21' ]
  551. ] ) );
  552. const tableCell = root.getNodeByPath( [ 0, 0, 1 ] );
  553. tableUtils.splitCellHorizontally( tableCell, 2 );
  554. assertEqualMarkup( getData( model ), modelTable( [
  555. [ '00', '01[]' ],
  556. [ '10', '' ],
  557. [ '20', '21' ]
  558. ] ) );
  559. } );
  560. it( 'should copy colspan while splitting rowspanned cell', () => {
  561. setData( model, modelTable( [
  562. [ '00', { rowspan: 2, colspan: 2, contents: '01[]' } ],
  563. [ '10' ],
  564. [ '20', '21', '22' ]
  565. ] ) );
  566. const tableCell = root.getNodeByPath( [ 0, 0, 1 ] );
  567. tableUtils.splitCellHorizontally( tableCell, 2 );
  568. assertEqualMarkup( getData( model ), modelTable( [
  569. [ '00', { colspan: 2, contents: '01[]' } ],
  570. [ '10', { colspan: 2, contents: '' } ],
  571. [ '20', '21', '22' ]
  572. ] ) );
  573. } );
  574. it( 'should evenly distribute rowspan attribute', () => {
  575. setData( model, modelTable( [
  576. [ '00', { rowspan: 7, contents: '01[]' } ],
  577. [ '10' ],
  578. [ '20' ],
  579. [ '30' ],
  580. [ '40' ],
  581. [ '50' ],
  582. [ '60' ],
  583. [ '70', '71' ]
  584. ] ) );
  585. const tableCell = root.getNodeByPath( [ 0, 0, 1 ] );
  586. tableUtils.splitCellHorizontally( tableCell, 3 );
  587. assertEqualMarkup( getData( model ), modelTable( [
  588. [ '00', { rowspan: 3, contents: '01[]' } ],
  589. [ '10' ],
  590. [ '20' ],
  591. [ '30', { rowspan: 2, contents: '' } ],
  592. [ '40' ],
  593. [ '50', { rowspan: 2, contents: '' } ],
  594. [ '60' ],
  595. [ '70', '71' ]
  596. ] ) );
  597. } );
  598. it( 'should split rowspanned cell and updated other cells rowspan when splitting to bigger number of cells', () => {
  599. setData( model, modelTable( [
  600. [ '00', { rowspan: 2, contents: '01[]' } ],
  601. [ '10' ],
  602. [ '20', '21' ]
  603. ] ) );
  604. const tableCell = root.getNodeByPath( [ 0, 0, 1 ] );
  605. tableUtils.splitCellHorizontally( tableCell, 3 );
  606. assertEqualMarkup( getData( model ), modelTable( [
  607. [ { rowspan: 2, contents: '00' }, '01[]' ],
  608. [ '' ],
  609. [ '10', '' ],
  610. [ '20', '21' ]
  611. ] ) );
  612. } );
  613. it( 'should split rowspanned & colspaned cell', () => {
  614. setData( model, modelTable( [
  615. [ '00', { colspan: 2, contents: '01[]' } ],
  616. [ '10', '11' ]
  617. ] ) );
  618. const tableCell = root.getNodeByPath( [ 0, 0, 1 ] );
  619. tableUtils.splitCellHorizontally( tableCell, 3 );
  620. assertEqualMarkup( getData( model ), modelTable( [
  621. [ { rowspan: 3, contents: '00' }, { colspan: 2, contents: '01[]' } ],
  622. [ { colspan: 2, contents: '' } ],
  623. [ { colspan: 2, contents: '' } ],
  624. [ '10', '11' ]
  625. ] ) );
  626. } );
  627. it( 'should split table cell from a heading section', () => {
  628. setData( model, modelTable( [
  629. [ '00[]', '01', '02' ],
  630. [ '10', '11', '12' ],
  631. [ '20', '21', '22' ]
  632. ], { headingRows: 1 } ) );
  633. tableUtils.splitCellHorizontally( root.getNodeByPath( [ 0, 0, 0 ] ), 3 );
  634. assertEqualMarkup( getData( model ), modelTable( [
  635. [ '00[]', { rowspan: 3, contents: '01' }, { rowspan: 3, contents: '02' } ],
  636. [ '' ],
  637. [ '' ],
  638. [ '10', '11', '12' ],
  639. [ '20', '21', '22' ]
  640. ], { headingRows: 3 } ) );
  641. } );
  642. } );
  643. describe( 'getColumns()', () => {
  644. beforeEach( () => {
  645. return ModelTestEditor.create( {
  646. plugins: [ TableUtils ]
  647. } ).then( newEditor => {
  648. editor = newEditor;
  649. model = editor.model;
  650. root = model.document.getRoot( 'main' );
  651. tableUtils = editor.plugins.get( TableUtils );
  652. defaultSchema( model.schema );
  653. defaultConversion( editor.conversion );
  654. } );
  655. } );
  656. it( 'should return proper number of columns', () => {
  657. setData( model, modelTable( [
  658. [ '00', { colspan: 3, contents: '01' }, '04' ]
  659. ] ) );
  660. expect( tableUtils.getColumns( root.getNodeByPath( [ 0 ] ) ) ).to.equal( 5 );
  661. } );
  662. } );
  663. describe( 'getRows()', () => {
  664. beforeEach( () => {
  665. return ModelTestEditor.create( {
  666. plugins: [ TableUtils ]
  667. } ).then( newEditor => {
  668. editor = newEditor;
  669. model = editor.model;
  670. root = model.document.getRoot( 'main' );
  671. tableUtils = editor.plugins.get( TableUtils );
  672. defaultSchema( model.schema );
  673. defaultConversion( editor.conversion );
  674. } );
  675. } );
  676. it( 'should return proper number of columns for simple table', () => {
  677. setData( model, modelTable( [
  678. [ '00', '01' ],
  679. [ '10', '11' ]
  680. ] ) );
  681. expect( tableUtils.getRows( root.getNodeByPath( [ 0 ] ) ) ).to.equal( 2 );
  682. } );
  683. it( 'should return proper number of columns for a table with header', () => {
  684. setData( model, modelTable( [
  685. [ '00', '01' ],
  686. [ '10', '11' ]
  687. ], { headingRows: 1 } ) );
  688. expect( tableUtils.getRows( root.getNodeByPath( [ 0 ] ) ) ).to.equal( 2 );
  689. } );
  690. it( 'should return proper number of columns for rowspan table', () => {
  691. setData( model, modelTable( [
  692. [ '00', '01' ],
  693. [ { rowspan: 2, contents: '10' }, '11' ],
  694. [ '21' ]
  695. ] ) );
  696. expect( tableUtils.getRows( root.getNodeByPath( [ 0 ] ) ) ).to.equal( 3 );
  697. } );
  698. } );
  699. describe( 'removeRows()', () => {
  700. beforeEach( () => {
  701. return ModelTestEditor.create( {
  702. plugins: [ Paragraph, TableEditing, TableUtils ]
  703. } ).then( newEditor => {
  704. editor = newEditor;
  705. model = editor.model;
  706. root = model.document.getRoot( 'main' );
  707. tableUtils = editor.plugins.get( TableUtils );
  708. } );
  709. } );
  710. describe( 'single row', () => {
  711. it( 'should remove a given row from a table start', () => {
  712. setData( model, modelTable( [
  713. [ '00', '01' ],
  714. [ '10', '11' ],
  715. [ '20', '21' ]
  716. ] ) );
  717. tableUtils.removeRows( root.getChild( 0 ), { at: 0 } );
  718. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  719. [ '10', '11' ],
  720. [ '20', '21' ]
  721. ] ) );
  722. } );
  723. it( 'should remove last row', () => {
  724. setData( model, modelTable( [
  725. [ '00', '01' ],
  726. [ '10', '11' ]
  727. ] ) );
  728. tableUtils.removeRows( root.getChild( 0 ), { at: 1 } );
  729. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  730. [ '00', '01' ]
  731. ] ) );
  732. } );
  733. it( 'should change heading rows if removing a heading row', () => {
  734. setData( model, modelTable( [
  735. [ '00', '01' ],
  736. [ '10', '11' ],
  737. [ '20', '21' ]
  738. ], { headingRows: 2 } ) );
  739. tableUtils.removeRows( root.getChild( 0 ), { at: 1 } );
  740. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  741. [ '00', '01' ],
  742. [ '20', '21' ]
  743. ], { headingRows: 1 } ) );
  744. } );
  745. it( 'should decrease rowspan of table cells from previous rows', () => {
  746. // +----+----+----+----+----+
  747. // | 00 | 01 | 02 | 03 | 04 |
  748. // +----+ + + + +
  749. // | 10 | | | | |
  750. // +----+----+ + + +
  751. // | 20 | 21 | | | |
  752. // +----+----+----+ + +
  753. // | 30 | 31 | 32 | | |
  754. // +----+----+----+----+ +
  755. // | 40 | 41 | 42 | 43 | |
  756. // +----+----+----+----+----+
  757. // | 50 | 51 | 52 | 53 | 54 |
  758. // +----+----+----+----+----+
  759. setData( model, modelTable( [
  760. [ '00', { contents: '01', rowspan: 2 }, { contents: '02', rowspan: 3 }, { contents: '03', rowspan: 4 },
  761. { contents: '04', rowspan: 5 } ],
  762. [ '10' ],
  763. [ '20', '21' ],
  764. [ '30', '31', '32' ],
  765. [ '40', '41', '42', '43' ],
  766. [ '50', '51', '52', '53', '54' ]
  767. ] ) );
  768. tableUtils.removeRows( root.getChild( 0 ), { at: 1, rows: 1 } );
  769. // +----+----+----+----+----+
  770. // | 00 | 01 | 02 | 03 | 04 |
  771. // +----+----+ + + +
  772. // | 20 | 21 | | | |
  773. // +----+----+----+ + +
  774. // | 30 | 31 | 32 | | |
  775. // +----+----+----+----+ +
  776. // | 40 | 41 | 42 | 43 | |
  777. // +----+----+----+----+----+
  778. // | 50 | 51 | 52 | 53 | 54 |
  779. // +----+----+----+----+----+
  780. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  781. [ '00', '01', { contents: '02', rowspan: 2 }, { contents: '03', rowspan: 3 }, { contents: '04', rowspan: 4 } ],
  782. [ '20', '21' ],
  783. [ '30', '31', '32' ],
  784. [ '40', '41', '42', '43' ],
  785. [ '50', '51', '52', '53', '54' ]
  786. ] ) );
  787. } );
  788. it( 'should decrease rowspan of table cells from previous rows (rowspaned cells on different rows)', () => {
  789. setData( model, modelTable( [
  790. [ { rowspan: 4, contents: '00' }, { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  791. [ { rowspan: 2, contents: '13' }, '14' ],
  792. [ '22', '24' ],
  793. [ '31', '32', '33', '34' ]
  794. ] ) );
  795. tableUtils.removeRows( root.getChild( 0 ), { at: 2 } );
  796. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  797. [ { rowspan: 3, contents: '00' }, { rowspan: 2, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  798. [ '13', '14' ],
  799. [ '31', '32', '33', '34' ]
  800. ] ) );
  801. } );
  802. it( 'should move rowspaned cells to a row below removing it\'s row', () => {
  803. setData( model, modelTable( [
  804. [ { rowspan: 3, contents: '00' }, { rowspan: 2, contents: '01' }, '02' ],
  805. [ '12' ],
  806. [ '21', '22' ],
  807. [ '30', '31', '32' ]
  808. ] ) );
  809. tableUtils.removeRows( root.getChild( 0 ), { at: 0 } );
  810. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  811. [ { rowspan: 2, contents: '00' }, '01', '12' ],
  812. [ '21', '22' ],
  813. [ '30', '31', '32' ]
  814. ] ) );
  815. } );
  816. it( 'should move rowspaned cells to a row below removing it\'s row (other cell is overlapping removed row)', () => {
  817. setData( model, modelTable( [
  818. [ '00', { rowspan: 3, contents: '01' }, '02', '03', '04' ],
  819. [ '10', { rowspan: 2, contents: '12' }, '13', '14' ],
  820. [ '20', '23', '24' ]
  821. ] ) );
  822. tableUtils.removeRows( root.getChild( 0 ), { at: 1 } );
  823. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  824. [ '00', { rowspan: 2, contents: '01' }, '02', '03', '04' ],
  825. [ '20', '12', '23', '24' ]
  826. ] ) );
  827. } );
  828. } );
  829. describe( 'many rows', () => {
  830. it( 'should properly remove middle rows', () => {
  831. setData( model, modelTable( [
  832. [ '00', '01' ],
  833. [ '10', '11' ],
  834. [ '20', '21' ],
  835. [ '30', '31' ]
  836. ] ) );
  837. tableUtils.removeRows( root.getChild( 0 ), { at: 1, rows: 2 } );
  838. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  839. [ '00', '01' ],
  840. [ '30', '31' ]
  841. ] ) );
  842. } );
  843. it( 'should properly remove tailing rows', () => {
  844. setData( model, modelTable( [
  845. [ '00', '01' ],
  846. [ '10', '11' ],
  847. [ '20', '21' ],
  848. [ '30', '31' ]
  849. ] ) );
  850. tableUtils.removeRows( root.getChild( 0 ), { at: 2, rows: 2 } );
  851. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  852. [ '00', '01' ],
  853. [ '10', '11' ]
  854. ] ) );
  855. } );
  856. it( 'should properly remove beginning rows', () => {
  857. setData( model, modelTable( [
  858. [ '00', '01' ],
  859. [ '10', '11' ],
  860. [ '20', '21' ],
  861. [ '30', '31' ]
  862. ] ) );
  863. tableUtils.removeRows( root.getChild( 0 ), { at: 0, rows: 2 } );
  864. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  865. [ '20', '21' ],
  866. [ '30', '31' ]
  867. ] ) );
  868. } );
  869. it( 'should support removing multiple headings (removed rows in heading section)', () => {
  870. setData( model, modelTable( [
  871. [ '00', '01' ],
  872. [ '10', '11' ],
  873. [ '20', '21' ],
  874. [ '30', '31' ]
  875. ], { headingRows: 3 } ) );
  876. tableUtils.removeRows( root.getChild( 0 ), { at: 0, rows: 2 } );
  877. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  878. [ '20', '21' ],
  879. [ '30', '31' ]
  880. ], { headingRows: 1 } ) );
  881. } );
  882. it( 'should support removing multiple headings (removed rows in heading and body section)', () => {
  883. setData( model, modelTable( [
  884. [ '00', '01' ],
  885. [ '10', '11' ],
  886. [ '20', '21' ],
  887. [ '30', '31' ],
  888. [ '40', '41' ]
  889. ], { headingRows: 3 } ) );
  890. tableUtils.removeRows( root.getChild( 0 ), { at: 1, rows: 3 } );
  891. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  892. [ '00', '01' ],
  893. [ '40', '41' ]
  894. ], { headingRows: 1 } ) );
  895. } );
  896. it( 'should support removing mixed heading and cell rows', () => {
  897. setData( model, modelTable( [
  898. [ '00', '01' ],
  899. [ '10', '11' ],
  900. [ '20', '21' ]
  901. ], { headingRows: 1 } ) );
  902. tableUtils.removeRows( root.getChild( 0 ), { at: 0, rows: 2 } );
  903. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  904. [ '20', '21' ]
  905. ] ) );
  906. } );
  907. it( 'should move row-spaned cells to a row after removed rows section', () => {
  908. setData( model, modelTable( [
  909. [ '00', '01', '02', '03' ],
  910. [ { rowspan: 4, contents: '10' }, { rowspan: 3, contents: '11' }, { rowspan: 2, contents: '12' }, '13' ],
  911. [ { rowspan: 3, contents: '23' } ],
  912. [ '32' ],
  913. [ '41', '42' ]
  914. ] ) );
  915. tableUtils.removeRows( root.getChild( 0 ), { at: 1, rows: 2 } );
  916. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  917. [ '00', '01', '02', '03' ],
  918. [ { rowspan: 2, contents: '10' }, '11', '32', { rowspan: 2, contents: '23' } ],
  919. [ '41', '42' ]
  920. ] ) );
  921. } );
  922. it( 'should decrease rowspan of table cells from rows before removed rows section', () => {
  923. setData( model, modelTable( [
  924. [ { rowspan: 4, contents: '00' }, { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03' ],
  925. [ '13' ],
  926. [ '22', '23' ],
  927. [ '31', '32', '33' ]
  928. ] ) );
  929. tableUtils.removeRows( root.getChild( 0 ), { at: 1, rows: 2 } );
  930. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  931. [ { rowspan: 2, contents: '00' }, '01', '02', '03' ],
  932. [ '31', '32', '33' ]
  933. ] ) );
  934. } );
  935. it( 'should decrease rowspan of table cells from previous rows', () => {
  936. // +----+----+----+----+----+
  937. // | 00 | 01 | 02 | 03 | 04 |
  938. // +----+ + + + +
  939. // | 10 | | | | |
  940. // +----+----+ + + +
  941. // | 20 | 21 | | | |
  942. // +----+----+----+ + +
  943. // | 30 | 31 | 32 | | |
  944. // +----+----+----+----+ +
  945. // | 40 | 41 | 42 | 43 | |
  946. // +----+----+----+----+----+
  947. // | 50 | 51 | 52 | 53 | 44 |
  948. // +----+----+----+----+----+
  949. setData( model, modelTable( [
  950. [ '00', { contents: '01', rowspan: 2 }, { contents: '02', rowspan: 3 }, { contents: '03', rowspan: 4 },
  951. { contents: '04', rowspan: 5 } ],
  952. [ '10' ],
  953. [ '20', '21' ],
  954. [ '30', '31', '32' ],
  955. [ '40', '41', '42', '43' ],
  956. [ '50', '51', '52', '53', '54' ]
  957. ] ) );
  958. tableUtils.removeRows( root.getChild( 0 ), { at: 2, rows: 2 } );
  959. // +----+----+----+----+----+
  960. // | 00 | 01 | 02 | 03 | 04 |
  961. // +----+ + + + +
  962. // | 10 | | | | |
  963. // +----+----+----+----+ +
  964. // | 40 | 41 | 42 | 43 | |
  965. // +----+----+----+----+----+
  966. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  967. [ '00', { contents: '01', rowspan: 2 }, { contents: '02', rowspan: 2 }, { contents: '03', rowspan: 2 },
  968. { contents: '04', rowspan: 3 } ],
  969. [ '10' ],
  970. [ '40', '41', '42', '43' ],
  971. [ '50', '51', '52', '53', '54' ]
  972. ] ) );
  973. } );
  974. it( 'should create one undo step (1 batch)', () => {
  975. setData( model, modelTable( [
  976. [ '00', '01' ],
  977. [ '10', '11' ],
  978. [ '20', '21' ],
  979. [ '30', '31' ]
  980. ], { headingRows: 3 } ) );
  981. const createdBatches = new Set();
  982. model.on( 'applyOperation', ( evt, args ) => {
  983. const operation = args[ 0 ];
  984. createdBatches.add( operation.batch );
  985. } );
  986. tableUtils.removeRows( root.getChild( 0 ), { at: 0, rows: 2 } );
  987. expect( createdBatches.size ).to.equal( 1 );
  988. } );
  989. } );
  990. } );
  991. describe( 'removeColumns()', () => {
  992. beforeEach( () => {
  993. return ModelTestEditor.create( {
  994. plugins: [ TableUtils ]
  995. } ).then( newEditor => {
  996. editor = newEditor;
  997. model = editor.model;
  998. root = model.document.getRoot( 'main' );
  999. tableUtils = editor.plugins.get( TableUtils );
  1000. defaultSchema( model.schema );
  1001. defaultConversion( editor.conversion );
  1002. } );
  1003. } );
  1004. describe( 'single row', () => {
  1005. it( 'should remove a given column', () => {
  1006. setData( model, modelTable( [
  1007. [ '00', '01', '02' ],
  1008. [ '10', '11', '12' ],
  1009. [ '20', '21', '22' ]
  1010. ] ) );
  1011. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  1012. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1013. [ '00', '02' ],
  1014. [ '10', '12' ],
  1015. [ '20', '22' ]
  1016. ] ) );
  1017. } );
  1018. it( 'should remove a given column from a table start', () => {
  1019. setData( model, modelTable( [
  1020. [ '00', '01' ],
  1021. [ '10', '11' ],
  1022. [ '20', '21' ]
  1023. ] ) );
  1024. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 0 } );
  1025. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1026. [ '01' ],
  1027. [ '11' ],
  1028. [ '21' ]
  1029. ] ) );
  1030. } );
  1031. it( 'should change heading columns if removing a heading column', () => {
  1032. setData( model, modelTable( [
  1033. [ '00', '01' ],
  1034. [ '10', '11' ],
  1035. [ '20', '21' ]
  1036. ], { headingColumns: 2 } ) );
  1037. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 0 } );
  1038. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1039. [ '01' ],
  1040. [ '11' ],
  1041. [ '21' ]
  1042. ], { headingColumns: 1 } ) );
  1043. } );
  1044. it( 'should decrease colspan of table cells from previous column', () => {
  1045. setData( model, modelTable( [
  1046. [ { colspan: 4, contents: '00' }, '04' ],
  1047. [ { colspan: 3, contents: '10' }, '13', '14' ],
  1048. [ { colspan: 2, contents: '20' }, '22', '23', '24' ],
  1049. [ '30', { colspan: 2, contents: '31' }, '33', '34' ],
  1050. [ '40', '41', '42', '43', '44' ]
  1051. ] ) );
  1052. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 2 } );
  1053. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1054. [ { colspan: 3, contents: '00' }, '04' ],
  1055. [ { colspan: 2, contents: '10' }, '13', '14' ],
  1056. [ { colspan: 2, contents: '20' }, '23', '24' ],
  1057. [ '30', '31', '33', '34' ],
  1058. [ '40', '41', '43', '44' ]
  1059. ] ) );
  1060. } );
  1061. it( 'should decrease colspan of cells that are on removed column', () => {
  1062. setData( model, modelTable( [
  1063. [ { colspan: 3, contents: '00' }, '03' ],
  1064. [ { colspan: 2, contents: '10' }, '12', '13' ],
  1065. [ '20', '21', '22', '23' ]
  1066. ] ) );
  1067. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 0 } );
  1068. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1069. [ { colspan: 2, contents: '00' }, '03' ],
  1070. [ '10', '12', '13' ],
  1071. [ '21', '22', '23' ]
  1072. ] ) );
  1073. } );
  1074. it( 'should remove column with rowspan (first column)', () => {
  1075. setData( model, modelTable( [
  1076. [ { rowspan: 2, contents: '00' }, '01' ],
  1077. [ '11' ]
  1078. ] ) );
  1079. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 0 } );
  1080. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1081. [ '01' ],
  1082. [ '11' ]
  1083. ] ) );
  1084. } );
  1085. it( 'should remove column with rowspan (last column)', () => {
  1086. setData( model, modelTable( [
  1087. [ '00', { rowspan: 2, contents: '01' } ],
  1088. [ '10' ]
  1089. ] ) );
  1090. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  1091. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1092. [ '00' ],
  1093. [ '10' ]
  1094. ] ) );
  1095. } );
  1096. it( 'should remove column if other column is rowspanned (last column)', () => {
  1097. setData( model, modelTable( [
  1098. [ '00', { rowspan: 2, contents: '01' } ],
  1099. [ '10' ]
  1100. ] ) );
  1101. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 0 } );
  1102. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1103. [ '01' ]
  1104. ] ) );
  1105. } );
  1106. it( 'should remove column if other column is rowspanned (first column)', () => {
  1107. setData( model, modelTable( [
  1108. [ { rowspan: 2, contents: '00' }, '01' ],
  1109. [ '11' ]
  1110. ] ) );
  1111. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  1112. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1113. [ '00' ]
  1114. ] ) );
  1115. } );
  1116. it( 'should remove column if removing row with one column - other columns are spanned', () => {
  1117. setData( model, modelTable( [
  1118. [ '00', { rowspan: 2, contents: '01' }, { rowspan: 2, contents: '02' } ],
  1119. [ '10' ],
  1120. [ '20', '21', '22' ]
  1121. ] ) );
  1122. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 0 } );
  1123. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1124. [ '01', '02' ],
  1125. [ '21', '22' ]
  1126. ] ) );
  1127. } );
  1128. } );
  1129. describe( 'multiple columns', () => {
  1130. it( 'should properly remove two first columns', () => {
  1131. setData( model, modelTable( [
  1132. [ '00', '01', '02' ],
  1133. [ '10', '11', '12' ],
  1134. [ '20', '21', '22' ],
  1135. [ '30', '31', '32' ]
  1136. ] ) );
  1137. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 0, columns: 2 } );
  1138. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1139. [ '02' ],
  1140. [ '12' ],
  1141. [ '22' ],
  1142. [ '32' ]
  1143. ] ) );
  1144. } );
  1145. it( 'should properly remove two middle columns', () => {
  1146. setData( model, modelTable( [
  1147. [ '00', '01', '02', '03' ],
  1148. [ '10', '11', '12', '13' ],
  1149. [ '20', '21', '22', '23' ],
  1150. [ '30', '31', '32', '33' ]
  1151. ] ) );
  1152. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 1, columns: 2 } );
  1153. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1154. [ '00', '03' ],
  1155. [ '10', '13' ],
  1156. [ '20', '23' ],
  1157. [ '30', '33' ]
  1158. ] ) );
  1159. } );
  1160. it( 'should properly remove two last columns', () => {
  1161. setData( model, modelTable( [
  1162. [ '00', '01', '02' ],
  1163. [ '10', '11', '12' ],
  1164. [ '20', '21', '22' ],
  1165. [ '30', '31', '32' ]
  1166. ] ) );
  1167. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 1, columns: 2 } );
  1168. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1169. [ '00' ],
  1170. [ '10' ],
  1171. [ '20' ],
  1172. [ '30' ]
  1173. ] ) );
  1174. } );
  1175. it( 'should properly remove multiple heading columns', () => {
  1176. setData( model, modelTable( [
  1177. [ '00', '01', '02', '03', '04' ],
  1178. [ '10', '11', '12', '13', '14' ]
  1179. ], { headingColumns: 3 } ) );
  1180. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 1, columns: 3 } );
  1181. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1182. [ '00', '04' ],
  1183. [ '10', '14' ]
  1184. ], { headingColumns: 1 } ) );
  1185. } );
  1186. it( 'should properly calculate truncated colspans', () => {
  1187. setData( model, modelTable( [
  1188. [ { contents: '00', colspan: 3 } ],
  1189. [ '10', '11', '12' ],
  1190. [ '20', '21', '22' ]
  1191. ] ) );
  1192. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 0, columns: 2 } );
  1193. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1194. [ '00' ],
  1195. [ '12' ],
  1196. [ '22' ]
  1197. ] ) );
  1198. } );
  1199. } );
  1200. } );
  1201. } );