tableclipboard-paste.js 127 KB

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