tableclipboard-paste.js 108 KB

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