8
0

tablewalker.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**
  2. * @license Copyright (c) 2003-2020, 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, isSpanned, cell, cellIndex } ) => {
  30. const result = {
  31. row,
  32. column,
  33. data: cell && cell.getChild( 0 ).getChild( 0 ).data,
  34. index: cellIndex
  35. };
  36. if ( isSpanned ) {
  37. result.isSpanned = true;
  38. }
  39. return result;
  40. } );
  41. expect( formattedResult ).to.deep.equal( expected );
  42. }
  43. it( 'should iterate over a table', () => {
  44. testWalker( [
  45. [ '00', '01' ],
  46. [ '10', '11' ]
  47. ], [
  48. { row: 0, column: 0, index: 0, data: '00' },
  49. { row: 0, column: 1, index: 1, data: '01' },
  50. { row: 1, column: 0, index: 0, data: '10' },
  51. { row: 1, column: 1, index: 1, data: '11' }
  52. ] );
  53. } );
  54. it( 'should properly output column indexes of a table that has colspans', () => {
  55. testWalker( [
  56. [ { colspan: 2, contents: '00' }, '13' ]
  57. ], [
  58. { row: 0, column: 0, index: 0, data: '00' },
  59. { row: 0, column: 2, index: 1, data: '13' }
  60. ] );
  61. } );
  62. it( 'should properly output column indexes of a table that has rowspans', () => {
  63. testWalker( [
  64. [ { colspan: 2, rowspan: 3, contents: '00' }, '02' ],
  65. [ '12' ],
  66. [ '22' ],
  67. [ '30', '31', '32' ]
  68. ], [
  69. { row: 0, column: 0, index: 0, data: '00' },
  70. { row: 0, column: 2, index: 1, data: '02' },
  71. { row: 1, column: 2, index: 0, data: '12' },
  72. { row: 2, column: 2, index: 0, data: '22' },
  73. { row: 3, column: 0, index: 0, data: '30' },
  74. { row: 3, column: 1, index: 1, data: '31' },
  75. { row: 3, column: 2, index: 2, data: '32' }
  76. ] );
  77. } );
  78. it( 'should properly output column indexes of a table that has multiple rowspans', () => {
  79. testWalker( [
  80. [ { rowspan: 3, contents: '11' }, '12', '13' ],
  81. [ { rowspan: 2, contents: '22' }, '23' ],
  82. [ '33' ],
  83. [ '41', '42', '43' ]
  84. ], [
  85. { row: 0, column: 0, index: 0, data: '11' },
  86. { row: 0, column: 1, index: 1, data: '12' },
  87. { row: 0, column: 2, index: 2, data: '13' },
  88. { row: 1, column: 1, index: 0, data: '22' },
  89. { row: 1, column: 2, index: 1, data: '23' },
  90. { row: 2, column: 2, index: 0, data: '33' },
  91. { row: 3, column: 0, index: 0, data: '41' },
  92. { row: 3, column: 1, index: 1, data: '42' },
  93. { row: 3, column: 2, index: 2, data: '43' }
  94. ] );
  95. } );
  96. describe( 'option.startRow', () => {
  97. it( 'should start iterating from given row but with cell spans properly calculated', () => {
  98. testWalker( [
  99. [ { colspan: 2, rowspan: 3, contents: '11' }, '13' ],
  100. [ '23' ],
  101. [ '33' ],
  102. [ '41', '42', '43' ]
  103. ], [
  104. { row: 2, column: 2, index: 0, data: '33' },
  105. { row: 3, column: 0, index: 0, data: '41' },
  106. { row: 3, column: 1, index: 1, data: '42' },
  107. { row: 3, column: 2, index: 2, data: '43' }
  108. ], { startRow: 2 } );
  109. } );
  110. } );
  111. describe( 'option.endRow', () => {
  112. it( 'should stopp iterating after given row but with cell spans properly calculated', () => {
  113. testWalker( [
  114. [ { colspan: 2, rowspan: 3, contents: '11' }, '13' ],
  115. [ '23' ],
  116. [ '33' ],
  117. [ '41', '42', '43' ]
  118. ], [
  119. { row: 0, column: 0, index: 0, data: '11' },
  120. { row: 0, column: 2, index: 1, data: '13' },
  121. { row: 1, column: 2, index: 0, data: '23' },
  122. { row: 2, column: 2, index: 0, data: '33' }
  123. ], { endRow: 2 } );
  124. } );
  125. it( 'should iterate over given row only', () => {
  126. testWalker( [
  127. [ { colspan: 2, rowspan: 3, contents: '11' }, '13' ],
  128. [ '23' ],
  129. [ '33' ],
  130. [ '41', '42', '43' ]
  131. ], [
  132. { row: 0, column: 0, index: 0, data: '11' },
  133. { row: 0, column: 2, index: 1, data: '13' }
  134. ], { endRow: 0 } );
  135. } );
  136. } );
  137. describe( 'option.includeSpanned', () => {
  138. it( 'should output spanned cells at the end of a table', () => {
  139. testWalker( [
  140. [ '00', { rowspan: 2, contents: '01' } ],
  141. [ '10' ]
  142. ], [
  143. { row: 0, column: 0, index: 0, data: '00' },
  144. { row: 0, column: 1, index: 1, data: '01' },
  145. { row: 1, column: 0, index: 0, data: '10' },
  146. { row: 1, column: 1, index: 1, data: '01', isSpanned: true }
  147. ], { includeSpanned: true } );
  148. } );
  149. it( 'should output spanned cells', () => {
  150. testWalker( [
  151. [ { colspan: 2, rowspan: 3, contents: '00' }, '02' ],
  152. [ '12' ],
  153. [ '22' ],
  154. [ '30', { colspan: 2, contents: '31' } ]
  155. ], [
  156. { row: 0, column: 0, index: 0, data: '00' },
  157. { row: 0, column: 1, index: 0, data: '00', isSpanned: true },
  158. { row: 0, column: 2, index: 1, data: '02' },
  159. { row: 1, column: 0, index: 0, data: '00', isSpanned: true },
  160. { row: 1, column: 1, index: 0, data: '00', isSpanned: true },
  161. { row: 1, column: 2, index: 0, data: '12' },
  162. { row: 2, column: 0, index: 0, data: '00', isSpanned: true },
  163. { row: 2, column: 1, index: 0, data: '00', isSpanned: true },
  164. { row: 2, column: 2, index: 0, data: '22' },
  165. { row: 3, column: 0, index: 0, data: '30' },
  166. { row: 3, column: 1, index: 1, data: '31' },
  167. { row: 3, column: 2, index: 1, data: '31', isSpanned: true }
  168. ], { includeSpanned: true } );
  169. } );
  170. it( 'should output rowspanned cells at the end of a table row', () => {
  171. testWalker( [
  172. [ '00', { rowspan: 2, contents: '01' } ],
  173. [ '10' ]
  174. ], [
  175. { row: 0, column: 0, index: 0, data: '00' },
  176. { row: 0, column: 1, index: 1, data: '01' },
  177. { row: 1, column: 0, index: 0, data: '10' },
  178. { row: 1, column: 1, index: 1, data: '01', isSpanned: true }
  179. ], { includeSpanned: true } );
  180. } );
  181. it( 'should work with startRow & endRow options', () => {
  182. testWalker( [
  183. [ { colspan: 2, rowspan: 3, contents: '00' }, '02' ],
  184. [ '12' ],
  185. [ '22' ],
  186. [ '30', '31', '32' ]
  187. ], [
  188. { row: 1, column: 0, index: 0, data: '00', isSpanned: true },
  189. { row: 1, column: 1, index: 0, data: '00', isSpanned: true },
  190. { row: 1, column: 2, index: 0, data: '12' },
  191. { row: 2, column: 0, index: 0, data: '00', isSpanned: true },
  192. { row: 2, column: 1, index: 0, data: '00', isSpanned: true },
  193. { row: 2, column: 2, index: 0, data: '22' }
  194. ], { includeSpanned: true, startRow: 1, endRow: 2 } );
  195. } );
  196. it( 'should output rowspanned cells at the end of a table row with startRow & endRow options', () => {
  197. testWalker( [
  198. [ '00', { rowspan: 2, contents: '01' } ],
  199. [ '10' ],
  200. [ '20', '21' ]
  201. ], [
  202. { row: 0, column: 0, index: 0, data: '00' },
  203. { row: 0, column: 1, index: 1, data: '01' },
  204. { row: 1, column: 0, index: 0, data: '10' },
  205. { row: 1, column: 1, index: 1, data: '01', isSpanned: true }
  206. ], { startRow: 0, endRow: 1, includeSpanned: true } );
  207. } );
  208. } );
  209. describe( 'options.startColumn', () => {
  210. it( 'should output only cells on given column', () => {
  211. testWalker( [
  212. [ { colspan: 2, rowspan: 3, contents: '00' }, '02' ],
  213. [ '12' ],
  214. [ '22' ],
  215. [ '30', '31', '32' ]
  216. ], [
  217. { row: 3, column: 1, index: 1, data: '31' }
  218. ], { column: 1 } );
  219. } );
  220. it( 'should output only cells on given column, includeSpanned = true', () => {
  221. testWalker( [
  222. [ { colspan: 2, rowspan: 3, contents: '00' }, '02' ],
  223. [ '12' ],
  224. [ '22' ],
  225. [ '30', '31', '32' ]
  226. ], [
  227. { row: 0, column: 1, index: 0, data: '00', isSpanned: true },
  228. { row: 1, column: 1, index: 0, data: '00', isSpanned: true },
  229. { row: 2, column: 1, index: 0, data: '00', isSpanned: true },
  230. { row: 3, column: 1, index: 1, data: '31' }
  231. ], { column: 1, includeSpanned: true } );
  232. } );
  233. } );
  234. } );