8
0

tableclipboard-paste.js 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400
  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. /* globals document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import BlockQuoteEditing from '@ckeditor/ckeditor5-block-quote/src/blockquoteediting';
  8. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  9. import HorizontalLineEditing from '@ckeditor/ckeditor5-horizontal-line/src/horizontallineediting';
  10. import ImageCaptionEditing from '@ckeditor/ckeditor5-image/src/imagecaption/imagecaptionediting';
  11. import ImageEditing from '@ckeditor/ckeditor5-image/src/image/imageediting';
  12. import ListEditing from '@ckeditor/ckeditor5-list/src/listediting';
  13. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  14. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  15. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  16. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  17. import { assertSelectedCells, modelTable, viewTable } from './_utils/utils';
  18. import TableEditing from '../src/tableediting';
  19. import TableCellPropertiesEditing from '../src/tablecellproperties/tablecellpropertiesediting';
  20. import TableWalker from '../src/tablewalker';
  21. import TableClipboard from '../src/tableclipboard';
  22. describe( 'table clipboard', () => {
  23. let editor, model, modelRoot, tableSelection, viewDocument, element;
  24. testUtils.createSinonSandbox();
  25. beforeEach( () => {
  26. element = document.createElement( 'div' );
  27. document.body.appendChild( element );
  28. } );
  29. afterEach( async () => {
  30. await editor.destroy();
  31. element.remove();
  32. } );
  33. describe( 'Clipboard integration - paste (selection scenarios)', () => {
  34. beforeEach( async () => {
  35. await createEditor();
  36. setModelData( model, modelTable( [
  37. [ '00[]', '01', '02', '03' ],
  38. [ '10', '11', '12', '13' ],
  39. [ '20', '21', '22', '23' ],
  40. [ '30', '31', '32', '33' ]
  41. ] ) );
  42. } );
  43. it( 'should be disabled in a readonly mode', () => {
  44. editor.isReadOnly = true;
  45. tableSelection.setCellSelection(
  46. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  47. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  48. );
  49. const data = pasteTable( [
  50. [ 'aa', 'ab' ],
  51. [ 'ba', 'bb' ]
  52. ] );
  53. editor.isReadOnly = false;
  54. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  55. [ '00', '01', '02', '03' ],
  56. [ '10', '11', '12', '13' ],
  57. [ '20', '21', '22', '23' ],
  58. [ '30', '31', '32', '33' ]
  59. ] ) );
  60. sinon.assert.calledOnce( data.preventDefault );
  61. } );
  62. it( 'should allow normal paste if no table cells are selected', () => {
  63. const data = {
  64. dataTransfer: createDataTransfer(),
  65. preventDefault: sinon.spy(),
  66. stopPropagation: sinon.spy()
  67. };
  68. data.dataTransfer.setData( 'text/html', '<p>foo</p>' );
  69. viewDocument.fire( 'paste', data );
  70. editor.isReadOnly = false;
  71. assertEqualMarkup( getModelData( model ), modelTable( [
  72. [ '00foo[]', '01', '02', '03' ],
  73. [ '10', '11', '12', '13' ],
  74. [ '20', '21', '22', '23' ],
  75. [ '30', '31', '32', '33' ]
  76. ] ) );
  77. } );
  78. it( 'should not alter model.insertContent if selectable is different from document selection', () => {
  79. model.change( writer => {
  80. writer.setSelection( modelRoot.getNodeByPath( [ 0, 0, 0 ] ), 0 );
  81. const selectedTableCells = model.createSelection( [
  82. model.createRangeOn( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) ),
  83. model.createRangeOn( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) ),
  84. model.createRangeOn( modelRoot.getNodeByPath( [ 0, 1, 0 ] ) ),
  85. model.createRangeOn( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) )
  86. ] );
  87. const tableToInsert = editor.plugins.get( 'TableUtils' ).createTable( writer, 2, 2 );
  88. for ( const { cell } of new TableWalker( tableToInsert ) ) {
  89. writer.insertText( 'foo', cell.getChild( 0 ), 0 );
  90. }
  91. model.insertContent( tableToInsert, selectedTableCells );
  92. } );
  93. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  94. [ '', '', '02', '03' ],
  95. [ '', '', '12', '13' ],
  96. [ '20', '21', '22', '23' ],
  97. [ '30', '31', '32', '33' ]
  98. ] ) );
  99. } );
  100. it( 'should not alter model.insertContent if selection is outside table', () => {
  101. model.change( writer => {
  102. writer.insertElement( 'paragraph', modelRoot.getChild( 0 ), 'before' );
  103. writer.setSelection( modelRoot.getChild( 0 ), 'before' );
  104. } );
  105. const data = {
  106. dataTransfer: createDataTransfer(),
  107. preventDefault: sinon.spy(),
  108. stopPropagation: sinon.spy()
  109. };
  110. data.dataTransfer.setData( 'text/html', '<p>foo</p>' );
  111. viewDocument.fire( 'paste', data );
  112. editor.isReadOnly = false;
  113. assertEqualMarkup( getModelData( model ), '<paragraph>foo[]</paragraph>' + modelTable( [
  114. [ '00', '01', '02', '03' ],
  115. [ '10', '11', '12', '13' ],
  116. [ '20', '21', '22', '23' ],
  117. [ '30', '31', '32', '33' ]
  118. ] ) );
  119. } );
  120. it( 'should not alter model.insertContent if no table pasted', () => {
  121. tableSelection.setCellSelection(
  122. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  123. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  124. );
  125. const data = {
  126. dataTransfer: createDataTransfer(),
  127. preventDefault: sinon.spy(),
  128. stopPropagation: sinon.spy()
  129. };
  130. data.dataTransfer.setData( 'text/html', '<p>foo</p>' );
  131. viewDocument.fire( 'paste', data );
  132. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  133. [ 'foo', '', '02', '03' ],
  134. [ '', '', '12', '13' ],
  135. [ '20', '21', '22', '23' ],
  136. [ '30', '31', '32', '33' ]
  137. ] ) );
  138. } );
  139. it( 'should not alter model.insertContent if mixed content is pasted (table + paragraph)', () => {
  140. tableSelection.setCellSelection(
  141. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  142. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  143. );
  144. const table = viewTable( [
  145. [ 'aa', 'ab' ],
  146. [ 'ba', 'bb' ] ] );
  147. const data = {
  148. dataTransfer: createDataTransfer(),
  149. preventDefault: sinon.spy(),
  150. stopPropagation: sinon.spy()
  151. };
  152. data.dataTransfer.setData( 'text/html', `${ table }<p>foo</p>` );
  153. viewDocument.fire( 'paste', data );
  154. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  155. [ 'foo', '', '02', '03' ],
  156. [ '', '', '12', '13' ],
  157. [ '20', '21', '22', '23' ],
  158. [ '30', '31', '32', '33' ]
  159. ] ) );
  160. } );
  161. it( 'should not alter model.insertContent if mixed content is pasted (paragraph + table)', () => {
  162. tableSelection.setCellSelection(
  163. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  164. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  165. );
  166. const table = viewTable( [
  167. [ 'aa', 'ab' ],
  168. [ 'ba', 'bb' ] ] );
  169. const data = {
  170. dataTransfer: createDataTransfer(),
  171. preventDefault: sinon.spy(),
  172. stopPropagation: sinon.spy()
  173. };
  174. data.dataTransfer.setData( 'text/html', `<p>foo</p>${ table }` );
  175. viewDocument.fire( 'paste', data );
  176. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  177. [ 'foo', '', '02', '03' ],
  178. [ '', '', '12', '13' ],
  179. [ '20', '21', '22', '23' ],
  180. [ '30', '31', '32', '33' ]
  181. ] ) );
  182. } );
  183. it( 'should not alter model.insertContent if mixed content is pasted (table + table)', () => {
  184. tableSelection.setCellSelection(
  185. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  186. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  187. );
  188. const table = viewTable( [
  189. [ 'aa', 'ab' ],
  190. [ 'ba', 'bb' ] ] );
  191. const data = {
  192. dataTransfer: createDataTransfer(),
  193. preventDefault: sinon.spy(),
  194. stopPropagation: sinon.spy()
  195. };
  196. data.dataTransfer.setData( 'text/html', `${ table }${ table }` );
  197. viewDocument.fire( 'paste', data );
  198. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  199. [ '', '', '02', '03' ],
  200. [ '', '', '12', '13' ],
  201. [ '20', '21', '22', '23' ],
  202. [ '30', '31', '32', '33' ]
  203. ] ) );
  204. } );
  205. it( 'should alter model.insertContent if selectable is a document selection', () => {
  206. tableSelection.setCellSelection(
  207. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  208. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  209. );
  210. model.change( writer => {
  211. const tableToInsert = editor.plugins.get( 'TableUtils' ).createTable( writer, 2, 2 );
  212. for ( const { cell } of new TableWalker( tableToInsert ) ) {
  213. writer.insertText( 'foo', cell.getChild( 0 ), 0 );
  214. }
  215. model.insertContent( tableToInsert, editor.model.document.selection );
  216. } );
  217. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  218. [ 'foo', 'foo', '02', '03' ],
  219. [ 'foo', 'foo', '12', '13' ],
  220. [ '20', '21', '22', '23' ],
  221. [ '30', '31', '32', '33' ]
  222. ] ) );
  223. } );
  224. describe( 'single cell selected', () => {
  225. beforeEach( () => {
  226. setModelData( model, modelTable( [
  227. [ '00[]', '01', '02' ],
  228. [ '10', '11', '12' ],
  229. [ '20', '21', '22' ]
  230. ] ) );
  231. } );
  232. describe( 'with the selection on the middle cell', () => {
  233. beforeEach( () => {
  234. tableSelection.setCellSelection(
  235. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  236. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  237. );
  238. } );
  239. it( 'should replace the table cells (with single undo step)', () => {
  240. const batches = setupBatchWatch();
  241. pasteTable( [
  242. [ 'aa', 'ab' ],
  243. [ 'ba', 'bb' ]
  244. ] );
  245. expect( batches.size ).to.equal( 1 );
  246. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  247. [ '00', '01', '02' ],
  248. [ '10', 'aa', 'ab' ],
  249. [ '20', 'ba', 'bb' ]
  250. ] ) );
  251. } );
  252. it( 'should expand the table width and replace the table cells (with single undo step)', () => {
  253. const batches = setupBatchWatch();
  254. pasteTable( [
  255. [ 'aa', 'ab', 'ac', 'ad', 'ae' ],
  256. [ 'ba', 'bb', 'bc', 'bd', 'be' ]
  257. ] );
  258. expect( batches.size ).to.equal( 1 );
  259. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  260. [ '00', '01', '02', '', '', '' ],
  261. [ '10', 'aa', 'ab', 'ac', 'ad', 'ae' ],
  262. [ '20', 'ba', 'bb', 'bc', 'bd', 'be' ]
  263. ] ) );
  264. } );
  265. it( 'should expand the table height and replace the table cells (with single undo step)', () => {
  266. const batches = setupBatchWatch();
  267. pasteTable( [
  268. [ 'aa', 'ab' ],
  269. [ 'ba', 'bb' ],
  270. [ 'ca', 'cb' ],
  271. [ 'da', 'db' ],
  272. [ 'ea', 'eb' ]
  273. ] );
  274. expect( batches.size ).to.equal( 1 );
  275. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  276. [ '00', '01', '02' ],
  277. [ '10', 'aa', 'ab' ],
  278. [ '20', 'ba', 'bb' ],
  279. [ '', 'ca', 'cb' ],
  280. [ '', 'da', 'db' ],
  281. [ '', 'ea', 'eb' ]
  282. ] ) );
  283. } );
  284. it( 'should expand the table width, height and replace the table cells (with single undo step)', () => {
  285. const batches = setupBatchWatch();
  286. pasteTable( [
  287. [ 'aa', 'ab', 'ac', 'ad', 'ae' ],
  288. [ 'ba', 'bb', 'bc', 'bd', 'be' ],
  289. [ 'ca', 'cb', 'cc', 'cd', 'ce' ],
  290. [ 'da', 'db', 'dc', 'dd', 'de' ],
  291. [ 'ea', 'eb', 'ec', 'ed', 'ee' ]
  292. ] );
  293. expect( batches.size ).to.equal( 1 );
  294. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  295. [ '00', '01', '02', '', '', '' ],
  296. [ '10', 'aa', 'ab', 'ac', 'ad', 'ae' ],
  297. [ '20', 'ba', 'bb', 'bc', 'bd', 'be' ],
  298. [ '', 'ca', 'cb', 'cc', 'cd', 'ce' ],
  299. [ '', 'da', 'db', 'dc', 'dd', 'de' ],
  300. [ '', 'ea', 'eb', 'ec', 'ed', 'ee' ]
  301. ] ) );
  302. } );
  303. } );
  304. describe( 'with the selection on the first cell', () => {
  305. beforeEach( () => {
  306. tableSelection.setCellSelection(
  307. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  308. modelRoot.getNodeByPath( [ 0, 0, 0 ] )
  309. );
  310. } );
  311. it( 'should replace the table cells', () => {
  312. pasteTable( [
  313. [ 'aa', 'ab' ],
  314. [ 'ba', 'bb' ]
  315. ] );
  316. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  317. [ 'aa', 'ab', '02' ],
  318. [ 'ba', 'bb', '12' ],
  319. [ '20', '21', '22' ]
  320. ] ) );
  321. } );
  322. it( 'should expand the table and replace the table cells', () => {
  323. pasteTable( [
  324. [ 'aa', 'ab', 'ac', 'ad', 'ae' ],
  325. [ 'ba', 'bb', 'bc', 'bd', 'be' ],
  326. [ 'ca', 'cb', 'cc', 'cd', 'ce' ],
  327. [ 'da', 'db', 'dc', 'dd', 'de' ],
  328. [ 'ea', 'eb', 'ec', 'ed', 'ee' ]
  329. ] );
  330. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  331. [ 'aa', 'ab', 'ac', 'ad', 'ae' ],
  332. [ 'ba', 'bb', 'bc', 'bd', 'be' ],
  333. [ 'ca', 'cb', 'cc', 'cd', 'ce' ],
  334. [ 'da', 'db', 'dc', 'dd', 'de' ],
  335. [ 'ea', 'eb', 'ec', 'ed', 'ee' ]
  336. ] ) );
  337. } );
  338. } );
  339. describe( 'with the selection on the middle cell of the first row', () => {
  340. beforeEach( () => {
  341. tableSelection.setCellSelection(
  342. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  343. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  344. );
  345. } );
  346. it( 'should replace the table cells', () => {
  347. pasteTable( [
  348. [ 'aa', 'ab' ],
  349. [ 'ba', 'bb' ]
  350. ] );
  351. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  352. [ '00', 'aa', 'ab' ],
  353. [ '10', 'ba', 'bb' ],
  354. [ '20', '21', '22' ]
  355. ] ) );
  356. } );
  357. it( 'should expand the table and replace the table cells', () => {
  358. pasteTable( [
  359. [ 'aa', 'ab', 'ac', 'ad', 'ae' ],
  360. [ 'ba', 'bb', 'bc', 'bd', 'be' ],
  361. [ 'ca', 'cb', 'cc', 'cd', 'ce' ],
  362. [ 'da', 'db', 'dc', 'dd', 'de' ],
  363. [ 'ea', 'eb', 'ec', 'ed', 'ee' ]
  364. ] );
  365. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  366. [ '00', 'aa', 'ab', 'ac', 'ad', 'ae' ],
  367. [ '10', 'ba', 'bb', 'bc', 'bd', 'be' ],
  368. [ '20', 'ca', 'cb', 'cc', 'cd', 'ce' ],
  369. [ '', 'da', 'db', 'dc', 'dd', 'de' ],
  370. [ '', 'ea', 'eb', 'ec', 'ed', 'ee' ]
  371. ] ) );
  372. } );
  373. } );
  374. describe( 'with the selection on the last cell of the first row', () => {
  375. beforeEach( () => {
  376. tableSelection.setCellSelection(
  377. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  378. modelRoot.getNodeByPath( [ 0, 0, 2 ] )
  379. );
  380. } );
  381. it( 'should replace the table cells', () => {
  382. pasteTable( [
  383. [ 'aa', 'ab' ],
  384. [ 'ba', 'bb' ]
  385. ] );
  386. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  387. [ '00', '01', 'aa', 'ab' ],
  388. [ '10', '11', 'ba', 'bb' ],
  389. [ '20', '21', '22', '' ]
  390. ] ) );
  391. } );
  392. it( 'should expand the table and replace the table cells', () => {
  393. pasteTable( [
  394. [ 'aa', 'ab', 'ac', 'ad', 'ae' ],
  395. [ 'ba', 'bb', 'bc', 'bd', 'be' ],
  396. [ 'ca', 'cb', 'cc', 'cd', 'ce' ],
  397. [ 'da', 'db', 'dc', 'dd', 'de' ],
  398. [ 'ea', 'eb', 'ec', 'ed', 'ee' ]
  399. ] );
  400. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  401. [ '00', '01', 'aa', 'ab', 'ac', 'ad', 'ae' ],
  402. [ '10', '11', 'ba', 'bb', 'bc', 'bd', 'be' ],
  403. [ '20', '21', 'ca', 'cb', 'cc', 'cd', 'ce' ],
  404. [ '', '', 'da', 'db', 'dc', 'dd', 'de' ],
  405. [ '', '', 'ea', 'eb', 'ec', 'ed', 'ee' ]
  406. ] ) );
  407. } );
  408. } );
  409. describe( 'with the selection is on the middle cell of the first column', () => {
  410. beforeEach( () => {
  411. tableSelection.setCellSelection(
  412. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  413. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  414. );
  415. } );
  416. it( 'should replace the table cells', () => {
  417. pasteTable( [
  418. [ 'aa', 'ab' ],
  419. [ 'ba', 'bb' ]
  420. ] );
  421. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  422. [ '00', '01', '02' ],
  423. [ 'aa', 'ab', '12' ],
  424. [ 'ba', 'bb', '22' ]
  425. ] ) );
  426. } );
  427. it( 'should expand the table and replace the table cells', () => {
  428. pasteTable( [
  429. [ 'aa', 'ab', 'ac', 'ad', 'ae' ],
  430. [ 'ba', 'bb', 'bc', 'bd', 'be' ],
  431. [ 'ca', 'cb', 'cc', 'cd', 'ce' ],
  432. [ 'da', 'db', 'dc', 'dd', 'de' ],
  433. [ 'ea', 'eb', 'ec', 'ed', 'ee' ]
  434. ] );
  435. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  436. [ '00', '01', '02', '', '' ],
  437. [ 'aa', 'ab', 'ac', 'ad', 'ae' ],
  438. [ 'ba', 'bb', 'bc', 'bd', 'be' ],
  439. [ 'ca', 'cb', 'cc', 'cd', 'ce' ],
  440. [ 'da', 'db', 'dc', 'dd', 'de' ],
  441. [ 'ea', 'eb', 'ec', 'ed', 'ee' ]
  442. ] ) );
  443. } );
  444. } );
  445. describe( 'with the selection is on the last cell of the first column', () => {
  446. beforeEach( () => {
  447. tableSelection.setCellSelection(
  448. modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  449. modelRoot.getNodeByPath( [ 0, 2, 0 ] )
  450. );
  451. } );
  452. it( 'should replace the table cells', () => {
  453. pasteTable( [
  454. [ 'aa', 'ab' ],
  455. [ 'ba', 'bb' ]
  456. ] );
  457. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  458. [ '00', '01', '02' ],
  459. [ '10', '11', '12' ],
  460. [ 'aa', 'ab', '22' ],
  461. [ 'ba', 'bb', '' ]
  462. ] ) );
  463. } );
  464. it( 'should expand the table and replace the table cells', () => {
  465. pasteTable( [
  466. [ 'aa', 'ab', 'ac', 'ad', 'ae' ],
  467. [ 'ba', 'bb', 'bc', 'bd', 'be' ],
  468. [ 'ca', 'cb', 'cc', 'cd', 'ce' ],
  469. [ 'da', 'db', 'dc', 'dd', 'de' ],
  470. [ 'ea', 'eb', 'ec', 'ed', 'ee' ]
  471. ] );
  472. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  473. [ '00', '01', '02', '', '' ],
  474. [ '10', '11', '12', '', '' ],
  475. [ 'aa', 'ab', 'ac', 'ad', 'ae' ],
  476. [ 'ba', 'bb', 'bc', 'bd', 'be' ],
  477. [ 'ca', 'cb', 'cc', 'cd', 'ce' ],
  478. [ 'da', 'db', 'dc', 'dd', 'de' ],
  479. [ 'ea', 'eb', 'ec', 'ed', 'ee' ]
  480. ] ) );
  481. } );
  482. } );
  483. describe( 'with the selection is on the last cell of the last column', () => {
  484. beforeEach( () => {
  485. tableSelection.setCellSelection(
  486. modelRoot.getNodeByPath( [ 0, 2, 2 ] ),
  487. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  488. );
  489. } );
  490. it( 'should replace the table cells', () => {
  491. pasteTable( [
  492. [ 'aa', 'ab' ],
  493. [ 'ba', 'bb' ]
  494. ] );
  495. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  496. [ '00', '01', '02', '' ],
  497. [ '10', '11', '12', '' ],
  498. [ '20', '21', 'aa', 'ab' ],
  499. [ '', '', 'ba', 'bb' ]
  500. ] ) );
  501. } );
  502. it( 'should expand the table and replace the table cells', () => {
  503. pasteTable( [
  504. [ 'aa', 'ab', 'ac', 'ad', 'ae' ],
  505. [ 'ba', 'bb', 'bc', 'bd', 'be' ],
  506. [ 'ca', 'cb', 'cc', 'cd', 'ce' ],
  507. [ 'da', 'db', 'dc', 'dd', 'de' ],
  508. [ 'ea', 'eb', 'ec', 'ed', 'ee' ]
  509. ] );
  510. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  511. [ '00', '01', '02', '', '', '', '' ],
  512. [ '10', '11', '12', '', '', '', '' ],
  513. [ '20', '21', 'aa', 'ab', 'ac', 'ad', 'ae' ],
  514. [ '', '', 'ba', 'bb', 'bc', 'bd', 'be' ],
  515. [ '', '', 'ca', 'cb', 'cc', 'cd', 'ce' ],
  516. [ '', '', 'da', 'db', 'dc', 'dd', 'de' ],
  517. [ '', '', 'ea', 'eb', 'ec', 'ed', 'ee' ]
  518. ] ) );
  519. } );
  520. } );
  521. describe( 'the selection inside the table cell', () => {
  522. it( 'should replace the table cells when the collapsed selection is in the first cell', () => {
  523. pasteTable( [
  524. [ 'aa', 'ab' ],
  525. [ 'ba', 'bb' ]
  526. ] );
  527. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  528. [ 'aa', 'ab', '02' ],
  529. [ 'ba', 'bb', '12' ],
  530. [ '20', '21', '22' ]
  531. ] ) );
  532. } );
  533. it( 'should replace the table cells when the expanded selection is in the first cell', () => {
  534. model.change( writer => {
  535. writer.setSelection( modelRoot.getNodeByPath( [ 0, 0, 0 ] ), 'in' );
  536. } );
  537. pasteTable( [
  538. [ 'aa', 'ab' ],
  539. [ 'ba', 'bb' ]
  540. ] );
  541. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  542. [ 'aa', 'ab', '02' ],
  543. [ 'ba', 'bb', '12' ],
  544. [ '20', '21', '22' ]
  545. ] ) );
  546. } );
  547. it( 'should replace the table cells when selection is on the image inside the table cell', async () => {
  548. await editor.destroy();
  549. await createEditor( [ ImageEditing, ImageCaptionEditing ] );
  550. setModelData( model, modelTable( [
  551. [ '00', '01', '02' ],
  552. [ '10', '11', '12' ],
  553. [ '20', '21', '[<image src="/assets/sample.jpg"><caption></caption></image>]' ]
  554. ] ) );
  555. pasteTable( [
  556. [ 'aa', 'ab' ],
  557. [ 'ba', 'bb' ]
  558. ] );
  559. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  560. [ '00', '01', '02', '' ],
  561. [ '10', '11', '12', '' ],
  562. [ '20', '21', 'aa', 'ab' ],
  563. [ '', '', 'ba', 'bb' ]
  564. ] ) );
  565. } );
  566. it( 'should replace the table cells when selection is in the image caption inside the table cell', async () => {
  567. await editor.destroy();
  568. await createEditor( [ ImageEditing, ImageCaptionEditing ] );
  569. setModelData( model, modelTable( [
  570. [ '00', '01', '02' ],
  571. [ '10', '11', '12' ],
  572. [ '20', '21', '<image src="/assets/sample.jpg"><caption>fo[]o</caption></image>' ]
  573. ] ) );
  574. pasteTable( [
  575. [ 'aa', 'ab' ],
  576. [ 'ba', 'bb' ]
  577. ] );
  578. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  579. [ '00', '01', '02', '' ],
  580. [ '10', '11', '12', '' ],
  581. [ '20', '21', 'aa', 'ab' ],
  582. [ '', '', 'ba', 'bb' ]
  583. ] ) );
  584. } );
  585. } );
  586. describe( 'with spanned cells', () => {
  587. it( 'should replace the table structure', () => {
  588. // +----+----+----+----+----+----+
  589. // | 00 | 01 | 02 | 05 |
  590. // +----+----+----+----+----+----+
  591. // | 10 | 11 | 13 | 14 | 15 |
  592. // +----+ +----+----+----+
  593. // | 20 | | 23 | 25 |
  594. // +----+ +----+----+ +
  595. // | 30 | | 33 | |
  596. // +----+----+----+----+----+----+
  597. // | 40 | 42 | 43 | 44 |
  598. // +----+----+----+----+----+----+
  599. setModelData( model, modelTable( [
  600. [ '00', '01', { contents: '02', colspan: 3 }, '05' ],
  601. [ '10', { contents: '11', colspan: 2, rowspan: 3 }, '13', '14', '15' ],
  602. [ '20', { contents: '23', colspan: 2 }, { contents: '25', rowspan: 2 } ],
  603. [ '30', { contents: '33', colspan: 2 } ],
  604. [ { contents: '40', colspan: 2 }, '42', '43', { contents: '44', colspan: 2 } ]
  605. ] ) );
  606. tableSelection.setCellSelection(
  607. modelRoot.getNodeByPath( [ 0, 2, 1 ] ), // Cell 23.
  608. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  609. );
  610. // +----+----+----+----+----+
  611. // | aa | ac | ad | ae |
  612. // + + +----+----+
  613. // | | | bd | be |
  614. // +----+----+----+----+----+
  615. // | ca | cc | cd | ce |
  616. // +----+----+----+----+----+
  617. // | da | dd | de |
  618. // +----+----+----+----+ +
  619. // | ea | eb | ec | ed | |
  620. // +----+----+----+----+----+
  621. pasteTable( [
  622. [ { contents: 'aa', colspan: 2, rowspan: 2 }, { contents: 'ac', rowspan: 2 }, 'ad', 'ae' ],
  623. [ 'bd', 'be' ],
  624. [ { contents: 'ca', colspan: 2 }, 'cc', 'cd', 'ce' ],
  625. [ { contents: 'da', colspan: 3 }, 'dd', { contents: 'de', rowspan: 2 } ],
  626. [ 'ea', 'eb', 'ec', 'ed' ]
  627. ] );
  628. // +----+----+----+----+----+----+----+----+
  629. // | 00 | 01 | 02 | 05 | | |
  630. // +----+----+----+----+----+----+----+----+
  631. // | 10 | 11 | 13 | 14 | 15 | | |
  632. // +----+ +----+----+----+----+----+
  633. // | 20 | | aa | ac | ad | ae |
  634. // +----+ + + +----+----+
  635. // | 30 | | | | bd | be |
  636. // +----+----+----+----+----+----+----+----+
  637. // | 40 | 42 | ca | cc | cd | ce |
  638. // +----+----+----+----+----+----+----+----+
  639. // | | | | da | dd | de |
  640. // +----+----+----+----+----+----+----+ +
  641. // | | | | ea | eb | ec | ed | |
  642. // +----+----+----+----+----+----+----+----+
  643. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  644. [ '00', '01', { contents: '02', colspan: 3 }, '05', '', '' ],
  645. [ '10', { contents: '11', colspan: 2, rowspan: 3 }, '13', '14', '15', '', '' ],
  646. [ '20', { contents: 'aa', colspan: 2, rowspan: 2 }, { contents: 'ac', rowspan: 2 }, 'ad', 'ae' ],
  647. [ '30', 'bd', 'be' ],
  648. [ { contents: '40', colspan: 2 }, '42', { contents: 'ca', colspan: 2 }, 'cc', 'cd', 'ce' ],
  649. [ '', '', '', { contents: 'da', colspan: 3 }, 'dd', { contents: 'de', rowspan: 2 } ],
  650. [ '', '', '', 'ea', 'eb', 'ec', 'ed' ]
  651. ] ) );
  652. } );
  653. it( 'should fix non-rectangular are on matched table fragment', () => {
  654. // +----+----+----+
  655. // | 00 | 01 | 02 |
  656. // +----+----+ +
  657. // | 10 | 11 | |
  658. // +----+----+----+
  659. // | 20 | 22 |
  660. // +----+----+----+
  661. setModelData( model, modelTable( [
  662. [ '00', '01', { contents: '02', rowspan: 2 } ],
  663. [ '10', '11[]' ],
  664. [ { contents: '20', colspan: 2 }, '22' ]
  665. ] ) );
  666. pasteTable( [
  667. [ 'aa', 'ab' ],
  668. [ 'ba', 'bb' ]
  669. ] );
  670. // +----+----+----+
  671. // | 00 | 01 | 02 |
  672. // +----+----+----+
  673. // | 10 | aa | ab |
  674. // +----+----+----+
  675. // | 20 | ba | bb |
  676. // +----+----+----+
  677. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  678. [ '00', '01', '02' ],
  679. [ '10', 'aa', 'ab' ],
  680. [ '20', 'ba', 'bb' ]
  681. ] ) );
  682. } );
  683. } );
  684. } );
  685. describe( 'pasted table is equal to the selected area', () => {
  686. describe( 'no spans', () => {
  687. it( 'handles simple table paste to a simple table fragment - at the beginning of a table', () => {
  688. tableSelection.setCellSelection(
  689. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  690. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  691. );
  692. pasteTable( [
  693. [ 'aa', 'ab' ],
  694. [ 'ba', 'bb' ]
  695. ] );
  696. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  697. [ 'aa', 'ab', '02', '03' ],
  698. [ 'ba', 'bb', '12', '13' ],
  699. [ '20', '21', '22', '23' ],
  700. [ '30', '31', '32', '33' ]
  701. ] ) );
  702. assertSelectedCells( model, [
  703. [ 1, 1, 0, 0 ],
  704. [ 1, 1, 0, 0 ],
  705. [ 0, 0, 0, 0 ],
  706. [ 0, 0, 0, 0 ]
  707. ] );
  708. } );
  709. it( 'handles simple table paste to a simple table fragment - at the end of a table', () => {
  710. tableSelection.setCellSelection(
  711. modelRoot.getNodeByPath( [ 0, 2, 2 ] ),
  712. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  713. );
  714. pasteTable( [
  715. [ 'aa', 'ab' ],
  716. [ 'ba', 'bb' ]
  717. ] );
  718. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  719. [ '00', '01', '02', '03' ],
  720. [ '10', '11', '12', '13' ],
  721. [ '20', '21', 'aa', 'ab' ],
  722. [ '30', '31', 'ba', 'bb' ]
  723. ] ) );
  724. assertSelectedCells( model, [
  725. [ 0, 0, 0, 0 ],
  726. [ 0, 0, 0, 0 ],
  727. [ 0, 0, 1, 1 ],
  728. [ 0, 0, 1, 1 ]
  729. ] );
  730. } );
  731. it( 'handles simple table paste to a simple table fragment - in the middle of a table', () => {
  732. tableSelection.setCellSelection(
  733. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  734. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  735. );
  736. pasteTable( [
  737. [ 'aa', 'ab' ],
  738. [ 'ba', 'bb' ]
  739. ] );
  740. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  741. [ '00', '01', '02', '03' ],
  742. [ '10', 'aa', 'ab', '13' ],
  743. [ '20', 'ba', 'bb', '23' ],
  744. [ '30', '31', '32', '33' ]
  745. ] ) );
  746. assertSelectedCells( model, [
  747. [ 0, 0, 0, 0 ],
  748. [ 0, 1, 1, 0 ],
  749. [ 0, 1, 1, 0 ],
  750. [ 0, 0, 0, 0 ]
  751. ] );
  752. } );
  753. it( 'handles simple row paste to a simple row fragment - in the middle of a table', () => {
  754. tableSelection.setCellSelection(
  755. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  756. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  757. );
  758. pasteTable( [
  759. [ 'aa', 'ab' ]
  760. ] );
  761. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  762. [ '00', '01', '02', '03' ],
  763. [ '10', 'aa', 'ab', '13' ],
  764. [ '20', '21', '22', '23' ],
  765. [ '30', '31', '32', '33' ]
  766. ] ) );
  767. assertSelectedCells( model, [
  768. [ 0, 0, 0, 0 ],
  769. [ 0, 1, 1, 0 ],
  770. [ 0, 0, 0, 0 ],
  771. [ 0, 0, 0, 0 ]
  772. ] );
  773. } );
  774. it( 'handles simple column paste to a simple column fragment - in the middle of a table', () => {
  775. tableSelection.setCellSelection(
  776. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  777. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  778. );
  779. pasteTable( [
  780. [ 'aa' ],
  781. [ 'ba' ]
  782. ] );
  783. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  784. [ '00', '01', '02', '03' ],
  785. [ '10', 'aa', '12', '13' ],
  786. [ '20', 'ba', '22', '23' ],
  787. [ '30', '31', '32', '33' ]
  788. ] ) );
  789. assertSelectedCells( model, [
  790. [ 0, 0, 0, 0 ],
  791. [ 0, 1, 0, 0 ],
  792. [ 0, 1, 0, 0 ],
  793. [ 0, 0, 0, 0 ]
  794. ] );
  795. } );
  796. it( 'handles simple table paste to a simple table fragment - whole table selected', () => {
  797. tableSelection.setCellSelection(
  798. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  799. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  800. );
  801. pasteTable( [
  802. [ 'aa', 'ab', 'ac', 'ad' ],
  803. [ 'ba', 'bb', 'bc', 'bd' ],
  804. [ 'ca', 'cb', 'cc', 'cd' ],
  805. [ 'da', 'db', 'dc', 'dd' ]
  806. ] );
  807. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  808. [ 'aa', 'ab', 'ac', 'ad' ],
  809. [ 'ba', 'bb', 'bc', 'bd' ],
  810. [ 'ca', 'cb', 'cc', 'cd' ],
  811. [ 'da', 'db', 'dc', 'dd' ]
  812. ] ) );
  813. assertSelectedCells( model, [
  814. [ 1, 1, 1, 1 ],
  815. [ 1, 1, 1, 1 ],
  816. [ 1, 1, 1, 1 ],
  817. [ 1, 1, 1, 1 ]
  818. ] );
  819. } );
  820. } );
  821. describe( 'pasted table has spans', () => {
  822. it( 'handles pasting table that has cell with colspan', () => {
  823. tableSelection.setCellSelection(
  824. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  825. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  826. );
  827. pasteTable( [
  828. [ { colspan: 2, contents: 'aa' } ],
  829. [ 'ba', 'bb' ]
  830. ] );
  831. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  832. [ '00', '01', '02', '03' ],
  833. [ '10', { colspan: 2, contents: 'aa' }, '13' ],
  834. [ '20', 'ba', 'bb', '23' ],
  835. [ '30', '31', '32', '33' ]
  836. ] ) );
  837. /* eslint-disable no-multi-spaces */
  838. assertSelectedCells( model, [
  839. [ 0, 0, 0, 0 ],
  840. [ 0, 1, 0 ],
  841. [ 0, 1, 1, 0 ],
  842. [ 0, 0, 0, 0 ]
  843. ] );
  844. /* eslint-enable no-multi-spaces */
  845. } );
  846. it( 'handles pasting table that has many cells with various colspan', () => {
  847. tableSelection.setCellSelection(
  848. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  849. modelRoot.getNodeByPath( [ 0, 3, 2 ] )
  850. );
  851. pasteTable( [
  852. [ 'aa', { colspan: 2, contents: 'ab' } ],
  853. [ { colspan: 3, contents: 'ba' } ],
  854. [ 'ca', 'cb', 'cc' ],
  855. [ { colspan: 2, contents: 'da' }, 'dc' ]
  856. ] );
  857. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  858. [ 'aa', { colspan: 2, contents: 'ab' }, '03' ],
  859. [ { colspan: 3, contents: 'ba' }, '13' ],
  860. [ 'ca', 'cb', 'cc', '23' ],
  861. [ { colspan: 2, contents: 'da' }, 'dc', '33' ]
  862. ] ) );
  863. /* eslint-disable no-multi-spaces */
  864. assertSelectedCells( model, [
  865. [ 1, 1, 0 ],
  866. [ 1, 0 ],
  867. [ 1, 1, 1, 0 ],
  868. [ 1, 1, 0 ]
  869. ] );
  870. /* eslint-enable no-multi-spaces */
  871. } );
  872. it( 'handles pasting table that has cell with rowspan', () => {
  873. tableSelection.setCellSelection(
  874. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  875. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  876. );
  877. pasteTable( [
  878. [ { rowspan: 2, contents: 'aa' }, 'ab' ],
  879. [ 'bb' ]
  880. ] );
  881. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  882. [ '00', '01', '02', '03' ],
  883. [ '10', { rowspan: 2, contents: 'aa' }, 'ab', '13' ],
  884. [ '20', 'bb', '23' ],
  885. [ '30', '31', '32', '33' ]
  886. ] ) );
  887. /* eslint-disable no-multi-spaces */
  888. assertSelectedCells( model, [
  889. [ 0, 0, 0, 0 ],
  890. [ 0, 1, 1, 0 ],
  891. [ 0, 1, 0 ],
  892. [ 0, 0, 0, 0 ]
  893. ] );
  894. /* eslint-enable no-multi-spaces */
  895. } );
  896. it( 'handles pasting table that has many cells with various rowspan', () => {
  897. tableSelection.setCellSelection(
  898. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  899. modelRoot.getNodeByPath( [ 0, 2, 3 ] )
  900. );
  901. pasteTable( [
  902. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  903. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  904. [ 'cc', 'cd' ]
  905. ] );
  906. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  907. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  908. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  909. [ 'cc', 'cd' ],
  910. [ '30', '31', '32', '33' ]
  911. ] ) );
  912. /* eslint-disable no-multi-spaces */
  913. assertSelectedCells( model, [
  914. [ 1, 1, 1, 1 ],
  915. [ 1, 1 ],
  916. [ 1, 1 ],
  917. [ 0, 0, 0, 0 ]
  918. ] );
  919. /* eslint-enable no-multi-spaces */
  920. } );
  921. it( 'handles pasting multi-spanned table', () => {
  922. setModelData( model, modelTable( [
  923. [ '00', '01', '02', '03', '04', '05' ],
  924. [ '10', '11', '12', '13', '14', '15' ],
  925. [ '20', '21', '22', '23', '24', '25' ],
  926. [ '30', '31', '32', '33', '34', '35' ],
  927. [ '40', '41', '42', '43', '44', '45' ]
  928. ] ) );
  929. tableSelection.setCellSelection(
  930. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  931. modelRoot.getNodeByPath( [ 0, 3, 4 ] )
  932. );
  933. // +----+----+----+----+----+
  934. // | aa | ac | ad | ae |
  935. // +----+----+----+----+ +
  936. // | ba | bb | |
  937. // +----+ +----+
  938. // | ca | | ce |
  939. // + +----+----+----+----+
  940. // | | db | dc | dd |
  941. // +----+----+----+----+----+
  942. pasteTable( [
  943. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 } ],
  944. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 } ],
  945. [ { contents: 'ca', rowspan: 2 }, 'ce' ],
  946. [ 'db', 'dc', { contents: 'dd', colspan: 2 } ]
  947. ] );
  948. // +----+----+----+----+----+----+
  949. // | aa | ac | ad | ae | 05 |
  950. // +----+----+----+----+ +----+
  951. // | ba | bb | | 15 |
  952. // +----+ +----+----+
  953. // | ca | | ce | 25 |
  954. // + +----+----+----+----+----+
  955. // | | db | dc | dd | 35 |
  956. // +----+----+----+----+----+----+
  957. // | 40 | 41 | 42 | 43 | 44 | 45 |
  958. // +----+----+----+----+----+----+
  959. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  960. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 }, '05' ],
  961. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 }, '15' ],
  962. [ { contents: 'ca', rowspan: 2 }, 'ce', '25' ],
  963. [ 'db', 'dc', { contents: 'dd', colspan: 2 }, '35' ],
  964. [ '40', '41', '42', '43', '44', '45' ]
  965. ] ) );
  966. /* eslint-disable no-multi-spaces */
  967. assertSelectedCells( model, [
  968. [ 1, 1, 1, 1, 0 ],
  969. [ 1, 1, 0 ],
  970. [ 1, 1, 0 ],
  971. [ 1, 1, 1, 0 ],
  972. [ 0, 0, 0, 0, 0, 0 ]
  973. ] );
  974. /* eslint-enable no-multi-spaces */
  975. } );
  976. } );
  977. describe( 'content table has spans', () => {
  978. it( 'handles pasting simple table over a table with colspans (no colspan exceeds selection)', () => {
  979. setModelData( model, modelTable( [
  980. [ '00[]', '01', '02', '03' ],
  981. [ { colspan: 3, contents: '10' }, '13' ],
  982. [ { colspan: 2, contents: '20' }, '22', '23' ],
  983. [ '30', '31', { colspan: 2, contents: '31' } ]
  984. ] ) );
  985. tableSelection.setCellSelection(
  986. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  987. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  988. );
  989. pasteTable( [
  990. [ 'aa', 'ab', 'ac' ],
  991. [ 'ba', 'bb', 'bc' ],
  992. [ 'ca', 'cb', 'cc' ]
  993. ] );
  994. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  995. [ 'aa', 'ab', 'ac', '03' ],
  996. [ 'ba', 'bb', 'bc', '13' ],
  997. [ 'ca', 'cb', 'cc', '23' ],
  998. [ '30', '31', { colspan: 2, contents: '31' } ]
  999. ] ) );
  1000. /* eslint-disable no-multi-spaces */
  1001. assertSelectedCells( model, [
  1002. [ 1, 1, 1, 0 ],
  1003. [ 1, 1, 1, 0 ],
  1004. [ 1, 1, 1, 0 ],
  1005. [ 0, 0, 0 ]
  1006. ] );
  1007. /* eslint-enable no-multi-spaces */
  1008. } );
  1009. it( 'handles pasting simple table over a table with rowspans (no rowspan exceeds selection)', () => {
  1010. setModelData( model, modelTable( [
  1011. [ '00', { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03' ],
  1012. [ { rowspan: 2, contents: '10' }, '13' ],
  1013. [ '22', '23' ],
  1014. [ '30', '31', '32', '33' ]
  1015. ] ) );
  1016. tableSelection.setCellSelection(
  1017. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1018. modelRoot.getNodeByPath( [ 0, 2, 0 ] )
  1019. );
  1020. pasteTable( [
  1021. [ 'aa', 'ab', 'ac' ],
  1022. [ 'ba', 'bb', 'bc' ],
  1023. [ 'ca', 'cb', 'cc' ]
  1024. ] );
  1025. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1026. [ 'aa', 'ab', 'ac', '03' ],
  1027. [ 'ba', 'bb', 'bc', '13' ],
  1028. [ 'ca', 'cb', 'cc', '23' ],
  1029. [ '30', '31', '32', '33' ]
  1030. ] ) );
  1031. assertSelectedCells( model, [
  1032. [ 1, 1, 1, 0 ],
  1033. [ 1, 1, 1, 0 ],
  1034. [ 1, 1, 1, 0 ],
  1035. [ 0, 0, 0, 0 ]
  1036. ] );
  1037. } );
  1038. it( 'handles pasting simple table over table with multi-spans (no span exceeds selection)', () => {
  1039. // +----+----+----+----+----+----+
  1040. // | 00 | 02 | 03 | 05 |
  1041. // + + + +----+
  1042. // | | | | 15 |
  1043. // +----+----+----+ +----+
  1044. // | 20 | 21 | | 25 |
  1045. // + +----+----+----+----+----+
  1046. // | | 31 | 32 | 34 | 35 |
  1047. // +----+----+----+----+----+----+
  1048. // | 40 | 41 | 42 | 43 | 44 | 45 |
  1049. // +----+----+----+----+----+----+
  1050. setModelData( model, modelTable( [
  1051. [
  1052. { contents: '00', colspan: 2, rowspan: 2 },
  1053. { contents: '02', rowspan: 2 },
  1054. { contents: '03', colspan: 2, rowspan: 3 },
  1055. '05'
  1056. ],
  1057. [ '15' ],
  1058. [ { contents: '20', rowspan: 2 }, { contents: '21', colspan: 2 }, '25' ],
  1059. [ '31', { contents: '32', colspan: 2 }, '34', '35' ],
  1060. [ '40', '41', '42', '43', '44', '45' ]
  1061. ] ) );
  1062. tableSelection.setCellSelection(
  1063. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1064. modelRoot.getNodeByPath( [ 0, 3, 2 ] ) // Cell 34.
  1065. );
  1066. pasteTable( [
  1067. [ 'aa', 'ab', 'ac', 'ad', 'ae' ],
  1068. [ 'ba', 'bb', 'bc', 'bd', 'be' ],
  1069. [ 'ca', 'cb', 'cc', 'cd', 'ce' ],
  1070. [ 'da', 'db', 'dc', 'dd', 'de' ]
  1071. ] );
  1072. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1073. [ 'aa', 'ab', 'ac', 'ad', 'ae', '05' ],
  1074. [ 'ba', 'bb', 'bc', 'bd', 'be', '15' ],
  1075. [ 'ca', 'cb', 'cc', 'cd', 'ce', '25' ],
  1076. [ 'da', 'db', 'dc', 'dd', 'de', '35' ],
  1077. [ '40', '41', '42', '43', '44', '45' ]
  1078. ] ) );
  1079. assertSelectedCells( model, [
  1080. [ 1, 1, 1, 1, 1, 0 ],
  1081. [ 1, 1, 1, 1, 1, 0 ],
  1082. [ 1, 1, 1, 1, 1, 0 ],
  1083. [ 1, 1, 1, 1, 1, 0 ],
  1084. [ 0, 0, 0, 0, 0, 0 ]
  1085. ] );
  1086. } );
  1087. it( 'handles pasting simple table over a table with rowspan (rowspan before selection)', () => {
  1088. // +----+----+----+----+----+
  1089. // | 00 | 01 | 02 | 03 | 04 |
  1090. // +----+----+----+----+----+
  1091. // | 10 | 11 | 12 | 13 | 14 |
  1092. // +----+ +----+----+----+
  1093. // | 20 | | 22 | 23 | 24 |
  1094. // +----+ +----+----+----+
  1095. // | 30 | | 32 | 33 | 34 |
  1096. // +----+----+----+----+----+
  1097. // | 40 | 41 | 42 | 43 | 44 |
  1098. // +----+----+----+----+----+
  1099. setModelData( model, modelTable( [
  1100. [ '00', '01', '02', '03', '04' ],
  1101. [ '10', { contents: '11', rowspan: 3 }, '12', '13', '14' ],
  1102. [ '20', '22', '23', '24' ],
  1103. [ '30', '32', '33', '34' ],
  1104. [ '40', '41', '42', '43', '44' ]
  1105. ] ) );
  1106. tableSelection.setCellSelection(
  1107. modelRoot.getNodeByPath( [ 0, 1, 2 ] ),
  1108. modelRoot.getNodeByPath( [ 0, 3, 2 ] ) // Cell 33.
  1109. );
  1110. pasteTable( [
  1111. [ 'aa', 'ab' ],
  1112. [ 'ba', 'bb' ],
  1113. [ 'ca', 'cb' ]
  1114. ] );
  1115. // +----+----+----+----+----+
  1116. // | 00 | 01 | 02 | 03 | 04 |
  1117. // +----+----+----+----+----+
  1118. // | 10 | 11 | aa | ab | 14 |
  1119. // +----+ +----+----+----+
  1120. // | 20 | | ba | bb | 24 |
  1121. // +----+ +----+----+----+
  1122. // | 30 | | ca | cb | 34 |
  1123. // +----+----+----+----+----+
  1124. // | 40 | 41 | 42 | 43 | 44 |
  1125. // +----+----+----+----+----+
  1126. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1127. [ '00', '01', '02', '03', '04' ],
  1128. [ '10', { contents: '11', rowspan: 3 }, 'aa', 'ab', '14' ],
  1129. [ '20', 'ba', 'bb', '24' ],
  1130. [ '30', 'ca', 'cb', '34' ],
  1131. [ '40', '41', '42', '43', '44' ]
  1132. ] ) );
  1133. /* eslint-disable no-multi-spaces */
  1134. assertSelectedCells( model, [
  1135. [ 0, 0, 0, 0, 0 ],
  1136. [ 0, 0, 1, 1, 0 ],
  1137. [ 0, 1, 1, 0 ],
  1138. [ 0, 1, 1, 0 ],
  1139. [ 0, 0, 0, 0, 0 ]
  1140. ] );
  1141. /* eslint-enable no-multi-spaces */
  1142. } );
  1143. it( 'handles pasting simple table over a table with rowspans (rowspan before selection)', () => {
  1144. // +----+----+----+----+----+----+
  1145. // | 00 | 01 | 02 | 03 | 04 | 05 |
  1146. // +----+----+----+----+----+----+
  1147. // | 10 | 12 | 13 | 14 | 15 |
  1148. // +----+----+----+----+----+----+
  1149. // | 20 | 23 | 24 | 25 |
  1150. // +----+----+----+----+----+----+
  1151. // | 30 | 31 | 32 | 33 | 34 | 35 |
  1152. // +----+----+----+----+----+----+
  1153. setModelData( model, modelTable( [
  1154. [ '00', '01', '02', '03', '04', '05' ],
  1155. [ { contents: '10', colspan: 2 }, '12', '13', '14', '15' ],
  1156. [ { contents: '20', colspan: 3 }, '23', '24', '25' ],
  1157. [ '30', '31', '32', '33', '34', '35' ]
  1158. ] ) );
  1159. tableSelection.setCellSelection(
  1160. modelRoot.getNodeByPath( [ 0, 1, 2 ] ), // Cell 13.
  1161. modelRoot.getNodeByPath( [ 0, 2, 2 ] ) // Cell 24.
  1162. );
  1163. pasteTable( [
  1164. [ 'aa', 'ab' ],
  1165. [ 'ba', 'bb' ]
  1166. ] );
  1167. // +----+----+----+----+----+----+
  1168. // | 00 | 01 | 02 | 03 | 04 | 05 |
  1169. // +----+----+----+----+----+----+
  1170. // | 10 | 12 | aa | ab | 15 |
  1171. // +----+----+----+----+----+----+
  1172. // | 20 | ba | bb | 25 |
  1173. // +----+----+----+----+----+----+
  1174. // | 30 | 31 | 32 | 33 | 34 | 35 |
  1175. // +----+----+----+----+----+----+
  1176. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1177. [ '00', '01', '02', '03', '04', '05' ],
  1178. [ { contents: '10', colspan: 2 }, '12', 'aa', 'ab', '15' ],
  1179. [ { contents: '20', colspan: 3 }, 'ba', 'bb', '25' ],
  1180. [ '30', '31', '32', '33', '34', '35' ]
  1181. ] ) );
  1182. /* eslint-disable no-multi-spaces */
  1183. assertSelectedCells( model, [
  1184. [ 0, 0, 0, 0, 0, 0 ],
  1185. [ 0, 0, 1, 1, 0 ],
  1186. [ 0, 1, 1, 0 ],
  1187. [ 0, 0, 0, 0, 0, 0 ]
  1188. ] );
  1189. /* eslint-enable no-multi-spaces */
  1190. } );
  1191. it( 'handles pasting table that has cell with colspan (last row in selection is spanned)', () => {
  1192. // +----+----+----+----+
  1193. // | 00 | 01 | 02 | 03 |
  1194. // +----+----+----+----+
  1195. // | 10 | 11 | 13 |
  1196. // + + +----+
  1197. // | | | 23 |
  1198. // +----+----+----+----+
  1199. // | 30 | 31 | 32 | 33 |
  1200. // +----+----+----+----+
  1201. setModelData( model, modelTable( [
  1202. [ '00', '01', '02', '03' ],
  1203. [ { contents: '10', rowspan: 2 }, { contents: '11', colspan: 2, rowspan: 2 }, '13' ],
  1204. [ '23' ],
  1205. [ '30', '31', '32', '33' ]
  1206. ] ) );
  1207. // Select 02 -> 10 (selection 3x3)
  1208. tableSelection.setCellSelection(
  1209. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  1210. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  1211. );
  1212. pasteTable( [
  1213. [ 'aa', 'ab', 'ac' ],
  1214. [ 'ba', 'bb', 'bc' ],
  1215. [ 'ca', 'cb', 'cc' ]
  1216. ] );
  1217. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1218. [ 'aa', 'ab', 'ac', '03' ],
  1219. [ 'ba', 'bb', 'bc', '13' ],
  1220. [ 'ca', 'cb', 'cc', '23' ],
  1221. [ '30', '31', '32', '33' ]
  1222. ] ) );
  1223. assertSelectedCells( model, [
  1224. [ 1, 1, 1, 0 ],
  1225. [ 1, 1, 1, 0 ],
  1226. [ 1, 1, 1, 0 ],
  1227. [ 0, 0, 0, 0 ]
  1228. ] );
  1229. } );
  1230. it( 'handles pasting table that has cell with rowspan (multiple ending rows in the selection are spanned)', () => {
  1231. // +----+----+----+
  1232. // | 00 | 01 | 02 |
  1233. // +----+ + +
  1234. // | 10 | | |
  1235. // +----+ + +
  1236. // | 20 | | |
  1237. // +----+----+----+
  1238. // | 30 | 31 | 32 |
  1239. // +----+----+----+
  1240. setModelData( model, modelTable( [
  1241. [ '00', { contents: '01', rowspan: 3 }, { contents: '02', rowspan: 3 } ],
  1242. [ '10' ],
  1243. [ '20' ],
  1244. [ '30', '31', '32' ]
  1245. ] ) );
  1246. // Select 01 -> 02 (selection 2x2)
  1247. tableSelection.setCellSelection(
  1248. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  1249. modelRoot.getNodeByPath( [ 0, 0, 2 ] )
  1250. );
  1251. pasteTable( [
  1252. [ 'aa', 'ab' ],
  1253. [ 'ba', 'bb' ],
  1254. [ 'ca', 'cb' ]
  1255. ] );
  1256. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1257. [ '00', 'aa', 'ab' ],
  1258. [ '10', 'ba', 'bb' ],
  1259. [ '20', 'ca', 'cb' ],
  1260. [ '30', '31', '32' ]
  1261. ] ) );
  1262. assertSelectedCells( model, [
  1263. [ 0, 1, 1 ],
  1264. [ 0, 1, 1 ],
  1265. [ 0, 1, 1 ],
  1266. [ 0, 0, 0 ]
  1267. ] );
  1268. } );
  1269. it( 'handles pasting table that has cell with colspan (last column in selection is spanned)', () => {
  1270. // +----+----+----+----+
  1271. // | 00 | 01 | 03 |
  1272. // +----+----+----+----+
  1273. // | 10 | 11 | 13 |
  1274. // +----+ +----+
  1275. // | 20 | | 23 |
  1276. // +----+----+----+----+
  1277. // | 30 | 31 | 32 | 33 |
  1278. // +----+----+----+----+
  1279. setModelData( model, modelTable( [
  1280. [ '00', { contents: '01', colspan: 2 }, '03' ],
  1281. [ '10', { contents: '11', colspan: 2, rowspan: 2 }, '13' ],
  1282. [ '20', '23' ],
  1283. [ '30', '31', '32', '33' ]
  1284. ] ) );
  1285. // Select 20 -> 01 (selection 3x3)
  1286. tableSelection.setCellSelection(
  1287. modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  1288. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  1289. );
  1290. pasteTable( [
  1291. [ 'aa', 'ab', 'ac' ],
  1292. [ 'ba', 'bb', 'bc' ],
  1293. [ 'ca', 'cb', 'cc' ]
  1294. ] );
  1295. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1296. [ 'aa', 'ab', 'ac', '03' ],
  1297. [ 'ba', 'bb', 'bc', '13' ],
  1298. [ 'ca', 'cb', 'cc', '23' ],
  1299. [ '30', '31', '32', '33' ]
  1300. ] ) );
  1301. assertSelectedCells( model, [
  1302. [ 1, 1, 1, 0 ],
  1303. [ 1, 1, 1, 0 ],
  1304. [ 1, 1, 1, 0 ],
  1305. [ 0, 0, 0, 0 ]
  1306. ] );
  1307. } );
  1308. it( 'handles pasting table that has cell with colspan (multiple ending columns in the selection are spanned)', () => {
  1309. // +----+----+----+----+
  1310. // | 00 | 01 | 02 | 03 |
  1311. // +----+----+----+----+
  1312. // | 10 | 13 |
  1313. // +----+----+----+----+
  1314. // | 20 | 23 |
  1315. // +----+----+----+----+
  1316. setModelData( model, modelTable( [
  1317. [ '00', '01', '02', '03' ],
  1318. [ { contents: '10', colspan: 3 }, '13' ],
  1319. [ { contents: '20', colspan: 3 }, '23' ]
  1320. ] ) );
  1321. // Select 10 -> 20 (selection 3x2)
  1322. tableSelection.setCellSelection(
  1323. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  1324. modelRoot.getNodeByPath( [ 0, 2, 0 ] )
  1325. );
  1326. pasteTable( [
  1327. [ 'aa', 'ab', 'ac' ],
  1328. [ 'ba', 'bb', 'bc' ]
  1329. ] );
  1330. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1331. [ '00', '01', '02', '03' ],
  1332. [ 'aa', 'ab', 'ac', '13' ],
  1333. [ 'ba', 'bb', 'bc', '23' ]
  1334. ] ) );
  1335. assertSelectedCells( model, [
  1336. [ 0, 0, 0, 0 ],
  1337. [ 1, 1, 1, 0 ],
  1338. [ 1, 1, 1, 0 ]
  1339. ] );
  1340. } );
  1341. } );
  1342. describe( 'content and paste tables have spans', () => {
  1343. it( 'handles pasting colspanned table over table with colspans (no colspan exceeds selection)', () => {
  1344. setModelData( model, modelTable( [
  1345. [ '00[]', '01', '02', '03' ],
  1346. [ { colspan: 3, contents: '10' }, '13' ],
  1347. [ { colspan: 2, contents: '20' }, '22', '23' ],
  1348. [ '30', '31', { colspan: 2, contents: '31' } ]
  1349. ] ) );
  1350. tableSelection.setCellSelection(
  1351. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1352. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  1353. );
  1354. pasteTable( [
  1355. [ 'aa', { colspan: 2, contents: 'ab' } ],
  1356. [ { colspan: 3, contents: 'ba' } ],
  1357. [ 'ca', 'cb', 'cc' ]
  1358. ] );
  1359. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1360. [ 'aa', { colspan: 2, contents: 'ab' }, '03' ],
  1361. [ { colspan: 3, contents: 'ba' }, '13' ],
  1362. [ 'ca', 'cb', 'cc', '23' ],
  1363. [ '30', '31', { colspan: 2, contents: '31' } ]
  1364. ] ) );
  1365. /* eslint-disable no-multi-spaces */
  1366. assertSelectedCells( model, [
  1367. [ 1, 1, 0 ],
  1368. [ 1, 0 ],
  1369. [ 1, 1, 1, 0 ],
  1370. [ 0, 0, 0 ]
  1371. ] );
  1372. /* eslint-enable no-multi-spaces */
  1373. } );
  1374. it( 'handles pasting rowspanned table over table with rowspans (no rowspan exceeds selection)', () => {
  1375. setModelData( model, modelTable( [
  1376. [ { rowspan: 3, contents: '00' }, { rowspan: 2, contents: '01' }, '02', '03' ],
  1377. [ { rowspan: 2, contents: '12' }, '13' ],
  1378. [ '21', '23' ],
  1379. [ '30', '31', '32', '33' ]
  1380. ] ) );
  1381. tableSelection.setCellSelection(
  1382. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1383. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  1384. );
  1385. pasteTable( [
  1386. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  1387. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  1388. [ 'cc', 'cd' ]
  1389. ] );
  1390. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1391. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  1392. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  1393. [ 'cc', 'cd' ],
  1394. [ '30', '31', '32', '33' ]
  1395. ] ) );
  1396. /* eslint-disable no-multi-spaces */
  1397. assertSelectedCells( model, [
  1398. [ 1, 1, 1, 1 ],
  1399. [ 1, 1 ],
  1400. [ 1, 1 ],
  1401. [ 0, 0, 0, 0 ]
  1402. ] );
  1403. /* eslint-enable no-multi-spaces */
  1404. } );
  1405. it( 'handles pasting multi-spanned table over table with multi-spans (no span exceeds selection)', () => {
  1406. // +----+----+----+----+----+----+
  1407. // | 00 | 02 | 03 | 05 |
  1408. // + + + +----+
  1409. // | | | | 15 |
  1410. // +----+----+----+ +----+
  1411. // | 20 | 21 | | 25 |
  1412. // + +----+----+----+----+----+
  1413. // | | 31 | 32 | 34 | 35 |
  1414. // +----+----+----+----+----+----+
  1415. // | 40 | 41 | 42 | 43 | 44 | 45 |
  1416. // +----+----+----+----+----+----+
  1417. setModelData( model, modelTable( [
  1418. [
  1419. { contents: '00', colspan: 2, rowspan: 2 },
  1420. { contents: '02', rowspan: 2 },
  1421. { contents: '03', colspan: 2, rowspan: 3 },
  1422. '05'
  1423. ],
  1424. [ '15' ],
  1425. [ { contents: '20', rowspan: 2 }, { contents: '21', colspan: 2 }, '25' ],
  1426. [ '31', { contents: '32', colspan: 2 }, '34', '35' ],
  1427. [ '40', '41', '42', '43', '44', '45' ]
  1428. ] ) );
  1429. tableSelection.setCellSelection(
  1430. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1431. modelRoot.getNodeByPath( [ 0, 3, 2 ] )
  1432. );
  1433. // +----+----+----+----+----+
  1434. // | aa | ac | ad | ae |
  1435. // +----+----+----+----+ +
  1436. // | ba | bb | |
  1437. // +----+ +----+
  1438. // | ca | | ce |
  1439. // + +----+----+----+----+
  1440. // | | db | dc | dd |
  1441. // +----+----+----+----+----+
  1442. pasteTable( [
  1443. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 } ],
  1444. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 } ],
  1445. [ { contents: 'ca', rowspan: 2 }, 'ce' ],
  1446. [ 'db', 'dc', { contents: 'dd', colspan: 2 } ]
  1447. ] );
  1448. // +----+----+----+----+----+----+
  1449. // | aa | ac | ad | ae | 05 |
  1450. // +----+----+----+----+ +----+
  1451. // | ba | bb | | 15 |
  1452. // +----+ +----+----+
  1453. // | ca | | ce | 25 |
  1454. // + +----+----+----+----+----+
  1455. // | | db | dc | dd | 35 |
  1456. // +----+----+----+----+----+----+
  1457. // | 40 | 41 | 42 | 43 | 44 | 45 |
  1458. // +----+----+----+----+----+----+
  1459. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1460. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 }, '05' ],
  1461. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 }, '15' ],
  1462. [ { contents: 'ca', rowspan: 2 }, 'ce', '25' ],
  1463. [ 'db', 'dc', { contents: 'dd', colspan: 2 }, '35' ],
  1464. [ '40', '41', '42', '43', '44', '45' ]
  1465. ] ) );
  1466. /* eslint-disable no-multi-spaces */
  1467. assertSelectedCells( model, [
  1468. [ 1, 1, 1, 1, 0 ],
  1469. [ 1, 1, 0 ],
  1470. [ 1, 1, 0 ],
  1471. [ 1, 1, 1, 0 ],
  1472. [ 0, 0, 0, 0, 0, 0 ]
  1473. ] );
  1474. /* eslint-enable no-multi-spaces */
  1475. } );
  1476. it( 'handles pasting table that has cell with colspan (last row in selection is spanned)', () => {
  1477. // +----+----+----+----+
  1478. // | 00 | 01 | 02 | 03 |
  1479. // +----+----+----+----+
  1480. // | 10 | 11 | 13 |
  1481. // + + +----+
  1482. // | | | 23 |
  1483. // +----+----+----+----+
  1484. // | 30 | 31 | 32 | 33 |
  1485. // +----+----+----+----+
  1486. setModelData( model, modelTable( [
  1487. [ '00', '01', '02', '03' ],
  1488. [ { contents: '10', rowspan: 2 }, { contents: '11', colspan: 2, rowspan: 2 }, '13' ],
  1489. [ '23' ],
  1490. [ '30', '31', '32', '33' ]
  1491. ] ) );
  1492. // Select 02 -> 10 (selection 3x3)
  1493. tableSelection.setCellSelection(
  1494. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  1495. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  1496. );
  1497. // +----+----+----+
  1498. // | aa | ab | ac |
  1499. // +----+----+----+
  1500. // | ba | bc |
  1501. // + +----+
  1502. // | | cc |
  1503. // +----+----+----+
  1504. pasteTable( [
  1505. [ 'aa', 'ab', 'ac' ],
  1506. [ { contents: 'ba', colspan: 2, rowspan: 2 }, 'bc' ],
  1507. [ 'cc' ]
  1508. ] );
  1509. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1510. [ 'aa', 'ab', 'ac', '03' ],
  1511. [ { contents: 'ba', colspan: 2, rowspan: 2 }, 'bc', '13' ],
  1512. [ 'cc', '23' ],
  1513. [ '30', '31', '32', '33' ]
  1514. ] ) );
  1515. /* eslint-disable no-multi-spaces */
  1516. assertSelectedCells( model, [
  1517. [ 1, 1, 1, 0 ],
  1518. [ 1, 1, 0 ],
  1519. [ 1, 0 ],
  1520. [ 0, 0, 0, 0 ]
  1521. ] );
  1522. /* eslint-enable no-multi-spaces */
  1523. } );
  1524. it( 'handles pasting table that has cell with colspan (last column in selection is spanned)', () => {
  1525. // +----+----+----+----+
  1526. // | 00 | 01 | 03 |
  1527. // +----+----+----+----+
  1528. // | 10 | 11 | 13 |
  1529. // +----+ +----+
  1530. // | 20 | | 23 |
  1531. // +----+----+----+----+
  1532. // | 30 | 31 | 32 | 33 |
  1533. // +----+----+----+----+
  1534. setModelData( model, modelTable( [
  1535. [ '00', { contents: '01', colspan: 2 }, '03' ],
  1536. [ '10', { contents: '11', colspan: 2, rowspan: 2 }, '13' ],
  1537. [ '20', '23' ],
  1538. [ '30', '31', '32', '33' ]
  1539. ] ) );
  1540. // Select 20 -> 01 (selection 3x3)
  1541. tableSelection.setCellSelection(
  1542. modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  1543. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  1544. );
  1545. // +----+----+----+
  1546. // | aa | ab | ac |
  1547. // +----+----+----+
  1548. // | ba | bc |
  1549. // + +----+
  1550. // | | cc |
  1551. // +----+----+----+
  1552. pasteTable( [
  1553. [ 'aa', 'ab', 'ac' ],
  1554. [ { contents: 'ba', colspan: 2, rowspan: 2 }, 'bc' ],
  1555. [ 'cc' ]
  1556. ] );
  1557. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1558. [ 'aa', 'ab', 'ac', '03' ],
  1559. [ { contents: 'ba', colspan: 2, rowspan: 2 }, 'bc', '13' ],
  1560. [ 'cc', '23' ],
  1561. [ '30', '31', '32', '33' ]
  1562. ] ) );
  1563. /* eslint-disable no-multi-spaces */
  1564. assertSelectedCells( model, [
  1565. [ 1, 1, 1, 0 ],
  1566. [ 1, 1, 0 ],
  1567. [ 1, 0 ],
  1568. [ 0, 0, 0, 0 ]
  1569. ] );
  1570. /* eslint-enable no-multi-spaces */
  1571. } );
  1572. } );
  1573. describe( 'non-rectangular content table selection', () => {
  1574. it( 'should split cells outside the selected area before pasting (rowspan ends in selection)', () => {
  1575. // +----+----+----+
  1576. // | 00 | 01 | 02 |
  1577. // +----+ +----+
  1578. // | 10 | | 12 |
  1579. // +----+ +----+
  1580. // | 20 | | 22 |
  1581. // +----+ +----+
  1582. // | 30 | | 32 |
  1583. // +----+----+----+
  1584. // | 40 | 41 | 42 |
  1585. // +----+----+----+
  1586. setModelData( model, modelTable( [
  1587. [ '00', { contents: '01', rowspan: 4 }, '02' ],
  1588. [ '10', '12' ],
  1589. [ '20', '22' ],
  1590. [ '30', '32' ],
  1591. [ '40', '41', '42' ]
  1592. ] ) );
  1593. // Select 20 -> 32
  1594. tableSelection.setCellSelection(
  1595. modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  1596. modelRoot.getNodeByPath( [ 0, 3, 1 ] )
  1597. );
  1598. pasteTable( [
  1599. [ 'aa', 'ab', 'ac' ],
  1600. [ 'ba', 'bb', 'bc' ]
  1601. ] );
  1602. // +----+----+----+
  1603. // | 00 | 01 | 02 |
  1604. // +----+ +----+
  1605. // | 10 | | 12 |
  1606. // +----+----+----+
  1607. // | aa | ab | ac |
  1608. // +----+----+----+
  1609. // | ba | bb | bc |
  1610. // +----+----+----+
  1611. // | 40 | 41 | 42 |
  1612. // +----+----+----+
  1613. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1614. [ '00', { contents: '01', rowspan: 2 }, '02' ],
  1615. [ '10', '12' ],
  1616. [ 'aa', 'ab', 'ac' ],
  1617. [ 'ba', 'bb', 'bc' ],
  1618. [ '40', '41', '42' ]
  1619. ] ) );
  1620. } );
  1621. it( 'should split cells outside the selected area before pasting (rowspan ends after the selection)', () => {
  1622. // +----+----+----+
  1623. // | 00 | 01 | 02 |
  1624. // +----+ +----+
  1625. // | 10 | | 12 |
  1626. // +----+ +----+
  1627. // | 20 | | 22 |
  1628. // +----+ +----+
  1629. // | 30 | | 32 |
  1630. // +----+----+----+
  1631. // | 40 | 41 | 42 |
  1632. // +----+----+----+
  1633. setModelData( model, modelTable( [
  1634. [ '00', { contents: '01', rowspan: 4 }, '02' ],
  1635. [ '10', '12' ],
  1636. [ '20', '22' ],
  1637. [ '30', '32' ],
  1638. [ '40', '41', '42' ]
  1639. ] ) );
  1640. // Select 10 -> 22
  1641. tableSelection.setCellSelection(
  1642. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  1643. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  1644. );
  1645. pasteTable( [
  1646. [ 'aa', 'ab', 'ac' ],
  1647. [ 'ba', 'bb', 'bc' ]
  1648. ] );
  1649. // +----+----+----+
  1650. // | 00 | 01 | 02 |
  1651. // +----+----+----+
  1652. // | aa | ab | ac |
  1653. // +----+----+----+
  1654. // | ba | bb | bc |
  1655. // +----+----+----+
  1656. // | 30 | | 32 |
  1657. // +----+----+----+
  1658. // | 40 | 41 | 42 |
  1659. // +----+----+----+
  1660. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1661. [ '00', '01', '02' ],
  1662. [ 'aa', 'ab', 'ac' ],
  1663. [ 'ba', 'bb', 'bc' ],
  1664. [ '30', '', '32' ],
  1665. [ '40', '41', '42' ]
  1666. ] ) );
  1667. } );
  1668. it( 'should split cells inside the selected area before pasting (rowspan ends after the selection)', () => {
  1669. // +----+----+----+
  1670. // | 00 | 01 | 02 |
  1671. // +----+ +----+
  1672. // | 10 | | 12 |
  1673. // +----+ +----+
  1674. // | 20 | | 22 |
  1675. // +----+ +----+
  1676. // | 30 | | 32 |
  1677. // +----+----+----+
  1678. // | 40 | 41 | 42 |
  1679. // +----+----+----+
  1680. setModelData( model, modelTable( [
  1681. [ '00', { contents: '01', rowspan: 4 }, '02' ],
  1682. [ '10', '12' ],
  1683. [ '20', '22' ],
  1684. [ '30', '32' ],
  1685. [ '40', '41', '42' ]
  1686. ] ) );
  1687. // Select 00 -> 12
  1688. tableSelection.setCellSelection(
  1689. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1690. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  1691. );
  1692. pasteTable( [
  1693. [ 'aa', 'ab', 'ac' ],
  1694. [ 'ba', 'bb', 'bc' ]
  1695. ] );
  1696. // +----+----+----+
  1697. // | aa | ab | ac |
  1698. // +----+----+----+
  1699. // | ba | bb | bc |
  1700. // +----+----+----+
  1701. // | 20 | | 22 |
  1702. // +----+ +----+
  1703. // | 30 | | 32 |
  1704. // +----+----+----+
  1705. // | 40 | 41 | 42 |
  1706. // +----+----+----+
  1707. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1708. [ 'aa', 'ab', 'ac' ],
  1709. [ 'ba', 'bb', 'bc' ],
  1710. [ '20', { rowspan: 2, contents: '' }, '22' ],
  1711. [ '30', '32' ],
  1712. [ '40', '41', '42' ]
  1713. ] ) );
  1714. } );
  1715. it( 'should split cells outside the selected area before pasting (colspan ends in selection)', () => {
  1716. // +----+----+----+----+----+
  1717. // | 00 | 01 | 02 | 03 | 04 |
  1718. // +----+----+----+----+----+
  1719. // | 10 | 14 |
  1720. // +----+----+----+----+----+
  1721. // | 20 | 21 | 22 | 23 | 24 |
  1722. // +----+----+----+----+----+
  1723. setModelData( model, modelTable( [
  1724. [ '00', '01', '02', '03', '04' ],
  1725. [ { contents: '10', colspan: 4 }, '14' ],
  1726. [ '20', '21', '22', '23', '24' ]
  1727. ] ) );
  1728. // Select 02 -> 23
  1729. tableSelection.setCellSelection(
  1730. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  1731. modelRoot.getNodeByPath( [ 0, 2, 3 ] )
  1732. );
  1733. pasteTable( [
  1734. [ 'aa', 'ab' ],
  1735. [ 'ba', 'bb' ],
  1736. [ 'ca', 'cb' ]
  1737. ] );
  1738. // +----+----+----+----+----+
  1739. // | 00 | 01 | aa | ab | 04 |
  1740. // +----+----+----+----+----+
  1741. // | 10 | ba | bb | 14 |
  1742. // +----+----+----+----+----+
  1743. // | 20 | 21 | ca | cb | 24 |
  1744. // +----+----+----+----+----+
  1745. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1746. [ '00', '01', 'aa', 'ab', '04' ],
  1747. [ { contents: '10', colspan: 2 }, 'ba', 'bb', '14' ],
  1748. [ '20', '21', 'ca', 'cb', '24' ]
  1749. ] ) );
  1750. } );
  1751. it( 'should split cells outside the selected area before pasting (colspan ends after the selection)', () => {
  1752. // +----+----+----+----+----+
  1753. // | 00 | 01 | 02 | 03 | 04 |
  1754. // +----+----+----+----+----+
  1755. // | 10 | 14 |
  1756. // +----+----+----+----+----+
  1757. // | 20 | 21 | 22 | 23 | 24 |
  1758. // +----+----+----+----+----+
  1759. setModelData( model, modelTable( [
  1760. [ '00', '01', '02', '03', '04' ],
  1761. [ { contents: '10', colspan: 4 }, '14' ],
  1762. [ '20', '21', '22', '23', '24' ]
  1763. ] ) );
  1764. // Select 01 -> 22
  1765. tableSelection.setCellSelection(
  1766. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  1767. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  1768. );
  1769. pasteTable( [
  1770. [ 'aa', 'ab' ],
  1771. [ 'ba', 'bb' ],
  1772. [ 'ca', 'cb' ]
  1773. ] );
  1774. // +----+----+----+----+----+
  1775. // | 00 | aa | ab | 03 | 04 |
  1776. // +----+----+----+----+----+
  1777. // | 10 | ba | bb | | 14 |
  1778. // +----+----+----+----+----+
  1779. // | 20 | ca | cb | 23 | 24 |
  1780. // +----+----+----+----+----+
  1781. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1782. [ '00', 'aa', 'ab', '03', '04' ],
  1783. [ '10', 'ba', 'bb', '', '14' ],
  1784. [ '20', 'ca', 'cb', '23', '24' ]
  1785. ] ) );
  1786. } );
  1787. it( 'should split cells inside the selected area before pasting (colspan ends after the selection)', () => {
  1788. // +----+----+----+----+----+
  1789. // | 00 | 01 | 02 | 03 | 04 |
  1790. // +----+----+----+----+----+
  1791. // | 10 | 14 |
  1792. // +----+----+----+----+----+
  1793. // | 20 | 21 | 22 | 23 | 24 |
  1794. // +----+----+----+----+----+
  1795. setModelData( model, modelTable( [
  1796. [ '00', '01', '02', '03', '04' ],
  1797. [ { contents: '10', colspan: 4 }, '14' ],
  1798. [ '20', '21', '22', '23', '24' ]
  1799. ] ) );
  1800. // Select 00 -> 21
  1801. tableSelection.setCellSelection(
  1802. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1803. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  1804. );
  1805. pasteTable( [
  1806. [ 'aa', 'ab' ],
  1807. [ 'ba', 'bb' ],
  1808. [ 'ca', 'cb' ]
  1809. ] );
  1810. // +----+----+----+----+----+
  1811. // | aa | ab | 02 | 03 | 04 |
  1812. // +----+----+----+----+----+
  1813. // | ba | bb | | 14 |
  1814. // +----+----+----+----+----+
  1815. // | ca | cb | 22 | 23 | 24 |
  1816. // +----+----+----+----+----+
  1817. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1818. [ 'aa', 'ab', '02', '03', '04' ],
  1819. [ 'ba', 'bb', { colspan: 2, contents: '' }, '14' ],
  1820. [ 'ca', 'cb', '22', '23', '24' ]
  1821. ] ) );
  1822. } );
  1823. it( 'should split cells anchored outside selection rectangle that overlaps selection (above selection)', () => {
  1824. // +----+----+----+
  1825. // | 00 | 02 |
  1826. // + +----+
  1827. // | | 12 |
  1828. // +----+----+----+
  1829. // | 20 | 21 | 22 |
  1830. // +----+----+----+
  1831. setModelData( model, modelTable( [
  1832. [ { contents: '00', colspan: 2, rowspan: 2 }, '02' ],
  1833. [ '12' ],
  1834. [ '20', '21', '22' ]
  1835. ] ) );
  1836. // Select 21 -> 12
  1837. tableSelection.setCellSelection(
  1838. modelRoot.getNodeByPath( [ 0, 2, 1 ] ),
  1839. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  1840. );
  1841. pasteTable( [
  1842. [ 'aa', 'ab' ],
  1843. [ 'ba', 'bb' ]
  1844. ] );
  1845. // +----+----+----+
  1846. // | 00 | | 02 |
  1847. // + +----+----+
  1848. // | | aa | ab |
  1849. // +----+----+----+
  1850. // | 20 | ba | bb |
  1851. // +----+----+----+
  1852. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1853. [ { contents: '00', rowspan: 2 }, '', '02' ],
  1854. [ 'aa', 'ab' ],
  1855. [ '20', 'ba', 'bb' ]
  1856. ] ) );
  1857. } );
  1858. it( 'should split cells anchored outside selection rectangle that overlaps selection (below selection)', () => {
  1859. // +----+----+----+
  1860. // | 00 | 01 | 02 |
  1861. // +----+----+----+
  1862. // | 10 | 12 |
  1863. // + +----+
  1864. // | | 22 |
  1865. // +----+----+----+
  1866. setModelData( model, modelTable( [
  1867. [ '00', '01', '02' ],
  1868. [ { contents: '10', colspan: 2, rowspan: 2 }, '12' ],
  1869. [ '22' ]
  1870. ] ) );
  1871. // Select 01 -> 12
  1872. tableSelection.setCellSelection(
  1873. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  1874. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  1875. );
  1876. pasteTable( [
  1877. [ 'aa', 'ab' ],
  1878. [ 'ba', 'bb' ]
  1879. ] );
  1880. // +----+----+----+
  1881. // | 00 | aa | ab |
  1882. // +----+----+----+
  1883. // | 10 | ba | bb |
  1884. // + +----+----+
  1885. // | | | 22 |
  1886. // +----+----+----+
  1887. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1888. [ '00', 'aa', 'ab' ],
  1889. [ { contents: '10', rowspan: 2 }, 'ba', 'bb' ],
  1890. [ '', '22' ]
  1891. ] ) );
  1892. } );
  1893. it( 'should properly handle complex case', () => {
  1894. // +----+----+----+----+----+----+----+
  1895. // | 00 | 03 | 04 |
  1896. // + + +----+----+----+
  1897. // | | | 14 | 15 | 16 |
  1898. // + + +----+----+----+
  1899. // | | | 24 |
  1900. // +----+----+----+----+----+----+----+
  1901. // | 30 | 31 | 33 | 34 | 35 | 36 |
  1902. // + +----+----+ +----+----+----+
  1903. // | | 41 | 42 | | 44 | 45 |
  1904. // + +----+----+ +----+ +
  1905. // | | 51 | 52 | | 54 | |
  1906. // +----+----+----+ +----+ +
  1907. // | 60 | 61 | 62 | | 64 | |
  1908. // +----+----+----+----+----+----+----+
  1909. setModelData( model, modelTable( [
  1910. [ { contents: '00', colspan: 3, rowspan: 3 }, { contents: '03', rowspan: 3 }, { colspan: 3, contents: '04' } ],
  1911. [ '14', '15', '16' ],
  1912. [ { contents: '24', colspan: 3 } ],
  1913. [
  1914. { contents: '30', rowspan: 3 },
  1915. { contents: '31', colspan: 2 },
  1916. { contents: '33', rowspan: 4 },
  1917. '34', '35', '36'
  1918. ],
  1919. [ '41', '42', '44', { contents: '45', colspan: 2, rowspan: 3 } ],
  1920. [ '51', '52', '54' ],
  1921. [ '60', '61', '62', '64' ]
  1922. ] ) );
  1923. // Select 42 -> 24 (3x3 selection)
  1924. tableSelection.setCellSelection(
  1925. modelRoot.getNodeByPath( [ 0, 4, 1 ] ),
  1926. modelRoot.getNodeByPath( [ 0, 2, 0 ] )
  1927. );
  1928. pasteTable( [
  1929. [ 'aa', 'ab', 'ac' ],
  1930. [ 'ba', 'bb', 'bc' ],
  1931. [ 'ca', 'cb', 'cc' ]
  1932. ] );
  1933. // +----+----+----+----+----+----+----+
  1934. // | 00 | | 03 | 04 |
  1935. // + + + +----+----+----+
  1936. // | | | | 14 | 15 | 16 |
  1937. // + +----+----+----+----+----+
  1938. // | | aa | ab | ac | |
  1939. // +----+----+----+----+----+----+----+
  1940. // | 30 | 31 | ba | bb | bc | 35 | 36 |
  1941. // + +----+----+----+----+----+----+
  1942. // | | 41 | ca | cb | cc | 45 |
  1943. // + +----+----+----+----+ +
  1944. // | | 51 | 52 | | 54 | |
  1945. // +----+----+----+ +----+ +
  1946. // | 60 | 61 | 62 | | 64 | |
  1947. // +----+----+----+----+----+----+----+
  1948. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1949. [
  1950. { contents: '00', colspan: 2, rowspan: 3 },
  1951. { contents: '', rowspan: 2 },
  1952. { contents: '03', rowspan: 2 },
  1953. { contents: '04', colspan: 3 }
  1954. ],
  1955. [ '14', '15', '16' ],
  1956. [ 'aa', 'ab', 'ac', { contents: '', colspan: 2 } ],
  1957. [ { contents: '30', rowspan: 3 }, '31', 'ba', 'bb', 'bc', '35', '36' ],
  1958. [ '41', 'ca', 'cb', 'cc', { contents: '45', colspan: 2, rowspan: 3 } ],
  1959. [ '51', '52', { contents: '', rowspan: 2 }, '54' ],
  1960. [ '60', '61', '62', '64' ]
  1961. ] ) );
  1962. } );
  1963. } );
  1964. } );
  1965. describe( 'pasted table is bigger than the selected area', () => {
  1966. describe( 'no spans', () => {
  1967. it( 'handles simple table paste to a simple table fragment - at the beginning of a table', () => {
  1968. tableSelection.setCellSelection(
  1969. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1970. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  1971. );
  1972. pasteTable( [
  1973. [ 'aa', 'ab', 'ac' ],
  1974. [ 'ba', 'bb', 'bc' ],
  1975. [ 'ca', 'cb', 'cc' ]
  1976. ] );
  1977. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1978. [ 'aa', 'ab', '02', '03' ],
  1979. [ 'ba', 'bb', '12', '13' ],
  1980. [ '20', '21', '22', '23' ],
  1981. [ '30', '31', '32', '33' ]
  1982. ] ) );
  1983. assertSelectedCells( model, [
  1984. [ 1, 1, 0, 0 ],
  1985. [ 1, 1, 0, 0 ],
  1986. [ 0, 0, 0, 0 ],
  1987. [ 0, 0, 0, 0 ]
  1988. ] );
  1989. } );
  1990. it( 'handles simple table paste to a simple table fragment - at the end of a table', () => {
  1991. tableSelection.setCellSelection(
  1992. modelRoot.getNodeByPath( [ 0, 2, 2 ] ),
  1993. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  1994. );
  1995. pasteTable( [
  1996. [ 'aa', 'ab', 'ac' ],
  1997. [ 'ba', 'bb', 'bc' ],
  1998. [ 'ca', 'cb', 'cc' ]
  1999. ] );
  2000. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2001. [ '00', '01', '02', '03' ],
  2002. [ '10', '11', '12', '13' ],
  2003. [ '20', '21', 'aa', 'ab' ],
  2004. [ '30', '31', 'ba', 'bb' ]
  2005. ] ) );
  2006. assertSelectedCells( model, [
  2007. [ 0, 0, 0, 0 ],
  2008. [ 0, 0, 0, 0 ],
  2009. [ 0, 0, 1, 1 ],
  2010. [ 0, 0, 1, 1 ]
  2011. ] );
  2012. } );
  2013. it( 'handles simple table paste to a simple table fragment - in the middle of a table', () => {
  2014. tableSelection.setCellSelection(
  2015. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  2016. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  2017. );
  2018. pasteTable( [
  2019. [ 'aa', 'ab', 'ac' ],
  2020. [ 'ba', 'bb', 'bc' ],
  2021. [ 'ca', 'cb', 'cc' ]
  2022. ] );
  2023. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2024. [ '00', '01', '02', '03' ],
  2025. [ '10', 'aa', 'ab', '13' ],
  2026. [ '20', 'ba', 'bb', '23' ],
  2027. [ '30', '31', '32', '33' ]
  2028. ] ) );
  2029. assertSelectedCells( model, [
  2030. [ 0, 0, 0, 0 ],
  2031. [ 0, 1, 1, 0 ],
  2032. [ 0, 1, 1, 0 ],
  2033. [ 0, 0, 0, 0 ]
  2034. ] );
  2035. } );
  2036. it( 'handles paste to a simple row fragment - in the middle of a table', () => {
  2037. tableSelection.setCellSelection(
  2038. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  2039. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  2040. );
  2041. pasteTable( [
  2042. [ 'aa', 'ab', 'ac' ],
  2043. [ 'ba', 'bb', 'bc' ],
  2044. [ 'ca', 'cb', 'cc' ]
  2045. ] );
  2046. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2047. [ '00', '01', '02', '03' ],
  2048. [ '10', 'aa', 'ab', '13' ],
  2049. [ '20', '21', '22', '23' ],
  2050. [ '30', '31', '32', '33' ]
  2051. ] ) );
  2052. assertSelectedCells( model, [
  2053. [ 0, 0, 0, 0 ],
  2054. [ 0, 1, 1, 0 ],
  2055. [ 0, 0, 0, 0 ],
  2056. [ 0, 0, 0, 0 ]
  2057. ] );
  2058. } );
  2059. it( 'handles paste to a simple column fragment - in the middle of a table', () => {
  2060. tableSelection.setCellSelection(
  2061. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  2062. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  2063. );
  2064. pasteTable( [
  2065. [ 'aa', 'ab', 'ac' ],
  2066. [ 'ba', 'bb', 'bc' ],
  2067. [ 'ca', 'cb', 'cc' ]
  2068. ] );
  2069. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2070. [ '00', '01', '02', '03' ],
  2071. [ '10', 'aa', '12', '13' ],
  2072. [ '20', 'ba', '22', '23' ],
  2073. [ '30', '31', '32', '33' ]
  2074. ] ) );
  2075. assertSelectedCells( model, [
  2076. [ 0, 0, 0, 0 ],
  2077. [ 0, 1, 0, 0 ],
  2078. [ 0, 1, 0, 0 ],
  2079. [ 0, 0, 0, 0 ]
  2080. ] );
  2081. } );
  2082. it( 'handles simple table paste to a simple table fragment - whole table selected', () => {
  2083. tableSelection.setCellSelection(
  2084. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2085. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  2086. );
  2087. pasteTable( [
  2088. [ 'aa', 'ab', 'ac', 'ad', 'ae' ],
  2089. [ 'ba', 'bb', 'bc', 'bd', 'be' ],
  2090. [ 'ca', 'cb', 'cc', 'cd', 'ce' ],
  2091. [ 'da', 'db', 'dc', 'dd', 'de' ],
  2092. [ 'ea', 'eb', 'ec', 'ed', 'ee' ]
  2093. ] );
  2094. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2095. [ 'aa', 'ab', 'ac', 'ad' ],
  2096. [ 'ba', 'bb', 'bc', 'bd' ],
  2097. [ 'ca', 'cb', 'cc', 'cd' ],
  2098. [ 'da', 'db', 'dc', 'dd' ]
  2099. ] ) );
  2100. assertSelectedCells( model, [
  2101. [ 1, 1, 1, 1 ],
  2102. [ 1, 1, 1, 1 ],
  2103. [ 1, 1, 1, 1 ],
  2104. [ 1, 1, 1, 1 ]
  2105. ] );
  2106. } );
  2107. } );
  2108. describe( 'pasted table has spans', () => {
  2109. it( 'handles pasting table that has cell with colspan', () => {
  2110. tableSelection.setCellSelection(
  2111. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  2112. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  2113. );
  2114. pasteTable( [
  2115. [ { colspan: 3, contents: 'aa' } ],
  2116. [ 'ba', 'bb', 'bc' ]
  2117. ] );
  2118. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2119. [ '00', '01', '02', '03' ],
  2120. [ '10', { colspan: 2, contents: 'aa' }, '13' ],
  2121. [ '20', 'ba', 'bb', '23' ],
  2122. [ '30', '31', '32', '33' ]
  2123. ] ) );
  2124. /* eslint-disable no-multi-spaces */
  2125. assertSelectedCells( model, [
  2126. [ 0, 0, 0, 0 ],
  2127. [ 0, 1, 0 ],
  2128. [ 0, 1, 1, 0 ],
  2129. [ 0, 0, 0, 0 ]
  2130. ] );
  2131. /* eslint-enable no-multi-spaces */
  2132. } );
  2133. it( 'handles pasting table that has many cells with various colspan', () => {
  2134. tableSelection.setCellSelection(
  2135. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2136. modelRoot.getNodeByPath( [ 0, 3, 2 ] )
  2137. );
  2138. pasteTable( [
  2139. [ 'aa', { colspan: 3, contents: 'ab' } ],
  2140. [ { colspan: 4, contents: 'ba' } ],
  2141. [ 'ca', 'cb', 'cc', 'cd' ],
  2142. [ { colspan: 2, contents: 'da' }, 'dc', 'dd' ]
  2143. ] );
  2144. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2145. [ 'aa', { colspan: 2, contents: 'ab' }, '03' ],
  2146. [ { colspan: 3, contents: 'ba' }, '13' ],
  2147. [ 'ca', 'cb', 'cc', '23' ],
  2148. [ { colspan: 2, contents: 'da' }, 'dc', '33' ]
  2149. ] ) );
  2150. /* eslint-disable no-multi-spaces */
  2151. assertSelectedCells( model, [
  2152. [ 1, 1, 0 ],
  2153. [ 1, 0 ],
  2154. [ 1, 1, 1, 0 ],
  2155. [ 1, 1, 0 ]
  2156. ] );
  2157. /* eslint-enable no-multi-spaces */
  2158. } );
  2159. it( 'handles pasting table that has cell with rowspan', () => {
  2160. tableSelection.setCellSelection(
  2161. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  2162. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  2163. );
  2164. pasteTable( [
  2165. [ { rowspan: 3, contents: 'aa' }, 'ab' ],
  2166. [ 'bb' ],
  2167. [ 'cb' ]
  2168. ] );
  2169. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2170. [ '00', '01', '02', '03' ],
  2171. [ '10', { rowspan: 2, contents: 'aa' }, 'ab', '13' ],
  2172. [ '20', 'bb', '23' ],
  2173. [ '30', '31', '32', '33' ]
  2174. ] ) );
  2175. /* eslint-disable no-multi-spaces */
  2176. assertSelectedCells( model, [
  2177. [ 0, 0, 0, 0 ],
  2178. [ 0, 1, 1, 0 ],
  2179. [ 0, 1, 0 ],
  2180. [ 0, 0, 0, 0 ]
  2181. ] );
  2182. /* eslint-enable no-multi-spaces */
  2183. } );
  2184. it( 'handles pasting table that has many cells with various rowspan', () => {
  2185. tableSelection.setCellSelection(
  2186. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2187. modelRoot.getNodeByPath( [ 0, 2, 3 ] )
  2188. );
  2189. pasteTable( [
  2190. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad', 'ae' ],
  2191. [ { rowspan: 3, contents: 'ba' }, 'bd', 'be' ],
  2192. [ 'cc', 'cd', 'ce' ],
  2193. [ 'da', 'db', 'dc', 'dd' ]
  2194. ] );
  2195. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2196. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  2197. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  2198. [ 'cc', 'cd' ],
  2199. [ '30', '31', '32', '33' ]
  2200. ] ) );
  2201. /* eslint-disable no-multi-spaces */
  2202. assertSelectedCells( model, [
  2203. [ 1, 1, 1, 1 ],
  2204. [ 1, 1 ],
  2205. [ 1, 1 ],
  2206. [ 0, 0, 0, 0 ]
  2207. ] );
  2208. /* eslint-enable no-multi-spaces */
  2209. } );
  2210. it( 'handles pasting multi-spanned table', () => {
  2211. setModelData( model, modelTable( [
  2212. [ '00', '01', '02', '03', '04', '05' ],
  2213. [ '10', '11', '12', '13', '14', '15' ],
  2214. [ '20', '21', '22', '23', '24', '25' ],
  2215. [ '30', '31', '32', '33', '34', '35' ],
  2216. [ '40', '41', '42', '43', '44', '45' ]
  2217. ] ) );
  2218. tableSelection.setCellSelection(
  2219. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  2220. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  2221. );
  2222. // +----+----+----+----+----+
  2223. // | aa | ac | ad | ae |
  2224. // +----+----+----+----+ +
  2225. // | ba | bb | |
  2226. // +----+ +----+
  2227. // | ca | | ce |
  2228. // + +----+----+----+----+
  2229. // | | db | dc | dd |
  2230. // +----+----+----+----+----+
  2231. pasteTable( [
  2232. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 } ],
  2233. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 } ],
  2234. [ { contents: 'ca', rowspan: 2 }, 'ce' ],
  2235. [ 'db', 'dc', { contents: 'dd', colspan: 2 } ]
  2236. ] );
  2237. // +----+----+----+----+----+----+
  2238. // | 00 | 01 | 02 | 03 | 04 | 05 |
  2239. // +----+----+----+----+----+----+
  2240. // | 10 | aa | ac | 14 | 15 |
  2241. // +----+----+----+----+----+----+
  2242. // | 20 | ba | bb | 24 | 25 |
  2243. // +----+----+ +----+----+
  2244. // | 30 | ca | | 34 | 35 |
  2245. // +----+----+----+----+----+----+
  2246. // | 40 | 41 | 42 | 43 | 44 | 45 |
  2247. // +----+----+----+----+----+----+
  2248. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2249. [ '00', '01', '02', '03', '04', '05' ],
  2250. [ '10', { contents: 'aa', colspan: 2 }, 'ac', '14', '15' ],
  2251. [ '20', 'ba', { contents: 'bb', colspan: 2, rowspan: 2 }, '24', '25' ],
  2252. [ '30', 'ca', '34', '35' ],
  2253. [ '40', '41', '42', '43', '44', '45' ]
  2254. ] ) );
  2255. /* eslint-disable no-multi-spaces */
  2256. assertSelectedCells( model, [
  2257. [ 0, 0, 0, 0, 0, 0 ],
  2258. [ 0, 1, 1, 0, 0 ],
  2259. [ 0, 1, 1, 0, 0 ],
  2260. [ 0, 1, 0, 0 ],
  2261. [ 0, 0, 0, 0, 0, 0 ]
  2262. ] );
  2263. /* eslint-enable no-multi-spaces */
  2264. } );
  2265. } );
  2266. } );
  2267. describe( 'pasted table is smaller than the selected area', () => {
  2268. describe( 'no spans', () => {
  2269. beforeEach( () => {
  2270. setModelData( model, modelTable( [
  2271. [ '00', '01', '02', '03', '04', '05' ],
  2272. [ '10', '11', '12', '13', '14', '15' ],
  2273. [ '20', '21', '22', '23', '24', '25' ],
  2274. [ '30', '31', '32', '33', '34', '35' ],
  2275. [ '40', '41', '42', '43', '44', '45' ],
  2276. [ '50', '51', '52', '53', '54', '55' ]
  2277. ] ) );
  2278. } );
  2279. it( 'should repeat pasted cells horizontally', () => {
  2280. tableSelection.setCellSelection(
  2281. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2282. modelRoot.getNodeByPath( [ 0, 1, 4 ] )
  2283. );
  2284. pasteTable( [
  2285. [ 'aa', 'ab', 'ac' ],
  2286. [ 'ba', 'bb', 'bc' ],
  2287. [ 'ca', 'cb', 'cc' ]
  2288. ] );
  2289. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2290. [ 'aa', 'ab', 'ac', 'aa', 'ab', '05' ],
  2291. [ 'ba', 'bb', 'bc', 'ba', 'bb', '15' ],
  2292. [ '20', '21', '22', '23', '24', '25' ],
  2293. [ '30', '31', '32', '33', '34', '35' ],
  2294. [ '40', '41', '42', '43', '44', '45' ],
  2295. [ '50', '51', '52', '53', '54', '55' ]
  2296. ] ) );
  2297. assertSelectedCells( model, [
  2298. [ 1, 1, 1, 1, 1, 0 ],
  2299. [ 1, 1, 1, 1, 1, 0 ],
  2300. [ 0, 0, 0, 0, 0, 0 ],
  2301. [ 0, 0, 0, 0, 0, 0 ]
  2302. ] );
  2303. } );
  2304. it( 'should repeat pasted cells vertically', () => {
  2305. tableSelection.setCellSelection(
  2306. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2307. modelRoot.getNodeByPath( [ 0, 4, 1 ] )
  2308. );
  2309. pasteTable( [
  2310. [ 'aa', 'ab', 'ac' ],
  2311. [ 'ba', 'bb', 'bc' ],
  2312. [ 'ca', 'cb', 'cc' ]
  2313. ] );
  2314. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2315. [ 'aa', 'ab', '02', '03', '04', '05' ],
  2316. [ 'ba', 'bb', '12', '13', '14', '15' ],
  2317. [ 'ca', 'cb', '22', '23', '24', '25' ],
  2318. [ 'aa', 'ab', '32', '33', '34', '35' ],
  2319. [ 'ba', 'bb', '42', '43', '44', '45' ],
  2320. [ '50', '51', '52', '53', '54', '55' ]
  2321. ] ) );
  2322. assertSelectedCells( model, [
  2323. [ 1, 1, 0, 0, 0, 0 ],
  2324. [ 1, 1, 0, 0, 0, 0 ],
  2325. [ 1, 1, 0, 0, 0, 0 ],
  2326. [ 1, 1, 0, 0, 0, 0 ],
  2327. [ 1, 1, 0, 0, 0, 0 ],
  2328. [ 0, 0, 0, 0, 0, 0 ]
  2329. ] );
  2330. } );
  2331. it( 'should repeat pasted cells in both directions', () => {
  2332. tableSelection.setCellSelection(
  2333. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2334. modelRoot.getNodeByPath( [ 0, 4, 4 ] )
  2335. );
  2336. pasteTable( [
  2337. [ 'aa', 'ab', 'ac' ],
  2338. [ 'ba', 'bb', 'bc' ],
  2339. [ 'ca', 'cb', 'cc' ]
  2340. ] );
  2341. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2342. [ 'aa', 'ab', 'ac', 'aa', 'ab', '05' ],
  2343. [ 'ba', 'bb', 'bc', 'ba', 'bb', '15' ],
  2344. [ 'ca', 'cb', 'cc', 'ca', 'cb', '25' ],
  2345. [ 'aa', 'ab', 'ac', 'aa', 'ab', '35' ],
  2346. [ 'ba', 'bb', 'bc', 'ba', 'bb', '45' ],
  2347. [ '50', '51', '52', '53', '54', '55' ]
  2348. ] ) );
  2349. assertSelectedCells( model, [
  2350. [ 1, 1, 1, 1, 1, 0 ],
  2351. [ 1, 1, 1, 1, 1, 0 ],
  2352. [ 1, 1, 1, 1, 1, 0 ],
  2353. [ 1, 1, 1, 1, 1, 0 ],
  2354. [ 1, 1, 1, 1, 1, 0 ],
  2355. [ 0, 0, 0, 0, 0, 0 ]
  2356. ] );
  2357. } );
  2358. it( 'should repeat pasted cells in the both directions when pasted on table end', () => {
  2359. tableSelection.setCellSelection(
  2360. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  2361. modelRoot.getNodeByPath( [ 0, 5, 5 ] )
  2362. );
  2363. pasteTable( [
  2364. [ 'aa', 'ab', 'ac' ],
  2365. [ 'ba', 'bb', 'bc' ],
  2366. [ 'ca', 'cb', 'cc' ]
  2367. ] );
  2368. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2369. [ '00', '01', '02', '03', '04', '05' ],
  2370. [ '10', 'aa', 'ab', 'ac', 'aa', 'ab' ],
  2371. [ '20', 'ba', 'bb', 'bc', 'ba', 'bb' ],
  2372. [ '30', 'ca', 'cb', 'cc', 'ca', 'cb' ],
  2373. [ '40', 'aa', 'ab', 'ac', 'aa', 'ab' ],
  2374. [ '50', 'ba', 'bb', 'bc', 'ba', 'bb' ]
  2375. ] ) );
  2376. assertSelectedCells( model, [
  2377. [ 0, 0, 0, 0, 0, 0 ],
  2378. [ 0, 1, 1, 1, 1, 1 ],
  2379. [ 0, 1, 1, 1, 1, 1 ],
  2380. [ 0, 1, 1, 1, 1, 1 ],
  2381. [ 0, 1, 1, 1, 1, 1 ],
  2382. [ 0, 1, 1, 1, 1, 1 ]
  2383. ] );
  2384. } );
  2385. } );
  2386. describe( 'content table has spans', () => {
  2387. beforeEach( () => {
  2388. // +----+----+----+----+----+----+
  2389. // | 00 | 01 | 02 | 03 | 04 | 05 |
  2390. // +----+----+----+ +----+----+
  2391. // | 10 | 11 | 12 | | 14 | 15 |
  2392. // +----+----+ + +----+----+
  2393. // | 20 | 21 | | | 24 | 25 |
  2394. // +----+----+----+ + +----+
  2395. // | 30 | 31 | | | 35 |
  2396. // +----+----+----+----+----+----+
  2397. // | 40 | 44 |
  2398. // +----+----+----+----+ +
  2399. // | 50 | 51 | 52 | |
  2400. // +----+----+----+----+----+----+
  2401. // | 60 | 61 | 62 | 63 | 64 | 65 |
  2402. // +----+----+----+----+----+----+
  2403. setModelData( model, modelTable( [
  2404. [ '00', '01', '02', { contents: '03', rowspan: 4 }, '04', '05' ],
  2405. [ '10', '11', { contents: '12', rowspan: 2 }, '14', '15' ],
  2406. [ '20', '21', { contents: '24', rowspan: 2 }, '25' ],
  2407. [ '30', { contents: '31', colspan: 2 }, '35' ],
  2408. [ { contents: '40', colspan: 4 }, { contents: '44', colspan: 2, rowspan: 2 } ],
  2409. [ '50', '51', { contents: '52', colspan: 2 } ],
  2410. [ '60', '61', '62', '63', '64', '65' ]
  2411. ] ) );
  2412. } );
  2413. it( 'should split spanned cells on the selection edges (vertical spans)', () => {
  2414. tableSelection.setCellSelection(
  2415. modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  2416. modelRoot.getNodeByPath( [ 0, 4, 1 ] ) // Cell 44.
  2417. );
  2418. pasteTable( [
  2419. [ 'aa', 'ab' ],
  2420. [ 'ba', 'bb' ]
  2421. ] );
  2422. // +----+----+----+----+----+----+
  2423. // | 00 | 01 | 02 | 03 | 04 | 05 |
  2424. // +----+----+----+ +----+----+
  2425. // | 10 | 11 | 12 | | 14 | 15 |
  2426. // +----+----+----+----+----+----+
  2427. // | aa | ab | aa | ab | aa | 25 |
  2428. // +----+----+----+----+----+----+
  2429. // | ba | bb | ba | bb | ba | 35 |
  2430. // +----+----+----+----+----+----+
  2431. // | aa | ab | aa | ab | aa | |
  2432. // +----+----+----+----+----+ +
  2433. // | 50 | 51 | 52 | | |
  2434. // +----+----+----+----+----+----+
  2435. // | 60 | 61 | 62 | 63 | 64 | 65 |
  2436. // +----+----+----+----+----+----+
  2437. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2438. [ '00', '01', '02', { contents: '03', rowspan: 2 }, '04', '05' ],
  2439. [ '10', '11', '12', '14', '15' ],
  2440. [ 'aa', 'ab', 'aa', 'ab', 'aa', '25' ],
  2441. [ 'ba', 'bb', 'ba', 'bb', 'ba', '35' ],
  2442. [ 'aa', 'ab', 'aa', 'ab', 'aa', { contents: '', rowspan: 2 } ],
  2443. [ '50', '51', { contents: '52', colspan: 2 }, '' ],
  2444. [ '60', '61', '62', '63', '64', '65' ]
  2445. ] ) );
  2446. } );
  2447. it( 'should split spanned cells on the selection edges (horizontal spans)', () => {
  2448. tableSelection.setCellSelection(
  2449. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  2450. modelRoot.getNodeByPath( [ 0, 4, 1 ] ) // Cell 44.
  2451. );
  2452. pasteTable( [
  2453. [ 'aa', 'ab' ],
  2454. [ 'ba', 'bb' ]
  2455. ] );
  2456. // +----+----+----+----+----+----+
  2457. // | 00 | 01 | aa | ab | aa | 05 |
  2458. // +----+----+----+----+----+----+
  2459. // | 10 | 11 | ba | bb | ba | 15 |
  2460. // +----+----+----+----+----+----+
  2461. // | 20 | 21 | aa | ab | aa | 25 |
  2462. // +----+----+----+----+----+----+
  2463. // | 30 | 31 | ba | bb | ba | 35 |
  2464. // +----+----+----+----+----+----+
  2465. // | 40 | aa | ab | aa | |
  2466. // +----+----+----+----+----+ +
  2467. // | 50 | 51 | 52 | | |
  2468. // +----+----+----+----+----+----+
  2469. // | 60 | 61 | 62 | 63 | 64 | 65 |
  2470. // +----+----+----+----+----+----+
  2471. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2472. [ '00', '01', 'aa', 'ab', 'aa', '05' ],
  2473. [ '10', '11', 'ba', 'bb', 'ba', '15' ],
  2474. [ '20', '21', 'aa', 'ab', 'aa', '25' ],
  2475. [ '30', '31', 'ba', 'bb', 'ba', '35' ],
  2476. [ { contents: '40', colspan: 2 }, 'aa', 'ab', 'aa', { contents: '', rowspan: 2 } ],
  2477. [ '50', '51', { contents: '52', colspan: 2 }, '' ],
  2478. [ '60', '61', '62', '63', '64', '65' ]
  2479. ] ) );
  2480. } );
  2481. } );
  2482. describe( 'pasted table has spans', () => {
  2483. beforeEach( () => {
  2484. setModelData( model, modelTable( [
  2485. [ '00', '01', '02', '03', '04', '05', '06' ],
  2486. [ '10', '11', '12', '13', '14', '15', '16' ],
  2487. [ '20', '21', '22', '23', '24', '25', '26' ],
  2488. [ '30', '31', '32', '33', '34', '35', '36' ],
  2489. [ '40', '41', '42', '43', '44', '45', '46' ],
  2490. [ '50', '51', '52', '53', '54', '55', '56' ],
  2491. [ '60', '61', '62', '63', '64', '65', '66' ],
  2492. [ '70', '71', '72', '73', '74', '75', '76' ],
  2493. [ '80', '81', '82', '83', '84', '85', '86' ]
  2494. ] ) );
  2495. } );
  2496. it( 'should trim overlapping cells', () => {
  2497. tableSelection.setCellSelection(
  2498. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  2499. modelRoot.getNodeByPath( [ 0, 8, 6 ] )
  2500. );
  2501. // +----+----+----+----+
  2502. // | aa |
  2503. // +----+----+----+----+
  2504. // | ba | bb |
  2505. // + +----+----+----+
  2506. // | | cb | cc |
  2507. // + + +----+----+
  2508. // | | | dc | dd |
  2509. // + + + +----+
  2510. // | | | | ed |
  2511. // +----+----+----+----+
  2512. pasteTable( [
  2513. [ { contents: 'aa', colspan: 4 } ],
  2514. [ { contents: 'ba', rowspan: 4 }, { contents: 'bb', colspan: 3 } ],
  2515. [ { contents: 'cb', rowspan: 3 }, { contents: 'cc', colspan: 2 } ],
  2516. [ { contents: 'dc', rowspan: 2 }, 'dd' ],
  2517. [ 'ed' ]
  2518. ] );
  2519. // +----+----+----+----+----+----+----+
  2520. // | 00 | 01 | 02 | 03 | 04 | 05 | 06 |
  2521. // +----+----+----+----+----+----+----+
  2522. // | 10 | aa | aa |
  2523. // +----+----+----+----+----+----+----+
  2524. // | 20 | ba | bb | ba | bb |
  2525. // +----+ +----+----+----+ +----+
  2526. // | 30 | | cb | cc | | cb |
  2527. // +----+ + +----+----+ + +
  2528. // | 40 | | | dc | dd | | |
  2529. // +----+ + + +----+ + +
  2530. // | 50 | | | | ed | | |
  2531. // +----+----+----+----+----+----+----+
  2532. // | 60 | aa | aa |
  2533. // +----+----+----+----+----+----+----+
  2534. // | 70 | ba | bb | ba | bb |
  2535. // +----+ +----+----+----+ +----+
  2536. // | 80 | | cb | cc | | cb |
  2537. // +----+----+----+----+----+----+----+
  2538. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2539. [ '00', '01', '02', '03', '04', '05', '06' ],
  2540. [ '10', { contents: 'aa', colspan: 4 }, { contents: 'aa', colspan: 2 } ],
  2541. [ '20', { contents: 'ba', rowspan: 4 }, { contents: 'bb', colspan: 3 }, { contents: 'ba', rowspan: 4 }, 'bb' ],
  2542. [ '30', { contents: 'cb', rowspan: 3 }, { contents: 'cc', colspan: 2 }, { contents: 'cb', rowspan: 3 } ],
  2543. [ '40', { contents: 'dc', rowspan: 2 }, 'dd' ],
  2544. [ '50', 'ed' ],
  2545. [ '60', { contents: 'aa', colspan: 4 }, { contents: 'aa', colspan: 2 } ],
  2546. [ '70', { contents: 'ba', rowspan: 2 }, { contents: 'bb', colspan: 3 }, { contents: 'ba', rowspan: 2 }, 'bb' ],
  2547. [ '80', 'cb', { contents: 'cc', colspan: 2 }, 'cb' ]
  2548. ] ) );
  2549. } );
  2550. } );
  2551. } );
  2552. describe( 'fixing pasted table broken layout', () => {
  2553. it( 'should trim pasted cells\' width if they exceeds table width established by the first row', () => {
  2554. // Select 00 -> 22 (selection 2x3 - equal to expected fixed table)
  2555. tableSelection.setCellSelection(
  2556. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2557. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  2558. );
  2559. // +----+----+
  2560. // | aa | ab | <- First row establish table width=2.
  2561. // +----+----+----+----+
  2562. // | ba | bb | <- Cell "bb" sticks out by 2 slots.
  2563. // +----+----+----+----+
  2564. // | ca | <- Cell "ca" sticks out by 1 slot.
  2565. // +----+----+----+
  2566. pasteTable( [
  2567. [ 'aa', 'ab' ],
  2568. [ 'ba', { colspan: 3, contents: 'bb' } ],
  2569. [ { colspan: 3, contents: 'ca' } ]
  2570. ] );
  2571. // +----+----+----+----+
  2572. // | aa | ab | 02 | 03 |
  2573. // +----+----+----+----+
  2574. // | ba | bb | 12 | 13 |
  2575. // +----+----+----+----+
  2576. // | ca | 22 | 23 |
  2577. // +----+----+----+----+
  2578. // | 30 | 31 | 32 | 33 |
  2579. // +----+----+----+----+
  2580. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2581. [ 'aa', 'ab', '02', '03' ],
  2582. [ 'ba', 'bb', '12', '13' ],
  2583. [ { contents: 'ca', colspan: 2 }, '22', '23' ],
  2584. [ '30', '31', '32', '33' ]
  2585. ] ) );
  2586. /* eslint-disable no-multi-spaces */
  2587. assertSelectedCells( model, [
  2588. [ 1, 1, 0, 0 ],
  2589. [ 1, 1, 0, 0 ],
  2590. [ 1, 0, 0 ],
  2591. [ 0, 0, 0, 0 ]
  2592. ] );
  2593. /* eslint-enable no-multi-spaces */
  2594. } );
  2595. it( 'should trim pasted cells\' height if they exceeds table height established by the last row', () => {
  2596. // Select 00 -> 12 (selection 3x2 - equal to expected fixed table)
  2597. tableSelection.setCellSelection(
  2598. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2599. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  2600. );
  2601. // +----+----+----+
  2602. // | aa | ab | ac |
  2603. // +----+----+ +
  2604. // | ba | bb | | <- Last row establish table height=2.
  2605. // +----+ + +
  2606. // | | | <- Cell "ac" sticks out by 1 slot.
  2607. // + +----+
  2608. // | | <- Cell "bb" sticks out by 2 slots.
  2609. // +----+
  2610. pasteTable( [
  2611. [ 'aa', 'ab', { contents: 'ac', rowspan: 3 } ],
  2612. [ 'ba', { contents: 'bb', rowspan: 3 } ]
  2613. ] );
  2614. // +----+----+----+----+
  2615. // | aa | ab | ac | 03 |
  2616. // +----+----+ +----+
  2617. // | ba | bb | | 13 |
  2618. // +----+----+----+----+
  2619. // | 20 | 21 | 22 | 23 |
  2620. // +----+----+----+----+
  2621. // | 30 | 31 | 32 | 33 |
  2622. // +----+----+----+----+
  2623. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2624. [ 'aa', 'ab', { rowspan: 2, contents: 'ac' }, '03' ],
  2625. [ 'ba', 'bb', '13' ],
  2626. [ '20', '21', '22', '23' ],
  2627. [ '30', '31', '32', '33' ]
  2628. ] ) );
  2629. /* eslint-disable no-multi-spaces */
  2630. assertSelectedCells( model, [
  2631. [ 1, 1, 1, 0 ],
  2632. [ 1, 1, 0 ],
  2633. [ 0, 0, 0, 0 ],
  2634. [ 0, 0, 0, 0 ]
  2635. ] );
  2636. /* eslint-enable no-multi-spaces */
  2637. } );
  2638. it( 'should trim pasted cells\' height and width if they exceeds table height and width', () => {
  2639. // Select 00 -> 11 (selection 2x2 - equal to expected fixed table)
  2640. tableSelection.setCellSelection(
  2641. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2642. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  2643. );
  2644. // +----+----+
  2645. // | aa | ab |
  2646. // +----+----+----+----+
  2647. // | ba | bb | <- Cell "bb" sticks out by 2 slots in width and by 1 slot in height.
  2648. // +----+ +
  2649. // | |
  2650. // +----+----+----+
  2651. pasteTable( [
  2652. [ 'aa', 'ab' ],
  2653. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 } ]
  2654. ] );
  2655. // +----+----+----+----+
  2656. // | aa | ab | 02 | 03 |
  2657. // +----+----+----+----+
  2658. // | ba | bb | 12 | 13 |
  2659. // +----+----+----+----+
  2660. // | 20 | 21 | 22 | 23 |
  2661. // +----+----+----+----+
  2662. // | 30 | 31 | 32 | 33 |
  2663. // +----+----+----+----+
  2664. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2665. [ 'aa', 'ab', '02', '03' ],
  2666. [ 'ba', 'bb', '12', '13' ],
  2667. [ '20', '21', '22', '23' ],
  2668. [ '30', '31', '32', '33' ]
  2669. ] ) );
  2670. /* eslint-disable no-multi-spaces */
  2671. assertSelectedCells( model, [
  2672. [ 1, 1, 0, 0 ],
  2673. [ 1, 1, 0, 0 ],
  2674. [ 0, 0, 0, 0 ],
  2675. [ 0, 0, 0, 0 ]
  2676. ] );
  2677. /* eslint-enable no-multi-spaces */
  2678. } );
  2679. it(
  2680. 'should trim pasted cells\' width if they exceeds pasted table width (pasted height is bigger then selection height)',
  2681. () => {
  2682. // Select 00 -> 11 (selection 2x2 - smaller by height than the expected fixed table)
  2683. tableSelection.setCellSelection(
  2684. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2685. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  2686. );
  2687. // +----+----+
  2688. // | aa | ab | <- First row establish table width=2.
  2689. // +----+----+----+----+
  2690. // | ba | bb | <- Cell "bb" sticks out by 2 slots.
  2691. // +----+----+----+----+
  2692. // | ca | <- Cell "ca" sticks out by 1 slot.
  2693. // +----+----+----+
  2694. pasteTable( [
  2695. [ 'aa', 'ab' ],
  2696. [ 'ba', { colspan: 3, contents: 'bb' } ],
  2697. [ { colspan: 3, contents: 'ca' } ]
  2698. ] );
  2699. // +----+----+----+----+
  2700. // | aa | ab | 02 | 03 |
  2701. // +----+----+----+----+
  2702. // | ba | bb | 12 | 13 |
  2703. // +----+----+----+----+
  2704. // | 20 | 21 | 22 | 23 |
  2705. // +----+----+----+----+
  2706. // | 30 | 31 | 32 | 33 |
  2707. // +----+----+----+----+
  2708. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2709. [ 'aa', 'ab', '02', '03' ],
  2710. [ 'ba', 'bb', '12', '13' ],
  2711. [ '20', '21', '22', '23' ],
  2712. [ '30', '31', '32', '33' ]
  2713. ] ) );
  2714. assertSelectedCells( model, [
  2715. [ 1, 1, 0, 0 ],
  2716. [ 1, 1, 0, 0 ],
  2717. [ 0, 0, 0, 0 ],
  2718. [ 0, 0, 0, 0 ]
  2719. ] );
  2720. }
  2721. );
  2722. it(
  2723. 'should trim pasted cells\' height if they exceeds pasted table height (pasted width is bigger then selection width)',
  2724. () => {
  2725. // Select 00 -> 11 (selection 2x2 - smaller by width than the expected fixed table)
  2726. tableSelection.setCellSelection(
  2727. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2728. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  2729. );
  2730. // +----+----+----+
  2731. // | aa | ab | ac |
  2732. // +----+----+ +
  2733. // | ba | bb | | <- Last row establish table height=2.
  2734. // +----+ + +
  2735. // | | | <- Cell "ac" sticks out by 1 slot.
  2736. // + +----+
  2737. // | | <- Cell "bb" sticks out by 2 slots.
  2738. // +----+
  2739. pasteTable( [
  2740. [ 'aa', 'ab', { contents: 'ac', rowspan: 3 } ],
  2741. [ 'ba', { contents: 'bb', rowspan: 3 } ]
  2742. ] );
  2743. // +----+----+----+----+
  2744. // | aa | ab | 02 | 03 |
  2745. // +----+----+----+----+
  2746. // | ba | bb | 12 | 13 |
  2747. // +----+----+----+----+
  2748. // | 20 | 21 | 22 | 23 |
  2749. // +----+----+----+----+
  2750. // | 30 | 31 | 32 | 33 |
  2751. // +----+----+----+----+
  2752. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2753. [ 'aa', 'ab', '02', '03' ],
  2754. [ 'ba', 'bb', '12', '13' ],
  2755. [ '20', '21', '22', '23' ],
  2756. [ '30', '31', '32', '33' ]
  2757. ] ) );
  2758. /* eslint-disable no-multi-spaces */
  2759. assertSelectedCells( model, [
  2760. [ 1, 1, 0, 0 ],
  2761. [ 1, 1, 0, 0 ],
  2762. [ 0, 0, 0, 0 ],
  2763. [ 0, 0, 0, 0 ]
  2764. ] );
  2765. /* eslint-enable no-multi-spaces */
  2766. }
  2767. );
  2768. it(
  2769. `should trim pasted pasted cells' height and width if they exceeds pasted table dimensions
  2770. (pasted table is bigger then selection width)`,
  2771. () => {
  2772. // Select 00 -> 11 (selection 2x2 - smaller than the expected fixed table)
  2773. tableSelection.setCellSelection(
  2774. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2775. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  2776. );
  2777. // +----+----+----+
  2778. // | aa | ab | ac |
  2779. // +----+----+----+----+
  2780. // | ba | bb | <- Cell "bb" sticks out by 1 slots in width and by 1 slot in height.
  2781. // +----+ +
  2782. // | ca | |
  2783. // +----+ +
  2784. // | |
  2785. // +----+----+----+
  2786. pasteTable( [
  2787. [ 'aa', 'ab' ],
  2788. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 } ]
  2789. ] );
  2790. // +----+----+----+----+
  2791. // | aa | ab | 02 | 03 |
  2792. // +----+----+----+----+
  2793. // | ba | bb | 12 | 13 |
  2794. // +----+----+----+----+
  2795. // | 20 | 21 | 22 | 23 |
  2796. // +----+----+----+----+
  2797. // | 30 | 31 | 32 | 33 |
  2798. // +----+----+----+----+
  2799. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2800. [ 'aa', 'ab', '02', '03' ],
  2801. [ 'ba', 'bb', '12', '13' ],
  2802. [ '20', '21', '22', '23' ],
  2803. [ '30', '31', '32', '33' ]
  2804. ] ) );
  2805. /* eslint-disable no-multi-spaces */
  2806. assertSelectedCells( model, [
  2807. [ 1, 1, 0, 0 ],
  2808. [ 1, 1, 0, 0 ],
  2809. [ 0, 0, 0, 0 ],
  2810. [ 0, 0, 0, 0 ]
  2811. ] );
  2812. /* eslint-enable no-multi-spaces */
  2813. }
  2814. );
  2815. } );
  2816. } );
  2817. describe( 'Clipboard integration - paste (content scenarios)', () => {
  2818. it( 'handles multiple paragraphs in table cell', async () => {
  2819. await createEditor();
  2820. setModelData( model, modelTable( [
  2821. [ '00', '01', '02' ],
  2822. [ '01', '11', '12' ],
  2823. [ '02', '21', '22' ]
  2824. ] ) );
  2825. tableSelection.setCellSelection(
  2826. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2827. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  2828. );
  2829. pasteTable( [
  2830. [ '<p>a</p><p>a</p><p>a</p>', 'ab' ],
  2831. [ 'ba', 'bb' ]
  2832. ] );
  2833. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2834. [ '<paragraph>a</paragraph><paragraph>a</paragraph><paragraph>a</paragraph>', 'ab', '02' ],
  2835. [ 'ba', 'bb', '12' ],
  2836. [ '02', '21', '22' ]
  2837. ] ) );
  2838. } );
  2839. it( 'handles image in table cell', async () => {
  2840. await createEditor( [ ImageEditing, ImageCaptionEditing ] );
  2841. setModelData( model, modelTable( [
  2842. [ '00', '01', '02' ],
  2843. [ '01', '11', '12' ],
  2844. [ '02', '21', '22' ]
  2845. ] ) );
  2846. tableSelection.setCellSelection(
  2847. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2848. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  2849. );
  2850. pasteTable( [
  2851. [ '<img src="/assets/sample.jpg">', 'ab' ],
  2852. [ 'ba', 'bb' ]
  2853. ] );
  2854. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2855. [ '<image src="/assets/sample.jpg"><caption></caption></image>', 'ab', '02' ],
  2856. [ 'ba', 'bb', '12' ],
  2857. [ '02', '21', '22' ]
  2858. ] ) );
  2859. } );
  2860. it( 'handles mixed nested content in table cell', async () => {
  2861. await createEditor( [ ImageEditing, ImageCaptionEditing, BlockQuoteEditing, HorizontalLineEditing, ListEditing ] );
  2862. setModelData( model, modelTable( [
  2863. [ '00', '01', '02' ],
  2864. [ '01', '11', '12' ],
  2865. [ '02', '21', '22' ]
  2866. ] ) );
  2867. tableSelection.setCellSelection(
  2868. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2869. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  2870. );
  2871. const img = '<img src="/assets/sample.jpg">';
  2872. const list = '<ul><li>foo</li><li>bar</li></ul>';
  2873. const blockquote = `<blockquote><p>baz</p>${ list }</blockquote>`;
  2874. pasteTable( [
  2875. [ `${ img }${ list }${ blockquote }`, 'ab' ],
  2876. [ 'ba', 'bb' ]
  2877. ] );
  2878. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2879. [
  2880. '<image src="/assets/sample.jpg"><caption></caption></image>' +
  2881. '<listItem listIndent="0" listType="bulleted">foo</listItem>' +
  2882. '<listItem listIndent="0" listType="bulleted">bar</listItem>' +
  2883. '<blockQuote>' +
  2884. '<paragraph>baz</paragraph>' +
  2885. '<listItem listIndent="0" listType="bulleted">foo</listItem>' +
  2886. '<listItem listIndent="0" listType="bulleted">bar</listItem>' +
  2887. '</blockQuote>',
  2888. 'ab',
  2889. '02' ],
  2890. [ 'ba', 'bb', '12' ],
  2891. [ '02', '21', '22' ]
  2892. ] ) );
  2893. } );
  2894. it( 'handles table cell properties', async () => {
  2895. await createEditor( [ TableCellPropertiesEditing ] );
  2896. setModelData( model, modelTable( [
  2897. [ '00', '01', '02' ],
  2898. [ '01', '11', '12' ],
  2899. [ '02', '21', '22' ]
  2900. ] ) );
  2901. tableSelection.setCellSelection(
  2902. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2903. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  2904. );
  2905. pasteTable( [
  2906. [ { contents: 'aa', style: 'border:1px solid #f00;background:#ba7;width:1337px' }, 'ab' ],
  2907. [ 'ba', 'bb' ]
  2908. ] );
  2909. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  2910. expect( tableCell.getAttribute( 'borderColor' ) ).to.deep.equal( {
  2911. top: '#f00',
  2912. right: '#f00',
  2913. bottom: '#f00',
  2914. left: '#f00'
  2915. } );
  2916. expect( tableCell.getAttribute( 'borderStyle' ) ).to.deep.equal( {
  2917. top: 'solid',
  2918. right: 'solid',
  2919. bottom: 'solid',
  2920. left: 'solid'
  2921. } );
  2922. expect( tableCell.getAttribute( 'borderWidth' ) ).to.deep.equal( {
  2923. top: '1px',
  2924. right: '1px',
  2925. bottom: '1px',
  2926. left: '1px'
  2927. } );
  2928. expect( tableCell.getAttribute( 'backgroundColor' ) ).to.equal( '#ba7' );
  2929. expect( tableCell.getAttribute( 'width' ) ).to.equal( '1337px' );
  2930. } );
  2931. it( 'discards table properties', async () => {
  2932. await createEditor( [ TableCellPropertiesEditing ] );
  2933. setModelData( model, modelTable( [
  2934. [ '00', '01', '02' ],
  2935. [ '01', '11', '12' ],
  2936. [ '02', '21', '22' ]
  2937. ] ) );
  2938. tableSelection.setCellSelection(
  2939. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2940. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  2941. );
  2942. const tableStyle = 'border:1px solid #f00;background:#ba7;width:1337px';
  2943. const pastedTable = `<table style="${ tableStyle }"><tr><td>aa</td><td>ab</td></tr><tr><td>ba</td><td>bb</td></tr></table>`;
  2944. const data = {
  2945. dataTransfer: createDataTransfer(),
  2946. preventDefault: sinon.spy(),
  2947. stopPropagation: sinon.spy()
  2948. };
  2949. data.dataTransfer.setData( 'text/html', pastedTable );
  2950. viewDocument.fire( 'paste', data );
  2951. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2952. [ 'aa', 'ab', '02' ],
  2953. [ 'ba', 'bb', '12' ],
  2954. [ '02', '21', '22' ]
  2955. ] ) );
  2956. } );
  2957. } );
  2958. async function createEditor( extraPlugins = [] ) {
  2959. editor = await ClassicTestEditor.create( element, {
  2960. plugins: [ TableEditing, TableClipboard, Paragraph, Clipboard, ...extraPlugins ]
  2961. } );
  2962. model = editor.model;
  2963. modelRoot = model.document.getRoot();
  2964. viewDocument = editor.editing.view.document;
  2965. tableSelection = editor.plugins.get( 'TableSelection' );
  2966. }
  2967. function pasteTable( tableData ) {
  2968. const data = {
  2969. dataTransfer: createDataTransfer(),
  2970. preventDefault: sinon.spy(),
  2971. stopPropagation: sinon.spy()
  2972. };
  2973. data.dataTransfer.setData( 'text/html', viewTable( tableData ) );
  2974. viewDocument.fire( 'paste', data );
  2975. return data;
  2976. }
  2977. function createDataTransfer() {
  2978. const store = new Map();
  2979. return {
  2980. setData( type, data ) {
  2981. store.set( type, data );
  2982. },
  2983. getData( type ) {
  2984. return store.get( type );
  2985. }
  2986. };
  2987. }
  2988. function setupBatchWatch() {
  2989. const createdBatches = new Set();
  2990. model.on( 'applyOperation', ( evt, [ operation ] ) => {
  2991. if ( operation.isDocumentOperation ) {
  2992. createdBatches.add( operation.batch );
  2993. }
  2994. } );
  2995. return createdBatches;
  2996. }
  2997. } );