tablewalker.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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: '00' }, '02' ],
  69. [ '12' ],
  70. [ '22' ],
  71. [ '30', '31', '32' ]
  72. ], [
  73. { row: 0, column: 0, data: '00' },
  74. { row: 0, column: 2, data: '02' },
  75. { row: 1, column: 2, data: '12' },
  76. { row: 2, column: 2, data: '22' },
  77. { row: 3, column: 0, data: '30' },
  78. { row: 3, column: 1, data: '31' },
  79. { row: 3, column: 2, data: '32' }
  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. it( 'should output spanned cells at the end of a table', () => {
  101. testWalker( [
  102. [ '00', { rowspan: 2, contents: '01' } ],
  103. [ '10' ]
  104. ], [
  105. { row: 0, column: 0, data: '00' },
  106. { row: 0, column: 1, data: '01' },
  107. { row: 1, column: 0, data: '10' },
  108. { row: 1, column: 1, data: undefined }
  109. ], { includeSpanned: true } );
  110. } );
  111. describe( 'option.startRow', () => {
  112. it( 'should start iterating from 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: 2, column: 2, data: '33' },
  120. { row: 3, column: 0, data: '41' },
  121. { row: 3, column: 1, data: '42' },
  122. { row: 3, column: 2, data: '43' }
  123. ], { startRow: 2 } );
  124. } );
  125. } );
  126. describe( 'option.endRow', () => {
  127. it( 'should stopp iterating after given row but with cell spans properly calculated', () => {
  128. testWalker( [
  129. [ { colspan: 2, rowspan: 3, contents: '11' }, '13' ],
  130. [ '23' ],
  131. [ '33' ],
  132. [ '41', '42', '43' ]
  133. ], [
  134. { row: 0, column: 0, data: '11' },
  135. { row: 0, column: 2, data: '13' },
  136. { row: 1, column: 2, data: '23' },
  137. { row: 2, column: 2, data: '33' }
  138. ], { endRow: 2 } );
  139. } );
  140. } );
  141. describe( 'option.includeSpanned', () => {
  142. it( 'should output spanned cells as empty cell', () => {
  143. testWalker( [
  144. [ { colspan: 2, rowspan: 3, contents: '00' }, '02' ],
  145. [ '12' ],
  146. [ '22' ],
  147. [ '30', { colspan: 2, contents: '31' } ]
  148. ], [
  149. { row: 0, column: 0, data: '00' },
  150. { row: 0, column: 1, data: undefined },
  151. { row: 0, column: 2, data: '02' },
  152. { row: 1, column: 0, data: undefined },
  153. { row: 1, column: 1, data: undefined },
  154. { row: 1, column: 2, data: '12' },
  155. { row: 2, column: 0, data: undefined },
  156. { row: 2, column: 1, data: undefined },
  157. { row: 2, column: 2, data: '22' },
  158. { row: 3, column: 0, data: '30' },
  159. { row: 3, column: 1, data: '31' },
  160. { row: 3, column: 2, data: undefined }
  161. ], { includeSpanned: true } );
  162. } );
  163. it( 'should work with startRow & endRow options', () => {
  164. testWalker( [
  165. [ { colspan: 2, rowspan: 3, contents: '00' }, '02' ],
  166. [ '12' ],
  167. [ '22' ],
  168. [ '30', '31', '32' ]
  169. ], [
  170. { row: 1, column: 0, data: undefined },
  171. { row: 1, column: 1, data: undefined },
  172. { row: 1, column: 2, data: '12' },
  173. { row: 2, column: 0, data: undefined },
  174. { row: 2, column: 1, data: undefined },
  175. { row: 2, column: 2, data: '22' }
  176. ], { includeSpanned: true, startRow: 1, endRow: 2 } );
  177. } );
  178. } );
  179. describe( 'option.endRow', () => {
  180. it( 'should iterate over given row 0 only', () => {
  181. testWalker( [
  182. [ { colspan: 2, rowspan: 3, contents: '11' }, '13' ],
  183. [ '23' ],
  184. [ '33' ],
  185. [ '41', '42', '43' ]
  186. ], [
  187. { row: 0, column: 0, data: '11' },
  188. { row: 0, column: 2, data: '13' }
  189. ], { endRow: 0 } );
  190. } );
  191. } );
  192. } );