tablewalker.js 7.6 KB

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