upcasttable.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import upcastTable from '../../src/converters/upcasttable';
  9. describe( 'upcastTable()', () => {
  10. let editor, model;
  11. beforeEach( () => {
  12. return VirtualTestEditor.create()
  13. .then( newEditor => {
  14. editor = newEditor;
  15. model = editor.model;
  16. const conversion = editor.conversion;
  17. const schema = model.schema;
  18. schema.register( 'table', {
  19. allowWhere: '$block',
  20. allowAttributes: [ 'headingRows', 'headingColumns' ],
  21. isObject: true
  22. } );
  23. schema.register( 'tableRow', { allowIn: 'table' } );
  24. schema.register( 'tableCell', {
  25. allowIn: 'tableRow',
  26. allowContentOf: '$block',
  27. allowAttributes: [ 'colspan', 'rowspan' ],
  28. isLimit: true
  29. } );
  30. conversion.for( 'upcast' ).add( upcastTable() );
  31. // Table row upcast only since downcast conversion is done in `downcastTable()`.
  32. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableRow', view: 'tr' } ) );
  33. // Table cell conversion.
  34. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'td' } ) );
  35. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'th' } ) );
  36. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  37. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  38. // Since this part of test tests only view->model conversion editing pipeline is not necessary
  39. // so defining model->view converters won't be necessary.
  40. editor.editing.destroy();
  41. } );
  42. } );
  43. function expectModel( data ) {
  44. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( data );
  45. }
  46. it( 'should convert table figure', () => {
  47. editor.setData(
  48. '<figure class="table">' +
  49. '<table>' +
  50. '<tr><td>1</td></tr>' +
  51. '</table>' +
  52. '</figure>'
  53. );
  54. expectModel(
  55. '<table>' +
  56. '<tableRow><tableCell>1</tableCell></tableRow>' +
  57. '</table>'
  58. );
  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 not convert empty figure', () => {
  73. '<figure class="table"></figure>';
  74. expectModel( '' );
  75. } );
  76. it( 'should convert if figure do not have class="table" attribute', () => {
  77. editor.setData(
  78. '<figure>' +
  79. '<table>' +
  80. '<tr><td>1</td></tr>' +
  81. '</table>' +
  82. '</figure>'
  83. );
  84. expectModel(
  85. '<table>' +
  86. '<tableRow><tableCell>1</tableCell></tableRow>' +
  87. '</table>'
  88. );
  89. } );
  90. it( 'should create table model from table with one thead with one row', () => {
  91. editor.setData(
  92. '<table>' +
  93. '<thead><tr><td>1</td></tr></thead>' +
  94. '</table>'
  95. );
  96. expectModel(
  97. '<table headingRows="1">' +
  98. '<tableRow><tableCell>1</tableCell></tableRow>' +
  99. '</table>'
  100. );
  101. } );
  102. it( 'should create table model from table with one thead with more then on row', () => {
  103. editor.setData(
  104. '<table>' +
  105. '<thead>' +
  106. '<tr><td>1</td></tr>' +
  107. '<tr><td>2</td></tr>' +
  108. '<tr><td>3</td></tr>' +
  109. '</thead>' +
  110. '</table>'
  111. );
  112. expectModel(
  113. '<table headingRows="3">' +
  114. '<tableRow><tableCell>1</tableCell></tableRow>' +
  115. '<tableRow><tableCell>2</tableCell></tableRow>' +
  116. '<tableRow><tableCell>3</tableCell></tableRow>' +
  117. '</table>'
  118. );
  119. } );
  120. it( 'should create table model from table with two theads with one row', () => {
  121. editor.setData(
  122. '<table>' +
  123. '<thead><tr><td>1</td></tr></thead>' +
  124. '<tbody><tr><td>2</td></tr></tbody>' +
  125. '<thead><tr><td>3</td></tr></thead>' +
  126. '</table>'
  127. );
  128. expectModel(
  129. '<table headingRows="1">' +
  130. '<tableRow><tableCell>1</tableCell></tableRow>' +
  131. '<tableRow><tableCell>2</tableCell></tableRow>' +
  132. '<tableRow><tableCell>3</tableCell></tableRow>' +
  133. '</table>'
  134. );
  135. } );
  136. it( 'should create table model from table with thead after the tbody', () => {
  137. editor.setData(
  138. '<table>' +
  139. '<tbody><tr><td>2</td></tr></tbody>' +
  140. '<thead><tr><td>1</td></tr></thead>' +
  141. '</table>'
  142. );
  143. expectModel(
  144. '<table headingRows="1">' +
  145. '<tableRow><tableCell>1</tableCell></tableRow>' +
  146. '<tableRow><tableCell>2</tableCell></tableRow>' +
  147. '</table>'
  148. );
  149. } );
  150. it( 'should create table model from table with one tfoot with one row', () => {
  151. editor.setData(
  152. '<table>' +
  153. '<tfoot><tr><td>1</td></tr></tfoot>' +
  154. '</table>'
  155. );
  156. expectModel(
  157. '<table>' +
  158. '<tableRow><tableCell>1</tableCell></tableRow>' +
  159. '</table>'
  160. );
  161. } );
  162. it( 'should create valid table model from empty table', () => {
  163. editor.setData(
  164. '<table>' +
  165. '</table>'
  166. );
  167. expectModel(
  168. '<table><tableRow><tableCell></tableCell></tableRow></table>'
  169. );
  170. } );
  171. it( 'should skip unknown table children', () => {
  172. editor.setData(
  173. '<table>' +
  174. '<caption>foo</caption>' +
  175. '<tr><td>bar</td></tr>' +
  176. '</table>'
  177. );
  178. expectModel(
  179. '<table><tableRow><tableCell>bar</tableCell></tableRow></table>'
  180. );
  181. } );
  182. it( 'should create table model from some broken table', () => {
  183. editor.setData(
  184. '<table><td><p>foo</p></td></table>'
  185. );
  186. expectModel(
  187. '<table><tableRow><tableCell>foo</tableCell></tableRow></table>'
  188. );
  189. } );
  190. it( 'should fix if inside other blocks', () => {
  191. // Using <div> instead of <p> as it breaks on Edge.
  192. editor.model.schema.register( 'div', {
  193. inheritAllFrom: '$block'
  194. } );
  195. editor.conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'div', view: 'div' } ) );
  196. editor.setData(
  197. '<div>foo' +
  198. '<table>' +
  199. '<tbody><tr><td>2</td></tr></tbody>' +
  200. '<thead><tr><td>1</td></tr></thead>' +
  201. '</table>' +
  202. 'bar</div>'
  203. );
  204. expectModel(
  205. '<div>foo</div>' +
  206. '<table headingRows="1">' +
  207. '<tableRow><tableCell>1</tableCell></tableRow>' +
  208. '<tableRow><tableCell>2</tableCell></tableRow>' +
  209. '</table>' +
  210. '<div>bar</div>'
  211. );
  212. } );
  213. it( 'should be possible to overwrite table conversion', () => {
  214. editor.model.schema.register( 'fooTable', {
  215. allowWhere: '$block',
  216. allowAttributes: [ 'headingRows' ],
  217. isObject: true
  218. } );
  219. editor.conversion.elementToElement( { model: 'fooTable', view: 'table', converterPriority: 'high' } );
  220. editor.setData(
  221. '<table>' +
  222. '<thead><tr><td>foo</td></tr></thead>' +
  223. '</table>'
  224. );
  225. expectModel(
  226. '<fooTable></fooTable>'
  227. );
  228. } );
  229. describe( 'headingColumns', () => {
  230. it( 'should properly calculate heading columns', () => {
  231. editor.setData(
  232. '<table>' +
  233. '<tbody>' +
  234. // This row starts with 1 th (3 total).
  235. '<tr><th>21</th><td>22</td><th>23</th><th>24</th></tr>' +
  236. // This row starts with 2 th (2 total). This one has max number of heading columns: 2.
  237. '<tr><th>31</th><th>32</th><td>33</td><td>34</td></tr>' +
  238. // This row starts with 1 th (1 total).
  239. '<tr><th>41</th><td>42</td><td>43</td><td>44</td></tr>' +
  240. // This row starts with 0 th (3 total).
  241. '<tr><td>51</td><th>52</th><th>53</th><th>54</th></tr>' +
  242. '</tbody>' +
  243. '<thead>' +
  244. // This row has 4 ths but it is a thead.
  245. '<tr><th>11</th><th>12</th><th>13</th><th>14</th></tr>' +
  246. '</thead>' +
  247. '</table>'
  248. );
  249. expectModel(
  250. '<table headingColumns="2" headingRows="1">' +
  251. '<tableRow>' +
  252. '<tableCell>11</tableCell><tableCell>12</tableCell><tableCell>13</tableCell><tableCell>14</tableCell>' +
  253. '</tableRow>' +
  254. '<tableRow>' +
  255. '<tableCell>21</tableCell><tableCell>22</tableCell><tableCell>23</tableCell><tableCell>24</tableCell>' +
  256. '</tableRow>' +
  257. '<tableRow>' +
  258. '<tableCell>31</tableCell><tableCell>32</tableCell><tableCell>33</tableCell><tableCell>34</tableCell>' +
  259. '</tableRow>' +
  260. '<tableRow>' +
  261. '<tableCell>41</tableCell><tableCell>42</tableCell><tableCell>43</tableCell><tableCell>44</tableCell>' +
  262. '</tableRow>' +
  263. '<tableRow>' +
  264. '<tableCell>51</tableCell><tableCell>52</tableCell><tableCell>53</tableCell><tableCell>54</tableCell>' +
  265. '</tableRow>' +
  266. '</table>'
  267. );
  268. } );
  269. it( 'should calculate heading columns of cells with colspan', () => {
  270. editor.setData(
  271. '<table>' +
  272. '<tbody>' +
  273. // This row has colspan of 3 so it should be the whole table should have 3 heading columns.
  274. '<tr><th>21</th><th>22</th><td>23</td><td>24</td></tr>' +
  275. '<tr><th colspan="2">31</th><th>33</th><td>34</td></tr>' +
  276. '</tbody>' +
  277. '<thead>' +
  278. // This row has 4 ths but it is a thead.
  279. '<tr><th>11</th><th>12</th><th>13</th><th>14</th></tr>' +
  280. '</thead>' +
  281. '</table>'
  282. );
  283. expectModel(
  284. '<table headingColumns="3" headingRows="1">' +
  285. '<tableRow>' +
  286. '<tableCell>11</tableCell><tableCell>12</tableCell><tableCell>13</tableCell><tableCell>14</tableCell>' +
  287. '</tableRow>' +
  288. '<tableRow>' +
  289. '<tableCell>21</tableCell><tableCell>22</tableCell><tableCell>23</tableCell><tableCell>24</tableCell>' +
  290. '</tableRow>' +
  291. '<tableRow>' +
  292. '<tableCell colspan="2">31</tableCell><tableCell>33</tableCell><tableCell>34</tableCell>' +
  293. '</tableRow>' +
  294. '</table>'
  295. );
  296. } );
  297. } );
  298. } );