tableclipboard-paste.js 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352
  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. [ 'aa', 'ab', { contents: 'ac', rowspan: 3 } ],
  1824. [ 'ba', { contents: 'bb', rowspan: 3 } ]
  1825. ] );
  1826. // +----+----+----+----+
  1827. // | aa | ab | ac | 03 |
  1828. // +----+----+ +----+
  1829. // | ba | bb | | 13 |
  1830. // +----+----+----+----+
  1831. // | 20 | 21 | 22 | 23 |
  1832. // +----+----+----+----+
  1833. // | 30 | 31 | 32 | 33 |
  1834. // +----+----+----+----+
  1835. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1836. [ 'aa', 'ab', { rowspan: 2, contents: 'ac' }, '03' ],
  1837. [ 'ba', 'bb', '13' ],
  1838. [ '20', '21', '22', '23' ],
  1839. [ '30', '31', '32', '33' ]
  1840. ] ) );
  1841. /* eslint-disable no-multi-spaces */
  1842. assertSelectedCells( model, [
  1843. [ 1, 1, 1, 0 ],
  1844. [ 1, 1, 0 ],
  1845. [ 0, 0, 0, 0 ],
  1846. [ 0, 0, 0, 0 ]
  1847. ] );
  1848. /* eslint-enable no-multi-spaces */
  1849. } );
  1850. it( 'should trim pasted cells\' height and width if they exceeds table height and width', () => {
  1851. // Select 00 -> 11 (selection 2x2 - equal to expected fixed table)
  1852. tableSelection.setCellSelection(
  1853. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1854. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  1855. );
  1856. // +----+----+
  1857. // | aa | ab |
  1858. // +----+----+----+----+
  1859. // | ba | bb | <- Cell "bb" sticks out by 2 slots in width and by 1 slot in height.
  1860. // +----+ +
  1861. // | |
  1862. // +----+----+----+
  1863. pasteTable( [
  1864. [ 'aa', 'ab' ],
  1865. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 } ]
  1866. ] );
  1867. // +----+----+----+----+
  1868. // | aa | ab | 02 | 03 |
  1869. // +----+----+----+----+
  1870. // | ba | bb | 12 | 13 |
  1871. // +----+----+----+----+
  1872. // | 20 | 21 | 22 | 23 |
  1873. // +----+----+----+----+
  1874. // | 30 | 31 | 32 | 33 |
  1875. // +----+----+----+----+
  1876. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1877. [ 'aa', 'ab', '02', '03' ],
  1878. [ 'ba', 'bb', '12', '13' ],
  1879. [ '20', '21', '22', '23' ],
  1880. [ '30', '31', '32', '33' ]
  1881. ] ) );
  1882. /* eslint-disable no-multi-spaces */
  1883. assertSelectedCells( model, [
  1884. [ 1, 1, 0, 0 ],
  1885. [ 1, 1, 0, 0 ],
  1886. [ 0, 0, 0, 0 ],
  1887. [ 0, 0, 0, 0 ]
  1888. ] );
  1889. /* eslint-enable no-multi-spaces */
  1890. } );
  1891. } );
  1892. } );
  1893. describe( 'Clipboard integration - paste (content scenarios)', () => {
  1894. it( 'handles multiple paragraphs in table cell', async () => {
  1895. await createEditor();
  1896. setModelData( model, modelTable( [
  1897. [ '00', '01', '02' ],
  1898. [ '01', '11', '12' ],
  1899. [ '02', '21', '22' ]
  1900. ] ) );
  1901. tableSelection.setCellSelection(
  1902. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1903. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  1904. );
  1905. pasteTable( [
  1906. [ '<p>a</p><p>a</p><p>a</p>', 'ab' ],
  1907. [ 'ba', 'bb' ]
  1908. ] );
  1909. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1910. [ '<paragraph>a</paragraph><paragraph>a</paragraph><paragraph>a</paragraph>', 'ab', '02' ],
  1911. [ 'ba', 'bb', '12' ],
  1912. [ '02', '21', '22' ]
  1913. ] ) );
  1914. } );
  1915. it( 'handles image in table cell', async () => {
  1916. await createEditor( [ ImageEditing, ImageCaptionEditing ] );
  1917. setModelData( model, modelTable( [
  1918. [ '00', '01', '02' ],
  1919. [ '01', '11', '12' ],
  1920. [ '02', '21', '22' ]
  1921. ] ) );
  1922. tableSelection.setCellSelection(
  1923. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1924. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  1925. );
  1926. pasteTable( [
  1927. [ '<img src="/assets/sample.jpg">', 'ab' ],
  1928. [ 'ba', 'bb' ]
  1929. ] );
  1930. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1931. [ '<image src="/assets/sample.jpg"><caption></caption></image>', 'ab', '02' ],
  1932. [ 'ba', 'bb', '12' ],
  1933. [ '02', '21', '22' ]
  1934. ] ) );
  1935. } );
  1936. it( 'handles mixed nested content in table cell', async () => {
  1937. await createEditor( [ ImageEditing, ImageCaptionEditing, BlockQuoteEditing, HorizontalLineEditing, ListEditing ] );
  1938. setModelData( model, modelTable( [
  1939. [ '00', '01', '02' ],
  1940. [ '01', '11', '12' ],
  1941. [ '02', '21', '22' ]
  1942. ] ) );
  1943. tableSelection.setCellSelection(
  1944. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1945. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  1946. );
  1947. const img = '<img src="/assets/sample.jpg">';
  1948. const list = '<ul><li>foo</li><li>bar</li></ul>';
  1949. const blockquote = `<blockquote><p>baz</p>${ list }</blockquote>`;
  1950. pasteTable( [
  1951. [ `${ img }${ list }${ blockquote }`, 'ab' ],
  1952. [ 'ba', 'bb' ]
  1953. ] );
  1954. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1955. [
  1956. '<image src="/assets/sample.jpg"><caption></caption></image>' +
  1957. '<listItem listIndent="0" listType="bulleted">foo</listItem>' +
  1958. '<listItem listIndent="0" listType="bulleted">bar</listItem>' +
  1959. '<blockQuote>' +
  1960. '<paragraph>baz</paragraph>' +
  1961. '<listItem listIndent="0" listType="bulleted">foo</listItem>' +
  1962. '<listItem listIndent="0" listType="bulleted">bar</listItem>' +
  1963. '</blockQuote>',
  1964. 'ab',
  1965. '02' ],
  1966. [ 'ba', 'bb', '12' ],
  1967. [ '02', '21', '22' ]
  1968. ] ) );
  1969. } );
  1970. it( 'handles table cell properties', async () => {
  1971. await createEditor( [ TableCellPropertiesEditing ] );
  1972. setModelData( model, modelTable( [
  1973. [ '00', '01', '02' ],
  1974. [ '01', '11', '12' ],
  1975. [ '02', '21', '22' ]
  1976. ] ) );
  1977. tableSelection.setCellSelection(
  1978. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1979. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  1980. );
  1981. pasteTable( [
  1982. [ { contents: 'aa', style: 'border:1px solid #f00;background:#ba7;width:1337px' }, 'ab' ],
  1983. [ 'ba', 'bb' ]
  1984. ] );
  1985. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  1986. expect( tableCell.getAttribute( 'borderColor' ) ).to.deep.equal( {
  1987. top: '#f00',
  1988. right: '#f00',
  1989. bottom: '#f00',
  1990. left: '#f00'
  1991. } );
  1992. expect( tableCell.getAttribute( 'borderStyle' ) ).to.deep.equal( {
  1993. top: 'solid',
  1994. right: 'solid',
  1995. bottom: 'solid',
  1996. left: 'solid'
  1997. } );
  1998. expect( tableCell.getAttribute( 'borderWidth' ) ).to.deep.equal( {
  1999. top: '1px',
  2000. right: '1px',
  2001. bottom: '1px',
  2002. left: '1px'
  2003. } );
  2004. expect( tableCell.getAttribute( 'backgroundColor' ) ).to.equal( '#ba7' );
  2005. expect( tableCell.getAttribute( 'width' ) ).to.equal( '1337px' );
  2006. } );
  2007. it( 'discards table properties', async () => {
  2008. await createEditor( [ TableCellPropertiesEditing ] );
  2009. setModelData( model, modelTable( [
  2010. [ '00', '01', '02' ],
  2011. [ '01', '11', '12' ],
  2012. [ '02', '21', '22' ]
  2013. ] ) );
  2014. tableSelection.setCellSelection(
  2015. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  2016. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  2017. );
  2018. const tableStyle = 'border:1px solid #f00;background:#ba7;width:1337px';
  2019. const pastedTable = `<table style="${ tableStyle }"><tr><td>aa</td><td>ab</td></tr><tr><td>ba</td><td>bb</td></tr></table>`;
  2020. const data = {
  2021. dataTransfer: createDataTransfer(),
  2022. preventDefault: sinon.spy(),
  2023. stopPropagation: sinon.spy()
  2024. };
  2025. data.dataTransfer.setData( 'text/html', pastedTable );
  2026. viewDocument.fire( 'paste', data );
  2027. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  2028. [ 'aa', 'ab', '02' ],
  2029. [ 'ba', 'bb', '12' ],
  2030. [ '02', '21', '22' ]
  2031. ] ) );
  2032. } );
  2033. } );
  2034. async function createEditor( extraPlugins = [] ) {
  2035. editor = await ClassicTestEditor.create( element, {
  2036. plugins: [ TableEditing, TableClipboard, Paragraph, Clipboard, ...extraPlugins ]
  2037. } );
  2038. model = editor.model;
  2039. modelRoot = model.document.getRoot();
  2040. viewDocument = editor.editing.view.document;
  2041. tableSelection = editor.plugins.get( 'TableSelection' );
  2042. }
  2043. function pasteTable( tableData ) {
  2044. const data = {
  2045. dataTransfer: createDataTransfer(),
  2046. preventDefault: sinon.spy(),
  2047. stopPropagation: sinon.spy()
  2048. };
  2049. data.dataTransfer.setData( 'text/html', viewTable( tableData ) );
  2050. viewDocument.fire( 'paste', data );
  2051. return data;
  2052. }
  2053. function createDataTransfer() {
  2054. const store = new Map();
  2055. return {
  2056. setData( type, data ) {
  2057. store.set( type, data );
  2058. },
  2059. getData( type ) {
  2060. return store.get( type );
  2061. }
  2062. };
  2063. }
  2064. } );