8
0

tableclipboard-paste.js 121 KB

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