8
0

tableutils.js 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  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. setData( model, modelTable( [
  747. [ { rowspan: 4, contents: '00' }, { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  748. [ { rowspan: 2, contents: '13' }, '14' ],
  749. [ '22', '24' ],
  750. [ '31', '32', '33', '34' ]
  751. ] ) );
  752. tableUtils.removeRows( root.getChild( 0 ), { at: 2 } );
  753. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  754. [ { rowspan: 3, contents: '00' }, { rowspan: 2, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  755. [ '13', '14' ],
  756. [ '31', '32', '33', '34' ]
  757. ] ) );
  758. } );
  759. it( 'should move rowspaned cells to a row below removing it\'s row', () => {
  760. setData( model, modelTable( [
  761. [ { rowspan: 3, contents: '00' }, { rowspan: 2, contents: '01' }, '02' ],
  762. [ '12' ],
  763. [ '21', '22' ],
  764. [ '30', '31', '32' ]
  765. ] ) );
  766. tableUtils.removeRows( root.getChild( 0 ), { at: 0 } );
  767. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  768. [ { rowspan: 2, contents: '00' }, '01', '12' ],
  769. [ '21', '22' ],
  770. [ '30', '31', '32' ]
  771. ] ) );
  772. } );
  773. it( 'should move rowspaned cells to a row below removing it\'s row (other cell is overlapping removed row)', () => {
  774. setData( model, modelTable( [
  775. [ '00', { rowspan: 3, contents: '01' }, '02', '03', '04' ],
  776. [ '10', { rowspan: 2, contents: '12' }, '13', '14' ],
  777. [ '20', '23', '24' ]
  778. ] ) );
  779. tableUtils.removeRows( root.getChild( 0 ), { at: 1 } );
  780. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  781. [ '00', { rowspan: 2, contents: '01' }, '02', '03', '04' ],
  782. [ '20', '12', '23', '24' ]
  783. ] ) );
  784. } );
  785. } );
  786. describe( 'many rows', () => {
  787. it( 'should properly remove middle rows', () => {
  788. setData( model, modelTable( [
  789. [ '00', '01' ],
  790. [ '10', '11' ],
  791. [ '20', '21' ],
  792. [ '30', '31' ]
  793. ] ) );
  794. tableUtils.removeRows( root.getChild( 0 ), { at: 1, rows: 2 } );
  795. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  796. [ '00', '01' ],
  797. [ '30', '31' ]
  798. ] ) );
  799. } );
  800. it( 'should properly remove tailing rows', () => {
  801. setData( model, modelTable( [
  802. [ '00', '01' ],
  803. [ '10', '11' ],
  804. [ '20', '21' ],
  805. [ '30', '31' ]
  806. ] ) );
  807. tableUtils.removeRows( root.getChild( 0 ), { at: 2, rows: 2 } );
  808. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  809. [ '00', '01' ],
  810. [ '10', '11' ]
  811. ] ) );
  812. } );
  813. it( 'should properly remove beginning rows', () => {
  814. setData( model, modelTable( [
  815. [ '00', '01' ],
  816. [ '10', '11' ],
  817. [ '20', '21' ],
  818. [ '30', '31' ]
  819. ] ) );
  820. tableUtils.removeRows( root.getChild( 0 ), { at: 0, rows: 2 } );
  821. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  822. [ '20', '21' ],
  823. [ '30', '31' ]
  824. ] ) );
  825. } );
  826. it( 'should support removing multiple headings (removed rows in heading section)', () => {
  827. setData( model, modelTable( [
  828. [ '00', '01' ],
  829. [ '10', '11' ],
  830. [ '20', '21' ],
  831. [ '30', '31' ]
  832. ], { headingRows: 3 } ) );
  833. tableUtils.removeRows( root.getChild( 0 ), { at: 0, rows: 2 } );
  834. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  835. [ '20', '21' ],
  836. [ '30', '31' ]
  837. ], { headingRows: 1 } ) );
  838. } );
  839. it( 'should support removing multiple headings (removed rows in heading and body section)', () => {
  840. setData( model, modelTable( [
  841. [ '00', '01' ],
  842. [ '10', '11' ],
  843. [ '20', '21' ],
  844. [ '30', '31' ],
  845. [ '40', '41' ]
  846. ], { headingRows: 3 } ) );
  847. tableUtils.removeRows( root.getChild( 0 ), { at: 1, rows: 3 } );
  848. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  849. [ '00', '01' ],
  850. [ '40', '41' ]
  851. ], { headingRows: 1 } ) );
  852. } );
  853. it( 'should support removing mixed heading and cell rows', () => {
  854. setData( model, modelTable( [
  855. [ '00', '01' ],
  856. [ '10', '11' ],
  857. [ '20', '21' ]
  858. ], { headingRows: 1 } ) );
  859. tableUtils.removeRows( root.getChild( 0 ), { at: 0, rows: 2 } );
  860. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  861. [ '20', '21' ]
  862. ] ) );
  863. } );
  864. it( 'should move row-spaned cells to a row after removed rows section', () => {
  865. setData( model, modelTable( [
  866. [ '00', '01', '02', '03' ],
  867. [ { rowspan: 4, contents: '10' }, { rowspan: 3, contents: '11' }, { rowspan: 2, contents: '12' }, '13' ],
  868. [ { rowspan: 3, contents: '23' } ],
  869. [ '32' ],
  870. [ '41', '42' ]
  871. ] ) );
  872. tableUtils.removeRows( root.getChild( 0 ), { at: 1, rows: 2 } );
  873. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  874. [ '00', '01', '02', '03' ],
  875. [ { rowspan: 2, contents: '10' }, '11', '32', { rowspan: 2, contents: '23' } ],
  876. [ '41', '42' ]
  877. ] ) );
  878. } );
  879. it( 'should decrease rowspan of table cells from rows before removed rows section', () => {
  880. setData( model, modelTable( [
  881. [ { rowspan: 4, contents: '00' }, { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03' ],
  882. [ '13' ],
  883. [ '22', '23' ],
  884. [ '31', '32', '33' ]
  885. ] ) );
  886. tableUtils.removeRows( root.getChild( 0 ), { at: 1, rows: 2 } );
  887. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  888. [ { rowspan: 2, contents: '00' }, '01', '02', '03' ],
  889. [ '31', '32', '33' ]
  890. ] ) );
  891. } );
  892. it( 'should create one undo step (1 batch)', () => {
  893. setData( model, modelTable( [
  894. [ '00', '01' ],
  895. [ '10', '11' ],
  896. [ '20', '21' ],
  897. [ '30', '31' ]
  898. ], { headingRows: 3 } ) );
  899. const createdBatches = new Set();
  900. model.on( 'applyOperation', ( evt, args ) => {
  901. const operation = args[ 0 ];
  902. createdBatches.add( operation.batch );
  903. } );
  904. tableUtils.removeRows( root.getChild( 0 ), { at: 0, rows: 2 } );
  905. expect( createdBatches.size ).to.equal( 1 );
  906. } );
  907. } );
  908. } );
  909. describe( 'removeColumns()', () => {
  910. beforeEach( () => {
  911. return ModelTestEditor.create( {
  912. plugins: [ TableUtils ]
  913. } ).then( newEditor => {
  914. editor = newEditor;
  915. model = editor.model;
  916. root = model.document.getRoot( 'main' );
  917. tableUtils = editor.plugins.get( TableUtils );
  918. defaultSchema( model.schema );
  919. defaultConversion( editor.conversion );
  920. } );
  921. } );
  922. describe( 'single row', () => {
  923. it( 'should remove a given column', () => {
  924. setData( model, modelTable( [
  925. [ '00', '01', '02' ],
  926. [ '10', '11', '12' ],
  927. [ '20', '21', '22' ]
  928. ] ) );
  929. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  930. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  931. [ '00', '02' ],
  932. [ '10', '12' ],
  933. [ '20', '22' ]
  934. ] ) );
  935. } );
  936. it( 'should remove a given column from a table start', () => {
  937. setData( model, modelTable( [
  938. [ '00', '01' ],
  939. [ '10', '11' ],
  940. [ '20', '21' ]
  941. ] ) );
  942. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 0 } );
  943. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  944. [ '01' ],
  945. [ '11' ],
  946. [ '21' ]
  947. ] ) );
  948. } );
  949. it( 'should change heading columns if removing a heading column', () => {
  950. setData( model, modelTable( [
  951. [ '00', '01' ],
  952. [ '10', '11' ],
  953. [ '20', '21' ]
  954. ], { headingColumns: 2 } ) );
  955. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 0 } );
  956. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  957. [ '01' ],
  958. [ '11' ],
  959. [ '21' ]
  960. ], { headingColumns: 1 } ) );
  961. } );
  962. it( 'should decrease colspan of table cells from previous column', () => {
  963. setData( model, modelTable( [
  964. [ { colspan: 4, contents: '00' }, '04' ],
  965. [ { colspan: 3, contents: '10' }, '13', '14' ],
  966. [ { colspan: 2, contents: '20' }, '22', '23', '24' ],
  967. [ '30', { colspan: 2, contents: '31' }, '33', '34' ],
  968. [ '40', '41', '42', '43', '44' ]
  969. ] ) );
  970. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 2 } );
  971. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  972. [ { colspan: 3, contents: '00' }, '04' ],
  973. [ { colspan: 2, contents: '10' }, '13', '14' ],
  974. [ { colspan: 2, contents: '20' }, '23', '24' ],
  975. [ '30', '31', '33', '34' ],
  976. [ '40', '41', '43', '44' ]
  977. ] ) );
  978. } );
  979. it( 'should decrease colspan of cells that are on removed column', () => {
  980. setData( model, modelTable( [
  981. [ { colspan: 3, contents: '00' }, '03' ],
  982. [ { colspan: 2, contents: '10' }, '12', '13' ],
  983. [ '20', '21', '22', '23' ]
  984. ] ) );
  985. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 0 } );
  986. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  987. [ { colspan: 2, contents: '00' }, '03' ],
  988. [ '10', '12', '13' ],
  989. [ '21', '22', '23' ]
  990. ] ) );
  991. } );
  992. it( 'should remove column with rowspan (first column)', () => {
  993. setData( model, modelTable( [
  994. [ { rowspan: 2, contents: '00' }, '01' ],
  995. [ '11' ]
  996. ] ) );
  997. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 0 } );
  998. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  999. [ '01' ],
  1000. [ '11' ]
  1001. ] ) );
  1002. } );
  1003. it( 'should remove column with rowspan (last column)', () => {
  1004. setData( model, modelTable( [
  1005. [ '00', { rowspan: 2, contents: '01' } ],
  1006. [ '10' ]
  1007. ] ) );
  1008. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  1009. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1010. [ '00' ],
  1011. [ '10' ]
  1012. ] ) );
  1013. } );
  1014. it( 'should remove column if other column is rowspanned (last column)', () => {
  1015. setData( model, modelTable( [
  1016. [ '00', { rowspan: 2, contents: '01' } ],
  1017. [ '10' ]
  1018. ] ) );
  1019. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 0 } );
  1020. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1021. [ '01' ]
  1022. ] ) );
  1023. } );
  1024. it( 'should remove column if other column is rowspanned (first column)', () => {
  1025. setData( model, modelTable( [
  1026. [ { rowspan: 2, contents: '00' }, '01' ],
  1027. [ '11' ]
  1028. ] ) );
  1029. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  1030. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1031. [ '00' ]
  1032. ] ) );
  1033. } );
  1034. it( 'should remove column if removing row with one column - other columns are spanned', () => {
  1035. setData( model, modelTable( [
  1036. [ '00', { rowspan: 2, contents: '01' }, { rowspan: 2, contents: '02' } ],
  1037. [ '10' ],
  1038. [ '20', '21', '22' ]
  1039. ] ) );
  1040. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 0 } );
  1041. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1042. [ '01', '02' ],
  1043. [ '21', '22' ]
  1044. ] ) );
  1045. } );
  1046. } );
  1047. describe( 'multiple columns', () => {
  1048. it( 'should properly remove two first columns', () => {
  1049. setData( model, modelTable( [
  1050. [ '00', '01', '02' ],
  1051. [ '10', '11', '12' ],
  1052. [ '20', '21', '22' ],
  1053. [ '30', '31', '32' ]
  1054. ] ) );
  1055. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 0, columns: 2 } );
  1056. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1057. [ '02' ],
  1058. [ '12' ],
  1059. [ '22' ],
  1060. [ '32' ]
  1061. ] ) );
  1062. } );
  1063. it( 'should properly remove two middle columns', () => {
  1064. setData( model, modelTable( [
  1065. [ '00', '01', '02', '03' ],
  1066. [ '10', '11', '12', '13' ],
  1067. [ '20', '21', '22', '23' ],
  1068. [ '30', '31', '32', '33' ]
  1069. ] ) );
  1070. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 1, columns: 2 } );
  1071. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1072. [ '00', '03' ],
  1073. [ '10', '13' ],
  1074. [ '20', '23' ],
  1075. [ '30', '33' ]
  1076. ] ) );
  1077. } );
  1078. it( 'should properly remove two last columns', () => {
  1079. setData( model, modelTable( [
  1080. [ '00', '01', '02' ],
  1081. [ '10', '11', '12' ],
  1082. [ '20', '21', '22' ],
  1083. [ '30', '31', '32' ]
  1084. ] ) );
  1085. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 1, columns: 2 } );
  1086. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1087. [ '00' ],
  1088. [ '10' ],
  1089. [ '20' ],
  1090. [ '30' ]
  1091. ] ) );
  1092. } );
  1093. it( 'should properly remove multiple heading columns', () => {
  1094. setData( model, modelTable( [
  1095. [ '00', '01', '02', '03', '04' ],
  1096. [ '10', '11', '12', '13', '14' ]
  1097. ], { headingColumns: 3 } ) );
  1098. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 1, columns: 3 } );
  1099. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1100. [ '00', '04' ],
  1101. [ '10', '14' ]
  1102. ], { headingColumns: 1 } ) );
  1103. } );
  1104. it( 'should properly calculate truncated colspans', () => {
  1105. setData( model, modelTable( [
  1106. [ { contents: '00', colspan: 3 } ],
  1107. [ '10', '11', '12' ],
  1108. [ '20', '21', '22' ]
  1109. ] ) );
  1110. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 0, columns: 2 } );
  1111. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1112. [ '00' ],
  1113. [ '12' ],
  1114. [ '22' ]
  1115. ] ) );
  1116. } );
  1117. } );
  1118. } );
  1119. } );