tableclipboard-paste.js 122 KB

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