8
0

downcast.js 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. /**
  2. * @license Copyright (c) 2003-2019, 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( 'p', viewElement );
  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. it( 'should re-create table on reinsert', () => {
  148. model.schema.register( 'wrapper', {
  149. allowWhere: '$block',
  150. allowContentOf: '$root'
  151. } );
  152. editor.conversion.elementToElement( { model: 'wrapper', view: 'div' } );
  153. setModelData( model, modelTable( [ [ '[]' ] ] ) );
  154. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  155. '<figure class="table">' +
  156. '<table>' +
  157. '<tbody>' +
  158. '<tr><td></td></tr>' +
  159. '</tbody>' +
  160. '</table>' +
  161. '</figure>'
  162. ) );
  163. model.change( writer => {
  164. const table = model.document.getRoot().getChild( 0 );
  165. const range = writer.createRange( writer.createPositionBefore( table ), writer.createPositionAfter( table ) );
  166. const wrapper = writer.createElement( 'wrapper' );
  167. writer.wrap( range, wrapper );
  168. } );
  169. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  170. '<div>' +
  171. '<figure class="table">' +
  172. '<table>' +
  173. '<tbody>' +
  174. '<tr><td></td></tr>' +
  175. '</tbody>' +
  176. '</table>' +
  177. '</figure>' +
  178. '</div>'
  179. ) );
  180. } );
  181. describe( 'headingColumns attribute', () => {
  182. it( 'should mark heading columns table cells', () => {
  183. setModelData( model, modelTable( [
  184. [ '00', '01', '02' ],
  185. [ '10', '11', '12' ]
  186. ], { headingColumns: 2 } ) );
  187. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  188. '<figure class="table">' +
  189. '<table>' +
  190. '<tbody>' +
  191. '<tr><th>00</th><th>01</th><td>02</td></tr>' +
  192. '<tr><th>10</th><th>11</th><td>12</td></tr>' +
  193. '</tbody>' +
  194. '</table>' +
  195. '</figure>'
  196. ) );
  197. } );
  198. it( 'should mark heading columns table cells when one has colspan attribute', () => {
  199. setModelData( model, modelTable( [
  200. [ '00', '01', '02', '03' ],
  201. [ { colspan: 2, contents: '10' }, '12', '13' ]
  202. ], { headingColumns: 3 } ) );
  203. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  204. '<figure class="table">' +
  205. '<table>' +
  206. '<tbody>' +
  207. '<tr><th>00</th><th>01</th><th>02</th><td>03</td></tr>' +
  208. '<tr><th colspan="2">10</th><th>12</th><td>13</td></tr>' +
  209. '</tbody>' +
  210. '</table>' +
  211. '</figure>'
  212. ) );
  213. } );
  214. it( 'should work with colspan and rowspan attributes on table cells', () => {
  215. // The table in this test looks like a table below:
  216. //
  217. // Row headings | Normal cells
  218. // |
  219. // +----+----+----+----+
  220. // | 00 | 01 | 02 | 03 |
  221. // | +----+ +----+
  222. // | | 11 | | 13 |
  223. // |----+----+ +----+
  224. // | 20 | | 23 |
  225. // | +----+----+
  226. // | | 32 | 33 |
  227. // +----+----+----+----+
  228. setModelData( model, modelTable( [
  229. [ { rowspan: 2, contents: '00' }, '01', { rowspan: 3, contents: '02' }, '03' ],
  230. [ '11', '13' ],
  231. [ { colspan: 2, rowspan: 2, contents: '20' }, '23' ],
  232. [ '32', '33' ]
  233. ], { headingColumns: 3 } ) );
  234. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  235. '<figure class="table">' +
  236. '<table>' +
  237. '<tbody>' +
  238. '<tr><th rowspan="2">00</th><th>01</th><th rowspan="3">02</th><td>03</td></tr>' +
  239. '<tr><th>11</th><td>13</td></tr>' +
  240. '<tr><th colspan="2" rowspan="2">20</th><td>23</td></tr>' +
  241. '<tr><th>32</th><td>33</td></tr>' +
  242. '</tbody>' +
  243. '</table>' +
  244. '</figure>'
  245. ) );
  246. } );
  247. } );
  248. describe( 'options.asWidget=true', () => {
  249. beforeEach( () => {
  250. return VirtualTestEditor.create()
  251. .then( newEditor => {
  252. editor = newEditor;
  253. model = editor.model;
  254. doc = model.document;
  255. root = doc.getRoot( 'main' );
  256. viewDocument = editor.editing.view;
  257. defaultSchema( model.schema );
  258. defaultConversion( editor.conversion, true );
  259. } );
  260. } );
  261. it( 'should create table as a widget', () => {
  262. setModelData( model, modelTable( [ [ '' ] ] ) );
  263. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  264. '<figure class="ck-widget ck-widget_with-selection-handler table" contenteditable="false">' +
  265. '<div class="ck ck-widget__selection-handler"></div>' +
  266. '<table>' +
  267. '<tbody>' +
  268. '<tr>' +
  269. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true"><span></span></td>' +
  270. '</tr>' +
  271. '</tbody>' +
  272. '</table>' +
  273. '</figure>'
  274. ) );
  275. } );
  276. } );
  277. } );
  278. describe( 'downcastInsertRow()', () => {
  279. it( 'should react to changed rows', () => {
  280. setModelData( model, modelTable( [
  281. [ '00', '01' ]
  282. ] ) );
  283. const table = root.getChild( 0 );
  284. model.change( writer => {
  285. const row = writer.createElement( 'tableRow' );
  286. writer.insert( row, table, 1 );
  287. writer.insertElement( 'tableCell', row, 'end' );
  288. writer.insertElement( 'tableCell', row, 'end' );
  289. } );
  290. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  291. [ '00', '01' ],
  292. [ '', '' ]
  293. ] ) );
  294. } );
  295. it( 'should properly consume already added rows', () => {
  296. setModelData( model, modelTable( [
  297. [ '00', '01' ]
  298. ] ) );
  299. const table = root.getChild( 0 );
  300. model.change( writer => {
  301. const row = writer.createElement( 'tableRow' );
  302. writer.insert( row, table, 1 );
  303. writer.insertElement( 'tableCell', row, 'end' );
  304. writer.insertElement( 'tableCell', row, 'end' );
  305. } );
  306. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  307. [ '00', '01' ],
  308. [ '', '' ]
  309. ] ) );
  310. model.change( writer => {
  311. const row = writer.createElement( 'tableRow' );
  312. writer.insert( row, table, 2 );
  313. writer.insertElement( 'tableCell', row, 'end' );
  314. writer.insertElement( 'tableCell', row, 'end' );
  315. } );
  316. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  317. [ '00', '01' ],
  318. [ '', '' ],
  319. [ '', '' ]
  320. ] ) );
  321. } );
  322. it( 'should insert row on proper index', () => {
  323. setModelData( model, modelTable( [
  324. [ '00', '01' ],
  325. [ '21', '22' ],
  326. [ '31', '32' ]
  327. ] ) );
  328. const table = root.getChild( 0 );
  329. model.change( writer => {
  330. const row = writer.createElement( 'tableRow' );
  331. writer.insert( row, table, 1 );
  332. writer.insertElement( 'tableCell', row, 'end' );
  333. writer.insertElement( 'tableCell', row, 'end' );
  334. } );
  335. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  336. [ '00', '01' ],
  337. [ '', '' ],
  338. [ '21', '22' ],
  339. [ '31', '32' ]
  340. ] ) );
  341. } );
  342. it( 'should insert row on proper index when table has heading rows defined - insert in body', () => {
  343. setModelData( model, modelTable( [
  344. [ '00', '01' ],
  345. [ '21', '22' ],
  346. [ '31', '32' ]
  347. ], { headingRows: 1 } ) );
  348. const table = root.getChild( 0 );
  349. model.change( writer => {
  350. const row = writer.createElement( 'tableRow' );
  351. writer.insert( row, table, 1 );
  352. writer.insertElement( 'tableCell', row, 'end' );
  353. writer.insertElement( 'tableCell', row, 'end' );
  354. } );
  355. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  356. [ '00', '01' ],
  357. [ '', '' ],
  358. [ '21', '22' ],
  359. [ '31', '32' ]
  360. ], { headingRows: 1 } ) );
  361. } );
  362. it( 'should insert row on proper index when table has heading rows defined - insert in heading', () => {
  363. setModelData( model, modelTable( [
  364. [ '00', '01' ],
  365. [ '21', '22' ],
  366. [ '31', '32' ]
  367. ], { headingRows: 2 } ) );
  368. const table = root.getChild( 0 );
  369. model.change( writer => {
  370. const row = writer.createElement( 'tableRow' );
  371. writer.insert( row, table, 1 );
  372. writer.insertElement( 'tableCell', row, 'end' );
  373. writer.insertElement( 'tableCell', row, 'end' );
  374. } );
  375. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  376. [ '00', '01' ],
  377. [ '', '' ],
  378. [ '21', '22' ],
  379. [ '31', '32' ]
  380. ], { headingRows: 3 } ) );
  381. } );
  382. it( 'should react to changed rows when previous rows\' cells has rowspans', () => {
  383. setModelData( model, modelTable( [
  384. [ { rowspan: 3, contents: '00' }, '01' ],
  385. [ '22' ]
  386. ] ) );
  387. const table = root.getChild( 0 );
  388. model.change( writer => {
  389. const row = writer.createElement( 'tableRow' );
  390. writer.insert( row, table, 2 );
  391. writer.insertElement( 'tableCell', row, 'end' );
  392. } );
  393. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  394. [ { rowspan: 3, contents: '00' }, '01' ],
  395. [ '22' ],
  396. [ '' ]
  397. ] ) );
  398. } );
  399. it( 'should properly create row headings', () => {
  400. setModelData( model, modelTable( [
  401. [ { rowspan: 3, contents: '00' }, '01' ],
  402. [ '22' ]
  403. ], { headingColumns: 1 } ) );
  404. const table = root.getChild( 0 );
  405. model.change( writer => {
  406. const firstRow = writer.createElement( 'tableRow' );
  407. writer.insert( firstRow, table, 2 );
  408. writer.insert( writer.createElement( 'tableCell' ), firstRow, 'end' );
  409. const secondRow = writer.createElement( 'tableRow' );
  410. writer.insert( secondRow, table, 3 );
  411. writer.insert( writer.createElement( 'tableCell' ), secondRow, 'end' );
  412. writer.insert( writer.createElement( 'tableCell' ), secondRow, 'end' );
  413. } );
  414. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  415. [ { rowspan: 3, contents: '00', isHeading: true }, '01' ],
  416. [ '22' ],
  417. [ '' ],
  418. [ { contents: '', isHeading: true }, '' ]
  419. ] ) );
  420. } );
  421. describe( 'options.asWidget=true', () => {
  422. beforeEach( () => {
  423. return VirtualTestEditor.create()
  424. .then( newEditor => {
  425. editor = newEditor;
  426. model = editor.model;
  427. doc = model.document;
  428. root = doc.getRoot( 'main' );
  429. viewDocument = editor.editing.view;
  430. defaultSchema( model.schema );
  431. defaultConversion( editor.conversion, true );
  432. } );
  433. } );
  434. it( 'should create table cell inside inserted row as a widget', () => {
  435. setModelData( model, modelTable( [ [ '00' ] ] ) );
  436. const table = root.getChild( 0 );
  437. model.change( writer => {
  438. const firstRow = writer.createElement( 'tableRow' );
  439. writer.insert( firstRow, table, 1 );
  440. writer.insert( writer.createElement( 'tableCell' ), firstRow, 'end' );
  441. } );
  442. expect( formatTable(
  443. getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  444. '<figure class="ck-widget ck-widget_with-selection-handler table" contenteditable="false">' +
  445. '<div class="ck ck-widget__selection-handler"></div>' +
  446. '<table>' +
  447. '<tbody>' +
  448. '<tr>' +
  449. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  450. '<span>00</span>' +
  451. '</td>' +
  452. '</tr>' +
  453. '<tr>' +
  454. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true"></td>' +
  455. '</tr>' +
  456. '</tbody>' +
  457. '</table>' +
  458. '</figure>'
  459. ) );
  460. } );
  461. } );
  462. } );
  463. describe( 'downcastInsertCell()', () => {
  464. it( 'should add tableCell on proper index in tr', () => {
  465. setModelData( model, modelTable( [
  466. [ '00', '01' ]
  467. ] ) );
  468. const table = root.getChild( 0 );
  469. model.change( writer => {
  470. const row = table.getChild( 0 );
  471. writer.insertElement( 'tableCell', row, 1 );
  472. } );
  473. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  474. [ '00', '', '01' ]
  475. ] ) );
  476. } );
  477. it( 'should add tableCell on proper index in tr when previous have colspans', () => {
  478. setModelData( model, modelTable( [
  479. [ { colspan: 2, contents: '00' }, '13' ]
  480. ] ) );
  481. const table = root.getChild( 0 );
  482. model.change( writer => {
  483. const row = table.getChild( 0 );
  484. writer.insertElement( 'tableCell', row, 1 );
  485. } );
  486. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  487. [ { colspan: 2, contents: '00' }, '', '13' ]
  488. ] ) );
  489. } );
  490. it( 'should add tableCell on proper index in tr when previous row have rowspans', () => {
  491. setModelData( model, modelTable( [
  492. [ { rowspan: 2, contents: '00' }, '13' ],
  493. [ '11', '12' ]
  494. ] ) );
  495. const table = root.getChild( 0 );
  496. model.change( writer => {
  497. writer.insertElement( 'tableCell', table.getChild( 0 ), 1 );
  498. writer.insertElement( 'tableCell', table.getChild( 1 ), 0 );
  499. } );
  500. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  501. [ { rowspan: 2, contents: '00' }, '', '13' ],
  502. [ '', '11', '12' ]
  503. ] ) );
  504. } );
  505. it( 'split cell simulation - simple', () => {
  506. setModelData( model, modelTable( [
  507. [ '00', '01' ],
  508. [ '10', '11' ]
  509. ] ) );
  510. const table = root.getChild( 0 );
  511. model.change( writer => {
  512. const firstRow = table.getChild( 0 );
  513. const secondRow = table.getChild( 1 );
  514. writer.insertElement( 'tableCell', firstRow, 1 );
  515. writer.setAttribute( 'colspan', 2, secondRow.getChild( 0 ) );
  516. } );
  517. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  518. [ '00', '', '01' ],
  519. [ { colspan: 2, contents: '10' }, '11' ]
  520. ] ) );
  521. } );
  522. it( 'merge simulation - simple', () => {
  523. setModelData( model, modelTable( [
  524. [ '00', '01' ],
  525. [ '10', '11' ]
  526. ] ) );
  527. const table = root.getChild( 0 );
  528. model.change( writer => {
  529. const firstRow = table.getChild( 0 );
  530. writer.setAttribute( 'colspan', 2, firstRow.getChild( 0 ) );
  531. writer.remove( firstRow.getChild( 1 ) );
  532. } );
  533. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  534. [ { colspan: 2, contents: '00' } ],
  535. [ '10', '11' ]
  536. ] ) );
  537. } );
  538. describe( 'options.asWidget=true', () => {
  539. beforeEach( () => {
  540. return VirtualTestEditor.create()
  541. .then( newEditor => {
  542. editor = newEditor;
  543. model = editor.model;
  544. doc = model.document;
  545. root = doc.getRoot( 'main' );
  546. viewDocument = editor.editing.view;
  547. defaultSchema( model.schema );
  548. defaultConversion( editor.conversion, true );
  549. } );
  550. } );
  551. it( 'should create inserted table cell as a widget', () => {
  552. setModelData( model, modelTable( [ [ '00' ] ] ) );
  553. const table = root.getChild( 0 );
  554. model.change( writer => {
  555. const row = table.getChild( 0 );
  556. writer.insert( writer.createElement( 'tableCell' ), row, 'end' );
  557. } );
  558. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  559. '<figure class="ck-widget ck-widget_with-selection-handler table" contenteditable="false">' +
  560. '<div class="ck ck-widget__selection-handler"></div>' +
  561. '<table>' +
  562. '<tbody>' +
  563. '<tr>' +
  564. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  565. '<span>00</span>' +
  566. '</td>' +
  567. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true"></td>' +
  568. '</tr>' +
  569. '</tbody>' +
  570. '</table>' +
  571. '</figure>'
  572. ) );
  573. } );
  574. } );
  575. } );
  576. describe( 'downcastTableHeadingColumnsChange()', () => {
  577. it( 'should work for adding heading columns', () => {
  578. setModelData( model, modelTable( [
  579. [ '00', '01' ],
  580. [ '10', '11' ]
  581. ] ) );
  582. const table = root.getChild( 0 );
  583. model.change( writer => {
  584. writer.setAttribute( 'headingColumns', 1, table );
  585. } );
  586. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  587. [ { isHeading: true, contents: '00' }, '01' ],
  588. [ { isHeading: true, contents: '10' }, '11' ]
  589. ], { headingColumns: 1 } ) );
  590. } );
  591. it( 'should work for changing heading columns to a bigger number', () => {
  592. setModelData( model, modelTable( [
  593. [ '00', '01', '02', '03' ],
  594. [ '10', '11', '12', '13' ]
  595. ], { headingColumns: 1 } ) );
  596. const table = root.getChild( 0 );
  597. model.change( writer => {
  598. writer.setAttribute( 'headingColumns', 3, table );
  599. } );
  600. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  601. [ { isHeading: true, contents: '00' }, { isHeading: true, contents: '01' }, { isHeading: true, contents: '02' }, '03' ],
  602. [ { isHeading: true, contents: '10' }, { isHeading: true, contents: '11' }, { isHeading: true, contents: '12' }, '13' ]
  603. ] ) );
  604. } );
  605. it( 'should work for changing heading columns to a smaller number', () => {
  606. setModelData( model, modelTable( [
  607. [ { isHeading: true, contents: '00' }, { isHeading: true, contents: '01' }, { isHeading: true, contents: '02' }, '03' ],
  608. [ { isHeading: true, contents: '10' }, { isHeading: true, contents: '11' }, { isHeading: true, contents: '12' }, '13' ]
  609. ], { headingColumns: 3 } ) );
  610. const table = root.getChild( 0 );
  611. model.change( writer => {
  612. writer.setAttribute( 'headingColumns', 1, table );
  613. } );
  614. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  615. [ { isHeading: true, contents: '00' }, '01', '02', '03' ],
  616. [ { isHeading: true, contents: '10' }, '11', '12', '13' ]
  617. ], { headingColumns: 3 } ) );
  618. } );
  619. it( 'should work for removing heading columns', () => {
  620. setModelData( model, modelTable( [
  621. [ '00', '01' ],
  622. [ '10', '11' ]
  623. ], { headingColumns: 1 } ) );
  624. const table = root.getChild( 0 );
  625. model.change( writer => {
  626. writer.removeAttribute( 'headingColumns', table );
  627. } );
  628. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  629. [ '00', '01' ],
  630. [ '10', '11' ]
  631. ] ) );
  632. } );
  633. it( 'should be possible to overwrite', () => {
  634. editor.conversion.attributeToAttribute( { model: 'headingColumns', view: 'headingColumns', converterPriority: 'high' } );
  635. setModelData( model, modelTable( [ [ '00' ] ] ) );
  636. const table = root.getChild( 0 );
  637. model.change( writer => {
  638. writer.setAttribute( 'headingColumns', 1, table );
  639. } );
  640. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  641. '<figure class="table" headingColumns="1">' +
  642. '<table>' +
  643. '<tbody>' +
  644. '<tr><td>00</td></tr>' +
  645. '</tbody>' +
  646. '</table>' +
  647. '</figure>'
  648. ) );
  649. } );
  650. it( 'should work with adding table cells', () => {
  651. setModelData( model, modelTable( [
  652. [ { rowspan: 2, contents: '00' }, '01', '13', '14' ],
  653. [ '11', '12', '13' ],
  654. [ { colspan: 2, contents: '20' }, '22', '23' ]
  655. ], { headingColumns: 2 } ) );
  656. const table = root.getChild( 0 );
  657. model.change( writer => {
  658. // Inserting column in heading columns so update table's attribute also
  659. writer.setAttribute( 'headingColumns', 3, table );
  660. writer.insertElement( 'tableCell', table.getChild( 0 ), 2 );
  661. writer.insertElement( 'tableCell', table.getChild( 1 ), 1 );
  662. writer.insertElement( 'tableCell', table.getChild( 2 ), 1 );
  663. } );
  664. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  665. [
  666. { isHeading: true, rowspan: 2, contents: '00' },
  667. { isHeading: true, contents: '01' },
  668. { isHeading: true, contents: '' },
  669. '13',
  670. '14'
  671. ],
  672. [
  673. { isHeading: true, contents: '11' },
  674. { isHeading: true, contents: '' },
  675. '12',
  676. '13'
  677. ],
  678. [
  679. { isHeading: true, colspan: 2, contents: '20' },
  680. { isHeading: true, contents: '' },
  681. '22',
  682. '23'
  683. ]
  684. ] ) );
  685. } );
  686. describe( 'options.asWidget=true', () => {
  687. beforeEach( () => {
  688. return VirtualTestEditor.create()
  689. .then( newEditor => {
  690. editor = newEditor;
  691. model = editor.model;
  692. doc = model.document;
  693. root = doc.getRoot( 'main' );
  694. viewDocument = editor.editing.view;
  695. defaultSchema( model.schema );
  696. defaultConversion( editor.conversion, true );
  697. } );
  698. } );
  699. it( 'should create renamed cell as a widget', () => {
  700. setModelData( model, modelTable( [ [ '00' ] ] ) );
  701. const table = root.getChild( 0 );
  702. model.change( writer => {
  703. writer.setAttribute( 'headingRows', 1, table );
  704. } );
  705. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  706. '<figure class="ck-widget ck-widget_with-selection-handler table" contenteditable="false">' +
  707. '<div class="ck ck-widget__selection-handler"></div>' +
  708. '<table>' +
  709. '<thead>' +
  710. '<tr>' +
  711. '<th class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  712. '<span>00</span>' +
  713. '</th>' +
  714. '</tr>' +
  715. '</thead>' +
  716. '</table>' +
  717. '</figure>'
  718. ) );
  719. } );
  720. } );
  721. } );
  722. describe( 'downcastTableHeadingRowsChange()', () => {
  723. it( 'should work for adding heading rows', () => {
  724. setModelData( model, modelTable( [
  725. [ '00', '01' ],
  726. [ '10', '11' ],
  727. [ '20', '21' ]
  728. ] ) );
  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. ], { headingRows: 2 } ) );
  738. } );
  739. it( 'should work for changing number of heading rows to a bigger number', () => {
  740. setModelData( model, modelTable( [
  741. [ '00', '01' ],
  742. [ '10', '11' ],
  743. [ '20', '21' ]
  744. ], { headingRows: 1 } ) );
  745. const table = root.getChild( 0 );
  746. model.change( writer => {
  747. writer.setAttribute( 'headingRows', 2, table );
  748. } );
  749. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  750. [ '00', '01' ],
  751. [ '10', '11' ],
  752. [ '20', '21' ]
  753. ], { headingRows: 2 } ) );
  754. } );
  755. it( 'should work for changing number of heading rows to a smaller number', () => {
  756. setModelData( model, modelTable( [
  757. [ '00', '01' ],
  758. [ '10', '11' ],
  759. [ '20', '21' ],
  760. [ '30', '31' ]
  761. ], { headingRows: 3 } ) );
  762. const table = root.getChild( 0 );
  763. model.change( writer => {
  764. writer.setAttribute( 'headingRows', 2, table );
  765. } );
  766. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  767. [ '00', '01' ],
  768. [ '10', '11' ],
  769. [ '20', '21' ],
  770. [ '30', '31' ]
  771. ], { headingRows: 2 } ) );
  772. } );
  773. it( 'should work for removing heading rows', () => {
  774. setModelData( model, modelTable( [
  775. [ '00', '01' ],
  776. [ '10', '11' ]
  777. ], { headingRows: 2 } ) );
  778. const table = root.getChild( 0 );
  779. model.change( writer => {
  780. writer.removeAttribute( 'headingRows', table );
  781. } );
  782. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  783. [ '00', '01' ],
  784. [ '10', '11' ]
  785. ] ) );
  786. } );
  787. it( 'should work for making heading rows without tbody', () => {
  788. setModelData( model, modelTable( [
  789. [ '00', '01' ],
  790. [ '10', '11' ]
  791. ] ) );
  792. const table = root.getChild( 0 );
  793. model.change( writer => {
  794. writer.setAttribute( 'headingRows', 2, table );
  795. } );
  796. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  797. [ '00', '01' ],
  798. [ '10', '11' ]
  799. ], { headingRows: 2 } ) );
  800. } );
  801. it( 'should be possible to overwrite', () => {
  802. editor.conversion.attributeToAttribute( { model: 'headingRows', view: 'headingRows', converterPriority: 'high' } );
  803. setModelData( model, modelTable( [ [ '00' ] ] ) );
  804. const table = root.getChild( 0 );
  805. model.change( writer => {
  806. writer.setAttribute( 'headingRows', 1, table );
  807. } );
  808. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  809. '<figure class="table" headingRows="1">' +
  810. '<table>' +
  811. '<tbody>' +
  812. '<tr><td>00</td></tr>' +
  813. '</tbody>' +
  814. '</table>' +
  815. '</figure>'
  816. ) );
  817. } );
  818. it( 'should work with adding table rows at the beginning of a table', () => {
  819. setModelData( model, modelTable( [
  820. [ '00', '01' ],
  821. [ '10', '11' ]
  822. ], { headingRows: 1 } ) );
  823. const table = root.getChild( 0 );
  824. model.change( writer => {
  825. writer.setAttribute( 'headingRows', 2, table );
  826. const tableRow = writer.createElement( 'tableRow' );
  827. writer.insert( tableRow, table, 0 );
  828. writer.insertElement( 'tableCell', tableRow, 'end' );
  829. writer.insertElement( 'tableCell', tableRow, 'end' );
  830. } );
  831. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  832. [ '', '' ],
  833. [ '00', '01' ],
  834. [ '10', '11' ]
  835. ], { headingRows: 2 } ) );
  836. } );
  837. it( 'should work with adding a table row and expanding heading', () => {
  838. setModelData( model, modelTable( [
  839. [ '00', '01' ],
  840. [ '10', '11' ],
  841. [ '20', '21' ]
  842. ], { headingRows: 1 } ) );
  843. const table = root.getChild( 0 );
  844. model.change( writer => {
  845. writer.setAttribute( 'headingRows', 2, table );
  846. const tableRow = writer.createElement( 'tableRow' );
  847. writer.insert( tableRow, table, 1 );
  848. writer.insertElement( 'tableCell', tableRow, 'end' );
  849. writer.insertElement( 'tableCell', tableRow, 'end' );
  850. } );
  851. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  852. [ '00', '01' ],
  853. [ '', '' ],
  854. [ '10', '11' ],
  855. [ '20', '21' ]
  856. ], { headingRows: 2 } ) );
  857. } );
  858. describe( 'options.asWidget=true', () => {
  859. beforeEach( () => {
  860. return VirtualTestEditor.create()
  861. .then( newEditor => {
  862. editor = newEditor;
  863. model = editor.model;
  864. doc = model.document;
  865. root = doc.getRoot( 'main' );
  866. viewDocument = editor.editing.view;
  867. defaultSchema( model.schema );
  868. defaultConversion( editor.conversion, true );
  869. } );
  870. } );
  871. it( 'should create renamed cell as a widget', () => {
  872. setModelData( model, modelTable( [ [ '00' ] ] ) );
  873. const table = root.getChild( 0 );
  874. model.change( writer => {
  875. writer.setAttribute( 'headingColumns', 1, table );
  876. } );
  877. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  878. '<figure class="ck-widget ck-widget_with-selection-handler table" contenteditable="false">' +
  879. '<div class="ck ck-widget__selection-handler"></div>' +
  880. '<table>' +
  881. '<tbody>' +
  882. '<tr>' +
  883. '<th class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  884. '<span>00</span>' +
  885. '</th>' +
  886. '</tr>' +
  887. '</tbody>' +
  888. '</table>' +
  889. '</figure>'
  890. ) );
  891. } );
  892. } );
  893. } );
  894. describe( 'downcastRemoveRow()', () => {
  895. it( 'should react to removed row from the beginning of a tbody', () => {
  896. setModelData( model, modelTable( [
  897. [ '00[]', '01' ],
  898. [ '10', '11' ]
  899. ] ) );
  900. const table = root.getChild( 0 );
  901. model.change( writer => {
  902. writer.remove( table.getChild( 1 ) );
  903. } );
  904. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  905. [ '00', '01' ]
  906. ] ) );
  907. } );
  908. it( 'should react to removed row form the end of a tbody', () => {
  909. setModelData( model, modelTable( [
  910. [ '00[]', '01' ],
  911. [ '10', '11' ]
  912. ] ) );
  913. const table = root.getChild( 0 );
  914. model.change( writer => {
  915. writer.remove( table.getChild( 0 ) );
  916. } );
  917. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  918. [ '10', '11' ]
  919. ] ) );
  920. } );
  921. it( 'should react to removed row from the beginning of a thead', () => {
  922. setModelData( model, modelTable( [
  923. [ '00[]', '01' ],
  924. [ '10', '11' ]
  925. ], { headingRows: 2 } ) );
  926. const table = root.getChild( 0 );
  927. model.change( writer => {
  928. writer.remove( table.getChild( 1 ) );
  929. } );
  930. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  931. [ '00', '01' ]
  932. ], { headingRows: 2 } ) );
  933. } );
  934. it( 'should react to removed row form the end of a thead', () => {
  935. setModelData( model, modelTable( [
  936. [ '00[]', '01' ],
  937. [ '10', '11' ]
  938. ], { headingRows: 2 } ) );
  939. const table = root.getChild( 0 );
  940. model.change( writer => {
  941. writer.remove( table.getChild( 0 ) );
  942. } );
  943. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  944. [ '10', '11' ]
  945. ], { headingRows: 2 } ) );
  946. } );
  947. it( 'should remove empty thead section if a last row was removed from thead', () => {
  948. setModelData( model, modelTable( [
  949. [ '00[]', '01' ],
  950. [ '10', '11' ]
  951. ], { headingRows: 1 } ) );
  952. const table = root.getChild( 0 );
  953. model.change( writer => {
  954. writer.setAttribute( 'headingRows', 0, table );
  955. writer.remove( table.getChild( 0 ) );
  956. } );
  957. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  958. [ '10', '11' ]
  959. ] ) );
  960. } );
  961. it( 'should remove empty tbody section if a last row was removed from tbody', () => {
  962. setModelData( model, modelTable( [
  963. [ '00[]', '01' ],
  964. [ '10', '11' ]
  965. ], { headingRows: 1 } ) );
  966. const table = root.getChild( 0 );
  967. model.change( writer => {
  968. writer.remove( table.getChild( 1 ) );
  969. } );
  970. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  971. [ '00', '01' ]
  972. ], { headingRows: 1 } ) );
  973. } );
  974. } );
  975. describe( 'options.asWidget=true', () => {
  976. beforeEach( () => {
  977. return VirtualTestEditor.create()
  978. .then( newEditor => {
  979. editor = newEditor;
  980. model = editor.model;
  981. doc = model.document;
  982. root = doc.getRoot( 'main' );
  983. viewDocument = editor.editing.view;
  984. defaultSchema( model.schema );
  985. defaultConversion( editor.conversion, true );
  986. editor.conversion.for( 'downcast' ).add( paragraphInTableCell() );
  987. } );
  988. } );
  989. it( 'should rename <span> to <p> when more then one block content inside table cell', () => {
  990. setModelData( model, modelTable( [
  991. [ '00[]' ]
  992. ] ) );
  993. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  994. [ '00' ]
  995. ], { asWidget: true } ) );
  996. const table = root.getChild( 0 );
  997. model.change( writer => {
  998. const nodeByPath = table.getNodeByPath( [ 0, 0, 0 ] );
  999. const paragraph = writer.createElement( 'paragraph' );
  1000. writer.insert( paragraph, nodeByPath, 'after' );
  1001. writer.setSelection( nodeByPath.nextSibling, 0 );
  1002. } );
  1003. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  1004. [ '<p>00</p><p></p>' ]
  1005. ], { asWidget: true } ) );
  1006. } );
  1007. it( 'should rename <span> to <p> for single paragraph with attribute', () => {
  1008. model.schema.extend( '$block', { allowAttributes: 'foo' } );
  1009. editor.conversion.attributeToAttribute( { model: 'foo', view: 'foo' } );
  1010. setModelData( model, modelTable( [
  1011. [ '<paragraph foo="bar">00[]</paragraph>' ]
  1012. ] ) );
  1013. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  1014. [ '<p foo="bar">00</p>' ]
  1015. ], { asWidget: true } ) );
  1016. } );
  1017. } );
  1018. } );