converters.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 { createTable, downcastTable, downcastTableCell } 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' ],
  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( upcastElementToElement( { model: createTable, view: 'table' } ) );
  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( 'createTable()', () => {
  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. '<thead><tr><td>2</td></tr></thead>' +
  105. '</table>'
  106. );
  107. expectModel(
  108. '<table headingRows="1">' +
  109. '<tableRow><tableCell>1</tableCell></tableRow>' +
  110. '<tableRow><tableCell>2</tableCell></tableRow>' +
  111. '</table>'
  112. );
  113. } );
  114. it.skip( 'should create table model from table with thead after the tbody', () => {
  115. editor.setData(
  116. '<table>' +
  117. '<tbody><tr><td>2</td></tr></tbody>' +
  118. '<thead><tr><td>1</td></tr></thead>' +
  119. '</table>'
  120. );
  121. expectModel(
  122. '<table headingRows="1">' +
  123. '<tableRow><tableCell>1</tableCell></tableRow>' +
  124. '<tableRow><tableCell>2</tableCell></tableRow>' +
  125. '</table>'
  126. );
  127. } );
  128. } );
  129. describe( 'downcastTable()', () => {
  130. it( 'should create table with tbody', () => {
  131. setModelData( model,
  132. '<table>' +
  133. '<tableRow><tableCell></tableCell></tableRow>' +
  134. '</table>'
  135. );
  136. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  137. '<table>' +
  138. '<tbody>' +
  139. '<tr><td></td></tr>' +
  140. '</tbody>' +
  141. '</table>'
  142. );
  143. } );
  144. it( 'should create table with tbody and thead', () => {
  145. setModelData( model,
  146. '<table headingRows="1">' +
  147. '<tableRow><tableCell>1</tableCell></tableRow>' +
  148. '<tableRow><tableCell>2</tableCell></tableRow>' +
  149. '</table>'
  150. );
  151. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  152. '<table>' +
  153. '<thead>' +
  154. '<tr><td>1</td></tr>' +
  155. '</thead>' +
  156. '<tbody>' +
  157. '<tr><td>2</td></tr>' +
  158. '</tbody>' +
  159. '</table>'
  160. );
  161. } );
  162. it( 'should create table with thead', () => {
  163. setModelData( model,
  164. '<table headingRows="2">' +
  165. '<tableRow><tableCell>1</tableCell></tableRow>' +
  166. '<tableRow><tableCell>2</tableCell></tableRow>' +
  167. '</table>'
  168. );
  169. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  170. '<table>' +
  171. '<thead>' +
  172. '<tr><td>1</td></tr>' +
  173. '<tr><td>2</td></tr>' +
  174. '</thead>' +
  175. '</table>'
  176. );
  177. } );
  178. it( 'should be possible to overwrite', () => {
  179. editor.conversion.elementToElement( { model: 'tableRow', view: 'tr' } );
  180. editor.conversion.for( 'downcast' ).add( dispatcher => {
  181. dispatcher.on( 'insert:table', ( evt, data, conversionApi ) => {
  182. conversionApi.consumable.consume( data.item, 'insert' );
  183. const tableElement = conversionApi.writer.createContainerElement( 'table', { foo: 'bar' } );
  184. const viewPosition = conversionApi.mapper.toViewPosition( data.range.start );
  185. conversionApi.mapper.bindElements( data.item, tableElement );
  186. conversionApi.writer.insert( viewPosition, tableElement );
  187. }, { priority: 'high' } );
  188. } );
  189. setModelData( model,
  190. '<table>' +
  191. '<tableRow><tableCell></tableCell></tableRow>' +
  192. '</table>'
  193. );
  194. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  195. '<table foo="bar">' +
  196. '<tr><td></td></tr>' +
  197. '</table>'
  198. );
  199. } );
  200. } );
  201. describe( 'downcastTableCell()', () => {
  202. it( 'should be possible to overwrite row conversion', () => {
  203. editor.conversion.elementToElement( { model: 'tableCell', view: { name: 'td', class: 'foo' }, priority: 'high' } );
  204. setModelData( model,
  205. '<table>' +
  206. '<tableRow><tableCell></tableCell></tableRow>' +
  207. '</table>'
  208. );
  209. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  210. '<table>' +
  211. '<tbody>' +
  212. '<tr><td class="foo"></td></tr>' +
  213. '</tbody>' +
  214. '</table>'
  215. );
  216. } );
  217. } );
  218. } );