tableclipboard-paste.js 109 KB

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