converters.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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' ],
  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. } );
  209. describe( 'downcastTable()', () => {
  210. it( 'should create table with tbody', () => {
  211. setModelData( model,
  212. '<table>' +
  213. '<tableRow><tableCell></tableCell></tableRow>' +
  214. '</table>'
  215. );
  216. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  217. '<table>' +
  218. '<tbody>' +
  219. '<tr><td></td></tr>' +
  220. '</tbody>' +
  221. '</table>'
  222. );
  223. } );
  224. it( 'should create table with tbody and thead', () => {
  225. setModelData( model,
  226. '<table headingRows="1">' +
  227. '<tableRow><tableCell>1</tableCell></tableRow>' +
  228. '<tableRow><tableCell>2</tableCell></tableRow>' +
  229. '</table>'
  230. );
  231. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  232. '<table>' +
  233. '<thead>' +
  234. '<tr><th>1</th></tr>' +
  235. '</thead>' +
  236. '<tbody>' +
  237. '<tr><td>2</td></tr>' +
  238. '</tbody>' +
  239. '</table>'
  240. );
  241. } );
  242. it( 'should create table with thead', () => {
  243. setModelData( model,
  244. '<table headingRows="2">' +
  245. '<tableRow><tableCell>1</tableCell></tableRow>' +
  246. '<tableRow><tableCell>2</tableCell></tableRow>' +
  247. '</table>'
  248. );
  249. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  250. '<table>' +
  251. '<thead>' +
  252. '<tr><th>1</th></tr>' +
  253. '<tr><th>2</th></tr>' +
  254. '</thead>' +
  255. '</table>'
  256. );
  257. } );
  258. it( 'should be possible to overwrite', () => {
  259. editor.conversion.elementToElement( { model: 'tableRow', view: 'tr' } );
  260. editor.conversion.for( 'downcast' ).add( dispatcher => {
  261. dispatcher.on( 'insert:table', ( evt, data, conversionApi ) => {
  262. conversionApi.consumable.consume( data.item, 'insert' );
  263. const tableElement = conversionApi.writer.createContainerElement( 'table', { foo: 'bar' } );
  264. const viewPosition = conversionApi.mapper.toViewPosition( data.range.start );
  265. conversionApi.mapper.bindElements( data.item, tableElement );
  266. conversionApi.writer.insert( viewPosition, tableElement );
  267. }, { priority: 'high' } );
  268. } );
  269. setModelData( model,
  270. '<table>' +
  271. '<tableRow><tableCell></tableCell></tableRow>' +
  272. '</table>'
  273. );
  274. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  275. '<table foo="bar">' +
  276. '<tr><td></td></tr>' +
  277. '</table>'
  278. );
  279. } );
  280. } );
  281. describe( 'downcastTableCell()', () => {
  282. it( 'should be possible to overwrite row conversion', () => {
  283. editor.conversion.elementToElement( { model: 'tableCell', view: { name: 'td', class: 'foo' }, priority: 'high' } );
  284. setModelData( model,
  285. '<table>' +
  286. '<tableRow><tableCell></tableCell></tableRow>' +
  287. '</table>'
  288. );
  289. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  290. '<table>' +
  291. '<tbody>' +
  292. '<tr><td class="foo"></td></tr>' +
  293. '</tbody>' +
  294. '</table>'
  295. );
  296. } );
  297. } );
  298. } );