converters.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
  7. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  8. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. import { downcastTable, upcastTable } from '../src/converters';
  10. describe( 'Table converters', () => {
  11. let editor, model, viewDocument;
  12. beforeEach( () => {
  13. return VirtualTestEditor.create()
  14. .then( newEditor => {
  15. editor = newEditor;
  16. model = editor.model;
  17. viewDocument = editor.editing.view;
  18. const conversion = editor.conversion;
  19. const schema = model.schema;
  20. schema.register( 'table', {
  21. allowWhere: '$block',
  22. allowAttributes: [ 'headingRows', 'headingColumns' ],
  23. isBlock: true,
  24. isObject: true
  25. } );
  26. schema.register( 'tableRow', {
  27. allowIn: 'table',
  28. allowAttributes: [],
  29. isBlock: true,
  30. isLimit: true
  31. } );
  32. schema.register( 'tableCell', {
  33. allowIn: 'tableRow',
  34. allowContentOf: '$block',
  35. allowAttributes: [ 'colspan', 'rowspan' ],
  36. isBlock: true,
  37. isLimit: true
  38. } );
  39. conversion.for( 'upcast' ).add( upcastTable() );
  40. conversion.for( 'downcast' ).add( downcastTable() );
  41. // Table row upcast only since downcast conversion is done in `downcastTable()`.
  42. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableRow', view: 'tr' } ) );
  43. // Table cell conversion.
  44. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'td' } ) );
  45. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'th' } ) );
  46. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  47. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  48. } );
  49. } );
  50. describe( 'upcastTable()', () => {
  51. function expectModel( data ) {
  52. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( data );
  53. }
  54. beforeEach( () => {
  55. // Since this part of test tests only view->model conversion editing pipeline is not necessary
  56. // so defining model->view converters won't be necessary.
  57. editor.editing.destroy();
  58. } );
  59. it( 'should create table model from table without thead', () => {
  60. editor.setData(
  61. '<table>' +
  62. '<tr><td>1</td></tr>' +
  63. '</table>'
  64. );
  65. expectModel(
  66. '<table>' +
  67. '<tableRow><tableCell>1</tableCell></tableRow>' +
  68. '</table>'
  69. );
  70. } );
  71. it( 'should create table model from table with one thead with one row', () => {
  72. editor.setData(
  73. '<table>' +
  74. '<thead><tr><td>1</td></tr></thead>' +
  75. '</table>'
  76. );
  77. expectModel(
  78. '<table headingRows="1">' +
  79. '<tableRow><tableCell>1</tableCell></tableRow>' +
  80. '</table>'
  81. );
  82. } );
  83. it( 'should create table model from table with one thead with more then on row', () => {
  84. editor.setData(
  85. '<table>' +
  86. '<thead>' +
  87. '<tr><td>1</td></tr>' +
  88. '<tr><td>2</td></tr>' +
  89. '<tr><td>3</td></tr>' +
  90. '</thead>' +
  91. '</table>'
  92. );
  93. expectModel(
  94. '<table headingRows="3">' +
  95. '<tableRow><tableCell>1</tableCell></tableRow>' +
  96. '<tableRow><tableCell>2</tableCell></tableRow>' +
  97. '<tableRow><tableCell>3</tableCell></tableRow>' +
  98. '</table>'
  99. );
  100. } );
  101. it( 'should create table model from table with two theads with one row', () => {
  102. editor.setData(
  103. '<table>' +
  104. '<thead><tr><td>1</td></tr></thead>' +
  105. '<tbody><tr><td>2</td></tr></tbody>' +
  106. '<thead><tr><td>3</td></tr></thead>' +
  107. '</table>'
  108. );
  109. expectModel(
  110. '<table headingRows="1">' +
  111. '<tableRow><tableCell>1</tableCell></tableRow>' +
  112. '<tableRow><tableCell>2</tableCell></tableRow>' +
  113. '<tableRow><tableCell>3</tableCell></tableRow>' +
  114. '</table>'
  115. );
  116. } );
  117. it( 'should create table model from table with thead after the tbody', () => {
  118. editor.setData(
  119. '<table>' +
  120. '<tbody><tr><td>2</td></tr></tbody>' +
  121. '<thead><tr><td>1</td></tr></thead>' +
  122. '</table>'
  123. );
  124. expectModel(
  125. '<table headingRows="1">' +
  126. '<tableRow><tableCell>1</tableCell></tableRow>' +
  127. '<tableRow><tableCell>2</tableCell></tableRow>' +
  128. '</table>'
  129. );
  130. } );
  131. it( 'should create table model from table with one tfoot with one row', () => {
  132. editor.setData(
  133. '<table>' +
  134. '<tfoot><tr><td>1</td></tr></tfoot>' +
  135. '</table>'
  136. );
  137. expectModel(
  138. '<table>' +
  139. '<tableRow><tableCell>1</tableCell></tableRow>' +
  140. '</table>'
  141. );
  142. } );
  143. it( 'should create valid table model from empty table', () => {
  144. editor.setData(
  145. '<table>' +
  146. '</table>'
  147. );
  148. expectModel(
  149. '<table><tableRow><tableCell></tableCell></tableRow></table>'
  150. );
  151. } );
  152. it( 'should skip unknown table children', () => {
  153. editor.setData(
  154. '<table>' +
  155. '<caption>foo</caption>' +
  156. '<tr><td>bar</td></tr>' +
  157. '</table>'
  158. );
  159. expectModel(
  160. '<table><tableRow><tableCell>bar</tableCell></tableRow></table>'
  161. );
  162. } );
  163. it( 'should create table model from some broken table', () => {
  164. editor.setData(
  165. '<table><td><p>foo</p></td></table>'
  166. );
  167. expectModel(
  168. '<table><tableRow><tableCell>foo</tableCell></tableRow></table>'
  169. );
  170. } );
  171. it( 'should fix if inside other blocks', () => {
  172. // Using <div> instead of <p> as it breaks on Edge.
  173. editor.model.schema.register( 'div', {
  174. inheritAllFrom: '$block'
  175. } );
  176. editor.conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'div', view: 'div' } ) );
  177. editor.setData(
  178. '<div>foo' +
  179. '<table>' +
  180. '<tbody><tr><td>2</td></tr></tbody>' +
  181. '<thead><tr><td>1</td></tr></thead>' +
  182. '</table>' +
  183. 'bar</div>'
  184. );
  185. expectModel(
  186. '<div>foo</div>' +
  187. '<table headingRows="1">' +
  188. '<tableRow><tableCell>1</tableCell></tableRow>' +
  189. '<tableRow><tableCell>2</tableCell></tableRow>' +
  190. '</table>' +
  191. '<div>bar</div>'
  192. );
  193. } );
  194. it( 'should be possible to overwrite table conversion', () => {
  195. editor.model.schema.register( 'fooTable', {
  196. allowWhere: '$block',
  197. allowAttributes: [ 'headingRows' ],
  198. isBlock: true,
  199. isObject: true
  200. } );
  201. editor.conversion.elementToElement( { model: 'fooTable', view: 'table', priority: 'high' } );
  202. editor.setData(
  203. '<table>' +
  204. '<thead><tr><td>foo</td></tr></thead>' +
  205. '</table>'
  206. );
  207. expectModel(
  208. '<fooTable></fooTable>'
  209. );
  210. } );
  211. describe( 'headingColumns', () => {
  212. it( 'should properly calculate heading columns', () => {
  213. editor.setData(
  214. '<table>' +
  215. '<tbody>' +
  216. // This row starts with 1 th (3 total).
  217. '<tr><th>21</th><td>22</td><th>23</th><th>24</th></tr>' +
  218. // This row starts with 2 th (2 total). This one has max number of heading columns: 2.
  219. '<tr><th>31</th><th>32</th><td>33</td><td>34</td></tr>' +
  220. // This row starts with 1 th (1 total).
  221. '<tr><th>41</th><td>42</td><td>43</td><td>44</td></tr>' +
  222. // This row starts with 0 th (3 total).
  223. '<tr><td>51</td><th>52</th><th>53</th><th>54</th></tr>' +
  224. '</tbody>' +
  225. '<thead>' +
  226. // This row has 4 ths but it is a thead.
  227. '<tr><th>11</th><th>12</th><th>13</th><th>14</th></tr>' +
  228. '</thead>' +
  229. '</table>'
  230. );
  231. expectModel(
  232. '<table headingColumns="2" headingRows="1">' +
  233. '<tableRow>' +
  234. '<tableCell>11</tableCell><tableCell>12</tableCell><tableCell>13</tableCell><tableCell>14</tableCell>' +
  235. '</tableRow>' +
  236. '<tableRow>' +
  237. '<tableCell>21</tableCell><tableCell>22</tableCell><tableCell>23</tableCell><tableCell>24</tableCell>' +
  238. '</tableRow>' +
  239. '<tableRow>' +
  240. '<tableCell>31</tableCell><tableCell>32</tableCell><tableCell>33</tableCell><tableCell>34</tableCell>' +
  241. '</tableRow>' +
  242. '<tableRow>' +
  243. '<tableCell>41</tableCell><tableCell>42</tableCell><tableCell>43</tableCell><tableCell>44</tableCell>' +
  244. '</tableRow>' +
  245. '<tableRow>' +
  246. '<tableCell>51</tableCell><tableCell>52</tableCell><tableCell>53</tableCell><tableCell>54</tableCell>' +
  247. '</tableRow>' +
  248. '</table>'
  249. );
  250. } );
  251. it( 'should calculate heading columns of cells with colspan', () => {
  252. editor.setData(
  253. '<table>' +
  254. '<tbody>' +
  255. // This row has colspan of 3 so it should be the whole table should have 3 heading columns.
  256. '<tr><th>21</th><th>22</th><td>23</td><td>24</td></tr>' +
  257. '<tr><th colspan="2">31</th><th>33</th><td>34</td></tr>' +
  258. '</tbody>' +
  259. '<thead>' +
  260. // This row has 4 ths but it is a thead.
  261. '<tr><th>11</th><th>12</th><th>13</th><th>14</th></tr>' +
  262. '</thead>' +
  263. '</table>'
  264. );
  265. expectModel(
  266. '<table headingColumns="3" headingRows="1">' +
  267. '<tableRow>' +
  268. '<tableCell>11</tableCell><tableCell>12</tableCell><tableCell>13</tableCell><tableCell>14</tableCell>' +
  269. '</tableRow>' +
  270. '<tableRow>' +
  271. '<tableCell>21</tableCell><tableCell>22</tableCell><tableCell>23</tableCell><tableCell>24</tableCell>' +
  272. '</tableRow>' +
  273. '<tableRow>' +
  274. '<tableCell colspan="2">31</tableCell><tableCell>33</tableCell><tableCell>34</tableCell>' +
  275. '</tableRow>' +
  276. '</table>'
  277. );
  278. } );
  279. } );
  280. } );
  281. describe( 'downcastTable()', () => {
  282. it( 'should create table with tbody', () => {
  283. setModelData( model,
  284. '<table>' +
  285. '<tableRow><tableCell></tableCell></tableRow>' +
  286. '</table>'
  287. );
  288. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  289. '<table>' +
  290. '<tbody>' +
  291. '<tr><td></td></tr>' +
  292. '</tbody>' +
  293. '</table>'
  294. );
  295. } );
  296. it( 'should create table with tbody and thead', () => {
  297. setModelData( model,
  298. '<table headingRows="1">' +
  299. '<tableRow><tableCell>1</tableCell></tableRow>' +
  300. '<tableRow><tableCell>2</tableCell></tableRow>' +
  301. '</table>'
  302. );
  303. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  304. '<table>' +
  305. '<thead>' +
  306. '<tr><th>1</th></tr>' +
  307. '</thead>' +
  308. '<tbody>' +
  309. '<tr><td>2</td></tr>' +
  310. '</tbody>' +
  311. '</table>'
  312. );
  313. } );
  314. it( 'should create table with thead', () => {
  315. setModelData( model,
  316. '<table headingRows="2">' +
  317. '<tableRow><tableCell>1</tableCell></tableRow>' +
  318. '<tableRow><tableCell>2</tableCell></tableRow>' +
  319. '</table>'
  320. );
  321. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  322. '<table>' +
  323. '<thead>' +
  324. '<tr><th>1</th></tr>' +
  325. '<tr><th>2</th></tr>' +
  326. '</thead>' +
  327. '</table>'
  328. );
  329. } );
  330. it( 'should create table with heading columns and rows', () => {
  331. setModelData( model,
  332. '<table headingColumns="3" headingRows="1">' +
  333. '<tableRow>' +
  334. '<tableCell>11</tableCell><tableCell>12</tableCell><tableCell>13</tableCell><tableCell>14</tableCell>' +
  335. '</tableRow>' +
  336. '<tableRow>' +
  337. '<tableCell>21</tableCell><tableCell>22</tableCell><tableCell>23</tableCell><tableCell>24</tableCell>' +
  338. '</tableRow>' +
  339. '</table>'
  340. );
  341. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  342. '<table>' +
  343. '<thead>' +
  344. '<tr><th>11</th><th>12</th><th>13</th><th>14</th></tr>' +
  345. '</thead>' +
  346. '<tbody>' +
  347. '<tr><th>21</th><th>22</th><th>23</th><td>24</td></tr>' +
  348. '</tbody>' +
  349. '</table>'
  350. );
  351. } );
  352. it( 'should be possible to overwrite', () => {
  353. editor.conversion.elementToElement( { model: 'tableRow', view: 'tr' } );
  354. editor.conversion.elementToElement( { model: 'tableCell', view: 'td' } );
  355. editor.conversion.for( 'downcast' ).add( dispatcher => {
  356. dispatcher.on( 'insert:table', ( evt, data, conversionApi ) => {
  357. conversionApi.consumable.consume( data.item, 'insert' );
  358. const tableElement = conversionApi.writer.createContainerElement( 'table', { foo: 'bar' } );
  359. const viewPosition = conversionApi.mapper.toViewPosition( data.range.start );
  360. conversionApi.mapper.bindElements( data.item, tableElement );
  361. conversionApi.writer.insert( viewPosition, tableElement );
  362. }, { priority: 'high' } );
  363. } );
  364. setModelData( model,
  365. '<table>' +
  366. '<tableRow><tableCell></tableCell></tableRow>' +
  367. '</table>'
  368. );
  369. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  370. '<table foo="bar">' +
  371. '<tr><td></td></tr>' +
  372. '</table>'
  373. );
  374. } );
  375. describe( 'headingColumns attribute', () => {
  376. it( 'should mark heading columns table cells', () => {
  377. setModelData( model,
  378. '<table headingColumns="2">' +
  379. '<tableRow><tableCell>11</tableCell><tableCell>12</tableCell><tableCell>13</tableCell></tableRow>' +
  380. '<tableRow><tableCell>21</tableCell><tableCell>22</tableCell><tableCell>23</tableCell></tableRow>' +
  381. '</table>'
  382. );
  383. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  384. '<table>' +
  385. '<tbody>' +
  386. '<tr><th>11</th><th>12</th><td>13</td></tr>' +
  387. '<tr><th>21</th><th>22</th><td>23</td></tr>' +
  388. '</tbody>' +
  389. '</table>'
  390. );
  391. } );
  392. it( 'should mark heading columns table cells when one has colspan attribute', () => {
  393. setModelData( model,
  394. '<table headingColumns="3">' +
  395. '<tableRow>' +
  396. '<tableCell>11</tableCell><tableCell>12</tableCell><tableCell>13</tableCell><tableCell>14</tableCell>' +
  397. '</tableRow>' +
  398. '<tableRow><tableCell colspan="2">21</tableCell><tableCell>23</tableCell><tableCell>24</tableCell></tableRow>' +
  399. '</table>'
  400. );
  401. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  402. '<table>' +
  403. '<tbody>' +
  404. '<tr><th>11</th><th>12</th><th>13</th><td>14</td></tr>' +
  405. '<tr><th colspan="2">21</th><th>23</th><td>24</td></tr>' +
  406. '</tbody>' +
  407. '</table>'
  408. );
  409. } );
  410. } );
  411. } );
  412. } );