8
0

tableclipboard-paste.js 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971
  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.skip( '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. // TODO: fix needed...
  2572. describe.skip( 'content table has spans', () => {
  2573. beforeEach( () => {
  2574. // +----+----+----+----+----+----+
  2575. // | 00 | 01 | 02 | 03 | 04 | 05 |
  2576. // +----+----+----+ +----+----+
  2577. // | 10 | 11 | 12 | | 14 | 15 |
  2578. // +----+----+ + +----+----+
  2579. // | 20 | 21 | | | 24 | 25 |
  2580. // +----+----+----+ + +----+
  2581. // | 30 | 31 | | | 35 |
  2582. // +----+----+----+----+----+----+
  2583. // | 40 | 44 |
  2584. // +----+----+----+----+ +
  2585. // | 50 | 51 | 52 | |
  2586. // +----+----+----+----+----+----+
  2587. // | 60 | 61 | 62 | 63 | 64 | 65 |
  2588. // +----+----+----+----+----+----+
  2589. setModelData( model, modelTable( [
  2590. [ '00', '01', '02', { contents: '03', rowspan: 4 }, '04', '05' ],
  2591. [ '10', '11', { contents: '12', rowspan: 2 }, '14', '15' ],
  2592. [ '20', '21', { contents: '24', rowspan: 2 }, '25' ],
  2593. [ '30', { contents: '31', colspan: 2 }, '35' ],
  2594. [ { contents: '40', colspan: 4 }, { contents: '44', colspan: 2, rowspan: 2 } ],
  2595. [ '50', '51', { contents: '52', colspan: 2 } ],
  2596. [ '60', '61', '62', '63', '64', '65' ]
  2597. ] ) );
  2598. } );
  2599. it.skip( 'should split spanned cells on the selection edges (vertical spans)', () => {
  2600. tableSelection.setCellSelection(
  2601. modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  2602. modelRoot.getNodeByPath( [ 0, 4, 1 ] ) // Cell 44.
  2603. );
  2604. pasteTable( [
  2605. [ 'aa', 'ab' ],
  2606. [ 'ba', 'bb' ]
  2607. ] );
  2608. // +----+----+----+----+----+----+
  2609. // | 00 | 01 | 02 | 03 | 04 | 05 |
  2610. // +----+----+----+ +----+----+
  2611. // | 10 | 11 | 12 | | 14 | 15 |
  2612. // +----+----+----+----+----+----+
  2613. // | aa | ab | aa | ab | aa | 25 |
  2614. // +----+----+----+----+----+----+
  2615. // | ba | bb | ba | bb | ba | 35 |
  2616. // +----+----+----+----+----+----+
  2617. // | aa | ab | aa | ab | aa | |
  2618. // +----+----+----+----+----+ +
  2619. // | 50 | 51 | 52 | | |
  2620. // +----+----+----+----+----+----+
  2621. // | 60 | 61 | 62 | 63 | 64 | 65 |
  2622. // +----+----+----+----+----+----+
  2623. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2624. [ '00', '01', '02', { contents: '03', rowspan: 2 }, '04', '05' ],
  2625. [ '10', '11', '12', '14', '15' ],
  2626. [ 'aa', 'ab', 'aa', 'ab', 'aa', '25' ],
  2627. [ 'ba', 'bb', 'ba', 'bb', 'ba', '35' ],
  2628. [ 'aa', 'ab', 'aa', 'ab', 'aa', { contents: '', rowspan: 2 } ],
  2629. [ '50', '51', { contents: '52', colspan: 2 }, '' ],
  2630. [ '60', '61', '62', '63', '64', '65' ]
  2631. ] ) );
  2632. } );
  2633. it.skip( 'should split spanned cells on the selection edges (horizontal spans)', () => {
  2634. tableSelection.setCellSelection(
  2635. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  2636. modelRoot.getNodeByPath( [ 0, 4, 1 ] ) // Cell 44.
  2637. );
  2638. pasteTable( [
  2639. [ 'aa', 'ab' ],
  2640. [ 'ba', 'bb' ]
  2641. ] );
  2642. // +----+----+----+----+----+----+
  2643. // | 00 | 01 | aa | ab | aa | 05 |
  2644. // +----+----+----+----+----+----+
  2645. // | 10 | 11 | ba | bb | ba | 15 |
  2646. // +----+----+----+----+----+----+
  2647. // | 20 | 21 | aa | ab | aa | 25 |
  2648. // +----+----+----+----+----+----+
  2649. // | 30 | 31 | ba | bb | ba | 35 |
  2650. // +----+----+----+----+----+----+
  2651. // | 40 | aa | ab | aa | |
  2652. // +----+----+----+----+----+ +
  2653. // | 50 | 51 | 52 | | |
  2654. // +----+----+----+----+----+----+
  2655. // | 60 | 61 | 62 | 63 | 64 | 65 |
  2656. // +----+----+----+----+----+----+
  2657. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2658. [ '00', '01', 'aa', 'ab', 'aa', '05' ],
  2659. [ '10', '11', 'ba', 'bb', 'ba', '15' ],
  2660. [ '20', '21', 'aa', 'ab', 'aa', '25' ],
  2661. [ '30', '31', 'ba', 'bb', 'ba', '35' ],
  2662. [ { contents: '40', colspan: 2 }, 'aa', 'ab', 'aa', { contents: '', rowspan: 2 } ],
  2663. [ '50', '51', { contents: '52', colspan: 2 }, '' ],
  2664. [ '60', '61', '62', '63', '64', '65' ]
  2665. ] ) );
  2666. } );
  2667. } );
  2668. describe( 'pasted table has spans', () => {
  2669. beforeEach( () => {
  2670. setModelData( model, modelTable( [
  2671. [ '00', '01', '02', '03', '04', '05', '06' ],
  2672. [ '10', '11', '12', '13', '14', '15', '16' ],
  2673. [ '20', '21', '22', '23', '24', '25', '26' ],
  2674. [ '30', '31', '32', '33', '34', '35', '36' ],
  2675. [ '40', '41', '42', '43', '44', '45', '46' ],
  2676. [ '50', '51', '52', '53', '54', '55', '56' ],
  2677. [ '60', '61', '62', '63', '64', '65', '66' ],
  2678. [ '70', '71', '72', '73', '74', '75', '76' ],
  2679. [ '80', '81', '82', '83', '84', '85', '86' ]
  2680. ] ) );
  2681. } );
  2682. it( 'should trim overlapping cells', () => {
  2683. tableSelection.setCellSelection(
  2684. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  2685. modelRoot.getNodeByPath( [ 0, 8, 6 ] )
  2686. );
  2687. // +----+----+----+----+
  2688. // | aa |
  2689. // +----+----+----+----+
  2690. // | ba | bb |
  2691. // + +----+----+----+
  2692. // | | cb | cc |
  2693. // + + +----+----+
  2694. // | | | dc | dd |
  2695. // + + + +----+
  2696. // | | | | ed |
  2697. // +----+----+----+----+
  2698. pasteTable( [
  2699. [ { contents: 'aa', colspan: 4 } ],
  2700. [ { contents: 'ba', rowspan: 4 }, { contents: 'bb', colspan: 3 } ],
  2701. [ { contents: 'cb', rowspan: 3 }, { contents: 'cc', colspan: 2 } ],
  2702. [ { contents: 'dc', rowspan: 2 }, 'dd' ],
  2703. [ 'ed' ]
  2704. ] );
  2705. // +----+----+----+----+----+----+----+
  2706. // | 00 | 01 | 02 | 03 | 04 | 05 | 06 |
  2707. // +----+----+----+----+----+----+----+
  2708. // | 10 | aa | aa |
  2709. // +----+----+----+----+----+----+----+
  2710. // | 20 | ba | bb | ba | bb |
  2711. // +----+ +----+----+----+ +----+
  2712. // | 30 | | cb | cc | | cb |
  2713. // +----+ + +----+----+ + +
  2714. // | 40 | | | dc | dd | | |
  2715. // +----+ + + +----+ + +
  2716. // | 50 | | | | ed | | |
  2717. // +----+----+----+----+----+----+----+
  2718. // | 60 | aa | aa |
  2719. // +----+----+----+----+----+----+----+
  2720. // | 70 | ba | bb | ba | bb |
  2721. // +----+ +----+----+----+ +----+
  2722. // | 80 | | cb | cc | | cb |
  2723. // +----+----+----+----+----+----+----+
  2724. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2725. [ '00', '01', '02', '03', '04', '05', '06' ],
  2726. [ '10', { contents: 'aa', colspan: 4 }, { contents: 'aa', colspan: 2 } ],
  2727. [ '20', { contents: 'ba', rowspan: 4 }, { contents: 'bb', colspan: 3 }, { contents: 'ba', rowspan: 4 }, 'bb' ],
  2728. [ '30', { contents: 'cb', rowspan: 3 }, { contents: 'cc', colspan: 2 }, { contents: 'cb', rowspan: 3 } ],
  2729. [ '40', { contents: 'dc', rowspan: 2 }, 'dd' ],
  2730. [ '50', 'ed' ],
  2731. [ '60', { contents: 'aa', colspan: 4 }, { contents: 'aa', colspan: 2 } ],
  2732. [ '70', { contents: 'ba', rowspan: 2 }, { contents: 'bb', colspan: 3 }, { contents: 'ba', rowspan: 2 }, 'bb' ],
  2733. [ '80', 'cb', { contents: 'cc', colspan: 2 }, 'cb' ]
  2734. ] ) );
  2735. } );
  2736. } );
  2737. } );
  2738. describe( 'fixing pasted table broken layout', () => {
  2739. it( 'should trim pasted cells\' width if they exceeds table width established by the first row', () => {
  2740. // Select 00 -> 22 (selection 2x3 - equal to expected fixed table)
  2741. tableSelection.setCellSelection(
  2742. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2743. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  2744. );
  2745. // +----+----+
  2746. // | aa | ab | <- First row establish table width=2.
  2747. // +----+----+----+----+
  2748. // | ba | bb | <- Cell "bb" sticks out by 2 slots.
  2749. // +----+----+----+----+
  2750. // | ca | <- Cell "ca" sticks out by 1 slot.
  2751. // +----+----+----+
  2752. pasteTable( [
  2753. [ 'aa', 'ab' ],
  2754. [ 'ba', { colspan: 3, contents: 'bb' } ],
  2755. [ { colspan: 3, contents: 'ca' } ]
  2756. ] );
  2757. // +----+----+----+----+
  2758. // | aa | ab | 02 | 03 |
  2759. // +----+----+----+----+
  2760. // | ba | bb | 12 | 13 |
  2761. // +----+----+----+----+
  2762. // | ca | 22 | 23 |
  2763. // +----+----+----+----+
  2764. // | 30 | 31 | 32 | 33 |
  2765. // +----+----+----+----+
  2766. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2767. [ 'aa', 'ab', '02', '03' ],
  2768. [ 'ba', 'bb', '12', '13' ],
  2769. [ { contents: 'ca', colspan: 2 }, '22', '23' ],
  2770. [ '30', '31', '32', '33' ]
  2771. ] ) );
  2772. /* eslint-disable no-multi-spaces */
  2773. assertSelectedCells( model, [
  2774. [ 1, 1, 0, 0 ],
  2775. [ 1, 1, 0, 0 ],
  2776. [ 1, 0, 0 ],
  2777. [ 0, 0, 0, 0 ]
  2778. ] );
  2779. /* eslint-enable no-multi-spaces */
  2780. } );
  2781. it( 'should trim pasted cells\' height if they exceeds table height established by the last row', () => {
  2782. // Select 00 -> 12 (selection 3x2 - equal to expected fixed table)
  2783. tableSelection.setCellSelection(
  2784. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2785. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  2786. );
  2787. // +----+----+----+
  2788. // | aa | ab | ac |
  2789. // +----+----+ +
  2790. // | ba | bb | | <- Last row establish table height=2.
  2791. // +----+ + +
  2792. // | | | <- Cell "ac" sticks out by 1 slot.
  2793. // + +----+
  2794. // | | <- Cell "bb" sticks out by 2 slots.
  2795. // +----+
  2796. pasteTable( [
  2797. [ 'aa', 'ab', { contents: 'ac', rowspan: 3 } ],
  2798. [ 'ba', { contents: 'bb', rowspan: 3 } ]
  2799. ] );
  2800. // +----+----+----+----+
  2801. // | aa | ab | ac | 03 |
  2802. // +----+----+ +----+
  2803. // | ba | bb | | 13 |
  2804. // +----+----+----+----+
  2805. // | 20 | 21 | 22 | 23 |
  2806. // +----+----+----+----+
  2807. // | 30 | 31 | 32 | 33 |
  2808. // +----+----+----+----+
  2809. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2810. [ 'aa', 'ab', { rowspan: 2, contents: 'ac' }, '03' ],
  2811. [ 'ba', 'bb', '13' ],
  2812. [ '20', '21', '22', '23' ],
  2813. [ '30', '31', '32', '33' ]
  2814. ] ) );
  2815. /* eslint-disable no-multi-spaces */
  2816. assertSelectedCells( model, [
  2817. [ 1, 1, 1, 0 ],
  2818. [ 1, 1, 0 ],
  2819. [ 0, 0, 0, 0 ],
  2820. [ 0, 0, 0, 0 ]
  2821. ] );
  2822. /* eslint-enable no-multi-spaces */
  2823. } );
  2824. it( 'should trim pasted cells\' height and width if they exceeds table height and width', () => {
  2825. // Select 00 -> 11 (selection 2x2 - equal to expected fixed table)
  2826. tableSelection.setCellSelection(
  2827. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2828. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  2829. );
  2830. // +----+----+
  2831. // | aa | ab |
  2832. // +----+----+----+----+
  2833. // | ba | bb | <- Cell "bb" sticks out by 2 slots in width and by 1 slot in height.
  2834. // +----+ +
  2835. // | |
  2836. // +----+----+----+
  2837. pasteTable( [
  2838. [ 'aa', 'ab' ],
  2839. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 } ]
  2840. ] );
  2841. // +----+----+----+----+
  2842. // | aa | ab | 02 | 03 |
  2843. // +----+----+----+----+
  2844. // | ba | bb | 12 | 13 |
  2845. // +----+----+----+----+
  2846. // | 20 | 21 | 22 | 23 |
  2847. // +----+----+----+----+
  2848. // | 30 | 31 | 32 | 33 |
  2849. // +----+----+----+----+
  2850. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2851. [ 'aa', 'ab', '02', '03' ],
  2852. [ 'ba', 'bb', '12', '13' ],
  2853. [ '20', '21', '22', '23' ],
  2854. [ '30', '31', '32', '33' ]
  2855. ] ) );
  2856. /* eslint-disable no-multi-spaces */
  2857. assertSelectedCells( model, [
  2858. [ 1, 1, 0, 0 ],
  2859. [ 1, 1, 0, 0 ],
  2860. [ 0, 0, 0, 0 ],
  2861. [ 0, 0, 0, 0 ]
  2862. ] );
  2863. /* eslint-enable no-multi-spaces */
  2864. } );
  2865. it(
  2866. 'should trim pasted cells\' width if they exceeds pasted table width (pasted height is bigger then selection height)',
  2867. () => {
  2868. // Select 00 -> 11 (selection 2x2 - smaller by height than the expected fixed table)
  2869. tableSelection.setCellSelection(
  2870. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2871. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  2872. );
  2873. // +----+----+
  2874. // | aa | ab | <- First row establish table width=2.
  2875. // +----+----+----+----+
  2876. // | ba | bb | <- Cell "bb" sticks out by 2 slots.
  2877. // +----+----+----+----+
  2878. // | ca | <- Cell "ca" sticks out by 1 slot.
  2879. // +----+----+----+
  2880. pasteTable( [
  2881. [ 'aa', 'ab' ],
  2882. [ 'ba', { colspan: 3, contents: 'bb' } ],
  2883. [ { colspan: 3, contents: 'ca' } ]
  2884. ] );
  2885. // +----+----+----+----+
  2886. // | aa | ab | 02 | 03 |
  2887. // +----+----+----+----+
  2888. // | ba | bb | 12 | 13 |
  2889. // +----+----+----+----+
  2890. // | 20 | 21 | 22 | 23 |
  2891. // +----+----+----+----+
  2892. // | 30 | 31 | 32 | 33 |
  2893. // +----+----+----+----+
  2894. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2895. [ 'aa', 'ab', '02', '03' ],
  2896. [ 'ba', 'bb', '12', '13' ],
  2897. [ '20', '21', '22', '23' ],
  2898. [ '30', '31', '32', '33' ]
  2899. ] ) );
  2900. assertSelectedCells( model, [
  2901. [ 1, 1, 0, 0 ],
  2902. [ 1, 1, 0, 0 ],
  2903. [ 0, 0, 0, 0 ],
  2904. [ 0, 0, 0, 0 ]
  2905. ] );
  2906. }
  2907. );
  2908. it(
  2909. 'should trim pasted cells\' height if they exceeds pasted table height (pasted width is bigger then selection width)',
  2910. () => {
  2911. // Select 00 -> 11 (selection 2x2 - smaller by width than the expected fixed table)
  2912. tableSelection.setCellSelection(
  2913. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2914. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  2915. );
  2916. // +----+----+----+
  2917. // | aa | ab | ac |
  2918. // +----+----+ +
  2919. // | ba | bb | | <- Last row establish table height=2.
  2920. // +----+ + +
  2921. // | | | <- Cell "ac" sticks out by 1 slot.
  2922. // + +----+
  2923. // | | <- Cell "bb" sticks out by 2 slots.
  2924. // +----+
  2925. pasteTable( [
  2926. [ 'aa', 'ab', { contents: 'ac', rowspan: 3 } ],
  2927. [ 'ba', { contents: 'bb', rowspan: 3 } ]
  2928. ] );
  2929. // +----+----+----+----+
  2930. // | aa | ab | 02 | 03 |
  2931. // +----+----+----+----+
  2932. // | ba | bb | 12 | 13 |
  2933. // +----+----+----+----+
  2934. // | 20 | 21 | 22 | 23 |
  2935. // +----+----+----+----+
  2936. // | 30 | 31 | 32 | 33 |
  2937. // +----+----+----+----+
  2938. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2939. [ 'aa', 'ab', '02', '03' ],
  2940. [ 'ba', 'bb', '12', '13' ],
  2941. [ '20', '21', '22', '23' ],
  2942. [ '30', '31', '32', '33' ]
  2943. ] ) );
  2944. /* eslint-disable no-multi-spaces */
  2945. assertSelectedCells( model, [
  2946. [ 1, 1, 0, 0 ],
  2947. [ 1, 1, 0, 0 ],
  2948. [ 0, 0, 0, 0 ],
  2949. [ 0, 0, 0, 0 ]
  2950. ] );
  2951. /* eslint-enable no-multi-spaces */
  2952. }
  2953. );
  2954. it(
  2955. `should trim pasted pasted cells' height and width if they exceeds pasted table dimensions
  2956. (pasted table is bigger then selection width)`,
  2957. () => {
  2958. // Select 00 -> 11 (selection 2x2 - smaller than the expected fixed table)
  2959. tableSelection.setCellSelection(
  2960. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2961. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  2962. );
  2963. // +----+----+----+
  2964. // | aa | ab | ac |
  2965. // +----+----+----+----+
  2966. // | ba | bb | <- Cell "bb" sticks out by 1 slots in width and by 1 slot in height.
  2967. // +----+ +
  2968. // | ca | |
  2969. // +----+ +
  2970. // | |
  2971. // +----+----+----+
  2972. pasteTable( [
  2973. [ 'aa', 'ab' ],
  2974. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 } ]
  2975. ] );
  2976. // +----+----+----+----+
  2977. // | aa | ab | 02 | 03 |
  2978. // +----+----+----+----+
  2979. // | ba | bb | 12 | 13 |
  2980. // +----+----+----+----+
  2981. // | 20 | 21 | 22 | 23 |
  2982. // +----+----+----+----+
  2983. // | 30 | 31 | 32 | 33 |
  2984. // +----+----+----+----+
  2985. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2986. [ 'aa', 'ab', '02', '03' ],
  2987. [ 'ba', 'bb', '12', '13' ],
  2988. [ '20', '21', '22', '23' ],
  2989. [ '30', '31', '32', '33' ]
  2990. ] ) );
  2991. /* eslint-disable no-multi-spaces */
  2992. assertSelectedCells( model, [
  2993. [ 1, 1, 0, 0 ],
  2994. [ 1, 1, 0, 0 ],
  2995. [ 0, 0, 0, 0 ],
  2996. [ 0, 0, 0, 0 ]
  2997. ] );
  2998. /* eslint-enable no-multi-spaces */
  2999. }
  3000. );
  3001. } );
  3002. describe( 'headings overlapping selected area', () => {
  3003. beforeEach( () => {
  3004. setModelData( model, modelTable( [
  3005. [ '00', '01', '02', '03', '04', '05' ],
  3006. [ '10', '11', '12', '13', '14', '15' ],
  3007. [ '20', '21', '22', '23', '24', '25' ],
  3008. [ '30', '31', '32', '33', '34', '35' ],
  3009. [ '40', '41', '42', '43', '44', '45' ],
  3010. [ '50', '51', '52', '53', '54', '55' ]
  3011. ], { headingRows: 3, headingColumns: 3 } ) );
  3012. } );
  3013. it( 'should not split cells if they are not overlapping from headings', () => {
  3014. tableSelection.setCellSelection(
  3015. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  3016. modelRoot.getNodeByPath( [ 0, 0, 0 ] )
  3017. );
  3018. // +----+----+----+----+
  3019. // | aa | ad |
  3020. // + +----+
  3021. // | | bd |
  3022. // + +----+
  3023. // | | cd |
  3024. // +----+----+----+----+
  3025. // | da | db | dc | dd |
  3026. // +----+----+----+----+
  3027. pasteTable( [
  3028. [ { contents: 'aa', colspan: 3, rowspan: 3 }, 'ad' ],
  3029. [ 'bd' ],
  3030. [ 'cd' ],
  3031. [ 'da', 'db', 'dc', 'dd' ]
  3032. ] );
  3033. // +----+----+----+----+----+----+
  3034. // | aa | ad | 04 | 05 |
  3035. // + +----+----+----+
  3036. // | | bd | 14 | 15 |
  3037. // + +----+----+----+
  3038. // | | cd | 24 | 25 |
  3039. // +----+----+----+----+----+----+ <-- heading rows
  3040. // | da | db | dc | dd | 34 | 35 |
  3041. // +----+----+----+----+----+----+
  3042. // | 40 | 41 | 42 | 43 | 44 | 45 |
  3043. // +----+----+----+----+----+----+
  3044. // | 50 | 51 | 52 | 53 | 54 | 55 |
  3045. // +----+----+----+----+----+----+
  3046. // ^-- heading columns
  3047. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  3048. [ { contents: 'aa', colspan: 3, rowspan: 3 }, 'ad', '04', '05' ],
  3049. [ 'bd', '14', '15' ],
  3050. [ 'cd', '24', '25' ],
  3051. [ 'da', 'db', 'dc', 'dd', '34', '35' ],
  3052. [ '40', '41', '42', '43', '44', '45' ],
  3053. [ '50', '51', '52', '53', '54', '55' ]
  3054. ], { headingRows: 3, headingColumns: 3 } ) );
  3055. assertSelectionRangesSorted();
  3056. /* eslint-disable no-multi-spaces */
  3057. assertSelectedCells( model, [
  3058. [ 1, 1, 0, 0 ],
  3059. [ 1, 0, 0 ],
  3060. [ 1, 0, 0 ],
  3061. [ 1, 1, 1, 1, 0, 0 ],
  3062. [ 0, 0, 0, 0, 0, 0 ],
  3063. [ 0, 0, 0, 0, 0, 0 ]
  3064. ] );
  3065. /* eslint-enable no-multi-spaces */
  3066. } );
  3067. it( 'should split cells that overlap from headings', () => {
  3068. tableSelection.setCellSelection(
  3069. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  3070. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  3071. );
  3072. // +----+----+----+----+
  3073. // | aa | ad |
  3074. // + +----+
  3075. // | | bd |
  3076. // + +----+
  3077. // | | cd |
  3078. // +----+----+----+----+
  3079. // | da | db | dc | dd |
  3080. // +----+----+----+----+
  3081. pasteTable( [
  3082. [ { contents: 'aa', colspan: 3, rowspan: 3 }, 'ad' ],
  3083. [ 'bd' ],
  3084. [ 'cd' ],
  3085. [ 'da', 'db', 'dc', 'dd' ]
  3086. ] );
  3087. // +----+----+----+----+----+----+
  3088. // | 00 | 01 | 02 | 03 | 04 | 05 |
  3089. // +----+----+----+----+----+----+
  3090. // | 10 | aa | | ad | 15 |
  3091. // +----+ + +----+----+
  3092. // | 20 | | | bd | 25 |
  3093. // +----+----+----+----+----+----+ <-- heading rows
  3094. // | 30 | | | cd | 35 |
  3095. // +----+----+----+----+----+----+
  3096. // | 40 | da | db | dc | dd | 45 |
  3097. // +----+----+----+----+----+----+
  3098. // | 50 | 51 | 52 | 53 | 54 | 55 |
  3099. // +----+----+----+----+----+----+
  3100. // ^-- heading columns
  3101. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  3102. [ '00', '01', '02', '03', '04', '05' ],
  3103. [ '10', { contents: 'aa', colspan: 2, rowspan: 2 }, { contents: '', rowspan: 2 }, 'ad', '15' ],
  3104. [ '20', 'bd', '25' ],
  3105. [ '30', { contents: '', colspan: 2 }, '', 'cd', '35' ],
  3106. [ '40', 'da', 'db', 'dc', 'dd', '45' ],
  3107. [ '50', '51', '52', '53', '54', '55' ]
  3108. ], { headingRows: 3, headingColumns: 3 } ) );
  3109. assertSelectionRangesSorted();
  3110. /* eslint-disable no-multi-spaces */
  3111. assertSelectedCells( model, [
  3112. [ 0, 0, 0, 0, 0, 0 ],
  3113. [ 0, 1, 1, 1, 0 ],
  3114. [ 0, 1, 0 ],
  3115. [ 0, 1, 1, 1, 0 ],
  3116. [ 0, 1, 1, 1, 1, 0 ],
  3117. [ 0, 0, 0, 0, 0, 0 ]
  3118. ] );
  3119. /* eslint-enable no-multi-spaces */
  3120. } );
  3121. it( 'should split cells that overlap from heading rows', () => {
  3122. tableSelection.setCellSelection(
  3123. modelRoot.getNodeByPath( [ 0, 2, 3 ] ),
  3124. modelRoot.getNodeByPath( [ 0, 2, 3 ] )
  3125. );
  3126. // +----+----+----+----+
  3127. // | aa | ad |
  3128. // + +----+
  3129. // | | bd |
  3130. // + +----+
  3131. // | | cd |
  3132. // +----+----+----+----+
  3133. // | da | db | dc | dd |
  3134. // +----+----+----+----+
  3135. pasteTable( [
  3136. [ { contents: 'aa', colspan: 3, rowspan: 3 }, 'ad' ],
  3137. [ 'bd' ],
  3138. [ 'cd' ],
  3139. [ 'da', 'db', 'dc', 'dd' ]
  3140. ] );
  3141. // +----+----+----+----+----+----+----+
  3142. // | 00 | 01 | 02 | 03 | 04 | 05 | |
  3143. // +----+----+----+----+----+----+----+
  3144. // | 10 | 11 | 12 | 13 | 14 | 15 | |
  3145. // +----+----+----+----+----+----+----+
  3146. // | 20 | 21 | 22 | aa | ad |
  3147. // +----+----+----+----+----+----+----+ <-- heading rows
  3148. // | 30 | 31 | 32 | | bd |
  3149. // +----+----+----+ +----+
  3150. // | 40 | 41 | 42 | | cd |
  3151. // +----+----+----+----+----+----+----+
  3152. // | 50 | 51 | 52 | da | db | dc | dd |
  3153. // +----+----+----+----+----+----+----+
  3154. // ^-- heading columns
  3155. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  3156. [ '00', '01', '02', '03', '04', '05', '' ],
  3157. [ '10', '11', '12', '13', '14', '15', '' ],
  3158. [ '20', '21', '22', { contents: 'aa', colspan: 3 }, 'ad' ],
  3159. [ '30', '31', '32', { contents: '', colspan: 3, rowspan: 2 }, 'bd' ],
  3160. [ '40', '41', '42', 'cd' ],
  3161. [ '50', '51', '52', 'da', 'db', 'dc', 'dd' ]
  3162. ], { headingRows: 3, headingColumns: 3 } ) );
  3163. assertSelectionRangesSorted();
  3164. /* eslint-disable no-multi-spaces */
  3165. assertSelectedCells( model, [
  3166. [ 0, 0, 0, 0, 0, 0, 0 ],
  3167. [ 0, 0, 0, 0, 0, 0, 0 ],
  3168. [ 0, 0, 0, 1, 1 ],
  3169. [ 0, 0, 0, 1, 1 ],
  3170. [ 0, 0, 0, 1 ],
  3171. [ 0, 0, 0, 1, 1, 1, 1 ]
  3172. ] );
  3173. /* eslint-enable no-multi-spaces */
  3174. } );
  3175. it( 'should split cells that overlap from heading columns', () => {
  3176. tableSelection.setCellSelection(
  3177. modelRoot.getNodeByPath( [ 0, 3, 2 ] ),
  3178. modelRoot.getNodeByPath( [ 0, 3, 2 ] )
  3179. );
  3180. // +----+----+----+----+
  3181. // | aa | ad |
  3182. // + +----+
  3183. // | | bd |
  3184. // + +----+
  3185. // | | cd |
  3186. // +----+----+----+----+
  3187. // | da | db | dc | dd |
  3188. // +----+----+----+----+
  3189. pasteTable( [
  3190. [ { contents: 'aa', colspan: 3, rowspan: 3 }, 'ad' ],
  3191. [ 'bd' ],
  3192. [ 'cd' ],
  3193. [ 'da', 'db', 'dc', 'dd' ]
  3194. ] );
  3195. // +----+----+----+----+----+----+
  3196. // | 00 | 01 | 02 | 03 | 04 | 05 |
  3197. // +----+----+----+----+----+----+
  3198. // | 10 | 11 | 12 | 13 | 14 | 15 |
  3199. // +----+----+----+----+----+----+
  3200. // | 20 | 21 | 22 | 23 | 24 | 25 |
  3201. // +----+----+----+----+----+----+ <-- heading rows
  3202. // | 30 | 31 | aa | | ad |
  3203. // +----+----+ + +----+
  3204. // | 40 | 41 | | | bd |
  3205. // +----+----+ + +----+
  3206. // | 50 | 51 | | | cd |
  3207. // +----+----+----+----+----+----+
  3208. // | | | da | db | dc | dd |
  3209. // +----+----+----+----+----+----+
  3210. // ^-- heading columns
  3211. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  3212. [ '00', '01', '02', '03', '04', '05' ],
  3213. [ '10', '11', '12', '13', '14', '15' ],
  3214. [ '20', '21', '22', '23', '24', '25' ],
  3215. [ '30', '31', { contents: 'aa', rowspan: 3 }, { contents: '', colspan: 2, rowspan: 3 }, 'ad' ],
  3216. [ '40', '41', 'bd' ],
  3217. [ '50', '51', 'cd' ],
  3218. [ '', '', 'da', 'db', 'dc', 'dd' ]
  3219. ], { headingRows: 3, headingColumns: 3 } ) );
  3220. assertSelectionRangesSorted();
  3221. /* eslint-disable no-multi-spaces */
  3222. assertSelectedCells( model, [
  3223. [ 0, 0, 0, 0, 0, 0 ],
  3224. [ 0, 0, 0, 0, 0, 0 ],
  3225. [ 0, 0, 0, 0, 0, 0 ],
  3226. [ 0, 0, 1, 1, 1 ],
  3227. [ 0, 0, 1 ],
  3228. [ 0, 0, 1 ],
  3229. [ 0, 0, 1, 1, 1, 1 ]
  3230. ] );
  3231. /* eslint-enable no-multi-spaces */
  3232. } );
  3233. it( 'should split cells that overlap from headings (repeated pasted table)', () => {
  3234. setModelData( model, modelTable( [
  3235. [ '00', '01', '02', '03', '04' ],
  3236. [ '10', '11', '12', '13', '14' ],
  3237. [ '20', '21', '22', '23', '24' ],
  3238. [ '30', '31', '32', '33', '34' ],
  3239. [ '40', '41', '42', '43', '44' ]
  3240. ], { headingRows: 1, headingColumns: 1 } ) );
  3241. tableSelection.setCellSelection(
  3242. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  3243. modelRoot.getNodeByPath( [ 0, 4, 4 ] )
  3244. );
  3245. // +----+----+----+
  3246. // | aa | ac |
  3247. // + +----+
  3248. // | | bc |
  3249. // +----+----+----+
  3250. // | ca | cb | cc |
  3251. // +----+----+----+
  3252. pasteTable( [
  3253. [ { contents: 'aa', colspan: 2, rowspan: 2 }, 'ac' ],
  3254. [ 'bc' ],
  3255. [ 'ca', 'cb', 'cc' ]
  3256. ] );
  3257. // +----+----+----+----+----+
  3258. // | aa | | ac | aa |
  3259. // +----+----+----+----+----+ <-- heading rows
  3260. // | | | bc | |
  3261. // +----+----+----+----+----+
  3262. // | ca | cb | cc | ca | cb |
  3263. // +----+----+----+----+----+
  3264. // | aa | | ac | aa |
  3265. // + + +----+ +
  3266. // | | | bc | |
  3267. // +----+----+----+----+----+
  3268. // ^-- heading columns
  3269. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  3270. [ 'aa', '', 'ac', { contents: 'aa', colspan: 2 } ],
  3271. [ '', '', 'bc', { contents: '', colspan: 2 } ],
  3272. [ 'ca', 'cb', 'cc', 'ca', 'cb' ],
  3273. [ { contents: 'aa', rowspan: 2 }, { contents: '', rowspan: 2 }, 'ac', { contents: 'aa', colspan: 2, rowspan: 2 } ],
  3274. [ 'bc' ]
  3275. ], { headingRows: 1, headingColumns: 1 } ) );
  3276. assertSelectionRangesSorted();
  3277. /* eslint-disable no-multi-spaces */
  3278. assertSelectedCells( model, [
  3279. [ 1, 1, 1, 1 ],
  3280. [ 1, 1, 1, 1 ],
  3281. [ 1, 1, 1, 1, 1 ],
  3282. [ 1, 1, 1, 1 ],
  3283. [ 1 ]
  3284. ] );
  3285. /* eslint-enable no-multi-spaces */
  3286. } );
  3287. function assertSelectionRangesSorted() {
  3288. const selectionRanges = Array.from( model.document.selection.getRanges() );
  3289. const selectionRangesSorted = selectionRanges.slice().sort( ( a, b ) => a.start.isBefore( b.start ) ? -1 : 1 );
  3290. const selectionPaths = selectionRanges.map( ( { start } ) => start.path );
  3291. const sortedPaths = selectionRangesSorted.map( ( { start } ) => start.path );
  3292. expect( selectionPaths ).to.deep.equal( sortedPaths );
  3293. }
  3294. } );
  3295. } );
  3296. describe( 'Clipboard integration - paste (content scenarios)', () => {
  3297. it( 'handles multiple paragraphs in table cell', async () => {
  3298. await createEditor();
  3299. setModelData( model, modelTable( [
  3300. [ '00', '01', '02' ],
  3301. [ '01', '11', '12' ],
  3302. [ '02', '21', '22' ]
  3303. ] ) );
  3304. tableSelection.setCellSelection(
  3305. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  3306. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  3307. );
  3308. pasteTable( [
  3309. [ '<p>a</p><p>a</p><p>a</p>', 'ab' ],
  3310. [ 'ba', 'bb' ]
  3311. ] );
  3312. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  3313. [ '<paragraph>a</paragraph><paragraph>a</paragraph><paragraph>a</paragraph>', 'ab', '02' ],
  3314. [ 'ba', 'bb', '12' ],
  3315. [ '02', '21', '22' ]
  3316. ] ) );
  3317. } );
  3318. it( 'handles image in table cell', async () => {
  3319. await createEditor( [ ImageEditing, ImageCaptionEditing ] );
  3320. setModelData( model, modelTable( [
  3321. [ '00', '01', '02' ],
  3322. [ '01', '11', '12' ],
  3323. [ '02', '21', '22' ]
  3324. ] ) );
  3325. tableSelection.setCellSelection(
  3326. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  3327. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  3328. );
  3329. pasteTable( [
  3330. [ '<img src="/assets/sample.png">', 'ab' ],
  3331. [ 'ba', 'bb' ]
  3332. ] );
  3333. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  3334. [ '<image src="/assets/sample.png"><caption></caption></image>', 'ab', '02' ],
  3335. [ 'ba', 'bb', '12' ],
  3336. [ '02', '21', '22' ]
  3337. ] ) );
  3338. } );
  3339. it( 'handles mixed nested content in table cell', async () => {
  3340. await createEditor( [ ImageEditing, ImageCaptionEditing, BlockQuoteEditing, HorizontalLineEditing, ListEditing ] );
  3341. setModelData( model, modelTable( [
  3342. [ '00', '01', '02' ],
  3343. [ '01', '11', '12' ],
  3344. [ '02', '21', '22' ]
  3345. ] ) );
  3346. tableSelection.setCellSelection(
  3347. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  3348. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  3349. );
  3350. const img = '<img src="/assets/sample.png">';
  3351. const list = '<ul><li>foo</li><li>bar</li></ul>';
  3352. const blockquote = `<blockquote><p>baz</p>${ list }</blockquote>`;
  3353. pasteTable( [
  3354. [ `${ img }${ list }${ blockquote }`, 'ab' ],
  3355. [ 'ba', 'bb' ]
  3356. ] );
  3357. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  3358. [
  3359. '<image src="/assets/sample.png"><caption></caption></image>' +
  3360. '<listItem listIndent="0" listType="bulleted">foo</listItem>' +
  3361. '<listItem listIndent="0" listType="bulleted">bar</listItem>' +
  3362. '<blockQuote>' +
  3363. '<paragraph>baz</paragraph>' +
  3364. '<listItem listIndent="0" listType="bulleted">foo</listItem>' +
  3365. '<listItem listIndent="0" listType="bulleted">bar</listItem>' +
  3366. '</blockQuote>',
  3367. 'ab',
  3368. '02' ],
  3369. [ 'ba', 'bb', '12' ],
  3370. [ '02', '21', '22' ]
  3371. ] ) );
  3372. } );
  3373. it( 'handles table cell properties', async () => {
  3374. await createEditor( [ TableCellPropertiesEditing ] );
  3375. setModelData( model, modelTable( [
  3376. [ '00', '01', '02' ],
  3377. [ '01', '11', '12' ],
  3378. [ '02', '21', '22' ]
  3379. ] ) );
  3380. tableSelection.setCellSelection(
  3381. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  3382. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  3383. );
  3384. pasteTable( [
  3385. [ { contents: 'aa', style: 'border:1px solid #f00;background:#ba7;width:1337px' }, 'ab' ],
  3386. [ 'ba', 'bb' ]
  3387. ] );
  3388. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  3389. expect( tableCell.getAttribute( 'borderColor' ) ).to.deep.equal( {
  3390. top: '#f00',
  3391. right: '#f00',
  3392. bottom: '#f00',
  3393. left: '#f00'
  3394. } );
  3395. expect( tableCell.getAttribute( 'borderStyle' ) ).to.deep.equal( {
  3396. top: 'solid',
  3397. right: 'solid',
  3398. bottom: 'solid',
  3399. left: 'solid'
  3400. } );
  3401. expect( tableCell.getAttribute( 'borderWidth' ) ).to.deep.equal( {
  3402. top: '1px',
  3403. right: '1px',
  3404. bottom: '1px',
  3405. left: '1px'
  3406. } );
  3407. expect( tableCell.getAttribute( 'backgroundColor' ) ).to.equal( '#ba7' );
  3408. expect( tableCell.getAttribute( 'width' ) ).to.equal( '1337px' );
  3409. } );
  3410. it( 'discards table properties', async () => {
  3411. await createEditor( [ TableCellPropertiesEditing ] );
  3412. setModelData( model, modelTable( [
  3413. [ '00', '01', '02' ],
  3414. [ '01', '11', '12' ],
  3415. [ '02', '21', '22' ]
  3416. ] ) );
  3417. tableSelection.setCellSelection(
  3418. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  3419. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  3420. );
  3421. const tableStyle = 'border:1px solid #f00;background:#ba7;width:1337px';
  3422. const pastedTable = `<table style="${ tableStyle }"><tr><td>aa</td><td>ab</td></tr><tr><td>ba</td><td>bb</td></tr></table>`;
  3423. const data = {
  3424. dataTransfer: createDataTransfer(),
  3425. preventDefault: sinon.spy(),
  3426. stopPropagation: sinon.spy()
  3427. };
  3428. data.dataTransfer.setData( 'text/html', pastedTable );
  3429. viewDocument.fire( 'paste', data );
  3430. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  3431. [ 'aa', 'ab', '02' ],
  3432. [ 'ba', 'bb', '12' ],
  3433. [ '02', '21', '22' ]
  3434. ] ) );
  3435. } );
  3436. it( 'removes block fillers from empty cells (both td and th)', async () => {
  3437. await createEditor();
  3438. setModelData( model, modelTable( [
  3439. [ '00', '01', '02' ],
  3440. [ '01', '11', '12' ],
  3441. [ '02', '21', '22' ]
  3442. ] ) );
  3443. tableSelection.setCellSelection(
  3444. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  3445. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  3446. );
  3447. pasteTable( [
  3448. [ '&nbsp;', '&nbsp;' ],
  3449. [ '&nbsp;', '&nbsp;' ]
  3450. ], { headingRows: 1 } );
  3451. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  3452. [ '', '', '02' ],
  3453. [ '', '', '12' ],
  3454. [ '02', '21', '22' ]
  3455. ] ) );
  3456. } );
  3457. } );
  3458. async function createEditor( extraPlugins = [] ) {
  3459. editor = await ClassicTestEditor.create( element, {
  3460. plugins: [ TableEditing, TableClipboard, Paragraph, Clipboard, ...extraPlugins ]
  3461. } );
  3462. model = editor.model;
  3463. modelRoot = model.document.getRoot();
  3464. viewDocument = editor.editing.view.document;
  3465. tableSelection = editor.plugins.get( 'TableSelection' );
  3466. }
  3467. function pasteTable( tableData, attributes = {} ) {
  3468. const data = {
  3469. dataTransfer: createDataTransfer(),
  3470. preventDefault: sinon.spy(),
  3471. stopPropagation: sinon.spy()
  3472. };
  3473. data.dataTransfer.setData( 'text/html', viewTable( tableData, attributes ) );
  3474. viewDocument.fire( 'paste', data );
  3475. return data;
  3476. }
  3477. function createDataTransfer() {
  3478. const store = new Map();
  3479. return {
  3480. setData( type, data ) {
  3481. store.set( type, data );
  3482. },
  3483. getData( type ) {
  3484. return store.get( type );
  3485. }
  3486. };
  3487. }
  3488. function setupBatchWatch() {
  3489. const createdBatches = new Set();
  3490. model.on( 'applyOperation', ( evt, [ operation ] ) => {
  3491. if ( operation.isDocumentOperation ) {
  3492. createdBatches.add( operation.batch );
  3493. }
  3494. } );
  3495. return createdBatches;
  3496. }
  3497. } );