tableclipboard-paste.js 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606
  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, 2, 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, 2, 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.jpg"><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.jpg"><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. } );
  759. describe( 'with spanned cells', () => {
  760. it( 'should replace the table structure', () => {
  761. // +----+----+----+----+----+----+
  762. // | 00 | 01 | 02 | 05 |
  763. // +----+----+----+----+----+----+
  764. // | 10 | 11 | 13 | 14 | 15 |
  765. // +----+ +----+----+----+
  766. // | 20 | | 23 | 25 |
  767. // +----+ +----+----+ +
  768. // | 30 | | 33 | |
  769. // +----+----+----+----+----+----+
  770. // | 40 | 42 | 43 | 44 |
  771. // +----+----+----+----+----+----+
  772. setModelData( model, modelTable( [
  773. [ '00', '01', { contents: '02', colspan: 3 }, '05' ],
  774. [ '10', { contents: '11', colspan: 2, rowspan: 3 }, '13', '14', '15' ],
  775. [ '20', { contents: '23', colspan: 2 }, { contents: '25', rowspan: 2 } ],
  776. [ '30', { contents: '33', colspan: 2 } ],
  777. [ { contents: '40', colspan: 2 }, '42', '43', { contents: '44', colspan: 2 } ]
  778. ] ) );
  779. tableSelection.setCellSelection(
  780. modelRoot.getNodeByPath( [ 0, 2, 1 ] ), // Cell 23.
  781. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  782. );
  783. // +----+----+----+----+----+
  784. // | aa | ac | ad | ae |
  785. // + + +----+----+
  786. // | | | bd | be |
  787. // +----+----+----+----+----+
  788. // | ca | cc | cd | ce |
  789. // +----+----+----+----+----+
  790. // | da | dd | de |
  791. // +----+----+----+----+ +
  792. // | ea | eb | ec | ed | |
  793. // +----+----+----+----+----+
  794. pasteTable( [
  795. [ { contents: 'aa', colspan: 2, rowspan: 2 }, { contents: 'ac', rowspan: 2 }, 'ad', 'ae' ],
  796. [ 'bd', 'be' ],
  797. [ { contents: 'ca', colspan: 2 }, 'cc', 'cd', 'ce' ],
  798. [ { contents: 'da', colspan: 3 }, 'dd', { contents: 'de', rowspan: 2 } ],
  799. [ 'ea', 'eb', 'ec', 'ed' ]
  800. ] );
  801. // +----+----+----+----+----+----+----+----+
  802. // | 00 | 01 | 02 | 05 | | |
  803. // +----+----+----+----+----+----+----+----+
  804. // | 10 | 11 | 13 | 14 | 15 | | |
  805. // +----+ +----+----+----+----+----+
  806. // | 20 | | aa | ac | ad | ae |
  807. // +----+ + + +----+----+
  808. // | 30 | | | | bd | be |
  809. // +----+----+----+----+----+----+----+----+
  810. // | 40 | 42 | ca | cc | cd | ce |
  811. // +----+----+----+----+----+----+----+----+
  812. // | | | | da | dd | de |
  813. // +----+----+----+----+----+----+----+ +
  814. // | | | | ea | eb | ec | ed | |
  815. // +----+----+----+----+----+----+----+----+
  816. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  817. [ '00', '01', { contents: '02', colspan: 3 }, '05', '', '' ],
  818. [ '10', { contents: '11', colspan: 2, rowspan: 3 }, '13', '14', '15', '', '' ],
  819. [ '20', { contents: 'aa', colspan: 2, rowspan: 2 }, { contents: 'ac', rowspan: 2 }, 'ad', 'ae' ],
  820. [ '30', 'bd', 'be' ],
  821. [ { contents: '40', colspan: 2 }, '42', { contents: 'ca', colspan: 2 }, 'cc', 'cd', 'ce' ],
  822. [ '', '', '', { contents: 'da', colspan: 3 }, 'dd', { contents: 'de', rowspan: 2 } ],
  823. [ '', '', '', 'ea', 'eb', 'ec', 'ed' ]
  824. ] ) );
  825. } );
  826. it( 'should fix non-rectangular are on matched table fragment', () => {
  827. // +----+----+----+
  828. // | 00 | 01 | 02 |
  829. // +----+----+ +
  830. // | 10 | 11 | |
  831. // +----+----+----+
  832. // | 20 | 22 |
  833. // +----+----+----+
  834. setModelData( model, modelTable( [
  835. [ '00', '01', { contents: '02', rowspan: 2 } ],
  836. [ '10', '11[]' ],
  837. [ { contents: '20', colspan: 2 }, '22' ]
  838. ] ) );
  839. pasteTable( [
  840. [ 'aa', 'ab' ],
  841. [ 'ba', 'bb' ]
  842. ] );
  843. // +----+----+----+
  844. // | 00 | 01 | 02 |
  845. // +----+----+----+
  846. // | 10 | aa | ab |
  847. // +----+----+----+
  848. // | 20 | ba | bb |
  849. // +----+----+----+
  850. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  851. [ '00', '01', '02' ],
  852. [ '10', 'aa', 'ab' ],
  853. [ '20', 'ba', 'bb' ]
  854. ] ) );
  855. } );
  856. } );
  857. } );
  858. describe( 'pasted table is equal to the selected area', () => {
  859. describe( 'no spans', () => {
  860. it( 'handles simple table paste to a simple table fragment - at the beginning of a table', () => {
  861. tableSelection.setCellSelection(
  862. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  863. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  864. );
  865. pasteTable( [
  866. [ 'aa', 'ab' ],
  867. [ 'ba', 'bb' ]
  868. ] );
  869. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  870. [ 'aa', 'ab', '02', '03' ],
  871. [ 'ba', 'bb', '12', '13' ],
  872. [ '20', '21', '22', '23' ],
  873. [ '30', '31', '32', '33' ]
  874. ] ) );
  875. assertSelectedCells( model, [
  876. [ 1, 1, 0, 0 ],
  877. [ 1, 1, 0, 0 ],
  878. [ 0, 0, 0, 0 ],
  879. [ 0, 0, 0, 0 ]
  880. ] );
  881. } );
  882. it( 'handles simple table paste to a simple table fragment - at the end of a table', () => {
  883. tableSelection.setCellSelection(
  884. modelRoot.getNodeByPath( [ 0, 2, 2 ] ),
  885. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  886. );
  887. pasteTable( [
  888. [ 'aa', 'ab' ],
  889. [ 'ba', 'bb' ]
  890. ] );
  891. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  892. [ '00', '01', '02', '03' ],
  893. [ '10', '11', '12', '13' ],
  894. [ '20', '21', 'aa', 'ab' ],
  895. [ '30', '31', 'ba', 'bb' ]
  896. ] ) );
  897. assertSelectedCells( model, [
  898. [ 0, 0, 0, 0 ],
  899. [ 0, 0, 0, 0 ],
  900. [ 0, 0, 1, 1 ],
  901. [ 0, 0, 1, 1 ]
  902. ] );
  903. } );
  904. it( 'handles simple table paste to a simple table fragment - in the middle of a table', () => {
  905. tableSelection.setCellSelection(
  906. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  907. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  908. );
  909. pasteTable( [
  910. [ 'aa', 'ab' ],
  911. [ 'ba', 'bb' ]
  912. ] );
  913. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  914. [ '00', '01', '02', '03' ],
  915. [ '10', 'aa', 'ab', '13' ],
  916. [ '20', 'ba', 'bb', '23' ],
  917. [ '30', '31', '32', '33' ]
  918. ] ) );
  919. assertSelectedCells( model, [
  920. [ 0, 0, 0, 0 ],
  921. [ 0, 1, 1, 0 ],
  922. [ 0, 1, 1, 0 ],
  923. [ 0, 0, 0, 0 ]
  924. ] );
  925. } );
  926. it( 'handles simple row paste to a simple row fragment - in the middle of a table', () => {
  927. tableSelection.setCellSelection(
  928. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  929. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  930. );
  931. pasteTable( [
  932. [ 'aa', 'ab' ]
  933. ] );
  934. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  935. [ '00', '01', '02', '03' ],
  936. [ '10', 'aa', 'ab', '13' ],
  937. [ '20', '21', '22', '23' ],
  938. [ '30', '31', '32', '33' ]
  939. ] ) );
  940. assertSelectedCells( model, [
  941. [ 0, 0, 0, 0 ],
  942. [ 0, 1, 1, 0 ],
  943. [ 0, 0, 0, 0 ],
  944. [ 0, 0, 0, 0 ]
  945. ] );
  946. } );
  947. it( 'handles simple column paste to a simple column fragment - in the middle of a table', () => {
  948. tableSelection.setCellSelection(
  949. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  950. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  951. );
  952. pasteTable( [
  953. [ 'aa' ],
  954. [ 'ba' ]
  955. ] );
  956. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  957. [ '00', '01', '02', '03' ],
  958. [ '10', 'aa', '12', '13' ],
  959. [ '20', 'ba', '22', '23' ],
  960. [ '30', '31', '32', '33' ]
  961. ] ) );
  962. assertSelectedCells( model, [
  963. [ 0, 0, 0, 0 ],
  964. [ 0, 1, 0, 0 ],
  965. [ 0, 1, 0, 0 ],
  966. [ 0, 0, 0, 0 ]
  967. ] );
  968. } );
  969. it( 'handles simple table paste to a simple table fragment - whole table selected', () => {
  970. tableSelection.setCellSelection(
  971. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  972. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  973. );
  974. pasteTable( [
  975. [ 'aa', 'ab', 'ac', 'ad' ],
  976. [ 'ba', 'bb', 'bc', 'bd' ],
  977. [ 'ca', 'cb', 'cc', 'cd' ],
  978. [ 'da', 'db', 'dc', 'dd' ]
  979. ] );
  980. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  981. [ 'aa', 'ab', 'ac', 'ad' ],
  982. [ 'ba', 'bb', 'bc', 'bd' ],
  983. [ 'ca', 'cb', 'cc', 'cd' ],
  984. [ 'da', 'db', 'dc', 'dd' ]
  985. ] ) );
  986. assertSelectedCells( model, [
  987. [ 1, 1, 1, 1 ],
  988. [ 1, 1, 1, 1 ],
  989. [ 1, 1, 1, 1 ],
  990. [ 1, 1, 1, 1 ]
  991. ] );
  992. } );
  993. } );
  994. describe( 'pasted table has spans', () => {
  995. it( 'handles pasting table that has cell with colspan', () => {
  996. tableSelection.setCellSelection(
  997. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  998. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  999. );
  1000. pasteTable( [
  1001. [ { colspan: 2, contents: 'aa' } ],
  1002. [ 'ba', 'bb' ]
  1003. ] );
  1004. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1005. [ '00', '01', '02', '03' ],
  1006. [ '10', { colspan: 2, contents: 'aa' }, '13' ],
  1007. [ '20', 'ba', 'bb', '23' ],
  1008. [ '30', '31', '32', '33' ]
  1009. ] ) );
  1010. /* eslint-disable no-multi-spaces */
  1011. assertSelectedCells( model, [
  1012. [ 0, 0, 0, 0 ],
  1013. [ 0, 1, 0 ],
  1014. [ 0, 1, 1, 0 ],
  1015. [ 0, 0, 0, 0 ]
  1016. ] );
  1017. /* eslint-enable no-multi-spaces */
  1018. } );
  1019. it( 'handles pasting table that has many cells with various colspan', () => {
  1020. tableSelection.setCellSelection(
  1021. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1022. modelRoot.getNodeByPath( [ 0, 3, 2 ] )
  1023. );
  1024. pasteTable( [
  1025. [ 'aa', { colspan: 2, contents: 'ab' } ],
  1026. [ { colspan: 3, contents: 'ba' } ],
  1027. [ 'ca', 'cb', 'cc' ],
  1028. [ { colspan: 2, contents: 'da' }, 'dc' ]
  1029. ] );
  1030. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1031. [ 'aa', { colspan: 2, contents: 'ab' }, '03' ],
  1032. [ { colspan: 3, contents: 'ba' }, '13' ],
  1033. [ 'ca', 'cb', 'cc', '23' ],
  1034. [ { colspan: 2, contents: 'da' }, 'dc', '33' ]
  1035. ] ) );
  1036. /* eslint-disable no-multi-spaces */
  1037. assertSelectedCells( model, [
  1038. [ 1, 1, 0 ],
  1039. [ 1, 0 ],
  1040. [ 1, 1, 1, 0 ],
  1041. [ 1, 1, 0 ]
  1042. ] );
  1043. /* eslint-enable no-multi-spaces */
  1044. } );
  1045. it( 'handles pasting table that has cell with rowspan', () => {
  1046. tableSelection.setCellSelection(
  1047. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  1048. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  1049. );
  1050. pasteTable( [
  1051. [ { rowspan: 2, contents: 'aa' }, 'ab' ],
  1052. [ 'bb' ]
  1053. ] );
  1054. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1055. [ '00', '01', '02', '03' ],
  1056. [ '10', { rowspan: 2, contents: 'aa' }, 'ab', '13' ],
  1057. [ '20', 'bb', '23' ],
  1058. [ '30', '31', '32', '33' ]
  1059. ] ) );
  1060. /* eslint-disable no-multi-spaces */
  1061. assertSelectedCells( model, [
  1062. [ 0, 0, 0, 0 ],
  1063. [ 0, 1, 1, 0 ],
  1064. [ 0, 1, 0 ],
  1065. [ 0, 0, 0, 0 ]
  1066. ] );
  1067. /* eslint-enable no-multi-spaces */
  1068. } );
  1069. it( 'handles pasting table that has many cells with various rowspan', () => {
  1070. tableSelection.setCellSelection(
  1071. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1072. modelRoot.getNodeByPath( [ 0, 2, 3 ] )
  1073. );
  1074. pasteTable( [
  1075. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  1076. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  1077. [ 'cc', 'cd' ]
  1078. ] );
  1079. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1080. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  1081. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  1082. [ 'cc', 'cd' ],
  1083. [ '30', '31', '32', '33' ]
  1084. ] ) );
  1085. /* eslint-disable no-multi-spaces */
  1086. assertSelectedCells( model, [
  1087. [ 1, 1, 1, 1 ],
  1088. [ 1, 1 ],
  1089. [ 1, 1 ],
  1090. [ 0, 0, 0, 0 ]
  1091. ] );
  1092. /* eslint-enable no-multi-spaces */
  1093. } );
  1094. it( 'handles pasting multi-spanned table', () => {
  1095. setModelData( model, modelTable( [
  1096. [ '00', '01', '02', '03', '04', '05' ],
  1097. [ '10', '11', '12', '13', '14', '15' ],
  1098. [ '20', '21', '22', '23', '24', '25' ],
  1099. [ '30', '31', '32', '33', '34', '35' ],
  1100. [ '40', '41', '42', '43', '44', '45' ]
  1101. ] ) );
  1102. tableSelection.setCellSelection(
  1103. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1104. modelRoot.getNodeByPath( [ 0, 3, 4 ] )
  1105. );
  1106. // +----+----+----+----+----+
  1107. // | aa | ac | ad | ae |
  1108. // +----+----+----+----+ +
  1109. // | ba | bb | |
  1110. // +----+ +----+
  1111. // | ca | | ce |
  1112. // + +----+----+----+----+
  1113. // | | db | dc | dd |
  1114. // +----+----+----+----+----+
  1115. pasteTable( [
  1116. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 } ],
  1117. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 } ],
  1118. [ { contents: 'ca', rowspan: 2 }, 'ce' ],
  1119. [ 'db', 'dc', { contents: 'dd', colspan: 2 } ]
  1120. ] );
  1121. // +----+----+----+----+----+----+
  1122. // | aa | ac | ad | ae | 05 |
  1123. // +----+----+----+----+ +----+
  1124. // | ba | bb | | 15 |
  1125. // +----+ +----+----+
  1126. // | ca | | ce | 25 |
  1127. // + +----+----+----+----+----+
  1128. // | | db | dc | dd | 35 |
  1129. // +----+----+----+----+----+----+
  1130. // | 40 | 41 | 42 | 43 | 44 | 45 |
  1131. // +----+----+----+----+----+----+
  1132. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1133. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 }, '05' ],
  1134. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 }, '15' ],
  1135. [ { contents: 'ca', rowspan: 2 }, 'ce', '25' ],
  1136. [ 'db', 'dc', { contents: 'dd', colspan: 2 }, '35' ],
  1137. [ '40', '41', '42', '43', '44', '45' ]
  1138. ] ) );
  1139. /* eslint-disable no-multi-spaces */
  1140. assertSelectedCells( model, [
  1141. [ 1, 1, 1, 1, 0 ],
  1142. [ 1, 1, 0 ],
  1143. [ 1, 1, 0 ],
  1144. [ 1, 1, 1, 0 ],
  1145. [ 0, 0, 0, 0, 0, 0 ]
  1146. ] );
  1147. /* eslint-enable no-multi-spaces */
  1148. } );
  1149. } );
  1150. describe( 'content table has spans', () => {
  1151. it( 'handles pasting simple table over a table with colspans (no colspan exceeds selection)', () => {
  1152. setModelData( model, modelTable( [
  1153. [ '00[]', '01', '02', '03' ],
  1154. [ { colspan: 3, contents: '10' }, '13' ],
  1155. [ { colspan: 2, contents: '20' }, '22', '23' ],
  1156. [ '30', '31', { colspan: 2, contents: '31' } ]
  1157. ] ) );
  1158. tableSelection.setCellSelection(
  1159. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1160. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  1161. );
  1162. pasteTable( [
  1163. [ 'aa', 'ab', 'ac' ],
  1164. [ 'ba', 'bb', 'bc' ],
  1165. [ 'ca', 'cb', 'cc' ]
  1166. ] );
  1167. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1168. [ 'aa', 'ab', 'ac', '03' ],
  1169. [ 'ba', 'bb', 'bc', '13' ],
  1170. [ 'ca', 'cb', 'cc', '23' ],
  1171. [ '30', '31', { colspan: 2, contents: '31' } ]
  1172. ] ) );
  1173. /* eslint-disable no-multi-spaces */
  1174. assertSelectedCells( model, [
  1175. [ 1, 1, 1, 0 ],
  1176. [ 1, 1, 1, 0 ],
  1177. [ 1, 1, 1, 0 ],
  1178. [ 0, 0, 0 ]
  1179. ] );
  1180. /* eslint-enable no-multi-spaces */
  1181. } );
  1182. it( 'handles pasting simple table over a table with rowspans (no rowspan exceeds selection)', () => {
  1183. setModelData( model, modelTable( [
  1184. [ '00', { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03' ],
  1185. [ { rowspan: 2, contents: '10' }, '13' ],
  1186. [ '22', '23' ],
  1187. [ '30', '31', '32', '33' ]
  1188. ] ) );
  1189. tableSelection.setCellSelection(
  1190. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1191. modelRoot.getNodeByPath( [ 0, 2, 0 ] )
  1192. );
  1193. pasteTable( [
  1194. [ 'aa', 'ab', 'ac' ],
  1195. [ 'ba', 'bb', 'bc' ],
  1196. [ 'ca', 'cb', 'cc' ]
  1197. ] );
  1198. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1199. [ 'aa', 'ab', 'ac', '03' ],
  1200. [ 'ba', 'bb', 'bc', '13' ],
  1201. [ 'ca', 'cb', 'cc', '23' ],
  1202. [ '30', '31', '32', '33' ]
  1203. ] ) );
  1204. assertSelectedCells( model, [
  1205. [ 1, 1, 1, 0 ],
  1206. [ 1, 1, 1, 0 ],
  1207. [ 1, 1, 1, 0 ],
  1208. [ 0, 0, 0, 0 ]
  1209. ] );
  1210. } );
  1211. it( 'handles pasting simple table over table with multi-spans (no span exceeds selection)', () => {
  1212. // +----+----+----+----+----+----+
  1213. // | 00 | 02 | 03 | 05 |
  1214. // + + + +----+
  1215. // | | | | 15 |
  1216. // +----+----+----+ +----+
  1217. // | 20 | 21 | | 25 |
  1218. // + +----+----+----+----+----+
  1219. // | | 31 | 32 | 34 | 35 |
  1220. // +----+----+----+----+----+----+
  1221. // | 40 | 41 | 42 | 43 | 44 | 45 |
  1222. // +----+----+----+----+----+----+
  1223. setModelData( model, modelTable( [
  1224. [
  1225. { contents: '00', colspan: 2, rowspan: 2 },
  1226. { contents: '02', rowspan: 2 },
  1227. { contents: '03', colspan: 2, rowspan: 3 },
  1228. '05'
  1229. ],
  1230. [ '15' ],
  1231. [ { contents: '20', rowspan: 2 }, { contents: '21', colspan: 2 }, '25' ],
  1232. [ '31', { contents: '32', colspan: 2 }, '34', '35' ],
  1233. [ '40', '41', '42', '43', '44', '45' ]
  1234. ] ) );
  1235. tableSelection.setCellSelection(
  1236. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1237. modelRoot.getNodeByPath( [ 0, 3, 2 ] ) // Cell 34.
  1238. );
  1239. pasteTable( [
  1240. [ 'aa', 'ab', 'ac', 'ad', 'ae' ],
  1241. [ 'ba', 'bb', 'bc', 'bd', 'be' ],
  1242. [ 'ca', 'cb', 'cc', 'cd', 'ce' ],
  1243. [ 'da', 'db', 'dc', 'dd', 'de' ]
  1244. ] );
  1245. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1246. [ 'aa', 'ab', 'ac', 'ad', 'ae', '05' ],
  1247. [ 'ba', 'bb', 'bc', 'bd', 'be', '15' ],
  1248. [ 'ca', 'cb', 'cc', 'cd', 'ce', '25' ],
  1249. [ 'da', 'db', 'dc', 'dd', 'de', '35' ],
  1250. [ '40', '41', '42', '43', '44', '45' ]
  1251. ] ) );
  1252. assertSelectedCells( model, [
  1253. [ 1, 1, 1, 1, 1, 0 ],
  1254. [ 1, 1, 1, 1, 1, 0 ],
  1255. [ 1, 1, 1, 1, 1, 0 ],
  1256. [ 1, 1, 1, 1, 1, 0 ],
  1257. [ 0, 0, 0, 0, 0, 0 ]
  1258. ] );
  1259. } );
  1260. it( 'handles pasting simple table over a table with rowspan (rowspan before selection)', () => {
  1261. // +----+----+----+----+----+
  1262. // | 00 | 01 | 02 | 03 | 04 |
  1263. // +----+----+----+----+----+
  1264. // | 10 | 11 | 12 | 13 | 14 |
  1265. // +----+ +----+----+----+
  1266. // | 20 | | 22 | 23 | 24 |
  1267. // +----+ +----+----+----+
  1268. // | 30 | | 32 | 33 | 34 |
  1269. // +----+----+----+----+----+
  1270. // | 40 | 41 | 42 | 43 | 44 |
  1271. // +----+----+----+----+----+
  1272. setModelData( model, modelTable( [
  1273. [ '00', '01', '02', '03', '04' ],
  1274. [ '10', { contents: '11', rowspan: 3 }, '12', '13', '14' ],
  1275. [ '20', '22', '23', '24' ],
  1276. [ '30', '32', '33', '34' ],
  1277. [ '40', '41', '42', '43', '44' ]
  1278. ] ) );
  1279. tableSelection.setCellSelection(
  1280. modelRoot.getNodeByPath( [ 0, 1, 2 ] ),
  1281. modelRoot.getNodeByPath( [ 0, 3, 2 ] ) // Cell 33.
  1282. );
  1283. pasteTable( [
  1284. [ 'aa', 'ab' ],
  1285. [ 'ba', 'bb' ],
  1286. [ 'ca', 'cb' ]
  1287. ] );
  1288. // +----+----+----+----+----+
  1289. // | 00 | 01 | 02 | 03 | 04 |
  1290. // +----+----+----+----+----+
  1291. // | 10 | 11 | aa | ab | 14 |
  1292. // +----+ +----+----+----+
  1293. // | 20 | | ba | bb | 24 |
  1294. // +----+ +----+----+----+
  1295. // | 30 | | ca | cb | 34 |
  1296. // +----+----+----+----+----+
  1297. // | 40 | 41 | 42 | 43 | 44 |
  1298. // +----+----+----+----+----+
  1299. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1300. [ '00', '01', '02', '03', '04' ],
  1301. [ '10', { contents: '11', rowspan: 3 }, 'aa', 'ab', '14' ],
  1302. [ '20', 'ba', 'bb', '24' ],
  1303. [ '30', 'ca', 'cb', '34' ],
  1304. [ '40', '41', '42', '43', '44' ]
  1305. ] ) );
  1306. /* eslint-disable no-multi-spaces */
  1307. assertSelectedCells( model, [
  1308. [ 0, 0, 0, 0, 0 ],
  1309. [ 0, 0, 1, 1, 0 ],
  1310. [ 0, 1, 1, 0 ],
  1311. [ 0, 1, 1, 0 ],
  1312. [ 0, 0, 0, 0, 0 ]
  1313. ] );
  1314. /* eslint-enable no-multi-spaces */
  1315. } );
  1316. it( 'handles pasting simple table over a table with rowspans (rowspan before selection)', () => {
  1317. // +----+----+----+----+----+----+
  1318. // | 00 | 01 | 02 | 03 | 04 | 05 |
  1319. // +----+----+----+----+----+----+
  1320. // | 10 | 12 | 13 | 14 | 15 |
  1321. // +----+----+----+----+----+----+
  1322. // | 20 | 23 | 24 | 25 |
  1323. // +----+----+----+----+----+----+
  1324. // | 30 | 31 | 32 | 33 | 34 | 35 |
  1325. // +----+----+----+----+----+----+
  1326. setModelData( model, modelTable( [
  1327. [ '00', '01', '02', '03', '04', '05' ],
  1328. [ { contents: '10', colspan: 2 }, '12', '13', '14', '15' ],
  1329. [ { contents: '20', colspan: 3 }, '23', '24', '25' ],
  1330. [ '30', '31', '32', '33', '34', '35' ]
  1331. ] ) );
  1332. tableSelection.setCellSelection(
  1333. modelRoot.getNodeByPath( [ 0, 1, 2 ] ), // Cell 13.
  1334. modelRoot.getNodeByPath( [ 0, 2, 2 ] ) // Cell 24.
  1335. );
  1336. pasteTable( [
  1337. [ 'aa', 'ab' ],
  1338. [ 'ba', 'bb' ]
  1339. ] );
  1340. // +----+----+----+----+----+----+
  1341. // | 00 | 01 | 02 | 03 | 04 | 05 |
  1342. // +----+----+----+----+----+----+
  1343. // | 10 | 12 | aa | ab | 15 |
  1344. // +----+----+----+----+----+----+
  1345. // | 20 | ba | bb | 25 |
  1346. // +----+----+----+----+----+----+
  1347. // | 30 | 31 | 32 | 33 | 34 | 35 |
  1348. // +----+----+----+----+----+----+
  1349. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1350. [ '00', '01', '02', '03', '04', '05' ],
  1351. [ { contents: '10', colspan: 2 }, '12', 'aa', 'ab', '15' ],
  1352. [ { contents: '20', colspan: 3 }, 'ba', 'bb', '25' ],
  1353. [ '30', '31', '32', '33', '34', '35' ]
  1354. ] ) );
  1355. /* eslint-disable no-multi-spaces */
  1356. assertSelectedCells( model, [
  1357. [ 0, 0, 0, 0, 0, 0 ],
  1358. [ 0, 0, 1, 1, 0 ],
  1359. [ 0, 1, 1, 0 ],
  1360. [ 0, 0, 0, 0, 0, 0 ]
  1361. ] );
  1362. /* eslint-enable no-multi-spaces */
  1363. } );
  1364. it( 'handles pasting table that has cell with colspan (last row in selection is spanned)', () => {
  1365. // +----+----+----+----+
  1366. // | 00 | 01 | 02 | 03 |
  1367. // +----+----+----+----+
  1368. // | 10 | 11 | 13 |
  1369. // + + +----+
  1370. // | | | 23 |
  1371. // +----+----+----+----+
  1372. // | 30 | 31 | 32 | 33 |
  1373. // +----+----+----+----+
  1374. setModelData( model, modelTable( [
  1375. [ '00', '01', '02', '03' ],
  1376. [ { contents: '10', rowspan: 2 }, { contents: '11', colspan: 2, rowspan: 2 }, '13' ],
  1377. [ '23' ],
  1378. [ '30', '31', '32', '33' ]
  1379. ] ) );
  1380. // Select 02 -> 10 (selection 3x3)
  1381. tableSelection.setCellSelection(
  1382. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  1383. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  1384. );
  1385. pasteTable( [
  1386. [ 'aa', 'ab', 'ac' ],
  1387. [ 'ba', 'bb', 'bc' ],
  1388. [ 'ca', 'cb', 'cc' ]
  1389. ] );
  1390. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1391. [ 'aa', 'ab', 'ac', '03' ],
  1392. [ 'ba', 'bb', 'bc', '13' ],
  1393. [ 'ca', 'cb', 'cc', '23' ],
  1394. [ '30', '31', '32', '33' ]
  1395. ] ) );
  1396. assertSelectedCells( model, [
  1397. [ 1, 1, 1, 0 ],
  1398. [ 1, 1, 1, 0 ],
  1399. [ 1, 1, 1, 0 ],
  1400. [ 0, 0, 0, 0 ]
  1401. ] );
  1402. } );
  1403. it( 'handles pasting table that has cell with rowspan (multiple ending rows in the selection are spanned)', () => {
  1404. // +----+----+----+
  1405. // | 00 | 01 | 02 |
  1406. // +----+ + +
  1407. // | 10 | | |
  1408. // +----+ + +
  1409. // | 20 | | |
  1410. // +----+----+----+
  1411. // | 30 | 31 | 32 |
  1412. // +----+----+----+
  1413. setModelData( model, modelTable( [
  1414. [ '00', { contents: '01', rowspan: 3 }, { contents: '02', rowspan: 3 } ],
  1415. [ '10' ],
  1416. [ '20' ],
  1417. [ '30', '31', '32' ]
  1418. ] ) );
  1419. // Select 01 -> 02 (selection 2x2)
  1420. tableSelection.setCellSelection(
  1421. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  1422. modelRoot.getNodeByPath( [ 0, 0, 2 ] )
  1423. );
  1424. pasteTable( [
  1425. [ 'aa', 'ab' ],
  1426. [ 'ba', 'bb' ],
  1427. [ 'ca', 'cb' ]
  1428. ] );
  1429. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1430. [ '00', 'aa', 'ab' ],
  1431. [ '10', 'ba', 'bb' ],
  1432. [ '20', 'ca', 'cb' ],
  1433. [ '30', '31', '32' ]
  1434. ] ) );
  1435. assertSelectedCells( model, [
  1436. [ 0, 1, 1 ],
  1437. [ 0, 1, 1 ],
  1438. [ 0, 1, 1 ],
  1439. [ 0, 0, 0 ]
  1440. ] );
  1441. } );
  1442. it( 'handles pasting table that has cell with colspan (last column in selection is spanned)', () => {
  1443. // +----+----+----+----+
  1444. // | 00 | 01 | 03 |
  1445. // +----+----+----+----+
  1446. // | 10 | 11 | 13 |
  1447. // +----+ +----+
  1448. // | 20 | | 23 |
  1449. // +----+----+----+----+
  1450. // | 30 | 31 | 32 | 33 |
  1451. // +----+----+----+----+
  1452. setModelData( model, modelTable( [
  1453. [ '00', { contents: '01', colspan: 2 }, '03' ],
  1454. [ '10', { contents: '11', colspan: 2, rowspan: 2 }, '13' ],
  1455. [ '20', '23' ],
  1456. [ '30', '31', '32', '33' ]
  1457. ] ) );
  1458. // Select 20 -> 01 (selection 3x3)
  1459. tableSelection.setCellSelection(
  1460. modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  1461. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  1462. );
  1463. pasteTable( [
  1464. [ 'aa', 'ab', 'ac' ],
  1465. [ 'ba', 'bb', 'bc' ],
  1466. [ 'ca', 'cb', 'cc' ]
  1467. ] );
  1468. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1469. [ 'aa', 'ab', 'ac', '03' ],
  1470. [ 'ba', 'bb', 'bc', '13' ],
  1471. [ 'ca', 'cb', 'cc', '23' ],
  1472. [ '30', '31', '32', '33' ]
  1473. ] ) );
  1474. assertSelectedCells( model, [
  1475. [ 1, 1, 1, 0 ],
  1476. [ 1, 1, 1, 0 ],
  1477. [ 1, 1, 1, 0 ],
  1478. [ 0, 0, 0, 0 ]
  1479. ] );
  1480. } );
  1481. it( 'handles pasting table that has cell with colspan (multiple ending columns in the selection are spanned)', () => {
  1482. // +----+----+----+----+
  1483. // | 00 | 01 | 02 | 03 |
  1484. // +----+----+----+----+
  1485. // | 10 | 13 |
  1486. // +----+----+----+----+
  1487. // | 20 | 23 |
  1488. // +----+----+----+----+
  1489. setModelData( model, modelTable( [
  1490. [ '00', '01', '02', '03' ],
  1491. [ { contents: '10', colspan: 3 }, '13' ],
  1492. [ { contents: '20', colspan: 3 }, '23' ]
  1493. ] ) );
  1494. // Select 10 -> 20 (selection 3x2)
  1495. tableSelection.setCellSelection(
  1496. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  1497. modelRoot.getNodeByPath( [ 0, 2, 0 ] )
  1498. );
  1499. pasteTable( [
  1500. [ 'aa', 'ab', 'ac' ],
  1501. [ 'ba', 'bb', 'bc' ]
  1502. ] );
  1503. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1504. [ '00', '01', '02', '03' ],
  1505. [ 'aa', 'ab', 'ac', '13' ],
  1506. [ 'ba', 'bb', 'bc', '23' ]
  1507. ] ) );
  1508. assertSelectedCells( model, [
  1509. [ 0, 0, 0, 0 ],
  1510. [ 1, 1, 1, 0 ],
  1511. [ 1, 1, 1, 0 ]
  1512. ] );
  1513. } );
  1514. } );
  1515. describe( 'content and paste tables have spans', () => {
  1516. it( 'handles pasting colspanned table over table with colspans (no colspan exceeds selection)', () => {
  1517. setModelData( model, modelTable( [
  1518. [ '00[]', '01', '02', '03' ],
  1519. [ { colspan: 3, contents: '10' }, '13' ],
  1520. [ { colspan: 2, contents: '20' }, '22', '23' ],
  1521. [ '30', '31', { colspan: 2, contents: '31' } ]
  1522. ] ) );
  1523. tableSelection.setCellSelection(
  1524. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1525. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  1526. );
  1527. pasteTable( [
  1528. [ 'aa', { colspan: 2, contents: 'ab' } ],
  1529. [ { colspan: 3, contents: 'ba' } ],
  1530. [ 'ca', 'cb', 'cc' ]
  1531. ] );
  1532. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1533. [ 'aa', { colspan: 2, contents: 'ab' }, '03' ],
  1534. [ { colspan: 3, contents: 'ba' }, '13' ],
  1535. [ 'ca', 'cb', 'cc', '23' ],
  1536. [ '30', '31', { colspan: 2, contents: '31' } ]
  1537. ] ) );
  1538. /* eslint-disable no-multi-spaces */
  1539. assertSelectedCells( model, [
  1540. [ 1, 1, 0 ],
  1541. [ 1, 0 ],
  1542. [ 1, 1, 1, 0 ],
  1543. [ 0, 0, 0 ]
  1544. ] );
  1545. /* eslint-enable no-multi-spaces */
  1546. } );
  1547. it( 'handles pasting rowspanned table over table with rowspans (no rowspan exceeds selection)', () => {
  1548. setModelData( model, modelTable( [
  1549. [ { rowspan: 3, contents: '00' }, { rowspan: 2, contents: '01' }, '02', '03' ],
  1550. [ { rowspan: 2, contents: '12' }, '13' ],
  1551. [ '21', '23' ],
  1552. [ '30', '31', '32', '33' ]
  1553. ] ) );
  1554. tableSelection.setCellSelection(
  1555. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1556. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  1557. );
  1558. pasteTable( [
  1559. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  1560. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  1561. [ 'cc', 'cd' ]
  1562. ] );
  1563. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1564. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  1565. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  1566. [ 'cc', 'cd' ],
  1567. [ '30', '31', '32', '33' ]
  1568. ] ) );
  1569. /* eslint-disable no-multi-spaces */
  1570. assertSelectedCells( model, [
  1571. [ 1, 1, 1, 1 ],
  1572. [ 1, 1 ],
  1573. [ 1, 1 ],
  1574. [ 0, 0, 0, 0 ]
  1575. ] );
  1576. /* eslint-enable no-multi-spaces */
  1577. } );
  1578. it( 'handles pasting multi-spanned table over table with multi-spans (no span exceeds selection)', () => {
  1579. // +----+----+----+----+----+----+
  1580. // | 00 | 02 | 03 | 05 |
  1581. // + + + +----+
  1582. // | | | | 15 |
  1583. // +----+----+----+ +----+
  1584. // | 20 | 21 | | 25 |
  1585. // + +----+----+----+----+----+
  1586. // | | 31 | 32 | 34 | 35 |
  1587. // +----+----+----+----+----+----+
  1588. // | 40 | 41 | 42 | 43 | 44 | 45 |
  1589. // +----+----+----+----+----+----+
  1590. setModelData( model, modelTable( [
  1591. [
  1592. { contents: '00', colspan: 2, rowspan: 2 },
  1593. { contents: '02', rowspan: 2 },
  1594. { contents: '03', colspan: 2, rowspan: 3 },
  1595. '05'
  1596. ],
  1597. [ '15' ],
  1598. [ { contents: '20', rowspan: 2 }, { contents: '21', colspan: 2 }, '25' ],
  1599. [ '31', { contents: '32', colspan: 2 }, '34', '35' ],
  1600. [ '40', '41', '42', '43', '44', '45' ]
  1601. ] ) );
  1602. tableSelection.setCellSelection(
  1603. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1604. modelRoot.getNodeByPath( [ 0, 3, 2 ] )
  1605. );
  1606. // +----+----+----+----+----+
  1607. // | aa | ac | ad | ae |
  1608. // +----+----+----+----+ +
  1609. // | ba | bb | |
  1610. // +----+ +----+
  1611. // | ca | | ce |
  1612. // + +----+----+----+----+
  1613. // | | db | dc | dd |
  1614. // +----+----+----+----+----+
  1615. pasteTable( [
  1616. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 } ],
  1617. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 } ],
  1618. [ { contents: 'ca', rowspan: 2 }, 'ce' ],
  1619. [ 'db', 'dc', { contents: 'dd', colspan: 2 } ]
  1620. ] );
  1621. // +----+----+----+----+----+----+
  1622. // | aa | ac | ad | ae | 05 |
  1623. // +----+----+----+----+ +----+
  1624. // | ba | bb | | 15 |
  1625. // +----+ +----+----+
  1626. // | ca | | ce | 25 |
  1627. // + +----+----+----+----+----+
  1628. // | | db | dc | dd | 35 |
  1629. // +----+----+----+----+----+----+
  1630. // | 40 | 41 | 42 | 43 | 44 | 45 |
  1631. // +----+----+----+----+----+----+
  1632. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1633. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 }, '05' ],
  1634. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 }, '15' ],
  1635. [ { contents: 'ca', rowspan: 2 }, 'ce', '25' ],
  1636. [ 'db', 'dc', { contents: 'dd', colspan: 2 }, '35' ],
  1637. [ '40', '41', '42', '43', '44', '45' ]
  1638. ] ) );
  1639. /* eslint-disable no-multi-spaces */
  1640. assertSelectedCells( model, [
  1641. [ 1, 1, 1, 1, 0 ],
  1642. [ 1, 1, 0 ],
  1643. [ 1, 1, 0 ],
  1644. [ 1, 1, 1, 0 ],
  1645. [ 0, 0, 0, 0, 0, 0 ]
  1646. ] );
  1647. /* eslint-enable no-multi-spaces */
  1648. } );
  1649. it( 'handles pasting table that has cell with colspan (last row in selection is spanned)', () => {
  1650. // +----+----+----+----+
  1651. // | 00 | 01 | 02 | 03 |
  1652. // +----+----+----+----+
  1653. // | 10 | 11 | 13 |
  1654. // + + +----+
  1655. // | | | 23 |
  1656. // +----+----+----+----+
  1657. // | 30 | 31 | 32 | 33 |
  1658. // +----+----+----+----+
  1659. setModelData( model, modelTable( [
  1660. [ '00', '01', '02', '03' ],
  1661. [ { contents: '10', rowspan: 2 }, { contents: '11', colspan: 2, rowspan: 2 }, '13' ],
  1662. [ '23' ],
  1663. [ '30', '31', '32', '33' ]
  1664. ] ) );
  1665. // Select 02 -> 10 (selection 3x3)
  1666. tableSelection.setCellSelection(
  1667. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  1668. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  1669. );
  1670. // +----+----+----+
  1671. // | aa | ab | ac |
  1672. // +----+----+----+
  1673. // | ba | bc |
  1674. // + +----+
  1675. // | | cc |
  1676. // +----+----+----+
  1677. pasteTable( [
  1678. [ 'aa', 'ab', 'ac' ],
  1679. [ { contents: 'ba', colspan: 2, rowspan: 2 }, 'bc' ],
  1680. [ 'cc' ]
  1681. ] );
  1682. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1683. [ 'aa', 'ab', 'ac', '03' ],
  1684. [ { contents: 'ba', colspan: 2, rowspan: 2 }, 'bc', '13' ],
  1685. [ 'cc', '23' ],
  1686. [ '30', '31', '32', '33' ]
  1687. ] ) );
  1688. /* eslint-disable no-multi-spaces */
  1689. assertSelectedCells( model, [
  1690. [ 1, 1, 1, 0 ],
  1691. [ 1, 1, 0 ],
  1692. [ 1, 0 ],
  1693. [ 0, 0, 0, 0 ]
  1694. ] );
  1695. /* eslint-enable no-multi-spaces */
  1696. } );
  1697. it( 'handles pasting table that has cell with colspan (last column in selection is spanned)', () => {
  1698. // +----+----+----+----+
  1699. // | 00 | 01 | 03 |
  1700. // +----+----+----+----+
  1701. // | 10 | 11 | 13 |
  1702. // +----+ +----+
  1703. // | 20 | | 23 |
  1704. // +----+----+----+----+
  1705. // | 30 | 31 | 32 | 33 |
  1706. // +----+----+----+----+
  1707. setModelData( model, modelTable( [
  1708. [ '00', { contents: '01', colspan: 2 }, '03' ],
  1709. [ '10', { contents: '11', colspan: 2, rowspan: 2 }, '13' ],
  1710. [ '20', '23' ],
  1711. [ '30', '31', '32', '33' ]
  1712. ] ) );
  1713. // Select 20 -> 01 (selection 3x3)
  1714. tableSelection.setCellSelection(
  1715. modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  1716. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  1717. );
  1718. // +----+----+----+
  1719. // | aa | ab | ac |
  1720. // +----+----+----+
  1721. // | ba | bc |
  1722. // + +----+
  1723. // | | cc |
  1724. // +----+----+----+
  1725. pasteTable( [
  1726. [ 'aa', 'ab', 'ac' ],
  1727. [ { contents: 'ba', colspan: 2, rowspan: 2 }, 'bc' ],
  1728. [ 'cc' ]
  1729. ] );
  1730. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1731. [ 'aa', 'ab', 'ac', '03' ],
  1732. [ { contents: 'ba', colspan: 2, rowspan: 2 }, 'bc', '13' ],
  1733. [ 'cc', '23' ],
  1734. [ '30', '31', '32', '33' ]
  1735. ] ) );
  1736. /* eslint-disable no-multi-spaces */
  1737. assertSelectedCells( model, [
  1738. [ 1, 1, 1, 0 ],
  1739. [ 1, 1, 0 ],
  1740. [ 1, 0 ],
  1741. [ 0, 0, 0, 0 ]
  1742. ] );
  1743. /* eslint-enable no-multi-spaces */
  1744. } );
  1745. } );
  1746. describe( 'non-rectangular content table selection', () => {
  1747. it( 'should split cells outside the selected area before pasting (rowspan ends in selection)', () => {
  1748. // +----+----+----+
  1749. // | 00 | 01 | 02 |
  1750. // +----+ +----+
  1751. // | 10 | | 12 |
  1752. // +----+ +----+
  1753. // | 20 | | 22 |
  1754. // +----+ +----+
  1755. // | 30 | | 32 |
  1756. // +----+----+----+
  1757. // | 40 | 41 | 42 |
  1758. // +----+----+----+
  1759. setModelData( model, modelTable( [
  1760. [ '00', { contents: '01', rowspan: 4 }, '02' ],
  1761. [ '10', '12' ],
  1762. [ '20', '22' ],
  1763. [ '30', '32' ],
  1764. [ '40', '41', '42' ]
  1765. ] ) );
  1766. // Select 20 -> 32
  1767. tableSelection.setCellSelection(
  1768. modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  1769. modelRoot.getNodeByPath( [ 0, 3, 1 ] )
  1770. );
  1771. pasteTable( [
  1772. [ 'aa', 'ab', 'ac' ],
  1773. [ 'ba', 'bb', 'bc' ]
  1774. ] );
  1775. // +----+----+----+
  1776. // | 00 | 01 | 02 |
  1777. // +----+ +----+
  1778. // | 10 | | 12 |
  1779. // +----+----+----+
  1780. // | aa | ab | ac |
  1781. // +----+----+----+
  1782. // | ba | bb | bc |
  1783. // +----+----+----+
  1784. // | 40 | 41 | 42 |
  1785. // +----+----+----+
  1786. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1787. [ '00', { contents: '01', rowspan: 2 }, '02' ],
  1788. [ '10', '12' ],
  1789. [ 'aa', 'ab', 'ac' ],
  1790. [ 'ba', 'bb', 'bc' ],
  1791. [ '40', '41', '42' ]
  1792. ] ) );
  1793. } );
  1794. it( 'should split cells outside the selected area before pasting (rowspan ends after the selection)', () => {
  1795. // +----+----+----+
  1796. // | 00 | 01 | 02 |
  1797. // +----+ +----+
  1798. // | 10 | | 12 |
  1799. // +----+ +----+
  1800. // | 20 | | 22 |
  1801. // +----+ +----+
  1802. // | 30 | | 32 |
  1803. // +----+----+----+
  1804. // | 40 | 41 | 42 |
  1805. // +----+----+----+
  1806. setModelData( model, modelTable( [
  1807. [ '00', { contents: '01', rowspan: 4 }, '02' ],
  1808. [ '10', '12' ],
  1809. [ '20', '22' ],
  1810. [ '30', '32' ],
  1811. [ '40', '41', '42' ]
  1812. ] ) );
  1813. // Select 10 -> 22
  1814. tableSelection.setCellSelection(
  1815. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  1816. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  1817. );
  1818. pasteTable( [
  1819. [ 'aa', 'ab', 'ac' ],
  1820. [ 'ba', 'bb', 'bc' ]
  1821. ] );
  1822. // +----+----+----+
  1823. // | 00 | 01 | 02 |
  1824. // +----+----+----+
  1825. // | aa | ab | ac |
  1826. // +----+----+----+
  1827. // | ba | bb | bc |
  1828. // +----+----+----+
  1829. // | 30 | | 32 |
  1830. // +----+----+----+
  1831. // | 40 | 41 | 42 |
  1832. // +----+----+----+
  1833. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1834. [ '00', '01', '02' ],
  1835. [ 'aa', 'ab', 'ac' ],
  1836. [ 'ba', 'bb', 'bc' ],
  1837. [ '30', '', '32' ],
  1838. [ '40', '41', '42' ]
  1839. ] ) );
  1840. } );
  1841. it( 'should split cells inside the selected area before pasting (rowspan ends after the selection)', () => {
  1842. // +----+----+----+
  1843. // | 00 | 01 | 02 |
  1844. // +----+ +----+
  1845. // | 10 | | 12 |
  1846. // +----+ +----+
  1847. // | 20 | | 22 |
  1848. // +----+ +----+
  1849. // | 30 | | 32 |
  1850. // +----+----+----+
  1851. // | 40 | 41 | 42 |
  1852. // +----+----+----+
  1853. setModelData( model, modelTable( [
  1854. [ '00', { contents: '01', rowspan: 4 }, '02' ],
  1855. [ '10', '12' ],
  1856. [ '20', '22' ],
  1857. [ '30', '32' ],
  1858. [ '40', '41', '42' ]
  1859. ] ) );
  1860. // Select 00 -> 12
  1861. tableSelection.setCellSelection(
  1862. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1863. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  1864. );
  1865. pasteTable( [
  1866. [ 'aa', 'ab', 'ac' ],
  1867. [ 'ba', 'bb', 'bc' ]
  1868. ] );
  1869. // +----+----+----+
  1870. // | aa | ab | ac |
  1871. // +----+----+----+
  1872. // | ba | bb | bc |
  1873. // +----+----+----+
  1874. // | 20 | | 22 |
  1875. // +----+ +----+
  1876. // | 30 | | 32 |
  1877. // +----+----+----+
  1878. // | 40 | 41 | 42 |
  1879. // +----+----+----+
  1880. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1881. [ 'aa', 'ab', 'ac' ],
  1882. [ 'ba', 'bb', 'bc' ],
  1883. [ '20', { rowspan: 2, contents: '' }, '22' ],
  1884. [ '30', '32' ],
  1885. [ '40', '41', '42' ]
  1886. ] ) );
  1887. } );
  1888. it( 'should split cells outside the selected area before pasting (colspan ends in selection)', () => {
  1889. // +----+----+----+----+----+
  1890. // | 00 | 01 | 02 | 03 | 04 |
  1891. // +----+----+----+----+----+
  1892. // | 10 | 14 |
  1893. // +----+----+----+----+----+
  1894. // | 20 | 21 | 22 | 23 | 24 |
  1895. // +----+----+----+----+----+
  1896. setModelData( model, modelTable( [
  1897. [ '00', '01', '02', '03', '04' ],
  1898. [ { contents: '10', colspan: 4 }, '14' ],
  1899. [ '20', '21', '22', '23', '24' ]
  1900. ] ) );
  1901. // Select 02 -> 23
  1902. tableSelection.setCellSelection(
  1903. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  1904. modelRoot.getNodeByPath( [ 0, 2, 3 ] )
  1905. );
  1906. pasteTable( [
  1907. [ 'aa', 'ab' ],
  1908. [ 'ba', 'bb' ],
  1909. [ 'ca', 'cb' ]
  1910. ] );
  1911. // +----+----+----+----+----+
  1912. // | 00 | 01 | aa | ab | 04 |
  1913. // +----+----+----+----+----+
  1914. // | 10 | ba | bb | 14 |
  1915. // +----+----+----+----+----+
  1916. // | 20 | 21 | ca | cb | 24 |
  1917. // +----+----+----+----+----+
  1918. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1919. [ '00', '01', 'aa', 'ab', '04' ],
  1920. [ { contents: '10', colspan: 2 }, 'ba', 'bb', '14' ],
  1921. [ '20', '21', 'ca', 'cb', '24' ]
  1922. ] ) );
  1923. } );
  1924. it( 'should split cells outside the selected area before pasting (colspan ends after the selection)', () => {
  1925. // +----+----+----+----+----+
  1926. // | 00 | 01 | 02 | 03 | 04 |
  1927. // +----+----+----+----+----+
  1928. // | 10 | 14 |
  1929. // +----+----+----+----+----+
  1930. // | 20 | 21 | 22 | 23 | 24 |
  1931. // +----+----+----+----+----+
  1932. setModelData( model, modelTable( [
  1933. [ '00', '01', '02', '03', '04' ],
  1934. [ { contents: '10', colspan: 4 }, '14' ],
  1935. [ '20', '21', '22', '23', '24' ]
  1936. ] ) );
  1937. // Select 01 -> 22
  1938. tableSelection.setCellSelection(
  1939. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  1940. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  1941. );
  1942. pasteTable( [
  1943. [ 'aa', 'ab' ],
  1944. [ 'ba', 'bb' ],
  1945. [ 'ca', 'cb' ]
  1946. ] );
  1947. // +----+----+----+----+----+
  1948. // | 00 | aa | ab | 03 | 04 |
  1949. // +----+----+----+----+----+
  1950. // | 10 | ba | bb | | 14 |
  1951. // +----+----+----+----+----+
  1952. // | 20 | ca | cb | 23 | 24 |
  1953. // +----+----+----+----+----+
  1954. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1955. [ '00', 'aa', 'ab', '03', '04' ],
  1956. [ '10', 'ba', 'bb', '', '14' ],
  1957. [ '20', 'ca', 'cb', '23', '24' ]
  1958. ] ) );
  1959. } );
  1960. it( 'should split cells inside the selected area before pasting (colspan ends after the selection)', () => {
  1961. // +----+----+----+----+----+
  1962. // | 00 | 01 | 02 | 03 | 04 |
  1963. // +----+----+----+----+----+
  1964. // | 10 | 14 |
  1965. // +----+----+----+----+----+
  1966. // | 20 | 21 | 22 | 23 | 24 |
  1967. // +----+----+----+----+----+
  1968. setModelData( model, modelTable( [
  1969. [ '00', '01', '02', '03', '04' ],
  1970. [ { contents: '10', colspan: 4 }, '14' ],
  1971. [ '20', '21', '22', '23', '24' ]
  1972. ] ) );
  1973. // Select 00 -> 21
  1974. tableSelection.setCellSelection(
  1975. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1976. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  1977. );
  1978. pasteTable( [
  1979. [ 'aa', 'ab' ],
  1980. [ 'ba', 'bb' ],
  1981. [ 'ca', 'cb' ]
  1982. ] );
  1983. // +----+----+----+----+----+
  1984. // | aa | ab | 02 | 03 | 04 |
  1985. // +----+----+----+----+----+
  1986. // | ba | bb | | 14 |
  1987. // +----+----+----+----+----+
  1988. // | ca | cb | 22 | 23 | 24 |
  1989. // +----+----+----+----+----+
  1990. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1991. [ 'aa', 'ab', '02', '03', '04' ],
  1992. [ 'ba', 'bb', { colspan: 2, contents: '' }, '14' ],
  1993. [ 'ca', 'cb', '22', '23', '24' ]
  1994. ] ) );
  1995. } );
  1996. it( 'should split cells anchored outside selection rectangle that overlaps selection (above selection)', () => {
  1997. // +----+----+----+
  1998. // | 00 | 02 |
  1999. // + +----+
  2000. // | | 12 |
  2001. // +----+----+----+
  2002. // | 20 | 21 | 22 |
  2003. // +----+----+----+
  2004. setModelData( model, modelTable( [
  2005. [ { contents: '00', colspan: 2, rowspan: 2 }, '02' ],
  2006. [ '12' ],
  2007. [ '20', '21', '22' ]
  2008. ] ) );
  2009. // Select 21 -> 12
  2010. tableSelection.setCellSelection(
  2011. modelRoot.getNodeByPath( [ 0, 2, 1 ] ),
  2012. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  2013. );
  2014. pasteTable( [
  2015. [ 'aa', 'ab' ],
  2016. [ 'ba', 'bb' ]
  2017. ] );
  2018. // +----+----+----+
  2019. // | 00 | | 02 |
  2020. // + +----+----+
  2021. // | | aa | ab |
  2022. // +----+----+----+
  2023. // | 20 | ba | bb |
  2024. // +----+----+----+
  2025. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2026. [ { contents: '00', rowspan: 2 }, '', '02' ],
  2027. [ 'aa', 'ab' ],
  2028. [ '20', 'ba', 'bb' ]
  2029. ] ) );
  2030. } );
  2031. it( 'should split cells anchored outside selection rectangle that overlaps selection (below selection)', () => {
  2032. // +----+----+----+
  2033. // | 00 | 01 | 02 |
  2034. // +----+----+----+
  2035. // | 10 | 12 |
  2036. // + +----+
  2037. // | | 22 |
  2038. // +----+----+----+
  2039. setModelData( model, modelTable( [
  2040. [ '00', '01', '02' ],
  2041. [ { contents: '10', colspan: 2, rowspan: 2 }, '12' ],
  2042. [ '22' ]
  2043. ] ) );
  2044. // Select 01 -> 12
  2045. tableSelection.setCellSelection(
  2046. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  2047. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  2048. );
  2049. pasteTable( [
  2050. [ 'aa', 'ab' ],
  2051. [ 'ba', 'bb' ]
  2052. ] );
  2053. // +----+----+----+
  2054. // | 00 | aa | ab |
  2055. // +----+----+----+
  2056. // | 10 | ba | bb |
  2057. // + +----+----+
  2058. // | | | 22 |
  2059. // +----+----+----+
  2060. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2061. [ '00', 'aa', 'ab' ],
  2062. [ { contents: '10', rowspan: 2 }, 'ba', 'bb' ],
  2063. [ '', '22' ]
  2064. ] ) );
  2065. } );
  2066. it( 'should properly handle complex case', () => {
  2067. // +----+----+----+----+----+----+----+
  2068. // | 00 | 03 | 04 |
  2069. // + + +----+----+----+
  2070. // | | | 14 | 15 | 16 |
  2071. // + + +----+----+----+
  2072. // | | | 24 |
  2073. // +----+----+----+----+----+----+----+
  2074. // | 30 | 31 | 33 | 34 | 35 | 36 |
  2075. // + +----+----+ +----+----+----+
  2076. // | | 41 | 42 | | 44 | 45 |
  2077. // + +----+----+ +----+ +
  2078. // | | 51 | 52 | | 54 | |
  2079. // +----+----+----+ +----+ +
  2080. // | 60 | 61 | 62 | | 64 | |
  2081. // +----+----+----+----+----+----+----+
  2082. setModelData( model, modelTable( [
  2083. [ { contents: '00', colspan: 3, rowspan: 3 }, { contents: '03', rowspan: 3 }, { colspan: 3, contents: '04' } ],
  2084. [ '14', '15', '16' ],
  2085. [ { contents: '24', colspan: 3 } ],
  2086. [
  2087. { contents: '30', rowspan: 3 },
  2088. { contents: '31', colspan: 2 },
  2089. { contents: '33', rowspan: 4 },
  2090. '34', '35', '36'
  2091. ],
  2092. [ '41', '42', '44', { contents: '45', colspan: 2, rowspan: 3 } ],
  2093. [ '51', '52', '54' ],
  2094. [ '60', '61', '62', '64' ]
  2095. ] ) );
  2096. // Select 42 -> 24 (3x3 selection)
  2097. tableSelection.setCellSelection(
  2098. modelRoot.getNodeByPath( [ 0, 4, 1 ] ),
  2099. modelRoot.getNodeByPath( [ 0, 2, 0 ] )
  2100. );
  2101. pasteTable( [
  2102. [ 'aa', 'ab', 'ac' ],
  2103. [ 'ba', 'bb', 'bc' ],
  2104. [ 'ca', 'cb', 'cc' ]
  2105. ] );
  2106. // +----+----+----+----+----+----+----+
  2107. // | 00 | | 03 | 04 |
  2108. // + + + +----+----+----+
  2109. // | | | | 14 | 15 | 16 |
  2110. // + +----+----+----+----+----+
  2111. // | | aa | ab | ac | |
  2112. // +----+----+----+----+----+----+----+
  2113. // | 30 | 31 | ba | bb | bc | 35 | 36 |
  2114. // + +----+----+----+----+----+----+
  2115. // | | 41 | ca | cb | cc | 45 |
  2116. // + +----+----+----+----+ +
  2117. // | | 51 | 52 | | 54 | |
  2118. // +----+----+----+ +----+ +
  2119. // | 60 | 61 | 62 | | 64 | |
  2120. // +----+----+----+----+----+----+----+
  2121. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2122. [
  2123. { contents: '00', colspan: 2, rowspan: 3 },
  2124. { contents: '', rowspan: 2 },
  2125. { contents: '03', rowspan: 2 },
  2126. { contents: '04', colspan: 3 }
  2127. ],
  2128. [ '14', '15', '16' ],
  2129. [ 'aa', 'ab', 'ac', { contents: '', colspan: 2 } ],
  2130. [ { contents: '30', rowspan: 3 }, '31', 'ba', 'bb', 'bc', '35', '36' ],
  2131. [ '41', 'ca', 'cb', 'cc', { contents: '45', colspan: 2, rowspan: 3 } ],
  2132. [ '51', '52', { contents: '', rowspan: 2 }, '54' ],
  2133. [ '60', '61', '62', '64' ]
  2134. ] ) );
  2135. } );
  2136. } );
  2137. } );
  2138. describe( 'pasted table is bigger than the selected area', () => {
  2139. describe( 'no spans', () => {
  2140. it( 'handles simple table paste to a simple table fragment - at the beginning of a table', () => {
  2141. tableSelection.setCellSelection(
  2142. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2143. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  2144. );
  2145. pasteTable( [
  2146. [ 'aa', 'ab', 'ac' ],
  2147. [ 'ba', 'bb', 'bc' ],
  2148. [ 'ca', 'cb', 'cc' ]
  2149. ] );
  2150. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2151. [ 'aa', 'ab', '02', '03' ],
  2152. [ 'ba', 'bb', '12', '13' ],
  2153. [ '20', '21', '22', '23' ],
  2154. [ '30', '31', '32', '33' ]
  2155. ] ) );
  2156. assertSelectedCells( model, [
  2157. [ 1, 1, 0, 0 ],
  2158. [ 1, 1, 0, 0 ],
  2159. [ 0, 0, 0, 0 ],
  2160. [ 0, 0, 0, 0 ]
  2161. ] );
  2162. } );
  2163. it( 'handles simple table paste to a simple table fragment - at the end of a table', () => {
  2164. tableSelection.setCellSelection(
  2165. modelRoot.getNodeByPath( [ 0, 2, 2 ] ),
  2166. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  2167. );
  2168. pasteTable( [
  2169. [ 'aa', 'ab', 'ac' ],
  2170. [ 'ba', 'bb', 'bc' ],
  2171. [ 'ca', 'cb', 'cc' ]
  2172. ] );
  2173. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2174. [ '00', '01', '02', '03' ],
  2175. [ '10', '11', '12', '13' ],
  2176. [ '20', '21', 'aa', 'ab' ],
  2177. [ '30', '31', 'ba', 'bb' ]
  2178. ] ) );
  2179. assertSelectedCells( model, [
  2180. [ 0, 0, 0, 0 ],
  2181. [ 0, 0, 0, 0 ],
  2182. [ 0, 0, 1, 1 ],
  2183. [ 0, 0, 1, 1 ]
  2184. ] );
  2185. } );
  2186. it( 'handles simple table paste to a simple table fragment - in the middle of a table', () => {
  2187. tableSelection.setCellSelection(
  2188. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  2189. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  2190. );
  2191. pasteTable( [
  2192. [ 'aa', 'ab', 'ac' ],
  2193. [ 'ba', 'bb', 'bc' ],
  2194. [ 'ca', 'cb', 'cc' ]
  2195. ] );
  2196. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2197. [ '00', '01', '02', '03' ],
  2198. [ '10', 'aa', 'ab', '13' ],
  2199. [ '20', 'ba', 'bb', '23' ],
  2200. [ '30', '31', '32', '33' ]
  2201. ] ) );
  2202. assertSelectedCells( model, [
  2203. [ 0, 0, 0, 0 ],
  2204. [ 0, 1, 1, 0 ],
  2205. [ 0, 1, 1, 0 ],
  2206. [ 0, 0, 0, 0 ]
  2207. ] );
  2208. } );
  2209. it( 'handles paste to a simple row fragment - in the middle of a table', () => {
  2210. tableSelection.setCellSelection(
  2211. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  2212. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  2213. );
  2214. pasteTable( [
  2215. [ 'aa', 'ab', 'ac' ],
  2216. [ 'ba', 'bb', 'bc' ],
  2217. [ 'ca', 'cb', 'cc' ]
  2218. ] );
  2219. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2220. [ '00', '01', '02', '03' ],
  2221. [ '10', 'aa', 'ab', '13' ],
  2222. [ '20', '21', '22', '23' ],
  2223. [ '30', '31', '32', '33' ]
  2224. ] ) );
  2225. assertSelectedCells( model, [
  2226. [ 0, 0, 0, 0 ],
  2227. [ 0, 1, 1, 0 ],
  2228. [ 0, 0, 0, 0 ],
  2229. [ 0, 0, 0, 0 ]
  2230. ] );
  2231. } );
  2232. it( 'handles paste to a simple column fragment - in the middle of a table', () => {
  2233. tableSelection.setCellSelection(
  2234. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  2235. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  2236. );
  2237. pasteTable( [
  2238. [ 'aa', 'ab', 'ac' ],
  2239. [ 'ba', 'bb', 'bc' ],
  2240. [ 'ca', 'cb', 'cc' ]
  2241. ] );
  2242. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2243. [ '00', '01', '02', '03' ],
  2244. [ '10', 'aa', '12', '13' ],
  2245. [ '20', 'ba', '22', '23' ],
  2246. [ '30', '31', '32', '33' ]
  2247. ] ) );
  2248. assertSelectedCells( model, [
  2249. [ 0, 0, 0, 0 ],
  2250. [ 0, 1, 0, 0 ],
  2251. [ 0, 1, 0, 0 ],
  2252. [ 0, 0, 0, 0 ]
  2253. ] );
  2254. } );
  2255. it( 'handles simple table paste to a simple table fragment - whole table selected', () => {
  2256. tableSelection.setCellSelection(
  2257. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2258. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  2259. );
  2260. pasteTable( [
  2261. [ 'aa', 'ab', 'ac', 'ad', 'ae' ],
  2262. [ 'ba', 'bb', 'bc', 'bd', 'be' ],
  2263. [ 'ca', 'cb', 'cc', 'cd', 'ce' ],
  2264. [ 'da', 'db', 'dc', 'dd', 'de' ],
  2265. [ 'ea', 'eb', 'ec', 'ed', 'ee' ]
  2266. ] );
  2267. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2268. [ 'aa', 'ab', 'ac', 'ad' ],
  2269. [ 'ba', 'bb', 'bc', 'bd' ],
  2270. [ 'ca', 'cb', 'cc', 'cd' ],
  2271. [ 'da', 'db', 'dc', 'dd' ]
  2272. ] ) );
  2273. assertSelectedCells( model, [
  2274. [ 1, 1, 1, 1 ],
  2275. [ 1, 1, 1, 1 ],
  2276. [ 1, 1, 1, 1 ],
  2277. [ 1, 1, 1, 1 ]
  2278. ] );
  2279. } );
  2280. } );
  2281. describe( 'pasted table has spans', () => {
  2282. it( 'handles pasting table that has cell with colspan', () => {
  2283. tableSelection.setCellSelection(
  2284. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  2285. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  2286. );
  2287. pasteTable( [
  2288. [ { colspan: 3, contents: 'aa' } ],
  2289. [ 'ba', 'bb', 'bc' ]
  2290. ] );
  2291. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2292. [ '00', '01', '02', '03' ],
  2293. [ '10', { colspan: 2, contents: 'aa' }, '13' ],
  2294. [ '20', 'ba', 'bb', '23' ],
  2295. [ '30', '31', '32', '33' ]
  2296. ] ) );
  2297. /* eslint-disable no-multi-spaces */
  2298. assertSelectedCells( model, [
  2299. [ 0, 0, 0, 0 ],
  2300. [ 0, 1, 0 ],
  2301. [ 0, 1, 1, 0 ],
  2302. [ 0, 0, 0, 0 ]
  2303. ] );
  2304. /* eslint-enable no-multi-spaces */
  2305. } );
  2306. it( 'handles pasting table that has many cells with various colspan', () => {
  2307. tableSelection.setCellSelection(
  2308. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2309. modelRoot.getNodeByPath( [ 0, 3, 2 ] )
  2310. );
  2311. pasteTable( [
  2312. [ 'aa', { colspan: 3, contents: 'ab' } ],
  2313. [ { colspan: 4, contents: 'ba' } ],
  2314. [ 'ca', 'cb', 'cc', 'cd' ],
  2315. [ { colspan: 2, contents: 'da' }, 'dc', 'dd' ]
  2316. ] );
  2317. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2318. [ 'aa', { colspan: 2, contents: 'ab' }, '03' ],
  2319. [ { colspan: 3, contents: 'ba' }, '13' ],
  2320. [ 'ca', 'cb', 'cc', '23' ],
  2321. [ { colspan: 2, contents: 'da' }, 'dc', '33' ]
  2322. ] ) );
  2323. /* eslint-disable no-multi-spaces */
  2324. assertSelectedCells( model, [
  2325. [ 1, 1, 0 ],
  2326. [ 1, 0 ],
  2327. [ 1, 1, 1, 0 ],
  2328. [ 1, 1, 0 ]
  2329. ] );
  2330. /* eslint-enable no-multi-spaces */
  2331. } );
  2332. it( 'handles pasting table that has cell with rowspan', () => {
  2333. tableSelection.setCellSelection(
  2334. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  2335. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  2336. );
  2337. pasteTable( [
  2338. [ { rowspan: 3, contents: 'aa' }, 'ab' ],
  2339. [ 'bb' ],
  2340. [ 'cb' ]
  2341. ] );
  2342. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2343. [ '00', '01', '02', '03' ],
  2344. [ '10', { rowspan: 2, contents: 'aa' }, 'ab', '13' ],
  2345. [ '20', 'bb', '23' ],
  2346. [ '30', '31', '32', '33' ]
  2347. ] ) );
  2348. /* eslint-disable no-multi-spaces */
  2349. assertSelectedCells( model, [
  2350. [ 0, 0, 0, 0 ],
  2351. [ 0, 1, 1, 0 ],
  2352. [ 0, 1, 0 ],
  2353. [ 0, 0, 0, 0 ]
  2354. ] );
  2355. /* eslint-enable no-multi-spaces */
  2356. } );
  2357. it( 'handles pasting table that has many cells with various rowspan', () => {
  2358. tableSelection.setCellSelection(
  2359. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2360. modelRoot.getNodeByPath( [ 0, 2, 3 ] )
  2361. );
  2362. pasteTable( [
  2363. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad', 'ae' ],
  2364. [ { rowspan: 3, contents: 'ba' }, 'bd', 'be' ],
  2365. [ 'cc', 'cd', 'ce' ],
  2366. [ 'da', 'db', 'dc', 'dd' ]
  2367. ] );
  2368. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2369. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  2370. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  2371. [ 'cc', 'cd' ],
  2372. [ '30', '31', '32', '33' ]
  2373. ] ) );
  2374. /* eslint-disable no-multi-spaces */
  2375. assertSelectedCells( model, [
  2376. [ 1, 1, 1, 1 ],
  2377. [ 1, 1 ],
  2378. [ 1, 1 ],
  2379. [ 0, 0, 0, 0 ]
  2380. ] );
  2381. /* eslint-enable no-multi-spaces */
  2382. } );
  2383. it( 'handles pasting multi-spanned table', () => {
  2384. setModelData( model, modelTable( [
  2385. [ '00', '01', '02', '03', '04', '05' ],
  2386. [ '10', '11', '12', '13', '14', '15' ],
  2387. [ '20', '21', '22', '23', '24', '25' ],
  2388. [ '30', '31', '32', '33', '34', '35' ],
  2389. [ '40', '41', '42', '43', '44', '45' ]
  2390. ] ) );
  2391. tableSelection.setCellSelection(
  2392. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  2393. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  2394. );
  2395. // +----+----+----+----+----+
  2396. // | aa | ac | ad | ae |
  2397. // +----+----+----+----+ +
  2398. // | ba | bb | |
  2399. // +----+ +----+
  2400. // | ca | | ce |
  2401. // + +----+----+----+----+
  2402. // | | db | dc | dd |
  2403. // +----+----+----+----+----+
  2404. pasteTable( [
  2405. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 } ],
  2406. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 } ],
  2407. [ { contents: 'ca', rowspan: 2 }, 'ce' ],
  2408. [ 'db', 'dc', { contents: 'dd', colspan: 2 } ]
  2409. ] );
  2410. // +----+----+----+----+----+----+
  2411. // | 00 | 01 | 02 | 03 | 04 | 05 |
  2412. // +----+----+----+----+----+----+
  2413. // | 10 | aa | ac | 14 | 15 |
  2414. // +----+----+----+----+----+----+
  2415. // | 20 | ba | bb | 24 | 25 |
  2416. // +----+----+ +----+----+
  2417. // | 30 | ca | | 34 | 35 |
  2418. // +----+----+----+----+----+----+
  2419. // | 40 | 41 | 42 | 43 | 44 | 45 |
  2420. // +----+----+----+----+----+----+
  2421. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2422. [ '00', '01', '02', '03', '04', '05' ],
  2423. [ '10', { contents: 'aa', colspan: 2 }, 'ac', '14', '15' ],
  2424. [ '20', 'ba', { contents: 'bb', colspan: 2, rowspan: 2 }, '24', '25' ],
  2425. [ '30', 'ca', '34', '35' ],
  2426. [ '40', '41', '42', '43', '44', '45' ]
  2427. ] ) );
  2428. /* eslint-disable no-multi-spaces */
  2429. assertSelectedCells( model, [
  2430. [ 0, 0, 0, 0, 0, 0 ],
  2431. [ 0, 1, 1, 0, 0 ],
  2432. [ 0, 1, 1, 0, 0 ],
  2433. [ 0, 1, 0, 0 ],
  2434. [ 0, 0, 0, 0, 0, 0 ]
  2435. ] );
  2436. /* eslint-enable no-multi-spaces */
  2437. } );
  2438. } );
  2439. } );
  2440. describe( 'pasted table is smaller than the selected area', () => {
  2441. describe( 'no spans', () => {
  2442. beforeEach( () => {
  2443. setModelData( model, modelTable( [
  2444. [ '00', '01', '02', '03', '04', '05' ],
  2445. [ '10', '11', '12', '13', '14', '15' ],
  2446. [ '20', '21', '22', '23', '24', '25' ],
  2447. [ '30', '31', '32', '33', '34', '35' ],
  2448. [ '40', '41', '42', '43', '44', '45' ],
  2449. [ '50', '51', '52', '53', '54', '55' ]
  2450. ] ) );
  2451. } );
  2452. it( 'should repeat pasted cells horizontally', () => {
  2453. tableSelection.setCellSelection(
  2454. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2455. modelRoot.getNodeByPath( [ 0, 1, 4 ] )
  2456. );
  2457. pasteTable( [
  2458. [ 'aa', 'ab', 'ac' ],
  2459. [ 'ba', 'bb', 'bc' ],
  2460. [ 'ca', 'cb', 'cc' ]
  2461. ] );
  2462. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2463. [ 'aa', 'ab', 'ac', 'aa', 'ab', '05' ],
  2464. [ 'ba', 'bb', 'bc', 'ba', 'bb', '15' ],
  2465. [ '20', '21', '22', '23', '24', '25' ],
  2466. [ '30', '31', '32', '33', '34', '35' ],
  2467. [ '40', '41', '42', '43', '44', '45' ],
  2468. [ '50', '51', '52', '53', '54', '55' ]
  2469. ] ) );
  2470. assertSelectedCells( model, [
  2471. [ 1, 1, 1, 1, 1, 0 ],
  2472. [ 1, 1, 1, 1, 1, 0 ],
  2473. [ 0, 0, 0, 0, 0, 0 ],
  2474. [ 0, 0, 0, 0, 0, 0 ]
  2475. ] );
  2476. } );
  2477. it( 'should repeat pasted cells vertically', () => {
  2478. tableSelection.setCellSelection(
  2479. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2480. modelRoot.getNodeByPath( [ 0, 4, 1 ] )
  2481. );
  2482. pasteTable( [
  2483. [ 'aa', 'ab', 'ac' ],
  2484. [ 'ba', 'bb', 'bc' ],
  2485. [ 'ca', 'cb', 'cc' ]
  2486. ] );
  2487. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2488. [ 'aa', 'ab', '02', '03', '04', '05' ],
  2489. [ 'ba', 'bb', '12', '13', '14', '15' ],
  2490. [ 'ca', 'cb', '22', '23', '24', '25' ],
  2491. [ 'aa', 'ab', '32', '33', '34', '35' ],
  2492. [ 'ba', 'bb', '42', '43', '44', '45' ],
  2493. [ '50', '51', '52', '53', '54', '55' ]
  2494. ] ) );
  2495. assertSelectedCells( model, [
  2496. [ 1, 1, 0, 0, 0, 0 ],
  2497. [ 1, 1, 0, 0, 0, 0 ],
  2498. [ 1, 1, 0, 0, 0, 0 ],
  2499. [ 1, 1, 0, 0, 0, 0 ],
  2500. [ 1, 1, 0, 0, 0, 0 ],
  2501. [ 0, 0, 0, 0, 0, 0 ]
  2502. ] );
  2503. } );
  2504. it( 'should repeat pasted cells in both directions', () => {
  2505. tableSelection.setCellSelection(
  2506. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2507. modelRoot.getNodeByPath( [ 0, 4, 4 ] )
  2508. );
  2509. pasteTable( [
  2510. [ 'aa', 'ab', 'ac' ],
  2511. [ 'ba', 'bb', 'bc' ],
  2512. [ 'ca', 'cb', 'cc' ]
  2513. ] );
  2514. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2515. [ 'aa', 'ab', 'ac', 'aa', 'ab', '05' ],
  2516. [ 'ba', 'bb', 'bc', 'ba', 'bb', '15' ],
  2517. [ 'ca', 'cb', 'cc', 'ca', 'cb', '25' ],
  2518. [ 'aa', 'ab', 'ac', 'aa', 'ab', '35' ],
  2519. [ 'ba', 'bb', 'bc', 'ba', 'bb', '45' ],
  2520. [ '50', '51', '52', '53', '54', '55' ]
  2521. ] ) );
  2522. assertSelectedCells( model, [
  2523. [ 1, 1, 1, 1, 1, 0 ],
  2524. [ 1, 1, 1, 1, 1, 0 ],
  2525. [ 1, 1, 1, 1, 1, 0 ],
  2526. [ 1, 1, 1, 1, 1, 0 ],
  2527. [ 1, 1, 1, 1, 1, 0 ],
  2528. [ 0, 0, 0, 0, 0, 0 ]
  2529. ] );
  2530. } );
  2531. it( 'should repeat pasted cells in the both directions when pasted on table end', () => {
  2532. tableSelection.setCellSelection(
  2533. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  2534. modelRoot.getNodeByPath( [ 0, 5, 5 ] )
  2535. );
  2536. pasteTable( [
  2537. [ 'aa', 'ab', 'ac' ],
  2538. [ 'ba', 'bb', 'bc' ],
  2539. [ 'ca', 'cb', 'cc' ]
  2540. ] );
  2541. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2542. [ '00', '01', '02', '03', '04', '05' ],
  2543. [ '10', 'aa', 'ab', 'ac', 'aa', 'ab' ],
  2544. [ '20', 'ba', 'bb', 'bc', 'ba', 'bb' ],
  2545. [ '30', 'ca', 'cb', 'cc', 'ca', 'cb' ],
  2546. [ '40', 'aa', 'ab', 'ac', 'aa', 'ab' ],
  2547. [ '50', 'ba', 'bb', 'bc', 'ba', 'bb' ]
  2548. ] ) );
  2549. assertSelectedCells( model, [
  2550. [ 0, 0, 0, 0, 0, 0 ],
  2551. [ 0, 1, 1, 1, 1, 1 ],
  2552. [ 0, 1, 1, 1, 1, 1 ],
  2553. [ 0, 1, 1, 1, 1, 1 ],
  2554. [ 0, 1, 1, 1, 1, 1 ],
  2555. [ 0, 1, 1, 1, 1, 1 ]
  2556. ] );
  2557. } );
  2558. } );
  2559. describe( 'content table has spans', () => {
  2560. beforeEach( () => {
  2561. // +----+----+----+----+----+----+
  2562. // | 00 | 01 | 02 | 03 | 04 | 05 |
  2563. // +----+----+----+ +----+----+
  2564. // | 10 | 11 | 12 | | 14 | 15 |
  2565. // +----+----+ + +----+----+
  2566. // | 20 | 21 | | | 24 | 25 |
  2567. // +----+----+----+ + +----+
  2568. // | 30 | 31 | | | 35 |
  2569. // +----+----+----+----+----+----+
  2570. // | 40 | 44 |
  2571. // +----+----+----+----+ +
  2572. // | 50 | 51 | 52 | |
  2573. // +----+----+----+----+----+----+
  2574. // | 60 | 61 | 62 | 63 | 64 | 65 |
  2575. // +----+----+----+----+----+----+
  2576. setModelData( model, modelTable( [
  2577. [ '00', '01', '02', { contents: '03', rowspan: 4 }, '04', '05' ],
  2578. [ '10', '11', { contents: '12', rowspan: 2 }, '14', '15' ],
  2579. [ '20', '21', { contents: '24', rowspan: 2 }, '25' ],
  2580. [ '30', { contents: '31', colspan: 2 }, '35' ],
  2581. [ { contents: '40', colspan: 4 }, { contents: '44', colspan: 2, rowspan: 2 } ],
  2582. [ '50', '51', { contents: '52', colspan: 2 } ],
  2583. [ '60', '61', '62', '63', '64', '65' ]
  2584. ] ) );
  2585. } );
  2586. it( 'should split spanned cells on the selection edges (vertical spans)', () => {
  2587. tableSelection.setCellSelection(
  2588. modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  2589. modelRoot.getNodeByPath( [ 0, 4, 1 ] ) // Cell 44.
  2590. );
  2591. pasteTable( [
  2592. [ 'aa', 'ab' ],
  2593. [ 'ba', 'bb' ]
  2594. ] );
  2595. // +----+----+----+----+----+----+
  2596. // | 00 | 01 | 02 | 03 | 04 | 05 |
  2597. // +----+----+----+ +----+----+
  2598. // | 10 | 11 | 12 | | 14 | 15 |
  2599. // +----+----+----+----+----+----+
  2600. // | aa | ab | aa | ab | aa | 25 |
  2601. // +----+----+----+----+----+----+
  2602. // | ba | bb | ba | bb | ba | 35 |
  2603. // +----+----+----+----+----+----+
  2604. // | aa | ab | aa | ab | aa | |
  2605. // +----+----+----+----+----+ +
  2606. // | 50 | 51 | 52 | | |
  2607. // +----+----+----+----+----+----+
  2608. // | 60 | 61 | 62 | 63 | 64 | 65 |
  2609. // +----+----+----+----+----+----+
  2610. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2611. [ '00', '01', '02', { contents: '03', rowspan: 2 }, '04', '05' ],
  2612. [ '10', '11', '12', '14', '15' ],
  2613. [ 'aa', 'ab', 'aa', 'ab', 'aa', '25' ],
  2614. [ 'ba', 'bb', 'ba', 'bb', 'ba', '35' ],
  2615. [ 'aa', 'ab', 'aa', 'ab', 'aa', { contents: '', rowspan: 2 } ],
  2616. [ '50', '51', { contents: '52', colspan: 2 }, '' ],
  2617. [ '60', '61', '62', '63', '64', '65' ]
  2618. ] ) );
  2619. } );
  2620. it( 'should split spanned cells on the selection edges (horizontal spans)', () => {
  2621. tableSelection.setCellSelection(
  2622. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  2623. modelRoot.getNodeByPath( [ 0, 4, 1 ] ) // Cell 44.
  2624. );
  2625. pasteTable( [
  2626. [ 'aa', 'ab' ],
  2627. [ 'ba', 'bb' ]
  2628. ] );
  2629. // +----+----+----+----+----+----+
  2630. // | 00 | 01 | aa | ab | aa | 05 |
  2631. // +----+----+----+----+----+----+
  2632. // | 10 | 11 | ba | bb | ba | 15 |
  2633. // +----+----+----+----+----+----+
  2634. // | 20 | 21 | aa | ab | aa | 25 |
  2635. // +----+----+----+----+----+----+
  2636. // | 30 | 31 | ba | bb | ba | 35 |
  2637. // +----+----+----+----+----+----+
  2638. // | 40 | aa | ab | aa | |
  2639. // +----+----+----+----+----+ +
  2640. // | 50 | 51 | 52 | | |
  2641. // +----+----+----+----+----+----+
  2642. // | 60 | 61 | 62 | 63 | 64 | 65 |
  2643. // +----+----+----+----+----+----+
  2644. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2645. [ '00', '01', 'aa', 'ab', 'aa', '05' ],
  2646. [ '10', '11', 'ba', 'bb', 'ba', '15' ],
  2647. [ '20', '21', 'aa', 'ab', 'aa', '25' ],
  2648. [ '30', '31', 'ba', 'bb', 'ba', '35' ],
  2649. [ { contents: '40', colspan: 2 }, 'aa', 'ab', 'aa', { contents: '', rowspan: 2 } ],
  2650. [ '50', '51', { contents: '52', colspan: 2 }, '' ],
  2651. [ '60', '61', '62', '63', '64', '65' ]
  2652. ] ) );
  2653. } );
  2654. } );
  2655. describe( 'pasted table has spans', () => {
  2656. beforeEach( () => {
  2657. setModelData( model, modelTable( [
  2658. [ '00', '01', '02', '03', '04', '05', '06' ],
  2659. [ '10', '11', '12', '13', '14', '15', '16' ],
  2660. [ '20', '21', '22', '23', '24', '25', '26' ],
  2661. [ '30', '31', '32', '33', '34', '35', '36' ],
  2662. [ '40', '41', '42', '43', '44', '45', '46' ],
  2663. [ '50', '51', '52', '53', '54', '55', '56' ],
  2664. [ '60', '61', '62', '63', '64', '65', '66' ],
  2665. [ '70', '71', '72', '73', '74', '75', '76' ],
  2666. [ '80', '81', '82', '83', '84', '85', '86' ]
  2667. ] ) );
  2668. } );
  2669. it( 'should trim overlapping cells', () => {
  2670. tableSelection.setCellSelection(
  2671. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  2672. modelRoot.getNodeByPath( [ 0, 8, 6 ] )
  2673. );
  2674. // +----+----+----+----+
  2675. // | aa |
  2676. // +----+----+----+----+
  2677. // | ba | bb |
  2678. // + +----+----+----+
  2679. // | | cb | cc |
  2680. // + + +----+----+
  2681. // | | | dc | dd |
  2682. // + + + +----+
  2683. // | | | | ed |
  2684. // +----+----+----+----+
  2685. pasteTable( [
  2686. [ { contents: 'aa', colspan: 4 } ],
  2687. [ { contents: 'ba', rowspan: 4 }, { contents: 'bb', colspan: 3 } ],
  2688. [ { contents: 'cb', rowspan: 3 }, { contents: 'cc', colspan: 2 } ],
  2689. [ { contents: 'dc', rowspan: 2 }, 'dd' ],
  2690. [ 'ed' ]
  2691. ] );
  2692. // +----+----+----+----+----+----+----+
  2693. // | 00 | 01 | 02 | 03 | 04 | 05 | 06 |
  2694. // +----+----+----+----+----+----+----+
  2695. // | 10 | aa | aa |
  2696. // +----+----+----+----+----+----+----+
  2697. // | 20 | ba | bb | ba | bb |
  2698. // +----+ +----+----+----+ +----+
  2699. // | 30 | | cb | cc | | cb |
  2700. // +----+ + +----+----+ + +
  2701. // | 40 | | | dc | dd | | |
  2702. // +----+ + + +----+ + +
  2703. // | 50 | | | | ed | | |
  2704. // +----+----+----+----+----+----+----+
  2705. // | 60 | aa | aa |
  2706. // +----+----+----+----+----+----+----+
  2707. // | 70 | ba | bb | ba | bb |
  2708. // +----+ +----+----+----+ +----+
  2709. // | 80 | | cb | cc | | cb |
  2710. // +----+----+----+----+----+----+----+
  2711. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2712. [ '00', '01', '02', '03', '04', '05', '06' ],
  2713. [ '10', { contents: 'aa', colspan: 4 }, { contents: 'aa', colspan: 2 } ],
  2714. [ '20', { contents: 'ba', rowspan: 4 }, { contents: 'bb', colspan: 3 }, { contents: 'ba', rowspan: 4 }, 'bb' ],
  2715. [ '30', { contents: 'cb', rowspan: 3 }, { contents: 'cc', colspan: 2 }, { contents: 'cb', rowspan: 3 } ],
  2716. [ '40', { contents: 'dc', rowspan: 2 }, 'dd' ],
  2717. [ '50', 'ed' ],
  2718. [ '60', { contents: 'aa', colspan: 4 }, { contents: 'aa', colspan: 2 } ],
  2719. [ '70', { contents: 'ba', rowspan: 2 }, { contents: 'bb', colspan: 3 }, { contents: 'ba', rowspan: 2 }, 'bb' ],
  2720. [ '80', 'cb', { contents: 'cc', colspan: 2 }, 'cb' ]
  2721. ] ) );
  2722. } );
  2723. } );
  2724. } );
  2725. describe( 'fixing pasted table broken layout', () => {
  2726. it( 'should trim pasted cells\' width if they exceeds table width established by the first row', () => {
  2727. // Select 00 -> 22 (selection 2x3 - equal to expected fixed table)
  2728. tableSelection.setCellSelection(
  2729. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2730. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  2731. );
  2732. // +----+----+
  2733. // | aa | ab | <- First row establish table width=2.
  2734. // +----+----+----+----+
  2735. // | ba | bb | <- Cell "bb" sticks out by 2 slots.
  2736. // +----+----+----+----+
  2737. // | ca | <- Cell "ca" sticks out by 1 slot.
  2738. // +----+----+----+
  2739. pasteTable( [
  2740. [ 'aa', 'ab' ],
  2741. [ 'ba', { colspan: 3, contents: 'bb' } ],
  2742. [ { colspan: 3, contents: 'ca' } ]
  2743. ] );
  2744. // +----+----+----+----+
  2745. // | aa | ab | 02 | 03 |
  2746. // +----+----+----+----+
  2747. // | ba | bb | 12 | 13 |
  2748. // +----+----+----+----+
  2749. // | ca | 22 | 23 |
  2750. // +----+----+----+----+
  2751. // | 30 | 31 | 32 | 33 |
  2752. // +----+----+----+----+
  2753. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2754. [ 'aa', 'ab', '02', '03' ],
  2755. [ 'ba', 'bb', '12', '13' ],
  2756. [ { contents: 'ca', colspan: 2 }, '22', '23' ],
  2757. [ '30', '31', '32', '33' ]
  2758. ] ) );
  2759. /* eslint-disable no-multi-spaces */
  2760. assertSelectedCells( model, [
  2761. [ 1, 1, 0, 0 ],
  2762. [ 1, 1, 0, 0 ],
  2763. [ 1, 0, 0 ],
  2764. [ 0, 0, 0, 0 ]
  2765. ] );
  2766. /* eslint-enable no-multi-spaces */
  2767. } );
  2768. it( 'should trim pasted cells\' height if they exceeds table height established by the last row', () => {
  2769. // Select 00 -> 12 (selection 3x2 - equal to expected fixed table)
  2770. tableSelection.setCellSelection(
  2771. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2772. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  2773. );
  2774. // +----+----+----+
  2775. // | aa | ab | ac |
  2776. // +----+----+ +
  2777. // | ba | bb | | <- Last row establish table height=2.
  2778. // +----+ + +
  2779. // | | | <- Cell "ac" sticks out by 1 slot.
  2780. // + +----+
  2781. // | | <- Cell "bb" sticks out by 2 slots.
  2782. // +----+
  2783. pasteTable( [
  2784. [ 'aa', 'ab', { contents: 'ac', rowspan: 3 } ],
  2785. [ 'ba', { contents: 'bb', rowspan: 3 } ]
  2786. ] );
  2787. // +----+----+----+----+
  2788. // | aa | ab | ac | 03 |
  2789. // +----+----+ +----+
  2790. // | ba | bb | | 13 |
  2791. // +----+----+----+----+
  2792. // | 20 | 21 | 22 | 23 |
  2793. // +----+----+----+----+
  2794. // | 30 | 31 | 32 | 33 |
  2795. // +----+----+----+----+
  2796. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2797. [ 'aa', 'ab', { rowspan: 2, contents: 'ac' }, '03' ],
  2798. [ 'ba', 'bb', '13' ],
  2799. [ '20', '21', '22', '23' ],
  2800. [ '30', '31', '32', '33' ]
  2801. ] ) );
  2802. /* eslint-disable no-multi-spaces */
  2803. assertSelectedCells( model, [
  2804. [ 1, 1, 1, 0 ],
  2805. [ 1, 1, 0 ],
  2806. [ 0, 0, 0, 0 ],
  2807. [ 0, 0, 0, 0 ]
  2808. ] );
  2809. /* eslint-enable no-multi-spaces */
  2810. } );
  2811. it( 'should trim pasted cells\' height and width if they exceeds table height and width', () => {
  2812. // Select 00 -> 11 (selection 2x2 - equal to expected fixed table)
  2813. tableSelection.setCellSelection(
  2814. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2815. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  2816. );
  2817. // +----+----+
  2818. // | aa | ab |
  2819. // +----+----+----+----+
  2820. // | ba | bb | <- Cell "bb" sticks out by 2 slots in width and by 1 slot in height.
  2821. // +----+ +
  2822. // | |
  2823. // +----+----+----+
  2824. pasteTable( [
  2825. [ 'aa', 'ab' ],
  2826. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 } ]
  2827. ] );
  2828. // +----+----+----+----+
  2829. // | aa | ab | 02 | 03 |
  2830. // +----+----+----+----+
  2831. // | ba | bb | 12 | 13 |
  2832. // +----+----+----+----+
  2833. // | 20 | 21 | 22 | 23 |
  2834. // +----+----+----+----+
  2835. // | 30 | 31 | 32 | 33 |
  2836. // +----+----+----+----+
  2837. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2838. [ 'aa', 'ab', '02', '03' ],
  2839. [ 'ba', 'bb', '12', '13' ],
  2840. [ '20', '21', '22', '23' ],
  2841. [ '30', '31', '32', '33' ]
  2842. ] ) );
  2843. /* eslint-disable no-multi-spaces */
  2844. assertSelectedCells( model, [
  2845. [ 1, 1, 0, 0 ],
  2846. [ 1, 1, 0, 0 ],
  2847. [ 0, 0, 0, 0 ],
  2848. [ 0, 0, 0, 0 ]
  2849. ] );
  2850. /* eslint-enable no-multi-spaces */
  2851. } );
  2852. it(
  2853. 'should trim pasted cells\' width if they exceeds pasted table width (pasted height is bigger then selection height)',
  2854. () => {
  2855. // Select 00 -> 11 (selection 2x2 - smaller by height than the expected fixed table)
  2856. tableSelection.setCellSelection(
  2857. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2858. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  2859. );
  2860. // +----+----+
  2861. // | aa | ab | <- First row establish table width=2.
  2862. // +----+----+----+----+
  2863. // | ba | bb | <- Cell "bb" sticks out by 2 slots.
  2864. // +----+----+----+----+
  2865. // | ca | <- Cell "ca" sticks out by 1 slot.
  2866. // +----+----+----+
  2867. pasteTable( [
  2868. [ 'aa', 'ab' ],
  2869. [ 'ba', { colspan: 3, contents: 'bb' } ],
  2870. [ { colspan: 3, contents: 'ca' } ]
  2871. ] );
  2872. // +----+----+----+----+
  2873. // | aa | ab | 02 | 03 |
  2874. // +----+----+----+----+
  2875. // | ba | bb | 12 | 13 |
  2876. // +----+----+----+----+
  2877. // | 20 | 21 | 22 | 23 |
  2878. // +----+----+----+----+
  2879. // | 30 | 31 | 32 | 33 |
  2880. // +----+----+----+----+
  2881. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2882. [ 'aa', 'ab', '02', '03' ],
  2883. [ 'ba', 'bb', '12', '13' ],
  2884. [ '20', '21', '22', '23' ],
  2885. [ '30', '31', '32', '33' ]
  2886. ] ) );
  2887. assertSelectedCells( model, [
  2888. [ 1, 1, 0, 0 ],
  2889. [ 1, 1, 0, 0 ],
  2890. [ 0, 0, 0, 0 ],
  2891. [ 0, 0, 0, 0 ]
  2892. ] );
  2893. }
  2894. );
  2895. it(
  2896. 'should trim pasted cells\' height if they exceeds pasted table height (pasted width is bigger then selection width)',
  2897. () => {
  2898. // Select 00 -> 11 (selection 2x2 - smaller by width than the expected fixed table)
  2899. tableSelection.setCellSelection(
  2900. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2901. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  2902. );
  2903. // +----+----+----+
  2904. // | aa | ab | ac |
  2905. // +----+----+ +
  2906. // | ba | bb | | <- Last row establish table height=2.
  2907. // +----+ + +
  2908. // | | | <- Cell "ac" sticks out by 1 slot.
  2909. // + +----+
  2910. // | | <- Cell "bb" sticks out by 2 slots.
  2911. // +----+
  2912. pasteTable( [
  2913. [ 'aa', 'ab', { contents: 'ac', rowspan: 3 } ],
  2914. [ 'ba', { contents: 'bb', rowspan: 3 } ]
  2915. ] );
  2916. // +----+----+----+----+
  2917. // | aa | ab | 02 | 03 |
  2918. // +----+----+----+----+
  2919. // | ba | bb | 12 | 13 |
  2920. // +----+----+----+----+
  2921. // | 20 | 21 | 22 | 23 |
  2922. // +----+----+----+----+
  2923. // | 30 | 31 | 32 | 33 |
  2924. // +----+----+----+----+
  2925. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2926. [ 'aa', 'ab', '02', '03' ],
  2927. [ 'ba', 'bb', '12', '13' ],
  2928. [ '20', '21', '22', '23' ],
  2929. [ '30', '31', '32', '33' ]
  2930. ] ) );
  2931. /* eslint-disable no-multi-spaces */
  2932. assertSelectedCells( model, [
  2933. [ 1, 1, 0, 0 ],
  2934. [ 1, 1, 0, 0 ],
  2935. [ 0, 0, 0, 0 ],
  2936. [ 0, 0, 0, 0 ]
  2937. ] );
  2938. /* eslint-enable no-multi-spaces */
  2939. }
  2940. );
  2941. it(
  2942. `should trim pasted pasted cells' height and width if they exceeds pasted table dimensions
  2943. (pasted table is bigger then selection width)`,
  2944. () => {
  2945. // Select 00 -> 11 (selection 2x2 - smaller than the expected fixed table)
  2946. tableSelection.setCellSelection(
  2947. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2948. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  2949. );
  2950. // +----+----+----+
  2951. // | aa | ab | ac |
  2952. // +----+----+----+----+
  2953. // | ba | bb | <- Cell "bb" sticks out by 1 slots in width and by 1 slot in height.
  2954. // +----+ +
  2955. // | ca | |
  2956. // +----+ +
  2957. // | |
  2958. // +----+----+----+
  2959. pasteTable( [
  2960. [ 'aa', 'ab' ],
  2961. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 } ]
  2962. ] );
  2963. // +----+----+----+----+
  2964. // | aa | ab | 02 | 03 |
  2965. // +----+----+----+----+
  2966. // | ba | bb | 12 | 13 |
  2967. // +----+----+----+----+
  2968. // | 20 | 21 | 22 | 23 |
  2969. // +----+----+----+----+
  2970. // | 30 | 31 | 32 | 33 |
  2971. // +----+----+----+----+
  2972. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2973. [ 'aa', 'ab', '02', '03' ],
  2974. [ 'ba', 'bb', '12', '13' ],
  2975. [ '20', '21', '22', '23' ],
  2976. [ '30', '31', '32', '33' ]
  2977. ] ) );
  2978. /* eslint-disable no-multi-spaces */
  2979. assertSelectedCells( model, [
  2980. [ 1, 1, 0, 0 ],
  2981. [ 1, 1, 0, 0 ],
  2982. [ 0, 0, 0, 0 ],
  2983. [ 0, 0, 0, 0 ]
  2984. ] );
  2985. /* eslint-enable no-multi-spaces */
  2986. }
  2987. );
  2988. } );
  2989. } );
  2990. describe( 'Clipboard integration - paste (content scenarios)', () => {
  2991. it( 'handles multiple paragraphs in table cell', async () => {
  2992. await createEditor();
  2993. setModelData( model, modelTable( [
  2994. [ '00', '01', '02' ],
  2995. [ '01', '11', '12' ],
  2996. [ '02', '21', '22' ]
  2997. ] ) );
  2998. tableSelection.setCellSelection(
  2999. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  3000. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  3001. );
  3002. pasteTable( [
  3003. [ '<p>a</p><p>a</p><p>a</p>', 'ab' ],
  3004. [ 'ba', 'bb' ]
  3005. ] );
  3006. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  3007. [ '<paragraph>a</paragraph><paragraph>a</paragraph><paragraph>a</paragraph>', 'ab', '02' ],
  3008. [ 'ba', 'bb', '12' ],
  3009. [ '02', '21', '22' ]
  3010. ] ) );
  3011. } );
  3012. it( 'handles image in table cell', async () => {
  3013. await createEditor( [ ImageEditing, ImageCaptionEditing ] );
  3014. setModelData( model, modelTable( [
  3015. [ '00', '01', '02' ],
  3016. [ '01', '11', '12' ],
  3017. [ '02', '21', '22' ]
  3018. ] ) );
  3019. tableSelection.setCellSelection(
  3020. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  3021. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  3022. );
  3023. pasteTable( [
  3024. [ '<img src="/assets/sample.jpg">', 'ab' ],
  3025. [ 'ba', 'bb' ]
  3026. ] );
  3027. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  3028. [ '<image src="/assets/sample.jpg"><caption></caption></image>', 'ab', '02' ],
  3029. [ 'ba', 'bb', '12' ],
  3030. [ '02', '21', '22' ]
  3031. ] ) );
  3032. } );
  3033. it( 'handles mixed nested content in table cell', async () => {
  3034. await createEditor( [ ImageEditing, ImageCaptionEditing, BlockQuoteEditing, HorizontalLineEditing, ListEditing ] );
  3035. setModelData( model, modelTable( [
  3036. [ '00', '01', '02' ],
  3037. [ '01', '11', '12' ],
  3038. [ '02', '21', '22' ]
  3039. ] ) );
  3040. tableSelection.setCellSelection(
  3041. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  3042. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  3043. );
  3044. const img = '<img src="/assets/sample.jpg">';
  3045. const list = '<ul><li>foo</li><li>bar</li></ul>';
  3046. const blockquote = `<blockquote><p>baz</p>${ list }</blockquote>`;
  3047. pasteTable( [
  3048. [ `${ img }${ list }${ blockquote }`, 'ab' ],
  3049. [ 'ba', 'bb' ]
  3050. ] );
  3051. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  3052. [
  3053. '<image src="/assets/sample.jpg"><caption></caption></image>' +
  3054. '<listItem listIndent="0" listType="bulleted">foo</listItem>' +
  3055. '<listItem listIndent="0" listType="bulleted">bar</listItem>' +
  3056. '<blockQuote>' +
  3057. '<paragraph>baz</paragraph>' +
  3058. '<listItem listIndent="0" listType="bulleted">foo</listItem>' +
  3059. '<listItem listIndent="0" listType="bulleted">bar</listItem>' +
  3060. '</blockQuote>',
  3061. 'ab',
  3062. '02' ],
  3063. [ 'ba', 'bb', '12' ],
  3064. [ '02', '21', '22' ]
  3065. ] ) );
  3066. } );
  3067. it( 'handles table cell properties', async () => {
  3068. await createEditor( [ TableCellPropertiesEditing ] );
  3069. setModelData( model, modelTable( [
  3070. [ '00', '01', '02' ],
  3071. [ '01', '11', '12' ],
  3072. [ '02', '21', '22' ]
  3073. ] ) );
  3074. tableSelection.setCellSelection(
  3075. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  3076. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  3077. );
  3078. pasteTable( [
  3079. [ { contents: 'aa', style: 'border:1px solid #f00;background:#ba7;width:1337px' }, 'ab' ],
  3080. [ 'ba', 'bb' ]
  3081. ] );
  3082. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  3083. expect( tableCell.getAttribute( 'borderColor' ) ).to.deep.equal( {
  3084. top: '#f00',
  3085. right: '#f00',
  3086. bottom: '#f00',
  3087. left: '#f00'
  3088. } );
  3089. expect( tableCell.getAttribute( 'borderStyle' ) ).to.deep.equal( {
  3090. top: 'solid',
  3091. right: 'solid',
  3092. bottom: 'solid',
  3093. left: 'solid'
  3094. } );
  3095. expect( tableCell.getAttribute( 'borderWidth' ) ).to.deep.equal( {
  3096. top: '1px',
  3097. right: '1px',
  3098. bottom: '1px',
  3099. left: '1px'
  3100. } );
  3101. expect( tableCell.getAttribute( 'backgroundColor' ) ).to.equal( '#ba7' );
  3102. expect( tableCell.getAttribute( 'width' ) ).to.equal( '1337px' );
  3103. } );
  3104. it( 'discards table properties', async () => {
  3105. await createEditor( [ TableCellPropertiesEditing ] );
  3106. setModelData( model, modelTable( [
  3107. [ '00', '01', '02' ],
  3108. [ '01', '11', '12' ],
  3109. [ '02', '21', '22' ]
  3110. ] ) );
  3111. tableSelection.setCellSelection(
  3112. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  3113. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  3114. );
  3115. const tableStyle = 'border:1px solid #f00;background:#ba7;width:1337px';
  3116. const pastedTable = `<table style="${ tableStyle }"><tr><td>aa</td><td>ab</td></tr><tr><td>ba</td><td>bb</td></tr></table>`;
  3117. const data = {
  3118. dataTransfer: createDataTransfer(),
  3119. preventDefault: sinon.spy(),
  3120. stopPropagation: sinon.spy()
  3121. };
  3122. data.dataTransfer.setData( 'text/html', pastedTable );
  3123. viewDocument.fire( 'paste', data );
  3124. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  3125. [ 'aa', 'ab', '02' ],
  3126. [ 'ba', 'bb', '12' ],
  3127. [ '02', '21', '22' ]
  3128. ] ) );
  3129. } );
  3130. } );
  3131. async function createEditor( extraPlugins = [] ) {
  3132. editor = await ClassicTestEditor.create( element, {
  3133. plugins: [ TableEditing, TableClipboard, Paragraph, Clipboard, ...extraPlugins ]
  3134. } );
  3135. model = editor.model;
  3136. modelRoot = model.document.getRoot();
  3137. viewDocument = editor.editing.view.document;
  3138. tableSelection = editor.plugins.get( 'TableSelection' );
  3139. }
  3140. function pasteTable( tableData ) {
  3141. const data = {
  3142. dataTransfer: createDataTransfer(),
  3143. preventDefault: sinon.spy(),
  3144. stopPropagation: sinon.spy()
  3145. };
  3146. data.dataTransfer.setData( 'text/html', viewTable( tableData ) );
  3147. viewDocument.fire( 'paste', data );
  3148. return data;
  3149. }
  3150. function createDataTransfer() {
  3151. const store = new Map();
  3152. return {
  3153. setData( type, data ) {
  3154. store.set( type, data );
  3155. },
  3156. getData( type ) {
  3157. return store.get( type );
  3158. }
  3159. };
  3160. }
  3161. function setupBatchWatch() {
  3162. const createdBatches = new Set();
  3163. model.on( 'applyOperation', ( evt, [ operation ] ) => {
  3164. if ( operation.isDocumentOperation ) {
  3165. createdBatches.add( operation.batch );
  3166. }
  3167. } );
  3168. return createdBatches;
  3169. }
  3170. } );