8
0

tablewalker.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.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. } );