tablewalker.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  6. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  7. import { defaultConversion, defaultSchema, modelTable } from './_utils/utils';
  8. import TableWalker from '../src/tablewalker';
  9. describe( 'TableWalker', () => {
  10. let editor, model, doc, root;
  11. beforeEach( () => {
  12. return ModelTestEditor.create()
  13. .then( newEditor => {
  14. editor = newEditor;
  15. model = editor.model;
  16. doc = model.document;
  17. root = doc.getRoot( 'main' );
  18. defaultSchema( model.schema );
  19. defaultConversion( editor.conversion );
  20. } );
  21. } );
  22. function testWalker( tableData, expected, options ) {
  23. setData( model, modelTable( tableData ) );
  24. const iterator = new TableWalker( root.getChild( 0 ), options );
  25. const result = [];
  26. for ( const tableInfo of iterator ) {
  27. result.push( tableInfo );
  28. }
  29. const formattedResult = result.map( ( { row, column, cell, cellIndex } ) => ( {
  30. row,
  31. column,
  32. data: cell && cell.getChild( 0 ).getChild( 0 ).data,
  33. index: cellIndex
  34. } ) );
  35. expect( formattedResult ).to.deep.equal( expected );
  36. }
  37. it( 'should iterate over a table', () => {
  38. testWalker( [
  39. [ '00', '01' ],
  40. [ '10', '11' ]
  41. ], [
  42. { row: 0, column: 0, index: 0, data: '00' },
  43. { row: 0, column: 1, index: 1, data: '01' },
  44. { row: 1, column: 0, index: 0, data: '10' },
  45. { row: 1, column: 1, index: 1, data: '11' }
  46. ] );
  47. } );
  48. it( 'should properly output column indexes of a table that has colspans', () => {
  49. testWalker( [
  50. [ { colspan: 2, contents: '00' }, '13' ]
  51. ], [
  52. { row: 0, column: 0, index: 0, data: '00' },
  53. { row: 0, column: 2, index: 1, data: '13' }
  54. ] );
  55. } );
  56. it( 'should properly output column indexes of a table that has rowspans', () => {
  57. testWalker( [
  58. [ { colspan: 2, rowspan: 3, contents: '00' }, '02' ],
  59. [ '12' ],
  60. [ '22' ],
  61. [ '30', '31', '32' ]
  62. ], [
  63. { row: 0, column: 0, index: 0, data: '00' },
  64. { row: 0, column: 2, index: 1, data: '02' },
  65. { row: 1, column: 2, index: 0, data: '12' },
  66. { row: 2, column: 2, index: 0, data: '22' },
  67. { row: 3, column: 0, index: 0, data: '30' },
  68. { row: 3, column: 1, index: 1, data: '31' },
  69. { row: 3, column: 2, index: 2, data: '32' }
  70. ] );
  71. } );
  72. it( 'should properly output column indexes of a table that has multiple rowspans', () => {
  73. testWalker( [
  74. [ { rowspan: 3, contents: '11' }, '12', '13' ],
  75. [ { rowspan: 2, contents: '22' }, '23' ],
  76. [ '33' ],
  77. [ '41', '42', '43' ]
  78. ], [
  79. { row: 0, column: 0, index: 0, data: '11' },
  80. { row: 0, column: 1, index: 1, data: '12' },
  81. { row: 0, column: 2, index: 2, data: '13' },
  82. { row: 1, column: 1, index: 0, data: '22' },
  83. { row: 1, column: 2, index: 1, data: '23' },
  84. { row: 2, column: 2, index: 0, data: '33' },
  85. { row: 3, column: 0, index: 0, data: '41' },
  86. { row: 3, column: 1, index: 1, data: '42' },
  87. { row: 3, column: 2, index: 2, data: '43' }
  88. ] );
  89. } );
  90. describe( 'option.startRow', () => {
  91. it( 'should start iterating from given row but with cell spans properly calculated', () => {
  92. testWalker( [
  93. [ { colspan: 2, rowspan: 3, contents: '11' }, '13' ],
  94. [ '23' ],
  95. [ '33' ],
  96. [ '41', '42', '43' ]
  97. ], [
  98. { row: 2, column: 2, index: 0, data: '33' },
  99. { row: 3, column: 0, index: 0, data: '41' },
  100. { row: 3, column: 1, index: 1, data: '42' },
  101. { row: 3, column: 2, index: 2, data: '43' }
  102. ], { startRow: 2 } );
  103. } );
  104. } );
  105. describe( 'option.endRow', () => {
  106. it( 'should stopp iterating after given row but with cell spans properly calculated', () => {
  107. testWalker( [
  108. [ { colspan: 2, rowspan: 3, contents: '11' }, '13' ],
  109. [ '23' ],
  110. [ '33' ],
  111. [ '41', '42', '43' ]
  112. ], [
  113. { row: 0, column: 0, index: 0, data: '11' },
  114. { row: 0, column: 2, index: 1, data: '13' },
  115. { row: 1, column: 2, index: 0, data: '23' },
  116. { row: 2, column: 2, index: 0, data: '33' }
  117. ], { endRow: 2 } );
  118. } );
  119. it( 'should iterate over given row 0 only', () => {
  120. testWalker( [
  121. [ { colspan: 2, rowspan: 3, contents: '11' }, '13' ],
  122. [ '23' ],
  123. [ '33' ],
  124. [ '41', '42', '43' ]
  125. ], [
  126. { row: 0, column: 0, index: 0, data: '11' },
  127. { row: 0, column: 2, index: 1, data: '13' }
  128. ], { endRow: 0 } );
  129. } );
  130. } );
  131. describe( 'option.includeSpanned', () => {
  132. it( 'should output spanned cells at the end of a table', () => {
  133. testWalker( [
  134. [ '00', { rowspan: 2, contents: '01' } ],
  135. [ '10' ]
  136. ], [
  137. { row: 0, column: 0, index: 0, data: '00' },
  138. { row: 0, column: 1, index: 1, data: '01' },
  139. { row: 1, column: 0, index: 0, data: '10' },
  140. { row: 1, column: 1, index: 1, data: '01' }
  141. ], { includeSpanned: true } );
  142. } );
  143. it( 'should output spanned cells', () => {
  144. testWalker( [
  145. [ { colspan: 2, rowspan: 3, contents: '00' }, '02' ],
  146. [ '12' ],
  147. [ '22' ],
  148. [ '30', { colspan: 2, contents: '31' } ]
  149. ], [
  150. { row: 0, column: 0, index: 0, data: '00' },
  151. { row: 0, column: 1, index: 0, data: '00' },
  152. { row: 0, column: 2, index: 1, data: '02' },
  153. { row: 1, column: 0, index: 0, data: '00' },
  154. { row: 1, column: 1, index: 0, data: '00' },
  155. { row: 1, column: 2, index: 0, data: '12' },
  156. { row: 2, column: 0, index: 0, data: '00' },
  157. { row: 2, column: 1, index: 0, data: '00' },
  158. { row: 2, column: 2, index: 0, data: '22' },
  159. { row: 3, column: 0, index: 0, data: '30' },
  160. { row: 3, column: 1, index: 1, data: '31' },
  161. { row: 3, column: 2, index: 1, data: '31' }
  162. ], { includeSpanned: true } );
  163. } );
  164. it( 'should output rowspanned cells at the end of a table row', () => {
  165. testWalker( [
  166. [ '00', { rowspan: 2, contents: '01' } ],
  167. [ '10' ]
  168. ], [
  169. { row: 0, column: 0, index: 0, data: '00' },
  170. { row: 0, column: 1, index: 1, data: '01' },
  171. { row: 1, column: 0, index: 0, data: '10' },
  172. { row: 1, column: 1, index: 1, data: '01' }
  173. ], { includeSpanned: true } );
  174. } );
  175. it( 'should work with startRow & endRow options', () => {
  176. testWalker( [
  177. [ { colspan: 2, rowspan: 3, contents: '00' }, '02' ],
  178. [ '12' ],
  179. [ '22' ],
  180. [ '30', '31', '32' ]
  181. ], [
  182. { row: 1, column: 0, index: 0, data: '00' },
  183. { row: 1, column: 1, index: 0, data: '00' },
  184. { row: 1, column: 2, index: 0, data: '12' },
  185. { row: 2, column: 0, index: 0, data: '00' },
  186. { row: 2, column: 1, index: 0, data: '00' },
  187. { row: 2, column: 2, index: 0, data: '22' }
  188. ], { includeSpanned: true, startRow: 1, endRow: 2 } );
  189. } );
  190. it( 'should output rowspanned cells at the end of a table row with startRow & endRow options', () => {
  191. testWalker( [
  192. [ '00', { rowspan: 2, contents: '01' } ],
  193. [ '10' ],
  194. [ '20', '21' ]
  195. ], [
  196. { row: 0, column: 0, index: 0, data: '00' },
  197. { row: 0, column: 1, index: 1, data: '01' },
  198. { row: 1, column: 0, index: 0, data: '10' },
  199. { row: 1, column: 1, index: 1, data: '01' }
  200. ], { startRow: 0, endRow: 1, includeSpanned: true } );
  201. } );
  202. } );
  203. describe( 'options.startColumn', () => {
  204. it( 'should output only cells on given column', () => {
  205. testWalker( [
  206. [ { colspan: 2, rowspan: 3, contents: '00' }, '02' ],
  207. [ '12' ],
  208. [ '22' ],
  209. [ '30', '31', '32' ]
  210. ], [
  211. { row: 3, column: 1, index: 1, data: '31' }
  212. ], { column: 1 } );
  213. } );
  214. it( 'should output only cells on given column, includeSpanned = true', () => {
  215. testWalker( [
  216. [ { colspan: 2, rowspan: 3, contents: '00' }, '02' ],
  217. [ '12' ],
  218. [ '22' ],
  219. [ '30', '31', '32' ]
  220. ], [
  221. { row: 0, column: 1, index: 0, data: '00' },
  222. { row: 1, column: 1, index: 0, data: '00' },
  223. { row: 2, column: 1, index: 0, data: '00' },
  224. { row: 3, column: 1, index: 1, data: '31' }
  225. ], { column: 1, includeSpanned: true } );
  226. } );
  227. } );
  228. } );