tableclipboard-paste.js 77 KB

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