downcast.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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', priority: 'high' } );
  131. editor.conversion.elementToElement( { model: 'tableCell', view: 'td', priority: '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', priority: '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. describe( 'asWidget', () => {
  801. beforeEach( () => {
  802. return VirtualTestEditor.create()
  803. .then( newEditor => {
  804. editor = newEditor;
  805. model = editor.model;
  806. doc = model.document;
  807. root = doc.getRoot( 'main' );
  808. viewDocument = editor.editing.view;
  809. const conversion = editor.conversion;
  810. const schema = model.schema;
  811. schema.register( 'table', {
  812. allowWhere: '$block',
  813. allowAttributes: [ 'headingRows', 'headingColumns' ],
  814. isBlock: true,
  815. isObject: true
  816. } );
  817. schema.register( 'tableRow', {
  818. allowIn: 'table',
  819. allowAttributes: [],
  820. isBlock: true,
  821. isLimit: true
  822. } );
  823. schema.register( 'tableCell', {
  824. allowIn: 'tableRow',
  825. allowContentOf: '$block',
  826. allowAttributes: [ 'colspan', 'rowspan' ],
  827. isBlock: true,
  828. isLimit: true
  829. } );
  830. conversion.for( 'downcast' ).add( downcastInsertTable( { asWidget: true } ) );
  831. conversion.for( 'downcast' ).add( downcastInsertRow( { asWidget: true } ) );
  832. conversion.for( 'downcast' ).add( downcastInsertCell( { asWidget: true } ) );
  833. conversion.for( 'downcast' ).add( downcastAttributeChange( { attribute: 'headingRows', asWidget: true } ) );
  834. conversion.for( 'downcast' ).add( downcastAttributeChange( { attribute: 'headingColumns', asWidget: true } ) );
  835. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  836. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  837. } );
  838. } );
  839. it( 'should create renamed cell as a widget', () => {
  840. setModelData( model,
  841. '<table>' +
  842. '<tableRow><tableCell>foo</tableCell></tableRow>' +
  843. '</table>'
  844. );
  845. const table = root.getChild( 0 );
  846. model.change( writer => {
  847. writer.setAttribute( 'headingColumns', 1, table );
  848. } );
  849. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  850. '<table class="ck-widget" contenteditable="false">' +
  851. '<tbody>' +
  852. '<tr><th class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">foo</th></tr>' +
  853. '</tbody>' +
  854. '</table>'
  855. );
  856. } );
  857. } );
  858. } );
  859. describe( 'downcastRemoveRow()', () => {
  860. it( 'should react to removed row from the beginning of a tbody', () => {
  861. setModelData( model, modelTable( [
  862. [ '00[]', '01' ],
  863. [ '10', '11' ]
  864. ] ) );
  865. const table = root.getChild( 0 );
  866. model.change( writer => {
  867. writer.remove( table.getChild( 1 ) );
  868. } );
  869. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  870. [ '00', '01' ]
  871. ] ) );
  872. } );
  873. it( 'should react to removed row form the end of a tbody', () => {
  874. setModelData( model, modelTable( [
  875. [ '00[]', '01' ],
  876. [ '10', '11' ]
  877. ] ) );
  878. const table = root.getChild( 0 );
  879. model.change( writer => {
  880. writer.remove( table.getChild( 0 ) );
  881. } );
  882. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  883. [ '10', '11' ]
  884. ] ) );
  885. } );
  886. it( 'should react to removed row from the beginning of a thead', () => {
  887. setModelData( model, modelTable( [
  888. [ '00[]', '01' ],
  889. [ '10', '11' ]
  890. ], { headingRows: 2 } ) );
  891. const table = root.getChild( 0 );
  892. model.change( writer => {
  893. writer.remove( table.getChild( 1 ) );
  894. } );
  895. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  896. [ '00', '01' ]
  897. ], { headingRows: 2 } ) );
  898. } );
  899. it( 'should react to removed row form the end of a thead', () => {
  900. setModelData( model, modelTable( [
  901. [ '00[]', '01' ],
  902. [ '10', '11' ]
  903. ], { headingRows: 2 } ) );
  904. const table = root.getChild( 0 );
  905. model.change( writer => {
  906. writer.remove( table.getChild( 0 ) );
  907. } );
  908. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  909. [ '10', '11' ]
  910. ], { headingRows: 2 } ) );
  911. } );
  912. it( 'should remove empty thead section if a last row was removed from thead', () => {
  913. setModelData( model, modelTable( [
  914. [ '00[]', '01' ],
  915. [ '10', '11' ]
  916. ], { headingRows: 1 } ) );
  917. const table = root.getChild( 0 );
  918. model.change( writer => {
  919. writer.setAttribute( 'headingRows', 0, table );
  920. writer.remove( table.getChild( 0 ) );
  921. } );
  922. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  923. [ '10', '11' ]
  924. ] ) );
  925. } );
  926. it( 'should remove empty tbody section if a last row was removed from tbody', () => {
  927. setModelData( model, modelTable( [
  928. [ '00[]', '01' ],
  929. [ '10', '11' ]
  930. ], { headingRows: 1 } ) );
  931. const table = root.getChild( 0 );
  932. model.change( writer => {
  933. writer.remove( table.getChild( 1 ) );
  934. } );
  935. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  936. [ '00', '01' ]
  937. ], { headingRows: 1 } ) );
  938. } );
  939. } );
  940. } );