tablewalker.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /**
  2. * @license Copyright (c) 2003-2020, 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 CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  8. import { defaultConversion, defaultSchema, modelTable } from './_utils/utils';
  9. import TableWalker from '../src/tablewalker';
  10. describe( 'TableWalker', () => {
  11. let editor, model, doc, root;
  12. beforeEach( () => {
  13. return ModelTestEditor.create()
  14. .then( newEditor => {
  15. editor = newEditor;
  16. model = editor.model;
  17. doc = model.document;
  18. root = doc.getRoot( 'main' );
  19. defaultSchema( model.schema );
  20. defaultConversion( editor.conversion );
  21. } );
  22. } );
  23. function testWalker( tableData, expected, options ) {
  24. setData( model, modelTable( tableData ) );
  25. const iterator = new TableWalker( root.getChild( 0 ), options );
  26. const result = [];
  27. for ( const tableInfo of iterator ) {
  28. result.push( tableInfo );
  29. }
  30. const formattedResult = result.map( ( { row, column, isSpanned, cell, cellIndex } ) => {
  31. const result = {
  32. row,
  33. column,
  34. data: cell && cell.getChild( 0 ).getChild( 0 ).data,
  35. index: cellIndex
  36. };
  37. if ( isSpanned ) {
  38. result.isSpanned = true;
  39. }
  40. return result;
  41. } );
  42. expect( formattedResult ).to.deep.equal( expected );
  43. }
  44. it( 'should iterate over a table', () => {
  45. testWalker( [
  46. [ '00', '01' ],
  47. [ '10', '11' ]
  48. ], [
  49. { row: 0, column: 0, index: 0, data: '00' },
  50. { row: 0, column: 1, index: 1, data: '01' },
  51. { row: 1, column: 0, index: 0, data: '10' },
  52. { row: 1, column: 1, index: 1, data: '11' }
  53. ] );
  54. } );
  55. it( 'should properly output column indexes of a table that has colspans', () => {
  56. testWalker( [
  57. [ { colspan: 2, contents: '00' }, '13' ]
  58. ], [
  59. { row: 0, column: 0, index: 0, data: '00' },
  60. { row: 0, column: 2, index: 1, data: '13' }
  61. ] );
  62. } );
  63. it( 'should properly output column indexes of a table that has rowspans', () => {
  64. testWalker( [
  65. [ { colspan: 2, rowspan: 3, contents: '00' }, '02' ],
  66. [ '12' ],
  67. [ '22' ],
  68. [ '30', '31', '32' ]
  69. ], [
  70. { row: 0, column: 0, index: 0, data: '00' },
  71. { row: 0, column: 2, index: 1, data: '02' },
  72. { row: 1, column: 2, index: 0, data: '12' },
  73. { row: 2, column: 2, index: 0, data: '22' },
  74. { row: 3, column: 0, index: 0, data: '30' },
  75. { row: 3, column: 1, index: 1, data: '31' },
  76. { row: 3, column: 2, index: 2, data: '32' }
  77. ] );
  78. } );
  79. it( 'should properly output column indexes of a table that has multiple rowspans', () => {
  80. testWalker( [
  81. [ { rowspan: 3, contents: '11' }, '12', '13' ],
  82. [ { rowspan: 2, contents: '22' }, '23' ],
  83. [ '33' ],
  84. [ '41', '42', '43' ]
  85. ], [
  86. { row: 0, column: 0, index: 0, data: '11' },
  87. { row: 0, column: 1, index: 1, data: '12' },
  88. { row: 0, column: 2, index: 2, data: '13' },
  89. { row: 1, column: 1, index: 0, data: '22' },
  90. { row: 1, column: 2, index: 1, data: '23' },
  91. { row: 2, column: 2, index: 0, data: '33' },
  92. { row: 3, column: 0, index: 0, data: '41' },
  93. { row: 3, column: 1, index: 1, data: '42' },
  94. { row: 3, column: 2, index: 2, data: '43' }
  95. ] );
  96. } );
  97. describe( 'option.startRow', () => {
  98. it( 'should start iterating from given row but with cell spans properly calculated', () => {
  99. testWalker( [
  100. [ { colspan: 2, rowspan: 3, contents: '11' }, '13' ],
  101. [ '23' ],
  102. [ '33' ],
  103. [ '41', '42', '43' ]
  104. ], [
  105. { row: 2, column: 2, index: 0, data: '33' },
  106. { row: 3, column: 0, index: 0, data: '41' },
  107. { row: 3, column: 1, index: 1, data: '42' },
  108. { row: 3, column: 2, index: 2, data: '43' }
  109. ], { startRow: 2 } );
  110. } );
  111. } );
  112. describe( 'option.endRow', () => {
  113. it( 'should stopp iterating after given row but with cell spans properly calculated', () => {
  114. testWalker( [
  115. [ { colspan: 2, rowspan: 3, contents: '11' }, '13' ],
  116. [ '23' ],
  117. [ '33' ],
  118. [ '41', '42', '43' ]
  119. ], [
  120. { row: 0, column: 0, index: 0, data: '11' },
  121. { row: 0, column: 2, index: 1, data: '13' },
  122. { row: 1, column: 2, index: 0, data: '23' },
  123. { row: 2, column: 2, index: 0, data: '33' }
  124. ], { endRow: 2 } );
  125. } );
  126. it( 'should iterate over given row only', () => {
  127. testWalker( [
  128. [ { colspan: 2, rowspan: 3, contents: '11' }, '13' ],
  129. [ '23' ],
  130. [ '33' ],
  131. [ '41', '42', '43' ]
  132. ], [
  133. { row: 0, column: 0, index: 0, data: '11' },
  134. { row: 0, column: 2, index: 1, data: '13' }
  135. ], { endRow: 0 } );
  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: '01', isSpanned: true }
  148. ], { includeSpanned: true } );
  149. } );
  150. it( 'should output spanned cells', () => {
  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: 0, data: '00', isSpanned: true },
  159. { row: 0, column: 2, index: 1, data: '02' },
  160. { row: 1, column: 0, index: 0, data: '00', isSpanned: true },
  161. { row: 1, column: 1, index: 0, data: '00', isSpanned: true },
  162. { row: 1, column: 2, index: 0, data: '12' },
  163. { row: 2, column: 0, index: 0, data: '00', isSpanned: true },
  164. { row: 2, column: 1, index: 0, data: '00', isSpanned: true },
  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: 1, data: '31', isSpanned: true }
  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: '01', isSpanned: true }
  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: '00', isSpanned: true },
  190. { row: 1, column: 1, index: 0, data: '00', isSpanned: true },
  191. { row: 1, column: 2, index: 0, data: '12' },
  192. { row: 2, column: 0, index: 0, data: '00', isSpanned: true },
  193. { row: 2, column: 1, index: 0, data: '00', isSpanned: true },
  194. { row: 2, column: 2, index: 0, data: '22' }
  195. ], { includeSpanned: true, startRow: 1, endRow: 2 } );
  196. } );
  197. it( 'should output rowspanned cells at the end of a table row with startRow & endRow options', () => {
  198. testWalker( [
  199. [ '00', { rowspan: 2, contents: '01' } ],
  200. [ '10' ],
  201. [ '20', '21' ]
  202. ], [
  203. { row: 0, column: 0, index: 0, data: '00' },
  204. { row: 0, column: 1, index: 1, data: '01' },
  205. { row: 1, column: 0, index: 0, data: '10' },
  206. { row: 1, column: 1, index: 1, data: '01', isSpanned: true }
  207. ], { startRow: 0, endRow: 1, includeSpanned: true } );
  208. } );
  209. } );
  210. describe( 'options.startColumn', () => {
  211. it( 'should output only cells on given column', () => {
  212. testWalker( [
  213. [ { colspan: 2, rowspan: 3, contents: '00' }, '02' ],
  214. [ '12' ],
  215. [ '22' ],
  216. [ '30', '31', '32' ]
  217. ], [
  218. { row: 3, column: 1, index: 1, data: '31' }
  219. ], { column: 1 } );
  220. } );
  221. it( 'should output only cells on given column, includeSpanned = true', () => {
  222. testWalker( [
  223. [ { colspan: 2, rowspan: 3, contents: '00' }, '02' ],
  224. [ '12' ],
  225. [ '22' ],
  226. [ '30', '31', '32' ]
  227. ], [
  228. { row: 0, column: 1, index: 0, data: '00', isSpanned: true },
  229. { row: 1, column: 1, index: 0, data: '00', isSpanned: true },
  230. { row: 2, column: 1, index: 0, data: '00', isSpanned: true },
  231. { row: 3, column: 1, index: 1, data: '31' }
  232. ], { column: 1, includeSpanned: true } );
  233. } );
  234. } );
  235. it( 'should return deprecated column option value', () => {
  236. setData( model, modelTable( [
  237. [ 'a' ]
  238. ] ) );
  239. const walker = new TableWalker( root.getChild( 0 ), { column: 7 } );
  240. expect( walker.column ).to.equal( 7 );
  241. } );
  242. it( 'should throw error if deprecated api used improperly', () => {
  243. setData( model, modelTable( [
  244. [ 'a' ]
  245. ] ) );
  246. const walker = new TableWalker( root.getChild( 0 ), { startColumn: 1, endColumn: 2 } );
  247. expect( () => walker.column ).to.throw( CKEditorError, 'improper-use-of-deprecated-api' );
  248. } );
  249. } );