downcast.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import TableEditing from '../../src/tableediting';
  8. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  9. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  10. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  11. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  12. import { defaultConversion, defaultSchema, modelTable, viewTable } from '../_utils/utils';
  13. function paragraphInTableCell() {
  14. return dispatcher => dispatcher.on( 'insert:paragraph', ( evt, data, conversionApi ) => {
  15. const tableCell = data.item.parent;
  16. if ( tableCell.is( 'tableCell' ) && tableCell.childCount > 1 ) {
  17. for ( const child of tableCell.getChildren() ) {
  18. if ( child.name != 'paragraph' ) {
  19. continue;
  20. }
  21. const viewElement = conversionApi.mapper.toViewElement( child );
  22. if ( viewElement && viewElement.name === 'span' ) {
  23. conversionApi.mapper.unbindModelElement( tableCell );
  24. conversionApi.writer.removeStyle( 'display', viewElement );
  25. conversionApi.writer.rename( 'p', viewElement );
  26. conversionApi.mapper.bindElements( child, viewElement );
  27. }
  28. }
  29. }
  30. }, { converterPriority: 'highest' } );
  31. }
  32. describe( 'downcast converters', () => {
  33. let editor, model, doc, root, view;
  34. testUtils.createSinonSandbox();
  35. describe( 'downcastInsertTable()', () => {
  36. beforeEach( () => {
  37. return VirtualTestEditor.create( { plugins: [ Paragraph, TableEditing ] } )
  38. .then( newEditor => {
  39. editor = newEditor;
  40. model = editor.model;
  41. doc = model.document;
  42. root = doc.getRoot( 'main' );
  43. view = editor.editing.view;
  44. } );
  45. } );
  46. describe( 'editing pipeline', () => {
  47. it( 'should create table as a widget', () => {
  48. setModelData( model, modelTable( [ [ '' ] ] ) );
  49. assertEqualMarkup( getViewData( view, { withoutSelection: true } ),
  50. '<figure class="ck-widget ck-widget_with-selection-handle table" contenteditable="false">' +
  51. '<div class="ck ck-widget__selection-handle"></div>' +
  52. '<table>' +
  53. '<tbody>' +
  54. '<tr>' +
  55. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  56. '<span style="display:inline-block"></span>' +
  57. '</td>' +
  58. '</tr>' +
  59. '</tbody>' +
  60. '</table>' +
  61. '</figure>'
  62. );
  63. } );
  64. } );
  65. describe( 'data pipeline', () => {
  66. it( 'should create table with tbody and thead', () => {
  67. setModelData( model, modelTable( [
  68. [ '00' ],
  69. [ '10' ]
  70. ], { headingRows: 1 } ) );
  71. assertEqualMarkup( editor.getData(),
  72. '<figure class="table">' +
  73. '<table>' +
  74. '<thead>' +
  75. '<tr><th>00</th></tr>' +
  76. '</thead>' +
  77. '<tbody>' +
  78. '<tr><td>10</td></tr>' +
  79. '</tbody>' +
  80. '</table>' +
  81. '</figure>'
  82. );
  83. } );
  84. it( 'should create table with thead', () => {
  85. setModelData( model, modelTable( [
  86. [ '00' ],
  87. [ '10' ]
  88. ], { headingRows: 2 } ) );
  89. assertEqualMarkup( editor.getData(),
  90. '<figure class="table">' +
  91. '<table>' +
  92. '<thead>' +
  93. '<tr><th>00</th></tr>' +
  94. '<tr><th>10</th></tr>' +
  95. '</thead>' +
  96. '</table>' +
  97. '</figure>'
  98. );
  99. } );
  100. it( 'should create table with heading columns and rows', () => {
  101. setModelData( model, modelTable( [
  102. [ '00', '01', '02', '03' ],
  103. [ '10', '11', '12', '13' ]
  104. ], { headingColumns: 3, headingRows: 1 } ) );
  105. assertEqualMarkup( editor.getData(),
  106. '<figure class="table">' +
  107. '<table>' +
  108. '<thead>' +
  109. '<tr><th>00</th><th>01</th><th>02</th><th>03</th></tr>' +
  110. '</thead>' +
  111. '<tbody>' +
  112. '<tr><th>10</th><th>11</th><th>12</th><td>13</td></tr>' +
  113. '</tbody>' +
  114. '</table>' +
  115. '</figure>'
  116. );
  117. } );
  118. it( 'should create table with block content', () => {
  119. setModelData( model, modelTable( [
  120. [ '<paragraph>00</paragraph><paragraph>foo</paragraph>', '01' ]
  121. ] ) );
  122. assertEqualMarkup( editor.getData(),
  123. '<figure class="table">' +
  124. '<table>' +
  125. '<tbody>' +
  126. '<tr>' +
  127. '<td><p>00</p><p>foo</p></td>' +
  128. '<td>01</td>' +
  129. '</tr>' +
  130. '</tbody>' +
  131. '</table>' +
  132. '</figure>'
  133. );
  134. } );
  135. it( 'should create table with block content (attribute on paragraph)', () => {
  136. editor.conversion.attributeToAttribute(
  137. {
  138. model: { key: 'alignment', values: [ 'right', 'center', 'justify' ] },
  139. view: {
  140. right: { key: 'style', value: { 'text-align': 'right' } },
  141. center: { key: 'style', value: { 'text-align': 'center' } },
  142. justify: { key: 'style', value: { 'text-align': 'justify' } }
  143. }
  144. }
  145. );
  146. setModelData( model, modelTable( [
  147. [ '<paragraph alignment="right">00</paragraph>' ]
  148. ] ) );
  149. assertEqualMarkup( editor.getData(),
  150. '<figure class="table">' +
  151. '<table>' +
  152. '<tbody>' +
  153. '<tr>' +
  154. '<td><p style="text-align:right;">00</p></td>' +
  155. '</tr>' +
  156. '</tbody>' +
  157. '</table>' +
  158. '</figure>'
  159. );
  160. } );
  161. it( 'should be possible to overwrite', () => {
  162. editor.conversion.elementToElement( { model: 'tableRow', view: 'tr', converterPriority: 'high' } );
  163. editor.conversion.elementToElement( { model: 'tableCell', view: 'td', converterPriority: 'high' } );
  164. editor.conversion.for( 'downcast' ).add( dispatcher => {
  165. dispatcher.on( 'insert:table', ( evt, data, conversionApi ) => {
  166. conversionApi.consumable.consume( data.item, 'insert' );
  167. const tableElement = conversionApi.writer.createContainerElement( 'table', { foo: 'bar' } );
  168. const viewPosition = conversionApi.mapper.toViewPosition( data.range.start );
  169. conversionApi.mapper.bindElements( data.item, tableElement );
  170. conversionApi.writer.insert( viewPosition, tableElement );
  171. }, { priority: 'high' } );
  172. } );
  173. setModelData( model, modelTable( [ [ '' ] ] ) );
  174. assertEqualMarkup( editor.getData(),
  175. '<table foo="bar">' +
  176. '<tr><td><p>&nbsp;</p></td></tr>' +
  177. '</table>'
  178. );
  179. } );
  180. it( 'should re-create table on reinsert', () => {
  181. model.schema.register( 'wrapper', {
  182. allowWhere: '$block',
  183. allowContentOf: '$root'
  184. } );
  185. editor.conversion.elementToElement( { model: 'wrapper', view: 'div' } );
  186. setModelData( model, modelTable( [ [ '[]' ] ] ) );
  187. assertEqualMarkup( editor.getData(),
  188. '<figure class="table">' +
  189. '<table>' +
  190. '<tbody>' +
  191. '<tr><td>&nbsp;</td></tr>' +
  192. '</tbody>' +
  193. '</table>' +
  194. '</figure>'
  195. );
  196. model.change( writer => {
  197. const table = model.document.getRoot().getChild( 0 );
  198. const range = writer.createRange( writer.createPositionBefore( table ), writer.createPositionAfter( table ) );
  199. const wrapper = writer.createElement( 'wrapper' );
  200. writer.wrap( range, wrapper );
  201. } );
  202. assertEqualMarkup( editor.getData(),
  203. '<div>' +
  204. '<figure class="table">' +
  205. '<table>' +
  206. '<tbody>' +
  207. '<tr><td>&nbsp;</td></tr>' +
  208. '</tbody>' +
  209. '</table>' +
  210. '</figure>' +
  211. '</div>'
  212. );
  213. } );
  214. describe( 'headingColumns attribute', () => {
  215. it( 'should mark heading columns table cells', () => {
  216. setModelData( model, modelTable( [
  217. [ '00', '01', '02' ],
  218. [ '10', '11', '12' ]
  219. ], { headingColumns: 2 } ) );
  220. assertEqualMarkup( editor.getData(),
  221. '<figure class="table">' +
  222. '<table>' +
  223. '<tbody>' +
  224. '<tr><th>00</th><th>01</th><td>02</td></tr>' +
  225. '<tr><th>10</th><th>11</th><td>12</td></tr>' +
  226. '</tbody>' +
  227. '</table>' +
  228. '</figure>'
  229. );
  230. } );
  231. it( 'should mark heading columns table cells when one has colspan attribute', () => {
  232. setModelData( model, modelTable( [
  233. [ '00', '01', '02', '03' ],
  234. [ { colspan: 2, contents: '10' }, '12', '13' ]
  235. ], { headingColumns: 3 } ) );
  236. assertEqualMarkup( editor.getData(),
  237. '<figure class="table">' +
  238. '<table>' +
  239. '<tbody>' +
  240. '<tr><th>00</th><th>01</th><th>02</th><td>03</td></tr>' +
  241. '<tr><th colspan="2">10</th><th>12</th><td>13</td></tr>' +
  242. '</tbody>' +
  243. '</table>' +
  244. '</figure>'
  245. );
  246. } );
  247. it( 'should work with colspan and rowspan attributes on table cells', () => {
  248. // The table in this test looks like a table below:
  249. //
  250. // Row headings | Normal cells
  251. // |
  252. // +----+----+----+----+
  253. // | 00 | 01 | 02 | 03 |
  254. // | +----+ +----+
  255. // | | 11 | | 13 |
  256. // |----+----+ +----+
  257. // | 20 | | 23 |
  258. // | +----+----+
  259. // | | 32 | 33 |
  260. // +----+----+----+----+
  261. setModelData( model, modelTable( [
  262. [ { rowspan: 2, contents: '00' }, '01', { rowspan: 3, contents: '02' }, '03' ],
  263. [ '11', '13' ],
  264. [ { colspan: 2, rowspan: 2, contents: '20' }, '23' ],
  265. [ '32', '33' ]
  266. ], { headingColumns: 3 } ) );
  267. assertEqualMarkup( editor.getData(),
  268. '<figure class="table">' +
  269. '<table>' +
  270. '<tbody>' +
  271. '<tr><th rowspan="2">00</th><th>01</th><th rowspan="3">02</th><td>03</td></tr>' +
  272. '<tr><th>11</th><td>13</td></tr>' +
  273. '<tr><th rowspan="2" colspan="2">20</th><td>23</td></tr>' +
  274. '<tr><th>32</th><td>33</td></tr>' +
  275. '</tbody>' +
  276. '</table>' +
  277. '</figure>'
  278. );
  279. } );
  280. } );
  281. it( 'should create table with tbody', () => {
  282. setModelData( model, modelTable( [ [ '' ] ] ) );
  283. assertEqualMarkup( editor.getData(),
  284. '<figure class="table">' +
  285. '<table>' +
  286. '<tbody>' +
  287. '<tr><td>&nbsp;</td></tr>' +
  288. '</tbody>' +
  289. '</table>' +
  290. '</figure>'
  291. );
  292. } );
  293. } );
  294. } );
  295. describe( 'downcastInsertRow()', () => {
  296. // The beforeEach is duplicated due to ckeditor/ckeditor5#6574. New test are written using TableEditing.
  297. beforeEach( () => {
  298. return VirtualTestEditor.create( { plugins: [ Paragraph, TableEditing ] } )
  299. .then( newEditor => {
  300. editor = newEditor;
  301. model = editor.model;
  302. doc = model.document;
  303. root = doc.getRoot( 'main' );
  304. view = editor.editing.view;
  305. } );
  306. } );
  307. // The insert row downcast conversion is not executed in data pipeline.
  308. describe( 'editing pipeline', () => {
  309. it( 'should react to changed rows', () => {
  310. setModelData( model, modelTable( [
  311. [ '00', '01' ]
  312. ] ) );
  313. const table = root.getChild( 0 );
  314. model.change( writer => {
  315. const row = writer.createElement( 'tableRow' );
  316. writer.insert( row, table, 1 );
  317. writer.insertElement( 'tableCell', row, 'end' );
  318. writer.insertElement( 'tableCell', row, 'end' );
  319. } );
  320. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  321. [ '00', '01' ],
  322. [ '', '' ]
  323. ], { asWidget: true } ) );
  324. } );
  325. it( 'should properly consume already added rows', () => {
  326. setModelData( model, modelTable( [
  327. [ '00', '01' ]
  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. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  337. [ '00', '01' ],
  338. [ '', '' ]
  339. ], { asWidget: true } ) );
  340. model.change( writer => {
  341. const row = writer.createElement( 'tableRow' );
  342. writer.insert( row, table, 2 );
  343. writer.insertElement( 'tableCell', row, 'end' );
  344. writer.insertElement( 'tableCell', row, 'end' );
  345. } );
  346. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  347. [ '00', '01' ],
  348. [ '', '' ],
  349. [ '', '' ]
  350. ], { asWidget: true } ) );
  351. } );
  352. it( 'should insert row on proper index', () => {
  353. setModelData( model, modelTable( [
  354. [ '00', '01' ],
  355. [ '21', '22' ],
  356. [ '31', '32' ]
  357. ] ) );
  358. const table = root.getChild( 0 );
  359. model.change( writer => {
  360. const row = writer.createElement( 'tableRow' );
  361. writer.insert( row, table, 1 );
  362. writer.insertElement( 'tableCell', row, 'end' );
  363. writer.insertElement( 'tableCell', row, 'end' );
  364. } );
  365. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  366. [ '00', '01' ],
  367. [ '', '' ],
  368. [ '21', '22' ],
  369. [ '31', '32' ]
  370. ], { asWidget: true } ) );
  371. } );
  372. it( 'should insert row on proper index when table has heading rows defined - insert in body', () => {
  373. setModelData( model, modelTable( [
  374. [ '00', '01' ],
  375. [ '21', '22' ],
  376. [ '31', '32' ]
  377. ], { headingRows: 1, asWidget: true } ) );
  378. const table = root.getChild( 0 );
  379. model.change( writer => {
  380. const row = writer.createElement( 'tableRow' );
  381. writer.insert( row, table, 1 );
  382. writer.insertElement( 'tableCell', row, 'end' );
  383. writer.insertElement( 'tableCell', row, 'end' );
  384. } );
  385. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  386. [ '00', '01' ],
  387. [ '', '' ],
  388. [ '21', '22' ],
  389. [ '31', '32' ]
  390. ], { headingRows: 1, asWidget: true } ) );
  391. } );
  392. it( 'should insert row on proper index when table has heading rows defined - insert in heading', () => {
  393. setModelData( model, modelTable( [
  394. [ '00', '01' ],
  395. [ '21', '22' ],
  396. [ '31', '32' ]
  397. ], { headingRows: 2 } ) );
  398. const table = root.getChild( 0 );
  399. model.change( writer => {
  400. const row = writer.createElement( 'tableRow' );
  401. writer.insert( row, table, 1 );
  402. writer.insertElement( 'tableCell', row, 'end' );
  403. writer.insertElement( 'tableCell', row, 'end' );
  404. } );
  405. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  406. [ '00', '01' ],
  407. [ '', '' ],
  408. [ '21', '22' ],
  409. [ '31', '32' ]
  410. ], { headingRows: 3, asWidget: true } ) );
  411. } );
  412. it( 'should react to changed rows when previous rows\' cells has rowspans', () => {
  413. setModelData( model, modelTable( [
  414. [ { rowspan: 2, contents: '00' }, '01' ],
  415. [ '22' ]
  416. ] ) );
  417. const table = root.getChild( 0 );
  418. model.change( writer => {
  419. const row = writer.createElement( 'tableRow' );
  420. writer.insert( row, table, 2 );
  421. writer.insertElement( 'tableCell', row, 'end' );
  422. writer.insertElement( 'tableCell', row, 'end' );
  423. } );
  424. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  425. [ { rowspan: 2, contents: '00' }, '01' ],
  426. [ '22' ],
  427. [ '', '' ]
  428. ], { asWidget: true } ) );
  429. } );
  430. it( 'should properly create row headings', () => {
  431. setModelData( model, modelTable( [
  432. [ { rowspan: 2, contents: '00' }, '01' ],
  433. [ '22' ]
  434. ], { headingColumns: 1 } ) );
  435. const table = root.getChild( 0 );
  436. model.change( writer => {
  437. const firstRow = writer.createElement( 'tableRow' );
  438. writer.insert( firstRow, table, 2 );
  439. writer.insert( writer.createElement( 'tableCell' ), firstRow, 'end' );
  440. const secondRow = writer.createElement( 'tableRow' );
  441. writer.insert( secondRow, table, 3 );
  442. writer.insert( writer.createElement( 'tableCell' ), secondRow, 'end' );
  443. writer.insert( writer.createElement( 'tableCell' ), secondRow, 'end' );
  444. } );
  445. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  446. [ { rowspan: 2, contents: '00', isHeading: true }, '01' ],
  447. [ '22' ],
  448. [ { contents: '', isHeading: true }, '' ],
  449. [ { contents: '', isHeading: true }, '' ]
  450. ], { asWidget: true } ) );
  451. } );
  452. it( 'should create table cell inside inserted row as a widget', () => {
  453. setModelData( model, modelTable( [ [ '00' ] ] ) );
  454. const table = root.getChild( 0 );
  455. model.change( writer => {
  456. const firstRow = writer.createElement( 'tableRow' );
  457. writer.insert( firstRow, table, 1 );
  458. writer.insert( writer.createElement( 'tableCell' ), firstRow, 'end' );
  459. } );
  460. assertEqualMarkup( getViewData( view, { withoutSelection: true } ),
  461. '<figure class="ck-widget ck-widget_with-selection-handle table" contenteditable="false">' +
  462. '<div class="ck ck-widget__selection-handle"></div>' +
  463. '<table>' +
  464. '<tbody>' +
  465. '<tr>' +
  466. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  467. '<span style="display:inline-block">00</span>' +
  468. '</td>' +
  469. '</tr>' +
  470. '<tr>' +
  471. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  472. '<span style="display:inline-block"></span>' +
  473. '</td>' +
  474. '</tr>' +
  475. '</tbody>' +
  476. '</table>' +
  477. '</figure>'
  478. );
  479. } );
  480. } );
  481. } );
  482. describe( 'downcastInsertCell()', () => {
  483. // The beforeEach is duplicated due to ckeditor/ckeditor5#6574. New test are written using TableEditing.
  484. beforeEach( () => {
  485. return VirtualTestEditor.create()
  486. .then( newEditor => {
  487. editor = newEditor;
  488. model = editor.model;
  489. doc = model.document;
  490. root = doc.getRoot( 'main' );
  491. view = editor.editing.view;
  492. defaultSchema( model.schema );
  493. defaultConversion( editor.conversion );
  494. } );
  495. } );
  496. it( 'should add tableCell on proper index in tr', () => {
  497. setModelData( model, modelTable( [
  498. [ '00', '01' ]
  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. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  506. [ '00', '', '01' ]
  507. ] ) );
  508. } );
  509. it( 'should add tableCell on proper index in tr when previous have colspans', () => {
  510. setModelData( model, modelTable( [
  511. [ { colspan: 2, contents: '00' }, '13' ]
  512. ] ) );
  513. const table = root.getChild( 0 );
  514. model.change( writer => {
  515. const row = table.getChild( 0 );
  516. writer.insertElement( 'tableCell', row, 1 );
  517. } );
  518. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  519. [ { colspan: 2, contents: '00' }, '', '13' ]
  520. ] ) );
  521. } );
  522. it( 'should add tableCell on proper index in tr when previous row have rowspans', () => {
  523. setModelData( model, modelTable( [
  524. [ { rowspan: 2, contents: '00' }, '13' ],
  525. [ '11', '12' ]
  526. ] ) );
  527. const table = root.getChild( 0 );
  528. model.change( writer => {
  529. writer.insertElement( 'tableCell', table.getChild( 0 ), 1 );
  530. writer.insertElement( 'tableCell', table.getChild( 1 ), 0 );
  531. } );
  532. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  533. [ { rowspan: 2, contents: '00' }, '', '13' ],
  534. [ '', '11', '12' ]
  535. ] ) );
  536. } );
  537. it( 'split cell simulation - simple', () => {
  538. setModelData( model, modelTable( [
  539. [ '00', '01' ],
  540. [ '10', '11' ]
  541. ] ) );
  542. const table = root.getChild( 0 );
  543. model.change( writer => {
  544. const firstRow = table.getChild( 0 );
  545. const secondRow = table.getChild( 1 );
  546. writer.insertElement( 'tableCell', firstRow, 1 );
  547. writer.setAttribute( 'colspan', 2, secondRow.getChild( 0 ) );
  548. } );
  549. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  550. [ '00', '', '01' ],
  551. [ { colspan: 2, contents: '10' }, '11' ]
  552. ] ) );
  553. } );
  554. it( 'merge simulation - simple', () => {
  555. setModelData( model, modelTable( [
  556. [ '00', '01' ],
  557. [ '10', '11' ]
  558. ] ) );
  559. const table = root.getChild( 0 );
  560. model.change( writer => {
  561. const firstRow = table.getChild( 0 );
  562. writer.setAttribute( 'colspan', 2, firstRow.getChild( 0 ) );
  563. writer.remove( firstRow.getChild( 1 ) );
  564. } );
  565. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  566. [ { colspan: 2, contents: '00' } ],
  567. [ '10', '11' ]
  568. ] ) );
  569. } );
  570. describe( 'options.asWidget=true', () => {
  571. beforeEach( () => {
  572. return VirtualTestEditor.create()
  573. .then( newEditor => {
  574. editor = newEditor;
  575. model = editor.model;
  576. doc = model.document;
  577. root = doc.getRoot( 'main' );
  578. view = editor.editing.view;
  579. defaultSchema( model.schema );
  580. defaultConversion( editor.conversion, true );
  581. } );
  582. } );
  583. it( 'should create inserted table cell as a widget', () => {
  584. setModelData( model, modelTable( [ [ '00' ] ] ) );
  585. const table = root.getChild( 0 );
  586. model.change( writer => {
  587. const row = table.getChild( 0 );
  588. writer.insert( writer.createElement( 'tableCell' ), row, 'end' );
  589. } );
  590. assertEqualMarkup( getViewData( view, { withoutSelection: true } ),
  591. '<figure class="ck-widget ck-widget_with-selection-handle table" contenteditable="false">' +
  592. '<div class="ck ck-widget__selection-handle"></div>' +
  593. '<table>' +
  594. '<tbody>' +
  595. '<tr>' +
  596. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  597. '<span style="display:inline-block">00</span>' +
  598. '</td>' +
  599. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true"></td>' +
  600. '</tr>' +
  601. '</tbody>' +
  602. '</table>' +
  603. '</figure>'
  604. );
  605. } );
  606. } );
  607. } );
  608. describe( 'downcastTableHeadingColumnsChange()', () => {
  609. beforeEach( () => {
  610. return VirtualTestEditor.create( { plugins: [ Paragraph, TableEditing ] } )
  611. .then( newEditor => {
  612. editor = newEditor;
  613. model = editor.model;
  614. doc = model.document;
  615. root = doc.getRoot( 'main' );
  616. view = editor.editing.view;
  617. } );
  618. } );
  619. // The heading columns change downcast conversion is not executed in data pipeline.
  620. describe( 'editing pipeline', () => {
  621. it( 'should work for adding heading columns', () => {
  622. setModelData( model, modelTable( [
  623. [ '00', '01' ],
  624. [ '10', '11' ]
  625. ] ) );
  626. const table = root.getChild( 0 );
  627. model.change( writer => {
  628. writer.setAttribute( 'headingColumns', 1, table );
  629. } );
  630. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  631. [ { isHeading: true, contents: '00' }, '01' ],
  632. [ { isHeading: true, contents: '10' }, '11' ]
  633. ], { headingColumns: 1, asWidget: true } ) );
  634. } );
  635. it( 'should work for changing heading columns to a bigger number', () => {
  636. setModelData( model, modelTable( [
  637. [ '00', '01', '02', '03' ],
  638. [ '10', '11', '12', '13' ]
  639. ], { headingColumns: 1 } ) );
  640. const table = root.getChild( 0 );
  641. model.change( writer => {
  642. writer.setAttribute( 'headingColumns', 3, table );
  643. } );
  644. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  645. [ { isHeading: true, contents: '00' }, { isHeading: true, contents: '01' }, { isHeading: true, contents: '02' }, '03' ],
  646. [ { isHeading: true, contents: '10' }, { isHeading: true, contents: '11' }, { isHeading: true, contents: '12' }, '13' ]
  647. ], { asWidget: true } ) );
  648. } );
  649. it( 'should work for changing heading columns to a smaller number', () => {
  650. setModelData( model, modelTable( [
  651. [ { isHeading: true, contents: '00' }, { isHeading: true, contents: '01' }, { isHeading: true, contents: '02' }, '03' ],
  652. [ { isHeading: true, contents: '10' }, { isHeading: true, contents: '11' }, { isHeading: true, contents: '12' }, '13' ]
  653. ], { headingColumns: 3 } ) );
  654. const table = root.getChild( 0 );
  655. model.change( writer => {
  656. writer.setAttribute( 'headingColumns', 1, table );
  657. } );
  658. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  659. [ { isHeading: true, contents: '00' }, '01', '02', '03' ],
  660. [ { isHeading: true, contents: '10' }, '11', '12', '13' ]
  661. ], { headingColumns: 3, asWidget: true } ) );
  662. } );
  663. it( 'should work for removing heading columns', () => {
  664. setModelData( model, modelTable( [
  665. [ '00', '01' ],
  666. [ '10', '11' ]
  667. ], { headingColumns: 1 } ) );
  668. const table = root.getChild( 0 );
  669. model.change( writer => {
  670. writer.removeAttribute( 'headingColumns', table );
  671. } );
  672. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  673. [ '00', '01' ],
  674. [ '10', '11' ]
  675. ], { asWidget: true } ) );
  676. } );
  677. it( 'should be possible to overwrite', () => {
  678. editor.conversion.attributeToAttribute( { model: 'headingColumns', view: 'headingColumns', converterPriority: 'high' } );
  679. setModelData( model, modelTable( [ [ '00[] ' ] ] ) );
  680. const table = root.getChild( 0 );
  681. model.change( writer => {
  682. writer.setAttribute( 'headingColumns', 1, table );
  683. } );
  684. assertEqualMarkup( getViewData( view, { withoutSelection: true } ),
  685. '<figure class="ck-widget ck-widget_with-selection-handle table" contenteditable="false" headingColumns="1">' +
  686. '<div class="ck ck-widget__selection-handle"></div>' +
  687. '<table>' +
  688. '<tbody>' +
  689. '<tr>' +
  690. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  691. '<span style="display:inline-block">00</span>' +
  692. '</td>' +
  693. '</tr>' +
  694. '</tbody>' +
  695. '</table>' +
  696. '</figure>'
  697. );
  698. } );
  699. it( 'should work with adding table cells', () => {
  700. setModelData( model, modelTable( [
  701. [ { rowspan: 2, contents: '00' }, '01', '13', '14' ],
  702. [ '11', '12', '13' ],
  703. [ { colspan: 2, contents: '20' }, '22', '23' ]
  704. ], { headingColumns: 2 } ) );
  705. const table = root.getChild( 0 );
  706. model.change( writer => {
  707. // Inserting column in heading columns so update table's attribute also
  708. writer.setAttribute( 'headingColumns', 3, table );
  709. writer.insertElement( 'tableCell', table.getChild( 0 ), 2 );
  710. writer.insertElement( 'tableCell', table.getChild( 1 ), 1 );
  711. writer.insertElement( 'tableCell', table.getChild( 2 ), 1 );
  712. } );
  713. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  714. [
  715. { isHeading: true, rowspan: 2, contents: '00' },
  716. { isHeading: true, contents: '01' },
  717. { isHeading: true, contents: '' },
  718. '13',
  719. '14'
  720. ],
  721. [
  722. { isHeading: true, contents: '11' },
  723. { isHeading: true, contents: '' },
  724. '12',
  725. '13'
  726. ],
  727. [
  728. { isHeading: true, colspan: 2, contents: '20' },
  729. { isHeading: true, contents: '' },
  730. '22',
  731. '23'
  732. ]
  733. ], { asWidget: true } ) );
  734. } );
  735. it( 'should create renamed cell as a widget', () => {
  736. setModelData( model, modelTable( [ [ '00' ] ] ) );
  737. const table = root.getChild( 0 );
  738. model.change( writer => {
  739. writer.setAttribute( 'headingRows', 1, table );
  740. } );
  741. assertEqualMarkup( getViewData( view, { withoutSelection: true } ),
  742. '<figure class="ck-widget ck-widget_with-selection-handle table" contenteditable="false">' +
  743. '<div class="ck ck-widget__selection-handle"></div>' +
  744. '<table>' +
  745. '<thead>' +
  746. '<tr>' +
  747. '<th class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  748. '<span style="display:inline-block">00</span>' +
  749. '</th>' +
  750. '</tr>' +
  751. '</thead>' +
  752. '</table>' +
  753. '</figure>'
  754. );
  755. } );
  756. } );
  757. } );
  758. describe( 'downcastTableHeadingRowsChange()', () => {
  759. // The beforeEach is duplicated due to ckeditor/ckeditor5#6574. New test are written using TableEditing.
  760. beforeEach( () => {
  761. return VirtualTestEditor.create( { plugins: [ Paragraph, TableEditing ] } )
  762. .then( newEditor => {
  763. editor = newEditor;
  764. model = editor.model;
  765. doc = model.document;
  766. root = doc.getRoot( 'main' );
  767. view = editor.editing.view;
  768. } );
  769. } );
  770. // The heading rows change downcast conversion is not executed in data pipeline.
  771. describe( 'editing pipeline', () => {
  772. it( 'should work for adding heading rows', () => {
  773. setModelData( model, modelTable( [
  774. [ '00', '01' ],
  775. [ '10', '11' ],
  776. [ '20', '21' ]
  777. ] ) );
  778. const table = root.getChild( 0 );
  779. model.change( writer => {
  780. writer.setAttribute( 'headingRows', 2, table );
  781. } );
  782. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  783. [ '00', '01' ],
  784. [ '10', '11' ],
  785. [ '20', '21' ]
  786. ], { headingRows: 2, asWidget: true } ) );
  787. } );
  788. it( 'should work for changing number of heading rows to a bigger number', () => {
  789. setModelData( model, modelTable( [
  790. [ '00', '01' ],
  791. [ '10', '11' ],
  792. [ '20', '21' ]
  793. ], { headingRows: 1 } ) );
  794. const table = root.getChild( 0 );
  795. model.change( writer => {
  796. writer.setAttribute( 'headingRows', 2, table );
  797. } );
  798. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  799. [ '00', '01' ],
  800. [ '10', '11' ],
  801. [ '20', '21' ]
  802. ], { headingRows: 2, asWidget: true } ) );
  803. } );
  804. it( 'should work for changing number of heading rows to a smaller number', () => {
  805. setModelData( model, modelTable( [
  806. [ '00', '01' ],
  807. [ '10', '11' ],
  808. [ '20', '21' ],
  809. [ '30', '31' ]
  810. ], { headingRows: 3 } ) );
  811. const table = root.getChild( 0 );
  812. model.change( writer => {
  813. writer.setAttribute( 'headingRows', 2, table );
  814. } );
  815. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  816. [ '00', '01' ],
  817. [ '10', '11' ],
  818. [ '20', '21' ],
  819. [ '30', '31' ]
  820. ], { headingRows: 2, asWidget: true } ) );
  821. } );
  822. it( 'should work for removing heading rows', () => {
  823. setModelData( model, modelTable( [
  824. [ '00', '01' ],
  825. [ '10', '11' ]
  826. ], { headingRows: 2 } ) );
  827. const table = root.getChild( 0 );
  828. model.change( writer => {
  829. writer.removeAttribute( 'headingRows', table );
  830. } );
  831. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  832. [ '00', '01' ],
  833. [ '10', '11' ]
  834. ], { asWidget: true } ) );
  835. } );
  836. it( 'should work for making heading rows without tbody', () => {
  837. setModelData( model, modelTable( [
  838. [ '00', '01' ],
  839. [ '10', '11' ]
  840. ] ) );
  841. const table = root.getChild( 0 );
  842. model.change( writer => {
  843. writer.setAttribute( 'headingRows', 2, table );
  844. } );
  845. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  846. [ '00', '01' ],
  847. [ '10', '11' ]
  848. ], { headingRows: 2, asWidget: true } ) );
  849. } );
  850. it( 'should be possible to overwrite', () => {
  851. editor.conversion.attributeToAttribute( {
  852. model: 'headingRows',
  853. view: 'headingRows',
  854. converterPriority: 'high'
  855. } );
  856. setModelData( model, modelTable( [ [ '00' ] ] ) );
  857. const table = root.getChild( 0 );
  858. model.change( writer => {
  859. writer.setAttribute( 'headingRows', 1, table );
  860. } );
  861. assertEqualMarkup( getViewData( view, { withoutSelection: true } ),
  862. '<figure class="ck-widget ck-widget_with-selection-handle table" contenteditable="false" headingRows="1">' +
  863. '<div class="ck ck-widget__selection-handle"></div>' +
  864. '<table>' +
  865. '<tbody>' +
  866. '<tr>' +
  867. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  868. '<span style="display:inline-block">00</span>' +
  869. '</td>' +
  870. '</tr>' +
  871. '</tbody>' +
  872. '</table>' +
  873. '</figure>'
  874. );
  875. } );
  876. it( 'should work with adding table rows at the beginning of a table', () => {
  877. setModelData( model, modelTable( [
  878. [ '00', '01' ],
  879. [ '10', '11' ]
  880. ], { headingRows: 1 } ) );
  881. const table = root.getChild( 0 );
  882. model.change( writer => {
  883. writer.setAttribute( 'headingRows', 2, table );
  884. const tableRow = writer.createElement( 'tableRow' );
  885. writer.insert( tableRow, table, 0 );
  886. writer.insertElement( 'tableCell', tableRow, 'end' );
  887. writer.insertElement( 'tableCell', tableRow, 'end' );
  888. } );
  889. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  890. [ '', '' ],
  891. [ '00', '01' ],
  892. [ '10', '11' ]
  893. ], { headingRows: 2, asWidget: true } ) );
  894. } );
  895. it( 'should work with adding a table row and expanding heading', () => {
  896. setModelData( model, modelTable( [
  897. [ '00', '01' ],
  898. [ '10', '11' ],
  899. [ '20', '21' ]
  900. ], { headingRows: 1 } ) );
  901. const table = root.getChild( 0 );
  902. model.change( writer => {
  903. writer.setAttribute( 'headingRows', 2, table );
  904. const tableRow = writer.createElement( 'tableRow' );
  905. writer.insert( tableRow, table, 1 );
  906. writer.insertElement( 'tableCell', tableRow, 'end' );
  907. writer.insertElement( 'tableCell', tableRow, 'end' );
  908. } );
  909. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  910. [ '00', '01' ],
  911. [ '', '' ],
  912. [ '10', '11' ],
  913. [ '20', '21' ]
  914. ], { headingRows: 2, asWidget: true } ) );
  915. } );
  916. it( 'should create renamed cell as a widget', () => {
  917. setModelData( model, modelTable( [ [ '00' ] ] ) );
  918. const table = root.getChild( 0 );
  919. model.change( writer => {
  920. writer.setAttribute( 'headingColumns', 1, table );
  921. } );
  922. assertEqualMarkup( getViewData( view, { withoutSelection: true } ),
  923. '<figure class="ck-widget ck-widget_with-selection-handle table" contenteditable="false">' +
  924. '<div class="ck ck-widget__selection-handle"></div>' +
  925. '<table>' +
  926. '<tbody>' +
  927. '<tr>' +
  928. '<th class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  929. '<span style="display:inline-block">00</span>' +
  930. '</th>' +
  931. '</tr>' +
  932. '</tbody>' +
  933. '</table>' +
  934. '</figure>'
  935. );
  936. } );
  937. } );
  938. } );
  939. describe( 'downcastRemoveRow()', () => {
  940. // The beforeEach is duplicated due to ckeditor/ckeditor5#6574. New test are written using TableEditing.
  941. beforeEach( async () => {
  942. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, TableEditing ] } );
  943. model = editor.model;
  944. root = model.document.getRoot( 'main' );
  945. view = editor.editing.view;
  946. } );
  947. // The remove row downcast conversion is not executed in data pipeline.
  948. describe( 'editing pipeline', () => {
  949. it( 'should react to removed row from the beginning of a body rows (no heading rows)', () => {
  950. setModelData( model, modelTable( [
  951. [ '00[]', '01' ],
  952. [ '10', '11' ]
  953. ] ) );
  954. const table = root.getChild( 0 );
  955. model.change( writer => {
  956. writer.remove( table.getChild( 1 ) );
  957. } );
  958. assertEqualMarkup( getViewData( view, { withoutSelection: true } ),
  959. '<figure class="ck-widget ck-widget_with-selection-handle table" contenteditable="false">' +
  960. '<div class="ck ck-widget__selection-handle"></div>' +
  961. '<table>' +
  962. '<tbody>' +
  963. '<tr>' +
  964. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  965. '<span style="display:inline-block">00</span>' +
  966. '</td>' +
  967. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  968. '<span style="display:inline-block">01</span>' +
  969. '</td>' +
  970. '</tr>' +
  971. '</tbody>' +
  972. '</table>' +
  973. '</figure>'
  974. );
  975. } );
  976. it( 'should react to removed row form the end of a body rows (no heading rows)', () => {
  977. setModelData( model, modelTable( [
  978. [ '00[]', '01' ],
  979. [ '10', '11' ]
  980. ] ) );
  981. const table = root.getChild( 0 );
  982. model.change( writer => {
  983. writer.remove( table.getChild( 0 ) );
  984. } );
  985. assertEqualMarkup( getViewData( view, { withoutSelection: true } ),
  986. '<figure class="ck-widget ck-widget_with-selection-handle table" contenteditable="false">' +
  987. '<div class="ck ck-widget__selection-handle"></div>' +
  988. '<table>' +
  989. '<tbody>' +
  990. '<tr>' +
  991. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  992. '<span style="display:inline-block">10</span>' +
  993. '</td>' +
  994. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  995. '<span style="display:inline-block">11</span>' +
  996. '</td>' +
  997. '</tr>' +
  998. '</tbody>' +
  999. '</table>' +
  1000. '</figure>'
  1001. );
  1002. } );
  1003. it( 'should react to removed row from the beginning of a heading rows (no body rows)', () => {
  1004. setModelData( model, modelTable( [
  1005. [ '00[]', '01' ],
  1006. [ '10', '11' ]
  1007. ], { headingRows: 2 } ) );
  1008. const table = root.getChild( 0 );
  1009. model.change( writer => {
  1010. // Removing row from a heading section changes requires changing heading rows attribute.
  1011. writer.setAttribute( 'headingRows', 1, table );
  1012. writer.remove( table.getChild( 0 ) );
  1013. } );
  1014. assertEqualMarkup( getViewData( view, { withoutSelection: true } ),
  1015. '<figure class="ck-widget ck-widget_with-selection-handle table" contenteditable="false">' +
  1016. '<div class="ck ck-widget__selection-handle"></div>' +
  1017. '<table>' +
  1018. '<thead>' +
  1019. '<tr>' +
  1020. '<th class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  1021. '<span style="display:inline-block">10</span>' +
  1022. '</th>' +
  1023. '<th class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  1024. '<span style="display:inline-block">11</span>' +
  1025. '</th>' +
  1026. '</tr>' +
  1027. '</thead>' +
  1028. '</table>' +
  1029. '</figure>'
  1030. );
  1031. } );
  1032. it( 'should react to removed row form the end of a heading rows (no body rows)', () => {
  1033. setModelData( model, modelTable( [
  1034. [ '00[]', '01' ],
  1035. [ '10', '11' ]
  1036. ], { headingRows: 2 } ) );
  1037. const table = root.getChild( 0 );
  1038. model.change( writer => {
  1039. // Removing row from a heading section changes requires changing heading rows attribute.
  1040. writer.setAttribute( 'headingRows', 1, table );
  1041. writer.remove( table.getChild( 1 ) );
  1042. } );
  1043. assertEqualMarkup( getViewData( view, { withoutSelection: true } ),
  1044. '<figure class="ck-widget ck-widget_with-selection-handle table" contenteditable="false">' +
  1045. '<div class="ck ck-widget__selection-handle"></div>' +
  1046. '<table>' +
  1047. '<thead>' +
  1048. '<tr>' +
  1049. '<th class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  1050. '<span style="display:inline-block">00</span>' +
  1051. '</th>' +
  1052. '<th class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  1053. '<span style="display:inline-block">01</span>' +
  1054. '</th>' +
  1055. '</tr>' +
  1056. '</thead>' +
  1057. '</table>' +
  1058. '</figure>'
  1059. );
  1060. } );
  1061. it( 'should react to removed row form the end of a heading rows (first cell in body has colspan)', () => {
  1062. setModelData( model, modelTable( [
  1063. [ '00[]', '01', '02', '03' ],
  1064. [ { rowspan: 2, colspan: 2, contents: '10' }, '12', '13' ],
  1065. [ '22', '23' ]
  1066. ], { headingRows: 1 } ) );
  1067. const table = root.getChild( 0 );
  1068. model.change( writer => {
  1069. // Removing row from a heading section changes requires changing heading rows attribute.
  1070. writer.remove( table.getChild( 0 ) );
  1071. writer.setAttribute( 'headingRows', 0, table );
  1072. } );
  1073. assertEqualMarkup( getViewData( view, { withoutSelection: true } ),
  1074. '<figure class="ck-widget ck-widget_with-selection-handle table" contenteditable="false">' +
  1075. '<div class="ck ck-widget__selection-handle"></div>' +
  1076. '<table>' +
  1077. '<tbody>' +
  1078. '<tr>' +
  1079. '<td class="ck-editor__editable ck-editor__nested-editable" ' +
  1080. 'colspan="2" contenteditable="true" rowspan="2">' +
  1081. '<span style="display:inline-block">10</span>' +
  1082. '</td>' +
  1083. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  1084. '<span style="display:inline-block">12</span>' +
  1085. '</td>' +
  1086. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  1087. '<span style="display:inline-block">13</span>' +
  1088. '</td>' +
  1089. '</tr>' +
  1090. '<tr>' +
  1091. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  1092. '<span style="display:inline-block">22</span>' +
  1093. '</td>' +
  1094. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  1095. '<span style="display:inline-block">23</span>' +
  1096. '</td>' +
  1097. '</tr>' +
  1098. '</tbody>' +
  1099. '</table>' +
  1100. '</figure>'
  1101. );
  1102. } );
  1103. it( 'should remove empty thead if a last row was removed from a heading rows (has heading and body)', () => {
  1104. setModelData( model, modelTable( [
  1105. [ '00[]', '01' ],
  1106. [ '10', '11' ]
  1107. ], { headingRows: 1 } ) );
  1108. const table = root.getChild( 0 );
  1109. model.change( writer => {
  1110. // Removing row from a heading section changes requires changing heading rows attribute.
  1111. writer.removeAttribute( 'headingRows', table );
  1112. writer.remove( table.getChild( 0 ) );
  1113. } );
  1114. assertEqualMarkup( getViewData( view, { withoutSelection: true } ),
  1115. '<figure class="ck-widget ck-widget_with-selection-handle table" contenteditable="false">' +
  1116. '<div class="ck ck-widget__selection-handle"></div>' +
  1117. '<table>' +
  1118. '<tbody>' +
  1119. '<tr>' +
  1120. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  1121. '<span style="display:inline-block">10</span>' +
  1122. '</td>' +
  1123. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  1124. '<span style="display:inline-block">11</span>' +
  1125. '</td>' +
  1126. '</tr>' +
  1127. '</tbody>' +
  1128. '</table>' +
  1129. '</figure>'
  1130. );
  1131. } );
  1132. it( 'should remove empty tbody if a last row was removed a body rows (has heading and body)', () => {
  1133. setModelData( model, modelTable( [
  1134. [ '00[]', '01' ],
  1135. [ '10', '11' ]
  1136. ], { headingRows: 1 } ) );
  1137. const table = root.getChild( 0 );
  1138. model.change( writer => {
  1139. writer.remove( table.getChild( 1 ) );
  1140. } );
  1141. assertEqualMarkup( getViewData( view, { withoutSelection: true } ),
  1142. '<figure class="ck-widget ck-widget_with-selection-handle table" contenteditable="false">' +
  1143. '<div class="ck ck-widget__selection-handle"></div>' +
  1144. '<table>' +
  1145. '<thead>' +
  1146. '<tr>' +
  1147. '<th class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  1148. '<span style="display:inline-block">00</span>' +
  1149. '</th>' +
  1150. '<th class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  1151. '<span style="display:inline-block">01</span>' +
  1152. '</th>' +
  1153. '</tr>' +
  1154. '</thead>' +
  1155. '</table>' +
  1156. '</figure>'
  1157. );
  1158. } );
  1159. } );
  1160. } );
  1161. describe( 'options.asWidget=true', () => {
  1162. beforeEach( () => {
  1163. return VirtualTestEditor.create()
  1164. .then( newEditor => {
  1165. editor = newEditor;
  1166. model = editor.model;
  1167. doc = model.document;
  1168. root = doc.getRoot( 'main' );
  1169. view = editor.editing.view;
  1170. defaultSchema( model.schema );
  1171. defaultConversion( editor.conversion, true );
  1172. editor.conversion.for( 'downcast' ).add( paragraphInTableCell() );
  1173. } );
  1174. } );
  1175. it( 'should rename <span> to <p> when more then one block content inside table cell', () => {
  1176. setModelData( model, modelTable( [
  1177. [ '00[]' ]
  1178. ] ) );
  1179. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  1180. [ '00' ]
  1181. ], { asWidget: true } ) );
  1182. const table = root.getChild( 0 );
  1183. model.change( writer => {
  1184. const nodeByPath = table.getNodeByPath( [ 0, 0, 0 ] );
  1185. const paragraph = writer.createElement( 'paragraph' );
  1186. writer.insert( paragraph, nodeByPath, 'after' );
  1187. writer.setSelection( nodeByPath.nextSibling, 0 );
  1188. } );
  1189. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  1190. [ '<p>00</p><p></p>' ]
  1191. ], { asWidget: true } ) );
  1192. } );
  1193. it( 'should rename <span> to <p> for single paragraph with attribute', () => {
  1194. model.schema.extend( '$block', { allowAttributes: 'foo' } );
  1195. editor.conversion.attributeToAttribute( { model: 'foo', view: 'foo' } );
  1196. setModelData( model, modelTable( [
  1197. [ '<paragraph foo="bar">00[]</paragraph>' ]
  1198. ] ) );
  1199. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  1200. [ '<p foo="bar">00</p>' ]
  1201. ], { asWidget: true } ) );
  1202. } );
  1203. } );
  1204. } );