tableutils.js 46 KB

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