8
0

downcast.js 34 KB

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