tableclipboard-paste.js 110 KB

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