8
0

downcast.js 36 KB

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