tableclipboard-paste.js 121 KB

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