tablewalker.js 8.9 KB

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