8
0

tableutils.js 46 KB

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