tableclipboard-paste.js 63 KB

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