8
0

tableclipboard-paste.js 121 KB

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