converters.js 11 KB

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