converters.js 12 KB

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