tablewalker.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. isBlock: true,
  23. isObject: true
  24. } );
  25. schema.register( 'tableRow', {
  26. allowIn: 'table',
  27. allowAttributes: [],
  28. isBlock: true,
  29. isLimit: true
  30. } );
  31. schema.register( 'tableCell', {
  32. allowIn: 'tableRow',
  33. allowContentOf: '$block',
  34. allowAttributes: [ 'colspan', 'rowspan' ],
  35. isBlock: true,
  36. isLimit: true
  37. } );
  38. } );
  39. } );
  40. function testWalker( tableData, expected, options ) {
  41. setData( model, modelTable( tableData ) );
  42. const iterator = new TableWalker( root.getChild( 0 ), options );
  43. const result = [];
  44. for ( const tableInfo of iterator ) {
  45. result.push( tableInfo );
  46. }
  47. const formattedResult = result.map( ( { row, column, cell } ) => ( { row, column, data: cell && cell.getChild( 0 ).data } ) );
  48. expect( formattedResult ).to.deep.equal( expected );
  49. }
  50. it( 'should iterate over a table', () => {
  51. testWalker( [
  52. [ '11', '12' ]
  53. ], [
  54. { row: 0, column: 0, data: '11' },
  55. { row: 0, column: 1, data: '12' }
  56. ] );
  57. } );
  58. it( 'should properly output column indexes of a table that has colspans', () => {
  59. testWalker( [
  60. [ { colspan: 2, contents: '11' }, '13' ]
  61. ], [
  62. { row: 0, column: 0, data: '11' },
  63. { row: 0, column: 2, data: '13' }
  64. ] );
  65. } );
  66. it( 'should properly output column indexes of a table that has rowspans', () => {
  67. testWalker( [
  68. [ { colspan: 2, rowspan: 3, contents: '11' }, '13' ],
  69. [ '23' ],
  70. [ '33' ],
  71. [ '41', '42', '43' ]
  72. ], [
  73. { row: 0, column: 0, data: '11' },
  74. { row: 0, column: 2, data: '13' },
  75. { row: 1, column: 2, data: '23' },
  76. { row: 2, column: 2, data: '33' },
  77. { row: 3, column: 0, data: '41' },
  78. { row: 3, column: 1, data: '42' },
  79. { row: 3, column: 2, data: '43' }
  80. ] );
  81. } );
  82. it( 'should properly output column indexes of a table that has multiple rowspans', () => {
  83. testWalker( [
  84. [ { rowspan: 3, contents: '11' }, '12', '13' ],
  85. [ { rowspan: 2, contents: '22' }, '23' ],
  86. [ '33' ],
  87. [ '41', '42', '43' ]
  88. ], [
  89. { row: 0, column: 0, data: '11' },
  90. { row: 0, column: 1, data: '12' },
  91. { row: 0, column: 2, data: '13' },
  92. { row: 1, column: 1, data: '22' },
  93. { row: 1, column: 2, data: '23' },
  94. { row: 2, column: 2, data: '33' },
  95. { row: 3, column: 0, data: '41' },
  96. { row: 3, column: 1, data: '42' },
  97. { row: 3, column: 2, data: '43' }
  98. ] );
  99. } );
  100. describe( 'option.startRow', () => {
  101. it( 'should start iterating from given row but with cell spans properly calculated', () => {
  102. testWalker( [
  103. [ { colspan: 2, rowspan: 3, contents: '11' }, '13' ],
  104. [ '23' ],
  105. [ '33' ],
  106. [ '41', '42', '43' ]
  107. ], [
  108. { row: 2, column: 2, data: '33' },
  109. { row: 3, column: 0, data: '41' },
  110. { row: 3, column: 1, data: '42' },
  111. { row: 3, column: 2, data: '43' }
  112. ], { startRow: 2 } );
  113. } );
  114. } );
  115. describe( 'option.endRow', () => {
  116. it( 'should stopp iterating after given row but with cell spans properly calculated', () => {
  117. testWalker( [
  118. [ { colspan: 2, rowspan: 3, contents: '11' }, '13' ],
  119. [ '23' ],
  120. [ '33' ],
  121. [ '41', '42', '43' ]
  122. ], [
  123. { row: 0, column: 0, data: '11' },
  124. { row: 0, column: 2, data: '13' },
  125. { row: 1, column: 2, data: '23' },
  126. { row: 2, column: 2, data: '33' }
  127. ], { endRow: 2 } );
  128. } );
  129. } );
  130. describe( 'option.includeSpanned', () => {
  131. it( 'should output spanned cells as empty cell', () => {
  132. testWalker( [
  133. [ { colspan: 2, rowspan: 3, contents: '11' }, '13' ],
  134. [ '23' ],
  135. [ '33' ],
  136. [ '41', { colspan: 2, contents: '42' } ]
  137. ], [
  138. { row: 0, column: 0, data: '11' },
  139. { row: 0, column: 1, data: undefined },
  140. { row: 0, column: 2, data: '13' },
  141. { row: 1, column: 0, data: undefined },
  142. { row: 1, column: 1, data: undefined },
  143. { row: 1, column: 2, data: '23' },
  144. { row: 2, column: 0, data: undefined },
  145. { row: 2, column: 1, data: undefined },
  146. { row: 2, column: 2, data: '33' },
  147. { row: 3, column: 0, data: '41' },
  148. { row: 3, column: 1, data: '42' },
  149. { row: 3, column: 2, data: undefined }
  150. ], { includeSpanned: true } );
  151. } );
  152. it( 'should work with startRow & endRow options', () => {
  153. testWalker( [
  154. [ { colspan: 2, rowspan: 3, contents: '11' }, '13' ],
  155. [ '23' ],
  156. [ '33' ],
  157. [ '41', '42', '43' ]
  158. ], [
  159. { row: 1, column: 0, data: undefined },
  160. { row: 1, column: 1, data: undefined },
  161. { row: 1, column: 2, data: '23' },
  162. { row: 2, column: 0, data: undefined },
  163. { row: 2, column: 1, data: undefined },
  164. { row: 2, column: 2, data: '33' }
  165. ], { includeSpanned: true, startRow: 1, endRow: 2 } );
  166. } );
  167. } );
  168. describe( 'option.endRow', () => {
  169. it( 'should iterate over given row 0 only', () => {
  170. testWalker( [
  171. [ { colspan: 2, rowspan: 3, contents: '11' }, '13' ],
  172. [ '23' ],
  173. [ '33' ],
  174. [ '41', '42', '43' ]
  175. ], [
  176. { row: 0, column: 0, data: '11' },
  177. { row: 0, column: 2, data: '13' }
  178. ], { endRow: 0 } );
  179. } );
  180. } );
  181. } );