tableclipboard-paste.js 93 KB

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