downcast.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  7. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import { defaultConversion, defaultSchema, formatTable, formattedViewTable, modelTable } from '../_utils/utils';
  9. import env from '@ckeditor/ckeditor5-utils/src/env';
  10. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  11. function paragraphInTableCell() {
  12. return dispatcher => dispatcher.on( 'insert:paragraph', ( evt, data, conversionApi ) => {
  13. const tableCell = data.item.parent;
  14. if ( tableCell.is( 'tableCell' ) && tableCell.childCount > 1 ) {
  15. for ( const child of tableCell.getChildren() ) {
  16. if ( child.name != 'paragraph' ) {
  17. continue;
  18. }
  19. const viewElement = conversionApi.mapper.toViewElement( child );
  20. if ( viewElement && viewElement.name === 'span' ) {
  21. conversionApi.mapper.unbindModelElement( tableCell );
  22. conversionApi.writer.rename( viewElement, 'p' );
  23. conversionApi.mapper.bindElements( child, viewElement );
  24. }
  25. }
  26. }
  27. }, { converterPriority: 'highest' } );
  28. }
  29. describe( 'downcast converters', () => {
  30. let editor, model, doc, root, viewDocument;
  31. testUtils.createSinonSandbox();
  32. beforeEach( () => {
  33. // Most tests assume non-edge environment but we do not set `contenteditable=false` on Edge so stub `env.isEdge`.
  34. testUtils.sinon.stub( env, 'isEdge' ).get( () => false );
  35. return VirtualTestEditor.create()
  36. .then( newEditor => {
  37. editor = newEditor;
  38. model = editor.model;
  39. doc = model.document;
  40. root = doc.getRoot( 'main' );
  41. viewDocument = editor.editing.view;
  42. defaultSchema( model.schema );
  43. defaultConversion( editor.conversion );
  44. } );
  45. } );
  46. describe( 'downcastInsertTable()', () => {
  47. it( 'should create table with tbody', () => {
  48. setModelData( model, modelTable( [ [ '' ] ] ) );
  49. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  50. '<figure class="table">' +
  51. '<table>' +
  52. '<tbody>' +
  53. '<tr><td></td></tr>' +
  54. '</tbody>' +
  55. '</table>' +
  56. '</figure>'
  57. ) );
  58. } );
  59. it( 'should create table with tbody and thead', () => {
  60. setModelData( model, modelTable( [
  61. [ '00' ],
  62. [ '10' ]
  63. ], { headingRows: 1 } ) );
  64. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  65. '<figure class="table">' +
  66. '<table>' +
  67. '<thead>' +
  68. '<tr><th>00</th></tr>' +
  69. '</thead>' +
  70. '<tbody>' +
  71. '<tr><td>10</td></tr>' +
  72. '</tbody>' +
  73. '</table>' +
  74. '</figure>'
  75. ) );
  76. } );
  77. it( 'should create table with thead', () => {
  78. setModelData( model, modelTable( [
  79. [ '00' ],
  80. [ '10' ]
  81. ], { headingRows: 2 } ) );
  82. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  83. '<figure class="table">' +
  84. '<table>' +
  85. '<thead>' +
  86. '<tr><th>00</th></tr>' +
  87. '<tr><th>10</th></tr>' +
  88. '</thead>' +
  89. '</table>' +
  90. '</figure>'
  91. ) );
  92. } );
  93. it( 'should create table with heading columns and rows', () => {
  94. setModelData( model, modelTable( [
  95. [ '00', '01', '02', '03' ],
  96. [ '10', '11', '12', '13' ]
  97. ], { headingColumns: 3, headingRows: 1 } ) );
  98. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  99. '<figure class="table">' +
  100. '<table>' +
  101. '<thead>' +
  102. '<tr><th>00</th><th>01</th><th>02</th><th>03</th></tr>' +
  103. '</thead>' +
  104. '<tbody>' +
  105. '<tr><th>10</th><th>11</th><th>12</th><td>13</td></tr>' +
  106. '</tbody>' +
  107. '</table>' +
  108. '</figure>'
  109. ) );
  110. } );
  111. it( 'should create table with block content', () => {
  112. setModelData( model, modelTable( [
  113. [ '<paragraph>00</paragraph><paragraph>foo</paragraph>', '01' ]
  114. ] ) );
  115. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  116. '<figure class="table">' +
  117. '<table>' +
  118. '<tbody>' +
  119. '<tr>' +
  120. '<td><p>00</p><p>foo</p></td>' +
  121. '<td>01</td>' +
  122. '</tr>' +
  123. '</tbody>' +
  124. '</table>' +
  125. '</figure>'
  126. ) );
  127. } );
  128. it( 'should be possible to overwrite', () => {
  129. editor.conversion.elementToElement( { model: 'tableRow', view: 'tr', converterPriority: 'high' } );
  130. editor.conversion.elementToElement( { model: 'tableCell', view: 'td', converterPriority: 'high' } );
  131. editor.conversion.for( 'downcast' ).add( dispatcher => {
  132. dispatcher.on( 'insert:table', ( evt, data, conversionApi ) => {
  133. conversionApi.consumable.consume( data.item, 'insert' );
  134. const tableElement = conversionApi.writer.createContainerElement( 'table', { foo: 'bar' } );
  135. const viewPosition = conversionApi.mapper.toViewPosition( data.range.start );
  136. conversionApi.mapper.bindElements( data.item, tableElement );
  137. conversionApi.writer.insert( viewPosition, tableElement );
  138. }, { priority: 'high' } );
  139. } );
  140. setModelData( model, modelTable( [ [ '' ] ] ) );
  141. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  142. '<table foo="bar">' +
  143. '<tr><td><p></p></td></tr>' +
  144. '</table>'
  145. ) );
  146. } );
  147. describe( 'headingColumns attribute', () => {
  148. it( 'should mark heading columns table cells', () => {
  149. setModelData( model, modelTable( [
  150. [ '00', '01', '02' ],
  151. [ '10', '11', '12' ]
  152. ], { headingColumns: 2 } ) );
  153. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  154. '<figure class="table">' +
  155. '<table>' +
  156. '<tbody>' +
  157. '<tr><th>00</th><th>01</th><td>02</td></tr>' +
  158. '<tr><th>10</th><th>11</th><td>12</td></tr>' +
  159. '</tbody>' +
  160. '</table>' +
  161. '</figure>'
  162. ) );
  163. } );
  164. it( 'should mark heading columns table cells when one has colspan attribute', () => {
  165. setModelData( model, modelTable( [
  166. [ '00', '01', '02', '03' ],
  167. [ { colspan: 2, contents: '10' }, '12', '13' ]
  168. ], { headingColumns: 3 } ) );
  169. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  170. '<figure class="table">' +
  171. '<table>' +
  172. '<tbody>' +
  173. '<tr><th>00</th><th>01</th><th>02</th><td>03</td></tr>' +
  174. '<tr><th colspan="2">10</th><th>12</th><td>13</td></tr>' +
  175. '</tbody>' +
  176. '</table>' +
  177. '</figure>'
  178. ) );
  179. } );
  180. it( 'should work with colspan and rowspan attributes on table cells', () => {
  181. // The table in this test looks like a table below:
  182. //
  183. // Row headings | Normal cells
  184. // |
  185. // +----+----+----+----+
  186. // | 00 | 01 | 02 | 03 |
  187. // | +----+ +----+
  188. // | | 11 | | 13 |
  189. // |----+----+ +----+
  190. // | 20 | | 23 |
  191. // | +----+----+
  192. // | | 32 | 33 |
  193. // +----+----+----+----+
  194. setModelData( model, modelTable( [
  195. [ { rowspan: 2, contents: '00' }, '01', { rowspan: 3, contents: '02' }, '03' ],
  196. [ '11', '13' ],
  197. [ { colspan: 2, rowspan: 2, contents: '20' }, '23' ],
  198. [ '32', '33' ]
  199. ], { headingColumns: 3 } ) );
  200. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  201. '<figure class="table">' +
  202. '<table>' +
  203. '<tbody>' +
  204. '<tr><th rowspan="2">00</th><th>01</th><th rowspan="3">02</th><td>03</td></tr>' +
  205. '<tr><th>11</th><td>13</td></tr>' +
  206. '<tr><th colspan="2" rowspan="2">20</th><td>23</td></tr>' +
  207. '<tr><th>32</th><td>33</td></tr>' +
  208. '</tbody>' +
  209. '</table>' +
  210. '</figure>'
  211. ) );
  212. } );
  213. } );
  214. describe( 'options.asWidget=true', () => {
  215. beforeEach( () => {
  216. return VirtualTestEditor.create()
  217. .then( newEditor => {
  218. editor = newEditor;
  219. model = editor.model;
  220. doc = model.document;
  221. root = doc.getRoot( 'main' );
  222. viewDocument = editor.editing.view;
  223. defaultSchema( model.schema );
  224. defaultConversion( editor.conversion, true );
  225. } );
  226. } );
  227. it( 'should create table as a widget', () => {
  228. setModelData( model, modelTable( [ [ '' ] ] ) );
  229. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  230. '<figure class="ck-widget ck-widget_selectable table" contenteditable="false">' +
  231. '<div class="ck ck-widget__selection-handler"></div>' +
  232. '<table>' +
  233. '<tbody>' +
  234. '<tr>' +
  235. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true"><span></span></td>' +
  236. '</tr>' +
  237. '</tbody>' +
  238. '</table>' +
  239. '</figure>'
  240. ) );
  241. } );
  242. } );
  243. } );
  244. describe( 'downcastInsertRow()', () => {
  245. it( 'should react to changed rows', () => {
  246. setModelData( model, modelTable( [
  247. [ '00', '01' ]
  248. ] ) );
  249. const table = root.getChild( 0 );
  250. model.change( writer => {
  251. const row = writer.createElement( 'tableRow' );
  252. writer.insert( row, table, 1 );
  253. writer.insertElement( 'tableCell', row, 'end' );
  254. writer.insertElement( 'tableCell', row, 'end' );
  255. } );
  256. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  257. [ '00', '01' ],
  258. [ '', '' ]
  259. ] ) );
  260. } );
  261. it( 'should properly consume already added rows', () => {
  262. setModelData( model, modelTable( [
  263. [ '00', '01' ]
  264. ] ) );
  265. const table = root.getChild( 0 );
  266. model.change( writer => {
  267. const row = writer.createElement( 'tableRow' );
  268. writer.insert( row, table, 1 );
  269. writer.insertElement( 'tableCell', row, 'end' );
  270. writer.insertElement( 'tableCell', row, 'end' );
  271. } );
  272. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  273. [ '00', '01' ],
  274. [ '', '' ]
  275. ] ) );
  276. model.change( writer => {
  277. const row = writer.createElement( 'tableRow' );
  278. writer.insert( row, table, 2 );
  279. writer.insertElement( 'tableCell', row, 'end' );
  280. writer.insertElement( 'tableCell', row, 'end' );
  281. } );
  282. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  283. [ '00', '01' ],
  284. [ '', '' ],
  285. [ '', '' ]
  286. ] ) );
  287. } );
  288. it( 'should insert row on proper index', () => {
  289. setModelData( model, modelTable( [
  290. [ '00', '01' ],
  291. [ '21', '22' ],
  292. [ '31', '32' ]
  293. ] ) );
  294. const table = root.getChild( 0 );
  295. model.change( writer => {
  296. const row = writer.createElement( 'tableRow' );
  297. writer.insert( row, table, 1 );
  298. writer.insertElement( 'tableCell', row, 'end' );
  299. writer.insertElement( 'tableCell', row, 'end' );
  300. } );
  301. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  302. [ '00', '01' ],
  303. [ '', '' ],
  304. [ '21', '22' ],
  305. [ '31', '32' ]
  306. ] ) );
  307. } );
  308. it( 'should insert row on proper index when table has heading rows defined - insert in body', () => {
  309. setModelData( model, modelTable( [
  310. [ '00', '01' ],
  311. [ '21', '22' ],
  312. [ '31', '32' ]
  313. ], { headingRows: 1 } ) );
  314. const table = root.getChild( 0 );
  315. model.change( writer => {
  316. const row = writer.createElement( 'tableRow' );
  317. writer.insert( row, table, 1 );
  318. writer.insertElement( 'tableCell', row, 'end' );
  319. writer.insertElement( 'tableCell', row, 'end' );
  320. } );
  321. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  322. [ '00', '01' ],
  323. [ '', '' ],
  324. [ '21', '22' ],
  325. [ '31', '32' ]
  326. ], { headingRows: 1 } ) );
  327. } );
  328. it( 'should insert row on proper index when table has heading rows defined - insert in heading', () => {
  329. setModelData( model, modelTable( [
  330. [ '00', '01' ],
  331. [ '21', '22' ],
  332. [ '31', '32' ]
  333. ], { headingRows: 2 } ) );
  334. const table = root.getChild( 0 );
  335. model.change( writer => {
  336. const row = writer.createElement( 'tableRow' );
  337. writer.insert( row, table, 1 );
  338. writer.insertElement( 'tableCell', row, 'end' );
  339. writer.insertElement( 'tableCell', row, 'end' );
  340. } );
  341. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  342. [ '00', '01' ],
  343. [ '', '' ],
  344. [ '21', '22' ],
  345. [ '31', '32' ]
  346. ], { headingRows: 3 } ) );
  347. } );
  348. it( 'should react to changed rows when previous rows\' cells has rowspans', () => {
  349. setModelData( model, modelTable( [
  350. [ { rowspan: 3, contents: '00' }, '01' ],
  351. [ '22' ]
  352. ] ) );
  353. const table = root.getChild( 0 );
  354. model.change( writer => {
  355. const row = writer.createElement( 'tableRow' );
  356. writer.insert( row, table, 2 );
  357. writer.insertElement( 'tableCell', row, 'end' );
  358. } );
  359. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  360. [ { rowspan: 3, contents: '00' }, '01' ],
  361. [ '22' ],
  362. [ '' ]
  363. ] ) );
  364. } );
  365. it( 'should properly create row headings', () => {
  366. setModelData( model, modelTable( [
  367. [ { rowspan: 3, contents: '00' }, '01' ],
  368. [ '22' ]
  369. ], { headingColumns: 1 } ) );
  370. const table = root.getChild( 0 );
  371. model.change( writer => {
  372. const firstRow = writer.createElement( 'tableRow' );
  373. writer.insert( firstRow, table, 2 );
  374. writer.insert( writer.createElement( 'tableCell' ), firstRow, 'end' );
  375. const secondRow = writer.createElement( 'tableRow' );
  376. writer.insert( secondRow, table, 3 );
  377. writer.insert( writer.createElement( 'tableCell' ), secondRow, 'end' );
  378. writer.insert( writer.createElement( 'tableCell' ), secondRow, 'end' );
  379. } );
  380. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  381. [ { rowspan: 3, contents: '00', isHeading: true }, '01' ],
  382. [ '22' ],
  383. [ '' ],
  384. [ { contents: '', isHeading: true }, '' ]
  385. ] ) );
  386. } );
  387. describe( 'options.asWidget=true', () => {
  388. beforeEach( () => {
  389. return VirtualTestEditor.create()
  390. .then( newEditor => {
  391. editor = newEditor;
  392. model = editor.model;
  393. doc = model.document;
  394. root = doc.getRoot( 'main' );
  395. viewDocument = editor.editing.view;
  396. defaultSchema( model.schema );
  397. defaultConversion( editor.conversion, true );
  398. } );
  399. } );
  400. it( 'should create table cell inside inserted row as a widget', () => {
  401. setModelData( model, modelTable( [ [ '00' ] ] ) );
  402. const table = root.getChild( 0 );
  403. model.change( writer => {
  404. const firstRow = writer.createElement( 'tableRow' );
  405. writer.insert( firstRow, table, 1 );
  406. writer.insert( writer.createElement( 'tableCell' ), firstRow, 'end' );
  407. } );
  408. // TODO check span always?
  409. expect( formatTable(
  410. getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  411. '<figure class="ck-widget ck-widget_selectable table" contenteditable="false">' +
  412. '<div class="ck ck-widget__selection-handler"></div>' +
  413. '<table>' +
  414. '<tbody>' +
  415. '<tr>' +
  416. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  417. '<span>00</span>' +
  418. '</td>' +
  419. '</tr>' +
  420. '<tr>' +
  421. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true"></td>' +
  422. '</tr>' +
  423. '</tbody>' +
  424. '</table>' +
  425. '</figure>'
  426. ) );
  427. } );
  428. } );
  429. } );
  430. describe( 'downcastInsertCell()', () => {
  431. it( 'should add tableCell on proper index in tr', () => {
  432. setModelData( model, modelTable( [
  433. [ '00', '01' ]
  434. ] ) );
  435. const table = root.getChild( 0 );
  436. model.change( writer => {
  437. const row = table.getChild( 0 );
  438. writer.insertElement( 'tableCell', row, 1 );
  439. } );
  440. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  441. [ '00', '', '01' ]
  442. ] ) );
  443. } );
  444. it( 'should add tableCell on proper index in tr when previous have colspans', () => {
  445. setModelData( model, modelTable( [
  446. [ { colspan: 2, contents: '00' }, '13' ]
  447. ] ) );
  448. const table = root.getChild( 0 );
  449. model.change( writer => {
  450. const row = table.getChild( 0 );
  451. writer.insertElement( 'tableCell', row, 1 );
  452. } );
  453. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  454. [ { colspan: 2, contents: '00' }, '', '13' ]
  455. ] ) );
  456. } );
  457. it( 'should add tableCell on proper index in tr when previous row have rowspans', () => {
  458. setModelData( model, modelTable( [
  459. [ { rowspan: 2, contents: '00' }, '13' ],
  460. [ '11', '12' ]
  461. ] ) );
  462. const table = root.getChild( 0 );
  463. model.change( writer => {
  464. writer.insertElement( 'tableCell', table.getChild( 0 ), 1 );
  465. writer.insertElement( 'tableCell', table.getChild( 1 ), 0 );
  466. } );
  467. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  468. [ { rowspan: 2, contents: '00' }, '', '13' ],
  469. [ '', '11', '12' ]
  470. ] ) );
  471. } );
  472. it( 'split cell simulation - simple', () => {
  473. setModelData( model, modelTable( [
  474. [ '00', '01' ],
  475. [ '10', '11' ]
  476. ] ) );
  477. const table = root.getChild( 0 );
  478. model.change( writer => {
  479. const firstRow = table.getChild( 0 );
  480. const secondRow = table.getChild( 1 );
  481. writer.insertElement( 'tableCell', firstRow, 1 );
  482. writer.setAttribute( 'colspan', 2, secondRow.getChild( 0 ) );
  483. } );
  484. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  485. [ '00', '', '01' ],
  486. [ { colspan: 2, contents: '10' }, '11' ]
  487. ] ) );
  488. } );
  489. it( 'merge simulation - simple', () => {
  490. setModelData( model, modelTable( [
  491. [ '00', '01' ],
  492. [ '10', '11' ]
  493. ] ) );
  494. const table = root.getChild( 0 );
  495. model.change( writer => {
  496. const firstRow = table.getChild( 0 );
  497. writer.setAttribute( 'colspan', 2, firstRow.getChild( 0 ) );
  498. writer.remove( firstRow.getChild( 1 ) );
  499. } );
  500. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  501. [ { colspan: 2, contents: '00' } ],
  502. [ '10', '11' ]
  503. ] ) );
  504. } );
  505. describe( 'options.asWidget=true', () => {
  506. beforeEach( () => {
  507. return VirtualTestEditor.create()
  508. .then( newEditor => {
  509. editor = newEditor;
  510. model = editor.model;
  511. doc = model.document;
  512. root = doc.getRoot( 'main' );
  513. viewDocument = editor.editing.view;
  514. defaultSchema( model.schema );
  515. defaultConversion( editor.conversion, true );
  516. } );
  517. } );
  518. it( 'should create inserted table cell as a widget', () => {
  519. setModelData( model, modelTable( [ [ '00' ] ] ) );
  520. const table = root.getChild( 0 );
  521. model.change( writer => {
  522. const row = table.getChild( 0 );
  523. writer.insert( writer.createElement( 'tableCell' ), row, 'end' );
  524. } );
  525. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  526. '<figure class="ck-widget ck-widget_selectable table" contenteditable="false">' +
  527. '<div class="ck ck-widget__selection-handler"></div>' +
  528. '<table>' +
  529. '<tbody>' +
  530. '<tr>' +
  531. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  532. '<span>00</span>' +
  533. '</td>' +
  534. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true"></td>' +
  535. '</tr>' +
  536. '</tbody>' +
  537. '</table>' +
  538. '</figure>'
  539. ) );
  540. } );
  541. } );
  542. } );
  543. describe( 'downcastTableHeadingColumnsChange()', () => {
  544. it( 'should work for adding heading columns', () => {
  545. setModelData( model, modelTable( [
  546. [ '00', '01' ],
  547. [ '10', '11' ]
  548. ] ) );
  549. const table = root.getChild( 0 );
  550. model.change( writer => {
  551. writer.setAttribute( 'headingColumns', 1, table );
  552. } );
  553. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  554. [ { isHeading: true, contents: '00' }, '01' ],
  555. [ { isHeading: true, contents: '10' }, '11' ]
  556. ], { headingColumns: 1 } ) );
  557. } );
  558. it( 'should work for changing heading columns to a bigger number', () => {
  559. setModelData( model, modelTable( [
  560. [ '00', '01', '02', '03' ],
  561. [ '10', '11', '12', '13' ]
  562. ], { headingColumns: 1 } ) );
  563. const table = root.getChild( 0 );
  564. model.change( writer => {
  565. writer.setAttribute( 'headingColumns', 3, table );
  566. } );
  567. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  568. [ { isHeading: true, contents: '00' }, { isHeading: true, contents: '01' }, { isHeading: true, contents: '02' }, '03' ],
  569. [ { isHeading: true, contents: '10' }, { isHeading: true, contents: '11' }, { isHeading: true, contents: '12' }, '13' ]
  570. ] ) );
  571. } );
  572. it( 'should work for changing heading columns to a smaller number', () => {
  573. setModelData( model, modelTable( [
  574. [ { isHeading: true, contents: '00' }, { isHeading: true, contents: '01' }, { isHeading: true, contents: '02' }, '03' ],
  575. [ { isHeading: true, contents: '10' }, { isHeading: true, contents: '11' }, { isHeading: true, contents: '12' }, '13' ]
  576. ], { headingColumns: 3 } ) );
  577. const table = root.getChild( 0 );
  578. model.change( writer => {
  579. writer.setAttribute( 'headingColumns', 1, table );
  580. } );
  581. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  582. [ { isHeading: true, contents: '00' }, '01', '02', '03' ],
  583. [ { isHeading: true, contents: '10' }, '11', '12', '13' ]
  584. ], { headingColumns: 3 } ) );
  585. } );
  586. it( 'should work for removing heading columns', () => {
  587. setModelData( model, modelTable( [
  588. [ '00', '01' ],
  589. [ '10', '11' ]
  590. ], { headingColumns: 1 } ) );
  591. const table = root.getChild( 0 );
  592. model.change( writer => {
  593. writer.removeAttribute( 'headingColumns', table );
  594. } );
  595. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  596. [ '00', '01' ],
  597. [ '10', '11' ]
  598. ] ) );
  599. } );
  600. it( 'should be possible to overwrite', () => {
  601. editor.conversion.attributeToAttribute( { model: 'headingColumns', view: 'headingColumns', converterPriority: 'high' } );
  602. setModelData( model, modelTable( [ [ '00' ] ] ) );
  603. const table = root.getChild( 0 );
  604. model.change( writer => {
  605. writer.setAttribute( 'headingColumns', 1, table );
  606. } );
  607. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  608. '<figure class="table" headingColumns="1">' +
  609. '<table>' +
  610. '<tbody>' +
  611. '<tr><td>00</td></tr>' +
  612. '</tbody>' +
  613. '</table>' +
  614. '</figure>'
  615. ) );
  616. } );
  617. it( 'should work with adding table cells', () => {
  618. setModelData( model, modelTable( [
  619. [ { rowspan: 2, contents: '00' }, '01', '13', '14' ],
  620. [ '11', '12', '13' ],
  621. [ { colspan: 2, contents: '20' }, '22', '23' ]
  622. ], { headingColumns: 2 } ) );
  623. const table = root.getChild( 0 );
  624. model.change( writer => {
  625. // Inserting column in heading columns so update table's attribute also
  626. writer.setAttribute( 'headingColumns', 3, table );
  627. writer.insertElement( 'tableCell', table.getChild( 0 ), 2 );
  628. writer.insertElement( 'tableCell', table.getChild( 1 ), 1 );
  629. writer.insertElement( 'tableCell', table.getChild( 2 ), 1 );
  630. } );
  631. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  632. [
  633. { isHeading: true, rowspan: 2, contents: '00' },
  634. { isHeading: true, contents: '01' },
  635. { isHeading: true, contents: '' },
  636. '13',
  637. '14'
  638. ],
  639. [
  640. { isHeading: true, contents: '11' },
  641. { isHeading: true, contents: '' },
  642. '12',
  643. '13'
  644. ],
  645. [
  646. { isHeading: true, colspan: 2, contents: '20' },
  647. { isHeading: true, contents: '' },
  648. '22',
  649. '23'
  650. ]
  651. ] ) );
  652. } );
  653. describe( 'options.asWidget=true', () => {
  654. beforeEach( () => {
  655. return VirtualTestEditor.create()
  656. .then( newEditor => {
  657. editor = newEditor;
  658. model = editor.model;
  659. doc = model.document;
  660. root = doc.getRoot( 'main' );
  661. viewDocument = editor.editing.view;
  662. defaultSchema( model.schema );
  663. defaultConversion( editor.conversion, true );
  664. } );
  665. } );
  666. it( 'should create renamed cell as a widget', () => {
  667. setModelData( model, modelTable( [ [ '00' ] ] ) );
  668. const table = root.getChild( 0 );
  669. model.change( writer => {
  670. writer.setAttribute( 'headingRows', 1, table );
  671. } );
  672. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  673. '<figure class="ck-widget ck-widget_selectable table" contenteditable="false">' +
  674. '<div class="ck ck-widget__selection-handler"></div>' +
  675. '<table>' +
  676. '<thead>' +
  677. '<tr>' +
  678. '<th class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  679. '<span>00</span>' +
  680. '</th>' +
  681. '</tr>' +
  682. '</thead>' +
  683. '</table>' +
  684. '</figure>'
  685. ) );
  686. } );
  687. } );
  688. } );
  689. describe( 'downcastTableHeadingRowsChange()', () => {
  690. it( 'should work for adding heading rows', () => {
  691. setModelData( model, modelTable( [
  692. [ '00', '01' ],
  693. [ '10', '11' ],
  694. [ '20', '21' ]
  695. ] ) );
  696. const table = root.getChild( 0 );
  697. model.change( writer => {
  698. writer.setAttribute( 'headingRows', 2, table );
  699. } );
  700. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  701. [ '00', '01' ],
  702. [ '10', '11' ],
  703. [ '20', '21' ]
  704. ], { headingRows: 2 } ) );
  705. } );
  706. it( 'should work for changing number of heading rows to a bigger number', () => {
  707. setModelData( model, modelTable( [
  708. [ '00', '01' ],
  709. [ '10', '11' ],
  710. [ '20', '21' ]
  711. ], { headingRows: 1 } ) );
  712. const table = root.getChild( 0 );
  713. model.change( writer => {
  714. writer.setAttribute( 'headingRows', 2, table );
  715. } );
  716. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  717. [ '00', '01' ],
  718. [ '10', '11' ],
  719. [ '20', '21' ]
  720. ], { headingRows: 2 } ) );
  721. } );
  722. it( 'should work for changing number of heading rows to a smaller number', () => {
  723. setModelData( model, modelTable( [
  724. [ '00', '01' ],
  725. [ '10', '11' ],
  726. [ '20', '21' ],
  727. [ '30', '31' ]
  728. ], { headingRows: 3 } ) );
  729. const table = root.getChild( 0 );
  730. model.change( writer => {
  731. writer.setAttribute( 'headingRows', 2, table );
  732. } );
  733. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  734. [ '00', '01' ],
  735. [ '10', '11' ],
  736. [ '20', '21' ],
  737. [ '30', '31' ]
  738. ], { headingRows: 2 } ) );
  739. } );
  740. it( 'should work for removing heading rows', () => {
  741. setModelData( model, modelTable( [
  742. [ '00', '01' ],
  743. [ '10', '11' ]
  744. ], { headingRows: 2 } ) );
  745. const table = root.getChild( 0 );
  746. model.change( writer => {
  747. writer.removeAttribute( 'headingRows', table );
  748. } );
  749. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  750. [ '00', '01' ],
  751. [ '10', '11' ]
  752. ] ) );
  753. } );
  754. it( 'should work for making heading rows without tbody', () => {
  755. setModelData( model, modelTable( [
  756. [ '00', '01' ],
  757. [ '10', '11' ]
  758. ] ) );
  759. const table = root.getChild( 0 );
  760. model.change( writer => {
  761. writer.setAttribute( 'headingRows', 2, table );
  762. } );
  763. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  764. [ '00', '01' ],
  765. [ '10', '11' ]
  766. ], { headingRows: 2 } ) );
  767. } );
  768. it( 'should be possible to overwrite', () => {
  769. editor.conversion.attributeToAttribute( { model: 'headingRows', view: 'headingRows', converterPriority: 'high' } );
  770. setModelData( model, modelTable( [ [ '00' ] ] ) );
  771. const table = root.getChild( 0 );
  772. model.change( writer => {
  773. writer.setAttribute( 'headingRows', 1, table );
  774. } );
  775. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  776. '<figure class="table" headingRows="1">' +
  777. '<table>' +
  778. '<tbody>' +
  779. '<tr><td>00</td></tr>' +
  780. '</tbody>' +
  781. '</table>' +
  782. '</figure>'
  783. ) );
  784. } );
  785. it( 'should work with adding table rows at the beginning of a table', () => {
  786. setModelData( model, modelTable( [
  787. [ '00', '01' ],
  788. [ '10', '11' ]
  789. ], { headingRows: 1 } ) );
  790. const table = root.getChild( 0 );
  791. model.change( writer => {
  792. writer.setAttribute( 'headingRows', 2, table );
  793. const tableRow = writer.createElement( 'tableRow' );
  794. writer.insert( tableRow, table, 0 );
  795. writer.insertElement( 'tableCell', tableRow, 'end' );
  796. writer.insertElement( 'tableCell', tableRow, 'end' );
  797. } );
  798. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  799. [ '', '' ],
  800. [ '00', '01' ],
  801. [ '10', '11' ]
  802. ], { headingRows: 2 } ) );
  803. } );
  804. describe( 'options.asWidget=true', () => {
  805. beforeEach( () => {
  806. return VirtualTestEditor.create()
  807. .then( newEditor => {
  808. editor = newEditor;
  809. model = editor.model;
  810. doc = model.document;
  811. root = doc.getRoot( 'main' );
  812. viewDocument = editor.editing.view;
  813. defaultSchema( model.schema );
  814. defaultConversion( editor.conversion, true );
  815. } );
  816. } );
  817. it( 'should create renamed cell as a widget', () => {
  818. setModelData( model, modelTable( [ [ '00' ] ] ) );
  819. const table = root.getChild( 0 );
  820. model.change( writer => {
  821. writer.setAttribute( 'headingColumns', 1, table );
  822. } );
  823. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  824. '<figure class="ck-widget ck-widget_selectable table" contenteditable="false">' +
  825. '<div class="ck ck-widget__selection-handler"></div>' +
  826. '<table>' +
  827. '<tbody>' +
  828. '<tr>' +
  829. '<th class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  830. '<span>00</span>' +
  831. '</th>' +
  832. '</tr>' +
  833. '</tbody>' +
  834. '</table>' +
  835. '</figure>'
  836. ) );
  837. } );
  838. } );
  839. } );
  840. describe( 'downcastRemoveRow()', () => {
  841. it( 'should react to removed row from the beginning of a tbody', () => {
  842. setModelData( model, modelTable( [
  843. [ '00[]', '01' ],
  844. [ '10', '11' ]
  845. ] ) );
  846. const table = root.getChild( 0 );
  847. model.change( writer => {
  848. writer.remove( table.getChild( 1 ) );
  849. } );
  850. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  851. [ '00', '01' ]
  852. ] ) );
  853. } );
  854. it( 'should react to removed row form the end of a tbody', () => {
  855. setModelData( model, modelTable( [
  856. [ '00[]', '01' ],
  857. [ '10', '11' ]
  858. ] ) );
  859. const table = root.getChild( 0 );
  860. model.change( writer => {
  861. writer.remove( table.getChild( 0 ) );
  862. } );
  863. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  864. [ '10', '11' ]
  865. ] ) );
  866. } );
  867. it( 'should react to removed row from the beginning of a thead', () => {
  868. setModelData( model, modelTable( [
  869. [ '00[]', '01' ],
  870. [ '10', '11' ]
  871. ], { headingRows: 2 } ) );
  872. const table = root.getChild( 0 );
  873. model.change( writer => {
  874. writer.remove( table.getChild( 1 ) );
  875. } );
  876. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  877. [ '00', '01' ]
  878. ], { headingRows: 2 } ) );
  879. } );
  880. it( 'should react to removed row form the end of a thead', () => {
  881. setModelData( model, modelTable( [
  882. [ '00[]', '01' ],
  883. [ '10', '11' ]
  884. ], { headingRows: 2 } ) );
  885. const table = root.getChild( 0 );
  886. model.change( writer => {
  887. writer.remove( table.getChild( 0 ) );
  888. } );
  889. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  890. [ '10', '11' ]
  891. ], { headingRows: 2 } ) );
  892. } );
  893. it( 'should remove empty thead section if a last row was removed from thead', () => {
  894. setModelData( model, modelTable( [
  895. [ '00[]', '01' ],
  896. [ '10', '11' ]
  897. ], { headingRows: 1 } ) );
  898. const table = root.getChild( 0 );
  899. model.change( writer => {
  900. writer.setAttribute( 'headingRows', 0, table );
  901. writer.remove( table.getChild( 0 ) );
  902. } );
  903. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  904. [ '10', '11' ]
  905. ] ) );
  906. } );
  907. it( 'should remove empty tbody section if a last row was removed from tbody', () => {
  908. setModelData( model, modelTable( [
  909. [ '00[]', '01' ],
  910. [ '10', '11' ]
  911. ], { headingRows: 1 } ) );
  912. const table = root.getChild( 0 );
  913. model.change( writer => {
  914. writer.remove( table.getChild( 1 ) );
  915. } );
  916. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  917. [ '00', '01' ]
  918. ], { headingRows: 1 } ) );
  919. } );
  920. } );
  921. describe( 'options.asWidget=true', () => {
  922. beforeEach( () => {
  923. return VirtualTestEditor.create()
  924. .then( newEditor => {
  925. editor = newEditor;
  926. model = editor.model;
  927. doc = model.document;
  928. root = doc.getRoot( 'main' );
  929. viewDocument = editor.editing.view;
  930. defaultSchema( model.schema );
  931. defaultConversion( editor.conversion, true );
  932. editor.conversion.for( 'downcast' ).add( paragraphInTableCell() );
  933. } );
  934. } );
  935. it( 'should rename <span> to <p> when more then one block content inside table cell', () => {
  936. setModelData( model, modelTable( [
  937. [ '00[]' ]
  938. ] ) );
  939. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  940. [ '00' ]
  941. ], { asWidget: true } ) );
  942. const table = root.getChild( 0 );
  943. model.change( writer => {
  944. const nodeByPath = table.getNodeByPath( [ 0, 0, 0 ] );
  945. const paragraph = writer.createElement( 'paragraph' );
  946. writer.insert( paragraph, nodeByPath, 'after' );
  947. writer.setSelection( nodeByPath.nextSibling, 0 );
  948. } );
  949. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  950. [ '<p>00</p><p></p>' ]
  951. ], { asWidget: true } ) );
  952. } );
  953. } );
  954. } );