tableclipboard-paste.js 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354
  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 (last column in selection is spanned)', () => {
  774. // +----+----+----+----+
  775. // | 00 | 01 | 03 |
  776. // +----+----+----+----+
  777. // | 10 | 11 | 13 |
  778. // +----+ +----+
  779. // | 20 | | 23 |
  780. // +----+----+----+----+
  781. // | 30 | 31 | 32 | 33 |
  782. // +----+----+----+----+
  783. setModelData( model, modelTable( [
  784. [ '00', { contents: '01', colspan: 2 }, '03' ],
  785. [ '10', { contents: '11', colspan: 2, rowspan: 2 }, '13' ],
  786. [ '20', '23' ],
  787. [ '30', '31', '32', '33' ]
  788. ] ) );
  789. // Select 20 -> 01 (selection 3x3)
  790. tableSelection.setCellSelection(
  791. modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  792. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  793. );
  794. pasteTable( [
  795. [ 'aa', 'ab', 'ac' ],
  796. [ 'ba', 'bb', 'bc' ],
  797. [ 'ca', 'cb', 'cc' ]
  798. ] );
  799. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  800. [ 'aa', 'ab', 'ac', '03' ],
  801. [ 'ba', 'bb', 'bc', '13' ],
  802. [ 'ca', 'cb', 'cc', '23' ],
  803. [ '30', '31', '32', '33' ]
  804. ] ) );
  805. assertSelectedCells( model, [
  806. [ 1, 1, 1, 0 ],
  807. [ 1, 1, 1, 0 ],
  808. [ 1, 1, 1, 0 ],
  809. [ 0, 0, 0, 0 ]
  810. ] );
  811. } );
  812. } );
  813. describe( 'content and paste tables have spans', () => {
  814. it( 'handles pasting colspanned table over table with colspans (no colspan exceeds selection)', () => {
  815. setModelData( model, modelTable( [
  816. [ '00[]', '01', '02', '03' ],
  817. [ { colspan: 3, contents: '10' }, '13' ],
  818. [ { colspan: 2, contents: '20' }, '22', '23' ],
  819. [ '30', '31', { colspan: 2, contents: '31' } ]
  820. ] ) );
  821. tableSelection.setCellSelection(
  822. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  823. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  824. );
  825. pasteTable( [
  826. [ 'aa', { colspan: 2, contents: 'ab' } ],
  827. [ { colspan: 3, contents: 'ba' } ],
  828. [ 'ca', 'cb', 'cc' ]
  829. ] );
  830. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  831. [ 'aa', { colspan: 2, contents: 'ab' }, '03' ],
  832. [ { colspan: 3, contents: 'ba' }, '13' ],
  833. [ 'ca', 'cb', 'cc', '23' ],
  834. [ '30', '31', { colspan: 2, contents: '31' } ]
  835. ] ) );
  836. /* eslint-disable no-multi-spaces */
  837. assertSelectedCells( model, [
  838. [ 1, 1, 0 ],
  839. [ 1, 0 ],
  840. [ 1, 1, 1, 0 ],
  841. [ 0, 0, 0 ]
  842. ] );
  843. /* eslint-enable no-multi-spaces */
  844. } );
  845. it( 'handles pasting rowspanned table over table with rowspans (no rowspan exceeds selection)', () => {
  846. setModelData( model, modelTable( [
  847. [ { rowspan: 3, contents: '00' }, { rowspan: 2, contents: '01' }, '02', '03' ],
  848. [ { rowspan: 2, contents: '12' }, '13' ],
  849. [ '21', '23' ],
  850. [ '30', '31', '32', '33' ]
  851. ] ) );
  852. tableSelection.setCellSelection(
  853. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  854. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  855. );
  856. pasteTable( [
  857. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  858. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  859. [ 'cc', 'cd' ]
  860. ] );
  861. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  862. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  863. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  864. [ 'cc', 'cd' ],
  865. [ '30', '31', '32', '33' ]
  866. ] ) );
  867. /* eslint-disable no-multi-spaces */
  868. assertSelectedCells( model, [
  869. [ 1, 1, 1, 1 ],
  870. [ 1, 1 ],
  871. [ 1, 1 ],
  872. [ 0, 0, 0, 0 ]
  873. ] );
  874. /* eslint-enable no-multi-spaces */
  875. } );
  876. it( 'handles pasting multi-spanned table over table with multi-spans (no span exceeds selection)', () => {
  877. // +----+----+----+----+----+----+
  878. // | 00 | 02 | 03 | 05 |
  879. // + + + +----+
  880. // | | | | 15 |
  881. // +----+----+----+ +----+
  882. // | 20 | 21 | | 25 |
  883. // + +----+----+----+----+----+
  884. // | | 31 | 32 | 34 | 35 |
  885. // +----+----+----+----+----+----+
  886. // | 40 | 41 | 42 | 43 | 44 | 45 |
  887. // +----+----+----+----+----+----+
  888. setModelData( model, modelTable( [
  889. [
  890. { contents: '00', colspan: 2, rowspan: 2 },
  891. { contents: '02', rowspan: 2 },
  892. { contents: '03', colspan: 2, rowspan: 3 },
  893. '05'
  894. ],
  895. [ '15' ],
  896. [ { contents: '20', rowspan: 2 }, { contents: '21', colspan: 2 }, '25' ],
  897. [ '31', { contents: '32', colspan: 2 }, '34', '35' ],
  898. [ '40', '41', '42', '43', '44', '45' ]
  899. ] ) );
  900. tableSelection.setCellSelection(
  901. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  902. modelRoot.getNodeByPath( [ 0, 3, 2 ] )
  903. );
  904. // +----+----+----+----+----+
  905. // | aa | ac | ad | ae |
  906. // +----+----+----+----+ +
  907. // | ba | bb | |
  908. // +----+ +----+
  909. // | ca | | ce |
  910. // + +----+----+----+----+
  911. // | | db | dc | dd |
  912. // +----+----+----+----+----+
  913. pasteTable( [
  914. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 } ],
  915. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 } ],
  916. [ { contents: 'ca', rowspan: 2 }, 'ce' ],
  917. [ 'db', 'dc', { contents: 'dd', colspan: 2 } ]
  918. ] );
  919. // +----+----+----+----+----+----+
  920. // | aa | ac | ad | ae | 05 |
  921. // +----+----+----+----+ +----+
  922. // | ba | bb | | 15 |
  923. // +----+ +----+----+
  924. // | ca | | ce | 25 |
  925. // + +----+----+----+----+----+
  926. // | | db | dc | dd | 35 |
  927. // +----+----+----+----+----+----+
  928. // | 40 | 41 | 42 | 43 | 44 | 45 |
  929. // +----+----+----+----+----+----+
  930. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  931. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 }, '05' ],
  932. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 }, '15' ],
  933. [ { contents: 'ca', rowspan: 2 }, 'ce', '25' ],
  934. [ 'db', 'dc', { contents: 'dd', colspan: 2 }, '35' ],
  935. [ '40', '41', '42', '43', '44', '45' ]
  936. ] ) );
  937. /* eslint-disable no-multi-spaces */
  938. assertSelectedCells( model, [
  939. [ 1, 1, 1, 1, 0 ],
  940. [ 1, 1, 0 ],
  941. [ 1, 1, 0 ],
  942. [ 1, 1, 1, 0 ],
  943. [ 0, 0, 0, 0, 0, 0 ]
  944. ] );
  945. /* eslint-enable no-multi-spaces */
  946. } );
  947. it( 'handles pasting table that has cell with colspan (last row in selection is spanned)', () => {
  948. // +----+----+----+----+
  949. // | 00 | 01 | 02 | 03 |
  950. // +----+----+----+----+
  951. // | 10 | 11 | 13 |
  952. // + + +----+
  953. // | | | 23 |
  954. // +----+----+----+----+
  955. // | 30 | 31 | 32 | 33 |
  956. // +----+----+----+----+
  957. setModelData( model, modelTable( [
  958. [ '00', '01', '02', '03' ],
  959. [ { contents: '10', rowspan: 2 }, { contents: '11', colspan: 2, rowspan: 2 }, '13' ],
  960. [ '23' ],
  961. [ '30', '31', '32', '33' ]
  962. ] ) );
  963. // Select 02 -> 10 (selection 3x3)
  964. tableSelection.setCellSelection(
  965. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  966. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  967. );
  968. // +----+----+----+
  969. // | aa | ab | ac |
  970. // +----+----+----+
  971. // | ba | bc |
  972. // + +----+
  973. // | | cc |
  974. // +----+----+----+
  975. pasteTable( [
  976. [ 'aa', 'ab', 'ac' ],
  977. [ { contents: 'ba', colspan: 2, rowspan: 2 }, 'bc' ],
  978. [ 'cc' ]
  979. ] );
  980. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  981. [ 'aa', 'ab', 'ac', '03' ],
  982. [ { contents: 'ba', colspan: 2, rowspan: 2 }, 'bc', '13' ],
  983. [ 'cc', '23' ],
  984. [ '30', '31', '32', '33' ]
  985. ] ) );
  986. /* eslint-disable no-multi-spaces */
  987. assertSelectedCells( model, [
  988. [ 1, 1, 1, 0 ],
  989. [ 1, 1, 0 ],
  990. [ 1, 0 ],
  991. [ 0, 0, 0, 0 ]
  992. ] );
  993. /* eslint-enable no-multi-spaces */
  994. } );
  995. it( 'handles pasting table that has cell with colspan (last column in selection is spanned)', () => {
  996. // +----+----+----+----+
  997. // | 00 | 01 | 03 |
  998. // +----+----+----+----+
  999. // | 10 | 11 | 13 |
  1000. // +----+ +----+
  1001. // | 20 | | 23 |
  1002. // +----+----+----+----+
  1003. // | 30 | 31 | 32 | 33 |
  1004. // +----+----+----+----+
  1005. setModelData( model, modelTable( [
  1006. [ '00', { contents: '01', colspan: 2 }, '03' ],
  1007. [ '10', { contents: '11', colspan: 2, rowspan: 2 }, '13' ],
  1008. [ '20', '23' ],
  1009. [ '30', '31', '32', '33' ]
  1010. ] ) );
  1011. // Select 20 -> 01 (selection 3x3)
  1012. tableSelection.setCellSelection(
  1013. modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  1014. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  1015. );
  1016. // +----+----+----+
  1017. // | aa | ab | ac |
  1018. // +----+----+----+
  1019. // | ba | bc |
  1020. // + +----+
  1021. // | | cc |
  1022. // +----+----+----+
  1023. pasteTable( [
  1024. [ 'aa', 'ab', 'ac' ],
  1025. [ { contents: 'ba', colspan: 2, rowspan: 2 }, 'bc' ],
  1026. [ 'cc' ]
  1027. ] );
  1028. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1029. [ 'aa', 'ab', 'ac', '03' ],
  1030. [ { contents: 'ba', colspan: 2, rowspan: 2 }, 'bc', '13' ],
  1031. [ 'cc', '23' ],
  1032. [ '30', '31', '32', '33' ]
  1033. ] ) );
  1034. /* eslint-disable no-multi-spaces */
  1035. assertSelectedCells( model, [
  1036. [ 1, 1, 1, 0 ],
  1037. [ 1, 1, 0 ],
  1038. [ 1, 0 ],
  1039. [ 0, 0, 0, 0 ]
  1040. ] );
  1041. /* eslint-enable no-multi-spaces */
  1042. } );
  1043. } );
  1044. describe( 'non-rectangular content table selection', () => {
  1045. it( 'should split cells outside the selected area before pasting (rowspan ends in selection)', () => {
  1046. // +----+----+----+
  1047. // | 00 | 01 | 02 |
  1048. // +----+ +----+
  1049. // | 10 | | 12 |
  1050. // +----+ +----+
  1051. // | 20 | | 22 |
  1052. // +----+ +----+
  1053. // | 30 | | 32 |
  1054. // +----+----+----+
  1055. // | 40 | 41 | 42 |
  1056. // +----+----+----+
  1057. setModelData( model, modelTable( [
  1058. [ '00', { contents: '01', rowspan: 4 }, '02' ],
  1059. [ '10', '12' ],
  1060. [ '20', '22' ],
  1061. [ '30', '32' ],
  1062. [ '40', '41', '42' ]
  1063. ] ) );
  1064. // Select 20 -> 32
  1065. tableSelection.setCellSelection(
  1066. modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  1067. modelRoot.getNodeByPath( [ 0, 3, 1 ] )
  1068. );
  1069. pasteTable( [
  1070. [ 'aa', 'ab', 'ac' ],
  1071. [ 'ba', 'bb', 'bc' ]
  1072. ] );
  1073. // +----+----+----+
  1074. // | 00 | 01 | 02 |
  1075. // +----+ +----+
  1076. // | 10 | | 12 |
  1077. // +----+----+----+
  1078. // | aa | ab | ac |
  1079. // +----+----+----+
  1080. // | ba | bb | bc |
  1081. // +----+----+----+
  1082. // | 40 | 41 | 42 |
  1083. // +----+----+----+
  1084. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1085. [ '00', { contents: '01', rowspan: 2 }, '02' ],
  1086. [ '10', '12' ],
  1087. [ 'aa', 'ab', 'ac' ],
  1088. [ 'ba', 'bb', 'bc' ],
  1089. [ '40', '41', '42' ]
  1090. ] ) );
  1091. } );
  1092. it( 'should split cells outside the selected area before pasting (rowspan ends after the selection)', () => {
  1093. // +----+----+----+
  1094. // | 00 | 01 | 02 |
  1095. // +----+ +----+
  1096. // | 10 | | 12 |
  1097. // +----+ +----+
  1098. // | 20 | | 22 |
  1099. // +----+ +----+
  1100. // | 30 | | 32 |
  1101. // +----+----+----+
  1102. // | 40 | 41 | 42 |
  1103. // +----+----+----+
  1104. setModelData( model, modelTable( [
  1105. [ '00', { contents: '01', rowspan: 4 }, '02' ],
  1106. [ '10', '12' ],
  1107. [ '20', '22' ],
  1108. [ '30', '32' ],
  1109. [ '40', '41', '42' ]
  1110. ] ) );
  1111. // Select 10 -> 22
  1112. tableSelection.setCellSelection(
  1113. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  1114. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  1115. );
  1116. pasteTable( [
  1117. [ 'aa', 'ab', 'ac' ],
  1118. [ 'ba', 'bb', 'bc' ]
  1119. ] );
  1120. // +----+----+----+
  1121. // | 00 | 01 | 02 |
  1122. // +----+----+----+
  1123. // | aa | ab | ac |
  1124. // +----+----+----+
  1125. // | ba | bb | bc |
  1126. // +----+----+----+
  1127. // | 30 | | 32 |
  1128. // +----+----+----+
  1129. // | 40 | 41 | 42 |
  1130. // +----+----+----+
  1131. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1132. [ '00', '01', '02' ],
  1133. [ 'aa', 'ab', 'ac' ],
  1134. [ 'ba', 'bb', 'bc' ],
  1135. [ '30', '', '32' ],
  1136. [ '40', '41', '42' ]
  1137. ] ) );
  1138. } );
  1139. it( 'should split cells inside the selected area before pasting (rowspan ends after the selection)', () => {
  1140. // +----+----+----+
  1141. // | 00 | 01 | 02 |
  1142. // +----+ +----+
  1143. // | 10 | | 12 |
  1144. // +----+ +----+
  1145. // | 20 | | 22 |
  1146. // +----+ +----+
  1147. // | 30 | | 32 |
  1148. // +----+----+----+
  1149. // | 40 | 41 | 42 |
  1150. // +----+----+----+
  1151. setModelData( model, modelTable( [
  1152. [ '00', { contents: '01', rowspan: 4 }, '02' ],
  1153. [ '10', '12' ],
  1154. [ '20', '22' ],
  1155. [ '30', '32' ],
  1156. [ '40', '41', '42' ]
  1157. ] ) );
  1158. // Select 00 -> 12
  1159. tableSelection.setCellSelection(
  1160. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1161. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  1162. );
  1163. pasteTable( [
  1164. [ 'aa', 'ab', 'ac' ],
  1165. [ 'ba', 'bb', 'bc' ]
  1166. ] );
  1167. // +----+----+----+
  1168. // | aa | ab | ac |
  1169. // +----+----+----+
  1170. // | ba | bb | bc |
  1171. // +----+----+----+
  1172. // | 20 | | 22 |
  1173. // +----+ +----+
  1174. // | 30 | | 32 |
  1175. // +----+----+----+
  1176. // | 40 | 41 | 42 |
  1177. // +----+----+----+
  1178. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1179. [ 'aa', 'ab', 'ac' ],
  1180. [ 'ba', 'bb', 'bc' ],
  1181. [ '20', { rowspan: 2, contents: '' }, '22' ],
  1182. [ '30', '32' ],
  1183. [ '40', '41', '42' ]
  1184. ] ) );
  1185. } );
  1186. it( 'should split cells outside the selected area before pasting (colspan ends in selection)', () => {
  1187. // +----+----+----+----+----+
  1188. // | 00 | 01 | 02 | 03 | 04 |
  1189. // +----+----+----+----+----+
  1190. // | 10 | 14 |
  1191. // +----+----+----+----+----+
  1192. // | 20 | 21 | 22 | 23 | 24 |
  1193. // +----+----+----+----+----+
  1194. setModelData( model, modelTable( [
  1195. [ '00', '01', '02', '03', '04' ],
  1196. [ { contents: '10', colspan: 4 }, '14' ],
  1197. [ '20', '21', '22', '23', '24' ]
  1198. ] ) );
  1199. // Select 02 -> 23
  1200. tableSelection.setCellSelection(
  1201. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  1202. modelRoot.getNodeByPath( [ 0, 2, 3 ] )
  1203. );
  1204. pasteTable( [
  1205. [ 'aa', 'ab' ],
  1206. [ 'ba', 'bb' ],
  1207. [ 'ca', 'cb' ]
  1208. ] );
  1209. // +----+----+----+----+----+
  1210. // | 00 | 01 | aa | ab | 04 |
  1211. // +----+----+----+----+----+
  1212. // | 10 | ba | bb | 14 |
  1213. // +----+----+----+----+----+
  1214. // | 20 | 21 | ca | cb | 24 |
  1215. // +----+----+----+----+----+
  1216. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1217. [ '00', '01', 'aa', 'ab', '04' ],
  1218. [ { contents: '10', colspan: 2 }, 'ba', 'bb', '14' ],
  1219. [ '20', '21', 'ca', 'cb', '24' ]
  1220. ] ) );
  1221. } );
  1222. it( 'should split cells outside the selected area before pasting (colspan ends after the selection)', () => {
  1223. // +----+----+----+----+----+
  1224. // | 00 | 01 | 02 | 03 | 04 |
  1225. // +----+----+----+----+----+
  1226. // | 10 | 14 |
  1227. // +----+----+----+----+----+
  1228. // | 20 | 21 | 22 | 23 | 24 |
  1229. // +----+----+----+----+----+
  1230. setModelData( model, modelTable( [
  1231. [ '00', '01', '02', '03', '04' ],
  1232. [ { contents: '10', colspan: 4 }, '14' ],
  1233. [ '20', '21', '22', '23', '24' ]
  1234. ] ) );
  1235. // Select 01 -> 22
  1236. tableSelection.setCellSelection(
  1237. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  1238. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  1239. );
  1240. pasteTable( [
  1241. [ 'aa', 'ab' ],
  1242. [ 'ba', 'bb' ],
  1243. [ 'ca', 'cb' ]
  1244. ] );
  1245. // +----+----+----+----+----+
  1246. // | 00 | aa | ab | 03 | 04 |
  1247. // +----+----+----+----+----+
  1248. // | 10 | ba | bb | | 14 |
  1249. // +----+----+----+----+----+
  1250. // | 20 | ca | cb | 23 | 24 |
  1251. // +----+----+----+----+----+
  1252. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1253. [ '00', 'aa', 'ab', '03', '04' ],
  1254. [ '10', 'ba', 'bb', '', '14' ],
  1255. [ '20', 'ca', 'cb', '23', '24' ]
  1256. ] ) );
  1257. } );
  1258. it( 'should split cells inside the selected area before pasting (colspan ends after the 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 00 -> 21
  1272. tableSelection.setCellSelection(
  1273. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1274. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  1275. );
  1276. pasteTable( [
  1277. [ 'aa', 'ab' ],
  1278. [ 'ba', 'bb' ],
  1279. [ 'ca', 'cb' ]
  1280. ] );
  1281. // +----+----+----+----+----+
  1282. // | aa | ab | 02 | 03 | 04 |
  1283. // +----+----+----+----+----+
  1284. // | ba | bb | | 14 |
  1285. // +----+----+----+----+----+
  1286. // | ca | cb | 22 | 23 | 24 |
  1287. // +----+----+----+----+----+
  1288. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1289. [ 'aa', 'ab', '02', '03', '04' ],
  1290. [ 'ba', 'bb', { colspan: 2, contents: '' }, '14' ],
  1291. [ 'ca', 'cb', '22', '23', '24' ]
  1292. ] ) );
  1293. } );
  1294. it( 'should split cells anchored outside selection rectangle that overlaps selection (above selection)', () => {
  1295. // +----+----+----+
  1296. // | 00 | 02 |
  1297. // + +----+
  1298. // | | 12 |
  1299. // +----+----+----+
  1300. // | 20 | 21 | 22 |
  1301. // +----+----+----+
  1302. setModelData( model, modelTable( [
  1303. [ { contents: '00', colspan: 2, rowspan: 2 }, '02' ],
  1304. [ '12' ],
  1305. [ '20', '21', '22' ]
  1306. ] ) );
  1307. // Select 21 -> 12
  1308. tableSelection.setCellSelection(
  1309. modelRoot.getNodeByPath( [ 0, 2, 1 ] ),
  1310. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  1311. );
  1312. pasteTable( [
  1313. [ 'aa', 'ab' ],
  1314. [ 'ba', 'bb' ]
  1315. ] );
  1316. // +----+----+----+
  1317. // | 00 | | 02 |
  1318. // + +----+----+
  1319. // | | aa | ab |
  1320. // +----+----+----+
  1321. // | 20 | ba | bb |
  1322. // +----+----+----+
  1323. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1324. [ { contents: '00', rowspan: 2 }, '', '02' ],
  1325. [ 'aa', 'ab' ],
  1326. [ '20', 'ba', 'bb' ]
  1327. ] ) );
  1328. } );
  1329. it( 'should split cells anchored outside selection rectangle that overlaps selection (below selection)', () => {
  1330. // +----+----+----+
  1331. // | 00 | 01 | 02 |
  1332. // +----+----+----+
  1333. // | 10 | 12 |
  1334. // + +----+
  1335. // | | 22 |
  1336. // +----+----+----+
  1337. setModelData( model, modelTable( [
  1338. [ '00', '01', '02' ],
  1339. [ { contents: '10', colspan: 2, rowspan: 2 }, '12' ],
  1340. [ '22' ]
  1341. ] ) );
  1342. // Select 01 -> 12
  1343. tableSelection.setCellSelection(
  1344. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  1345. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  1346. );
  1347. pasteTable( [
  1348. [ 'aa', 'ab' ],
  1349. [ 'ba', 'bb' ]
  1350. ] );
  1351. // +----+----+----+
  1352. // | 00 | aa | ab |
  1353. // +----+----+----+
  1354. // | 10 | ba | bb |
  1355. // + +----+----+
  1356. // | | | 22 |
  1357. // +----+----+----+
  1358. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1359. [ '00', 'aa', 'ab' ],
  1360. [ { contents: '10', rowspan: 2 }, 'ba', 'bb' ],
  1361. [ '', '22' ]
  1362. ] ) );
  1363. } );
  1364. it( 'should properly handle complex case', () => {
  1365. // +----+----+----+----+----+----+----+
  1366. // | 00 | 03 | 04 |
  1367. // + + +----+----+----+
  1368. // | | | 14 | 15 | 16 |
  1369. // + + +----+----+----+
  1370. // | | | 24 |
  1371. // +----+----+----+----+----+----+----+
  1372. // | 30 | 31 | 33 | 34 | 35 | 36 |
  1373. // + +----+----+ +----+----+----+
  1374. // | | 41 | 42 | | 44 | 45 |
  1375. // + +----+----+ +----+ +
  1376. // | | 51 | 52 | | 54 | |
  1377. // +----+----+----+ +----+ +
  1378. // | 60 | 61 | 62 | | 64 | |
  1379. // +----+----+----+----+----+----+----+
  1380. setModelData( model, modelTable( [
  1381. [ { contents: '00', colspan: 3, rowspan: 3 }, { contents: '03', rowspan: 3 }, { colspan: 3, contents: '04' } ],
  1382. [ '14', '15', '16' ],
  1383. [ { contents: '24', colspan: 3 } ],
  1384. [
  1385. { contents: '30', rowspan: 3 },
  1386. { contents: '31', colspan: 2 },
  1387. { contents: '33', rowspan: 4 },
  1388. '34', '35', '36'
  1389. ],
  1390. [ '41', '42', '44', { contents: '45', colspan: 2, rowspan: 3 } ],
  1391. [ '51', '52', '54' ],
  1392. [ '60', '61', '62', '64' ]
  1393. ] ) );
  1394. // Select 42 -> 24 (3x3 selection)
  1395. tableSelection.setCellSelection(
  1396. modelRoot.getNodeByPath( [ 0, 4, 1 ] ),
  1397. modelRoot.getNodeByPath( [ 0, 2, 0 ] )
  1398. );
  1399. pasteTable( [
  1400. [ 'aa', 'ab', 'ac' ],
  1401. [ 'ba', 'bb', 'bc' ],
  1402. [ 'ca', 'cb', 'cc' ]
  1403. ] );
  1404. // +----+----+----+----+----+----+----+
  1405. // | 00 | | 03 | 04 |
  1406. // + + + +----+----+----+
  1407. // | | | | 14 | 15 | 16 |
  1408. // + +----+----+----+----+----+
  1409. // | | aa | ab | ac | |
  1410. // +----+----+----+----+----+----+----+
  1411. // | 30 | 31 | ba | bb | bc | 35 | 36 |
  1412. // + +----+----+----+----+----+----+
  1413. // | | 41 | ca | cb | cc | 45 |
  1414. // + +----+----+----+----+ +
  1415. // | | 51 | 52 | | 54 | |
  1416. // +----+----+----+ +----+ +
  1417. // | 60 | 61 | 62 | | 64 | |
  1418. // +----+----+----+----+----+----+----+
  1419. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1420. [
  1421. { contents: '00', colspan: 2, rowspan: 3 },
  1422. { contents: '', rowspan: 2 },
  1423. { contents: '03', rowspan: 2 },
  1424. { contents: '04', colspan: 3 }
  1425. ],
  1426. [ '14', '15', '16' ],
  1427. [ 'aa', 'ab', 'ac', { contents: '', colspan: 2 } ],
  1428. [ { contents: '30', rowspan: 3 }, '31', 'ba', 'bb', 'bc', '35', '36' ],
  1429. [ '41', 'ca', 'cb', 'cc', { contents: '45', colspan: 2, rowspan: 3 } ],
  1430. [ '51', '52', { contents: '', rowspan: 2 }, '54' ],
  1431. [ '60', '61', '62', '64' ]
  1432. ] ) );
  1433. } );
  1434. } );
  1435. } );
  1436. describe( 'pasted table is bigger than the selected area', () => {
  1437. describe( 'no spans', () => {
  1438. it( 'handles simple table paste to a simple table fragment - at the beginning of a table', () => {
  1439. tableSelection.setCellSelection(
  1440. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1441. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  1442. );
  1443. pasteTable( [
  1444. [ 'aa', 'ab', 'ac' ],
  1445. [ 'ba', 'bb', 'bc' ],
  1446. [ 'ca', 'cb', 'cc' ]
  1447. ] );
  1448. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1449. [ 'aa', 'ab', '02', '03' ],
  1450. [ 'ba', 'bb', '12', '13' ],
  1451. [ '20', '21', '22', '23' ],
  1452. [ '30', '31', '32', '33' ]
  1453. ] ) );
  1454. assertSelectedCells( model, [
  1455. [ 1, 1, 0, 0 ],
  1456. [ 1, 1, 0, 0 ],
  1457. [ 0, 0, 0, 0 ],
  1458. [ 0, 0, 0, 0 ]
  1459. ] );
  1460. } );
  1461. it( 'handles simple table paste to a simple table fragment - at the end of a table', () => {
  1462. tableSelection.setCellSelection(
  1463. modelRoot.getNodeByPath( [ 0, 2, 2 ] ),
  1464. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  1465. );
  1466. pasteTable( [
  1467. [ 'aa', 'ab', 'ac' ],
  1468. [ 'ba', 'bb', 'bc' ],
  1469. [ 'ca', 'cb', 'cc' ]
  1470. ] );
  1471. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1472. [ '00', '01', '02', '03' ],
  1473. [ '10', '11', '12', '13' ],
  1474. [ '20', '21', 'aa', 'ab' ],
  1475. [ '30', '31', 'ba', 'bb' ]
  1476. ] ) );
  1477. assertSelectedCells( model, [
  1478. [ 0, 0, 0, 0 ],
  1479. [ 0, 0, 0, 0 ],
  1480. [ 0, 0, 1, 1 ],
  1481. [ 0, 0, 1, 1 ]
  1482. ] );
  1483. } );
  1484. it( 'handles simple table paste to a simple table fragment - in the middle of a table', () => {
  1485. tableSelection.setCellSelection(
  1486. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  1487. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  1488. );
  1489. pasteTable( [
  1490. [ 'aa', 'ab', 'ac' ],
  1491. [ 'ba', 'bb', 'bc' ],
  1492. [ 'ca', 'cb', 'cc' ]
  1493. ] );
  1494. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1495. [ '00', '01', '02', '03' ],
  1496. [ '10', 'aa', 'ab', '13' ],
  1497. [ '20', 'ba', 'bb', '23' ],
  1498. [ '30', '31', '32', '33' ]
  1499. ] ) );
  1500. assertSelectedCells( model, [
  1501. [ 0, 0, 0, 0 ],
  1502. [ 0, 1, 1, 0 ],
  1503. [ 0, 1, 1, 0 ],
  1504. [ 0, 0, 0, 0 ]
  1505. ] );
  1506. } );
  1507. it( 'handles paste to a simple row fragment - in the middle of a table', () => {
  1508. tableSelection.setCellSelection(
  1509. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  1510. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  1511. );
  1512. pasteTable( [
  1513. [ 'aa', 'ab', 'ac' ],
  1514. [ 'ba', 'bb', 'bc' ],
  1515. [ 'ca', 'cb', 'cc' ]
  1516. ] );
  1517. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1518. [ '00', '01', '02', '03' ],
  1519. [ '10', 'aa', 'ab', '13' ],
  1520. [ '20', '21', '22', '23' ],
  1521. [ '30', '31', '32', '33' ]
  1522. ] ) );
  1523. assertSelectedCells( model, [
  1524. [ 0, 0, 0, 0 ],
  1525. [ 0, 1, 1, 0 ],
  1526. [ 0, 0, 0, 0 ],
  1527. [ 0, 0, 0, 0 ]
  1528. ] );
  1529. } );
  1530. it( 'handles paste to a simple column fragment - in the middle of a table', () => {
  1531. tableSelection.setCellSelection(
  1532. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  1533. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  1534. );
  1535. pasteTable( [
  1536. [ 'aa', 'ab', 'ac' ],
  1537. [ 'ba', 'bb', 'bc' ],
  1538. [ 'ca', 'cb', 'cc' ]
  1539. ] );
  1540. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1541. [ '00', '01', '02', '03' ],
  1542. [ '10', 'aa', '12', '13' ],
  1543. [ '20', 'ba', '22', '23' ],
  1544. [ '30', '31', '32', '33' ]
  1545. ] ) );
  1546. assertSelectedCells( model, [
  1547. [ 0, 0, 0, 0 ],
  1548. [ 0, 1, 0, 0 ],
  1549. [ 0, 1, 0, 0 ],
  1550. [ 0, 0, 0, 0 ]
  1551. ] );
  1552. } );
  1553. it( 'handles simple table paste to a simple table fragment - whole table selected', () => {
  1554. tableSelection.setCellSelection(
  1555. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1556. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  1557. );
  1558. pasteTable( [
  1559. [ 'aa', 'ab', 'ac', 'ad', 'ae' ],
  1560. [ 'ba', 'bb', 'bc', 'bd', 'be' ],
  1561. [ 'ca', 'cb', 'cc', 'cd', 'ce' ],
  1562. [ 'da', 'db', 'dc', 'dd', 'de' ],
  1563. [ 'ea', 'eb', 'ec', 'ed', 'ee' ]
  1564. ] );
  1565. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1566. [ 'aa', 'ab', 'ac', 'ad' ],
  1567. [ 'ba', 'bb', 'bc', 'bd' ],
  1568. [ 'ca', 'cb', 'cc', 'cd' ],
  1569. [ 'da', 'db', 'dc', 'dd' ]
  1570. ] ) );
  1571. assertSelectedCells( model, [
  1572. [ 1, 1, 1, 1 ],
  1573. [ 1, 1, 1, 1 ],
  1574. [ 1, 1, 1, 1 ],
  1575. [ 1, 1, 1, 1 ]
  1576. ] );
  1577. } );
  1578. } );
  1579. describe( 'pasted table has spans', () => {
  1580. it( 'handles pasting table that has cell with colspan', () => {
  1581. tableSelection.setCellSelection(
  1582. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  1583. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  1584. );
  1585. pasteTable( [
  1586. [ { colspan: 3, contents: 'aa' } ],
  1587. [ 'ba', 'bb', 'bc' ]
  1588. ] );
  1589. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1590. [ '00', '01', '02', '03' ],
  1591. [ '10', { colspan: 2, contents: 'aa' }, '13' ],
  1592. [ '20', 'ba', 'bb', '23' ],
  1593. [ '30', '31', '32', '33' ]
  1594. ] ) );
  1595. /* eslint-disable no-multi-spaces */
  1596. assertSelectedCells( model, [
  1597. [ 0, 0, 0, 0 ],
  1598. [ 0, 1, 0 ],
  1599. [ 0, 1, 1, 0 ],
  1600. [ 0, 0, 0, 0 ]
  1601. ] );
  1602. /* eslint-enable no-multi-spaces */
  1603. } );
  1604. it( 'handles pasting table that has many cells with various colspan', () => {
  1605. tableSelection.setCellSelection(
  1606. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1607. modelRoot.getNodeByPath( [ 0, 3, 2 ] )
  1608. );
  1609. pasteTable( [
  1610. [ 'aa', { colspan: 3, contents: 'ab' } ],
  1611. [ { colspan: 4, contents: 'ba' } ],
  1612. [ 'ca', 'cb', 'cc', 'cd' ],
  1613. [ { colspan: 2, contents: 'da' }, 'dc', 'dd' ]
  1614. ] );
  1615. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1616. [ 'aa', { colspan: 2, contents: 'ab' }, '03' ],
  1617. [ { colspan: 3, contents: 'ba' }, '13' ],
  1618. [ 'ca', 'cb', 'cc', '23' ],
  1619. [ { colspan: 2, contents: 'da' }, 'dc', '33' ]
  1620. ] ) );
  1621. /* eslint-disable no-multi-spaces */
  1622. assertSelectedCells( model, [
  1623. [ 1, 1, 0 ],
  1624. [ 1, 0 ],
  1625. [ 1, 1, 1, 0 ],
  1626. [ 1, 1, 0 ]
  1627. ] );
  1628. /* eslint-enable no-multi-spaces */
  1629. } );
  1630. it( 'handles pasting table that has cell with rowspan', () => {
  1631. tableSelection.setCellSelection(
  1632. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  1633. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  1634. );
  1635. pasteTable( [
  1636. [ { rowspan: 3, contents: 'aa' }, 'ab' ],
  1637. [ 'bb' ],
  1638. [ 'cb' ]
  1639. ] );
  1640. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1641. [ '00', '01', '02', '03' ],
  1642. [ '10', { rowspan: 2, contents: 'aa' }, 'ab', '13' ],
  1643. [ '20', 'bb', '23' ],
  1644. [ '30', '31', '32', '33' ]
  1645. ] ) );
  1646. /* eslint-disable no-multi-spaces */
  1647. assertSelectedCells( model, [
  1648. [ 0, 0, 0, 0 ],
  1649. [ 0, 1, 1, 0 ],
  1650. [ 0, 1, 0 ],
  1651. [ 0, 0, 0, 0 ]
  1652. ] );
  1653. /* eslint-enable no-multi-spaces */
  1654. } );
  1655. it( 'handles pasting table that has many cells with various rowspan', () => {
  1656. tableSelection.setCellSelection(
  1657. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1658. modelRoot.getNodeByPath( [ 0, 2, 3 ] )
  1659. );
  1660. pasteTable( [
  1661. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad', 'ae' ],
  1662. [ { rowspan: 3, contents: 'ba' }, 'bd', 'be' ],
  1663. [ 'cc', 'cd', 'ce' ],
  1664. [ 'da', 'db', 'dc', 'dd' ]
  1665. ] );
  1666. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1667. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  1668. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  1669. [ 'cc', 'cd' ],
  1670. [ '30', '31', '32', '33' ]
  1671. ] ) );
  1672. /* eslint-disable no-multi-spaces */
  1673. assertSelectedCells( model, [
  1674. [ 1, 1, 1, 1 ],
  1675. [ 1, 1 ],
  1676. [ 1, 1 ],
  1677. [ 0, 0, 0, 0 ]
  1678. ] );
  1679. /* eslint-enable no-multi-spaces */
  1680. } );
  1681. it( 'handles pasting multi-spanned table', () => {
  1682. setModelData( model, modelTable( [
  1683. [ '00', '01', '02', '03', '04', '05' ],
  1684. [ '10', '11', '12', '13', '14', '15' ],
  1685. [ '20', '21', '22', '23', '24', '25' ],
  1686. [ '30', '31', '32', '33', '34', '35' ],
  1687. [ '40', '41', '42', '43', '44', '45' ]
  1688. ] ) );
  1689. tableSelection.setCellSelection(
  1690. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  1691. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  1692. );
  1693. // +----+----+----+----+----+
  1694. // | aa | ac | ad | ae |
  1695. // +----+----+----+----+ +
  1696. // | ba | bb | |
  1697. // +----+ +----+
  1698. // | ca | | ce |
  1699. // + +----+----+----+----+
  1700. // | | db | dc | dd |
  1701. // +----+----+----+----+----+
  1702. pasteTable( [
  1703. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 } ],
  1704. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 } ],
  1705. [ { contents: 'ca', rowspan: 2 }, 'ce' ],
  1706. [ 'db', 'dc', { contents: 'dd', colspan: 2 } ]
  1707. ] );
  1708. // +----+----+----+----+----+----+
  1709. // | 00 | 01 | 02 | 03 | 04 | 05 |
  1710. // +----+----+----+----+----+----+
  1711. // | 10 | aa | ac | 14 | 15 |
  1712. // +----+----+----+----+----+----+
  1713. // | 20 | ba | bb | 24 | 25 |
  1714. // +----+----+ +----+----+
  1715. // | 30 | ca | | 34 | 35 |
  1716. // +----+----+----+----+----+----+
  1717. // | 40 | 41 | 42 | 43 | 44 | 45 |
  1718. // +----+----+----+----+----+----+
  1719. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1720. [ '00', '01', '02', '03', '04', '05' ],
  1721. [ '10', { contents: 'aa', colspan: 2 }, 'ac', '14', '15' ],
  1722. [ '20', 'ba', { contents: 'bb', colspan: 2, rowspan: 2 }, '24', '25' ],
  1723. [ '30', 'ca', '34', '35' ],
  1724. [ '40', '41', '42', '43', '44', '45' ]
  1725. ] ) );
  1726. /* eslint-disable no-multi-spaces */
  1727. assertSelectedCells( model, [
  1728. [ 0, 0, 0, 0, 0, 0 ],
  1729. [ 0, 1, 1, 0, 0 ],
  1730. [ 0, 1, 1, 0, 0 ],
  1731. [ 0, 1, 0, 0 ],
  1732. [ 0, 0, 0, 0, 0, 0 ]
  1733. ] );
  1734. /* eslint-enable no-multi-spaces */
  1735. } );
  1736. } );
  1737. } );
  1738. describe( 'pasted table is smaller than the selected area', () => {
  1739. it( 'blocks this case', () => {
  1740. tableSelection.setCellSelection(
  1741. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1742. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  1743. );
  1744. // Catches the temporary console log in the CK_DEBUG mode.
  1745. sinon.stub( console, 'log' );
  1746. pasteTable( [
  1747. [ 'aa', 'ab' ],
  1748. [ 'ba', 'bb' ]
  1749. ] );
  1750. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1751. [ '00', '01', '02', '03' ],
  1752. [ '10', '11', '12', '13' ],
  1753. [ '20', '21', '22', '23' ],
  1754. [ '30', '31', '32', '33' ]
  1755. ] ) );
  1756. assertSelectedCells( model, [
  1757. [ 1, 1, 1, 1 ],
  1758. [ 1, 1, 1, 1 ],
  1759. [ 1, 1, 1, 1 ],
  1760. [ 1, 1, 1, 1 ]
  1761. ] );
  1762. } );
  1763. } );
  1764. describe( 'fixing pasted table broken layout', () => {
  1765. it( 'should trim pasted cells\' width if they exceeds table width established by the first row', () => {
  1766. // Select 00 -> 22 (selection 2x3 - equal to expected fixed table)
  1767. tableSelection.setCellSelection(
  1768. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1769. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  1770. );
  1771. // +----+----+
  1772. // | aa | ab | <- First row establish table width=2.
  1773. // +----+----+----+----+
  1774. // | ba | bb | <- Cell "bb" sticks out by 2 slots.
  1775. // +----+----+----+----+
  1776. // | ca | <- Cell "ca" sticks out by 1 slot.
  1777. // +----+----+----+
  1778. pasteTable( [
  1779. [ 'aa', 'ab' ],
  1780. [ 'ba', { colspan: 3, contents: 'bb' } ],
  1781. [ { colspan: 3, contents: 'ca' } ]
  1782. ] );
  1783. // +----+----+----+----+
  1784. // | aa | ab | 02 | 03 |
  1785. // +----+----+----+----+
  1786. // | ba | bb | 12 | 13 |
  1787. // +----+----+----+----+
  1788. // | ca | 22 | 23 |
  1789. // +----+----+----+----+
  1790. // | 30 | 31 | 32 | 33 |
  1791. // +----+----+----+----+
  1792. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1793. [ 'aa', 'ab', '02', '03' ],
  1794. [ 'ba', 'bb', '12', '13' ],
  1795. [ { contents: 'ca', colspan: 2 }, '22', '23' ],
  1796. [ '30', '31', '32', '33' ]
  1797. ] ) );
  1798. /* eslint-disable no-multi-spaces */
  1799. assertSelectedCells( model, [
  1800. [ 1, 1, 0, 0 ],
  1801. [ 1, 1, 0, 0 ],
  1802. [ 1, 0, 0 ],
  1803. [ 0, 0, 0, 0 ]
  1804. ] );
  1805. /* eslint-enable no-multi-spaces */
  1806. } );
  1807. it( 'should trim pasted cells\' height if they exceeds table height established by the last row', () => {
  1808. // Select 00 -> 12 (selection 3x2 - equal to expected fixed table)
  1809. tableSelection.setCellSelection(
  1810. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1811. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  1812. );
  1813. // +----+----+----+
  1814. // | aa | ab | ac |
  1815. // +----+----+ +
  1816. // | ba | bb | | <- Last row establish table height=2.
  1817. // +----+ + +
  1818. // | | | <- Cell "ac" sticks out by 1 slot.
  1819. // + +----+
  1820. // | | <- Cell "bb" sticks out by 2 slots.
  1821. // +----+
  1822. pasteTable( [
  1823. [
  1824. [ 'aa', 'ab', { contents: 'ac', rowspan: 3 } ],
  1825. [ 'ba', { contents: 'bb', rowspan: 3 } ]
  1826. ]
  1827. ] );
  1828. // +----+----+----+----+
  1829. // | aa | ab | ac | 03 |
  1830. // +----+----+ +----+
  1831. // | ba | bb | | 13 |
  1832. // +----+----+----+----+
  1833. // | 20 | 21 | 22 | 23 |
  1834. // +----+----+----+----+
  1835. // | 30 | 31 | 32 | 33 |
  1836. // +----+----+----+----+
  1837. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1838. [ 'aa', 'ab', '02', '03' ],
  1839. [ 'ba', 'bb', '12', '13' ],
  1840. [ { contents: 'ca', colspan: 2 }, '22', '23' ],
  1841. [ '30', '31', '32', '33' ]
  1842. ] ) );
  1843. /* eslint-disable no-multi-spaces */
  1844. assertSelectedCells( model, [
  1845. [ 1, 1, 1, 0 ],
  1846. [ 1, 1, 0 ],
  1847. [ 0, 0, 0, 0 ],
  1848. [ 0, 0, 0, 0 ]
  1849. ] );
  1850. /* eslint-enable no-multi-spaces */
  1851. } );
  1852. it( 'should trim pasted cells\' height and width if they exceeds table height and width', () => {
  1853. // Select 00 -> 11 (selection 2x2 - equal to expected fixed table)
  1854. tableSelection.setCellSelection(
  1855. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1856. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  1857. );
  1858. // +----+----+
  1859. // | aa | ab |
  1860. // +----+----+----+----+
  1861. // | ba | bb | <- Cell "bb" sticks out by 2 slots in width and by 1 slot in height.
  1862. // +----+ +
  1863. // | |
  1864. // +----+----+----+
  1865. pasteTable( [
  1866. [ 'aa', 'ab' ],
  1867. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 } ]
  1868. ] );
  1869. // +----+----+----+----+
  1870. // | aa | ab | 02 | 03 |
  1871. // +----+----+----+----+
  1872. // | ba | bb | 12 | 13 |
  1873. // +----+----+----+----+
  1874. // | 20 | 21 | 22 | 23 |
  1875. // +----+----+----+----+
  1876. // | 30 | 31 | 32 | 33 |
  1877. // +----+----+----+----+
  1878. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1879. [ 'aa', 'ab', '02', '03' ],
  1880. [ 'ba', 'bb', '12', '13' ],
  1881. [ '20', '21', '22', '23' ],
  1882. [ '30', '31', '32', '33' ]
  1883. ] ) );
  1884. /* eslint-disable no-multi-spaces */
  1885. assertSelectedCells( model, [
  1886. [ 1, 1, 0, 0 ],
  1887. [ 1, 1, 0, 0 ],
  1888. [ 0, 0, 0, 0 ],
  1889. [ 0, 0, 0, 0 ]
  1890. ] );
  1891. /* eslint-enable no-multi-spaces */
  1892. } );
  1893. } );
  1894. } );
  1895. describe( 'Clipboard integration - paste (content scenarios)', () => {
  1896. it( 'handles multiple paragraphs in table cell', async () => {
  1897. await createEditor();
  1898. setModelData( model, modelTable( [
  1899. [ '00', '01', '02' ],
  1900. [ '01', '11', '12' ],
  1901. [ '02', '21', '22' ]
  1902. ] ) );
  1903. tableSelection.setCellSelection(
  1904. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1905. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  1906. );
  1907. pasteTable( [
  1908. [ '<p>a</p><p>a</p><p>a</p>', 'ab' ],
  1909. [ 'ba', 'bb' ]
  1910. ] );
  1911. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1912. [ '<paragraph>a</paragraph><paragraph>a</paragraph><paragraph>a</paragraph>', 'ab', '02' ],
  1913. [ 'ba', 'bb', '12' ],
  1914. [ '02', '21', '22' ]
  1915. ] ) );
  1916. } );
  1917. it( 'handles image in table cell', async () => {
  1918. await createEditor( [ ImageEditing, ImageCaptionEditing ] );
  1919. setModelData( model, modelTable( [
  1920. [ '00', '01', '02' ],
  1921. [ '01', '11', '12' ],
  1922. [ '02', '21', '22' ]
  1923. ] ) );
  1924. tableSelection.setCellSelection(
  1925. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1926. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  1927. );
  1928. pasteTable( [
  1929. [ '<img src="/assets/sample.jpg">', 'ab' ],
  1930. [ 'ba', 'bb' ]
  1931. ] );
  1932. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1933. [ '<image src="/assets/sample.jpg"><caption></caption></image>', 'ab', '02' ],
  1934. [ 'ba', 'bb', '12' ],
  1935. [ '02', '21', '22' ]
  1936. ] ) );
  1937. } );
  1938. it( 'handles mixed nested content in table cell', async () => {
  1939. await createEditor( [ ImageEditing, ImageCaptionEditing, BlockQuoteEditing, HorizontalLineEditing, ListEditing ] );
  1940. setModelData( model, modelTable( [
  1941. [ '00', '01', '02' ],
  1942. [ '01', '11', '12' ],
  1943. [ '02', '21', '22' ]
  1944. ] ) );
  1945. tableSelection.setCellSelection(
  1946. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1947. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  1948. );
  1949. const img = '<img src="/assets/sample.jpg">';
  1950. const list = '<ul><li>foo</li><li>bar</li></ul>';
  1951. const blockquote = `<blockquote><p>baz</p>${ list }</blockquote>`;
  1952. pasteTable( [
  1953. [ `${ img }${ list }${ blockquote }`, 'ab' ],
  1954. [ 'ba', 'bb' ]
  1955. ] );
  1956. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1957. [
  1958. '<image src="/assets/sample.jpg"><caption></caption></image>' +
  1959. '<listItem listIndent="0" listType="bulleted">foo</listItem>' +
  1960. '<listItem listIndent="0" listType="bulleted">bar</listItem>' +
  1961. '<blockQuote>' +
  1962. '<paragraph>baz</paragraph>' +
  1963. '<listItem listIndent="0" listType="bulleted">foo</listItem>' +
  1964. '<listItem listIndent="0" listType="bulleted">bar</listItem>' +
  1965. '</blockQuote>',
  1966. 'ab',
  1967. '02' ],
  1968. [ 'ba', 'bb', '12' ],
  1969. [ '02', '21', '22' ]
  1970. ] ) );
  1971. } );
  1972. it( 'handles table cell properties', async () => {
  1973. await createEditor( [ TableCellPropertiesEditing ] );
  1974. setModelData( model, modelTable( [
  1975. [ '00', '01', '02' ],
  1976. [ '01', '11', '12' ],
  1977. [ '02', '21', '22' ]
  1978. ] ) );
  1979. tableSelection.setCellSelection(
  1980. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1981. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  1982. );
  1983. pasteTable( [
  1984. [ { contents: 'aa', style: 'border:1px solid #f00;background:#ba7;width:1337px' }, 'ab' ],
  1985. [ 'ba', 'bb' ]
  1986. ] );
  1987. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  1988. expect( tableCell.getAttribute( 'borderColor' ) ).to.deep.equal( {
  1989. top: '#f00',
  1990. right: '#f00',
  1991. bottom: '#f00',
  1992. left: '#f00'
  1993. } );
  1994. expect( tableCell.getAttribute( 'borderStyle' ) ).to.deep.equal( {
  1995. top: 'solid',
  1996. right: 'solid',
  1997. bottom: 'solid',
  1998. left: 'solid'
  1999. } );
  2000. expect( tableCell.getAttribute( 'borderWidth' ) ).to.deep.equal( {
  2001. top: '1px',
  2002. right: '1px',
  2003. bottom: '1px',
  2004. left: '1px'
  2005. } );
  2006. expect( tableCell.getAttribute( 'backgroundColor' ) ).to.equal( '#ba7' );
  2007. expect( tableCell.getAttribute( 'width' ) ).to.equal( '1337px' );
  2008. } );
  2009. it( 'discards table properties', async () => {
  2010. await createEditor( [ TableCellPropertiesEditing ] );
  2011. setModelData( model, modelTable( [
  2012. [ '00', '01', '02' ],
  2013. [ '01', '11', '12' ],
  2014. [ '02', '21', '22' ]
  2015. ] ) );
  2016. tableSelection.setCellSelection(
  2017. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2018. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  2019. );
  2020. const tableStyle = 'border:1px solid #f00;background:#ba7;width:1337px';
  2021. const pastedTable = `<table style="${ tableStyle }"><tr><td>aa</td><td>ab</td></tr><tr><td>ba</td><td>bb</td></tr></table>`;
  2022. const data = {
  2023. dataTransfer: createDataTransfer(),
  2024. preventDefault: sinon.spy(),
  2025. stopPropagation: sinon.spy()
  2026. };
  2027. data.dataTransfer.setData( 'text/html', pastedTable );
  2028. viewDocument.fire( 'paste', data );
  2029. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2030. [ 'aa', 'ab', '02' ],
  2031. [ 'ba', 'bb', '12' ],
  2032. [ '02', '21', '22' ]
  2033. ] ) );
  2034. } );
  2035. } );
  2036. async function createEditor( extraPlugins = [] ) {
  2037. editor = await ClassicTestEditor.create( element, {
  2038. plugins: [ TableEditing, TableClipboard, Paragraph, Clipboard, ...extraPlugins ]
  2039. } );
  2040. model = editor.model;
  2041. modelRoot = model.document.getRoot();
  2042. viewDocument = editor.editing.view.document;
  2043. tableSelection = editor.plugins.get( 'TableSelection' );
  2044. }
  2045. function pasteTable( tableData ) {
  2046. const data = {
  2047. dataTransfer: createDataTransfer(),
  2048. preventDefault: sinon.spy(),
  2049. stopPropagation: sinon.spy()
  2050. };
  2051. data.dataTransfer.setData( 'text/html', viewTable( tableData ) );
  2052. viewDocument.fire( 'paste', data );
  2053. return data;
  2054. }
  2055. function createDataTransfer() {
  2056. const store = new Map();
  2057. return {
  2058. setData( type, data ) {
  2059. store.set( type, data );
  2060. },
  2061. getData( type ) {
  2062. return store.get( type );
  2063. }
  2064. };
  2065. }
  2066. } );