8
0

tablewalker.js 7.4 KB

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