8
0

downcast.js 35 KB

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