tableclipboard-paste.js 108 KB

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