| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
- import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
- import { modelTable } from './_utils/utils';
- import TableWalker from '../src/tablewalker';
- describe( 'TableWalker', () => {
- let editor, model, doc, root;
- beforeEach( () => {
- return ModelTestEditor.create()
- .then( newEditor => {
- editor = newEditor;
- model = editor.model;
- doc = model.document;
- root = doc.getRoot( 'main' );
- const schema = model.schema;
- schema.register( 'table', {
- allowWhere: '$block',
- allowAttributes: [ 'headingRows', 'headingColumns' ],
- isBlock: true,
- isObject: true
- } );
- schema.register( 'tableRow', {
- allowIn: 'table',
- allowAttributes: [],
- isBlock: true,
- isLimit: true
- } );
- schema.register( 'tableCell', {
- allowIn: 'tableRow',
- allowContentOf: '$block',
- allowAttributes: [ 'colspan', 'rowspan' ],
- isBlock: true,
- isLimit: true
- } );
- } );
- } );
- function testWalker( tableData, expected, options ) {
- setData( model, modelTable( tableData ) );
- const iterator = new TableWalker( root.getChild( 0 ), options );
- const result = [];
- for ( const tableInfo of iterator ) {
- result.push( tableInfo );
- }
- const formattedResult = result.map( ( { row, column, cell } ) => ( { row, column, data: cell && cell.getChild( 0 ).data } ) );
- expect( formattedResult ).to.deep.equal( expected );
- }
- it( 'should iterate over a table', () => {
- testWalker( [
- [ '11', '12' ]
- ], [
- { row: 0, column: 0, data: '11' },
- { row: 0, column: 1, data: '12' }
- ] );
- } );
- it( 'should properly output column indexes of a table that has colspans', () => {
- testWalker( [
- [ { colspan: 2, contents: '11' }, '13' ]
- ], [
- { row: 0, column: 0, data: '11' },
- { row: 0, column: 2, data: '13' }
- ] );
- } );
- it( 'should properly output column indexes of a table that has rowspans', () => {
- testWalker( [
- [ { colspan: 2, rowspan: 3, contents: '11' }, '13' ],
- [ '23' ],
- [ '33' ],
- [ '41', '42', '43' ]
- ], [
- { row: 0, column: 0, data: '11' },
- { row: 0, column: 2, data: '13' },
- { row: 1, column: 2, data: '23' },
- { row: 2, column: 2, data: '33' },
- { row: 3, column: 0, data: '41' },
- { row: 3, column: 1, data: '42' },
- { row: 3, column: 2, data: '43' }
- ] );
- } );
- it( 'should properly output column indexes of a table that has multiple rowspans', () => {
- testWalker( [
- [ { rowspan: 3, contents: '11' }, '12', '13' ],
- [ { rowspan: 2, contents: '22' }, '23' ],
- [ '33' ],
- [ '41', '42', '43' ]
- ], [
- { row: 0, column: 0, data: '11' },
- { row: 0, column: 1, data: '12' },
- { row: 0, column: 2, data: '13' },
- { row: 1, column: 1, data: '22' },
- { row: 1, column: 2, data: '23' },
- { row: 2, column: 2, data: '33' },
- { row: 3, column: 0, data: '41' },
- { row: 3, column: 1, data: '42' },
- { row: 3, column: 2, data: '43' }
- ] );
- } );
- describe( 'option.startRow', () => {
- it( 'should start iterating from given row but with cell spans properly calculated', () => {
- testWalker( [
- [ { colspan: 2, rowspan: 3, contents: '11' }, '13' ],
- [ '23' ],
- [ '33' ],
- [ '41', '42', '43' ]
- ], [
- { row: 2, column: 2, data: '33' },
- { row: 3, column: 0, data: '41' },
- { row: 3, column: 1, data: '42' },
- { row: 3, column: 2, data: '43' }
- ], { startRow: 2 } );
- } );
- } );
- describe( 'option.endRow', () => {
- it( 'should stopp iterating after given row but with cell spans properly calculated', () => {
- testWalker( [
- [ { colspan: 2, rowspan: 3, contents: '11' }, '13' ],
- [ '23' ],
- [ '33' ],
- [ '41', '42', '43' ]
- ], [
- { row: 0, column: 0, data: '11' },
- { row: 0, column: 2, data: '13' },
- { row: 1, column: 2, data: '23' },
- { row: 2, column: 2, data: '33' }
- ], { endRow: 2 } );
- } );
- } );
- describe( 'option.includeSpanned', () => {
- it( 'should output spanned cells as empty cell', () => {
- testWalker( [
- [ { colspan: 2, rowspan: 3, contents: '11' }, '13' ],
- [ '23' ],
- [ '33' ],
- [ '41', { colspan: 2, contents: '42' } ]
- ], [
- { row: 0, column: 0, data: '11' },
- { row: 0, column: 1, data: undefined },
- { row: 0, column: 2, data: '13' },
- { row: 1, column: 0, data: undefined },
- { row: 1, column: 1, data: undefined },
- { row: 1, column: 2, data: '23' },
- { row: 2, column: 0, data: undefined },
- { row: 2, column: 1, data: undefined },
- { row: 2, column: 2, data: '33' },
- { row: 3, column: 0, data: '41' },
- { row: 3, column: 1, data: '42' },
- { row: 3, column: 2, data: undefined }
- ], { includeSpanned: true } );
- } );
- it( 'should work with startRow & endRow options', () => {
- testWalker( [
- [ { colspan: 2, rowspan: 3, contents: '11' }, '13' ],
- [ '23' ],
- [ '33' ],
- [ '41', '42', '43' ]
- ], [
- { row: 1, column: 0, data: undefined },
- { row: 1, column: 1, data: undefined },
- { row: 1, column: 2, data: '23' },
- { row: 2, column: 0, data: undefined },
- { row: 2, column: 1, data: undefined },
- { row: 2, column: 2, data: '33' }
- ], { includeSpanned: true, startRow: 1, endRow: 2 } );
- } );
- } );
- describe( 'option.endRow', () => {
- it( 'should iterate over given row 0 only', () => {
- testWalker( [
- [ { colspan: 2, rowspan: 3, contents: '11' }, '13' ],
- [ '23' ],
- [ '33' ],
- [ '41', '42', '43' ]
- ], [
- { row: 0, column: 0, data: '11' },
- { row: 0, column: 2, data: '13' }
- ], { endRow: 0 } );
- } );
- } );
- } );
|