/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
import { getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
import upcastTable, { upcastTableCell } from '../../src/converters/upcasttable';
import { formatTable } from '../_utils/utils';
import Paragraph from '../../../ckeditor5-paragraph/src/paragraph';
describe( 'upcastTable()', () => {
let editor, model;
beforeEach( () => {
return VirtualTestEditor
.create( {
plugins: [ Paragraph ]
} )
.then( newEditor => {
editor = newEditor;
model = editor.model;
const conversion = editor.conversion;
const schema = model.schema;
schema.register( 'table', {
allowWhere: '$block',
allowAttributes: [ 'headingRows', 'headingColumns' ],
isLimit: true,
isObject: true
} );
schema.register( 'tableRow', {
allowIn: 'table',
isLimit: true
} );
schema.register( 'tableCell', {
allowIn: 'tableRow',
allowAttributes: [ 'colspan', 'rowspan' ],
isLimit: true
} );
schema.extend( '$block', { allowIn: 'tableCell' } );
conversion.for( 'upcast' ).add( upcastTable() );
// Table row upcast only since downcast conversion is done in `downcastTable()`.
conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableRow', view: 'tr' } ) );
// Table cell conversion.
conversion.for( 'upcast' ).add( upcastTableCell( 'td' ) );
conversion.for( 'upcast' ).add( upcastTableCell( 'th' ) );
conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
// Since this part of test tests only view->model conversion editing pipeline is not necessary
// so defining model->view converters won't be necessary.
editor.editing.destroy();
} );
} );
function expectModel( data ) {
expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formatTable( data ) );
}
it( 'should convert table figure', () => {
editor.setData(
'' +
'' +
''
);
expectModel(
'
'
);
} );
it( 'should create table model from table without thead', () => {
editor.setData(
''
);
expectModel(
''
);
} );
it( 'should not convert empty figure', () => {
'';
expectModel( '' );
} );
it( 'should convert if figure do not have class="table" attribute', () => {
editor.setData(
'' +
'' +
''
);
expectModel(
''
);
} );
it( 'should create table model from table with one thead with one row', () => {
editor.setData(
''
);
expectModel(
''
);
} );
it( 'should create table model from table with one thead with more then one row', () => {
editor.setData(
'' +
'' +
'| 1 |
' +
'| 2 |
' +
'| 3 |
' +
'' +
'
'
);
expectModel(
''
);
} );
it( 'should create table model from table with two theads with one row', () => {
editor.setData(
''
);
expectModel(
''
);
} );
it( 'should create table model from table with thead after the tbody', () => {
editor.setData(
''
);
expectModel(
''
);
} );
it( 'should create table model from table with one tfoot with one row', () => {
editor.setData(
''
);
expectModel(
''
);
} );
it( 'should create valid table model from empty table', () => {
editor.setData(
''
);
expectModel(
''
);
} );
it( 'should skip unknown table children', () => {
editor.setData(
''
);
expectModel(
''
);
} );
it( 'should create table model from some broken table', () => {
editor.setData(
''
);
expectModel(
''
);
} );
it( 'should fix if inside other blocks', () => {
// Using instead of
as it breaks on Edge.
editor.model.schema.register( 'div', {
inheritAllFrom: '$block'
} );
editor.conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'div', view: 'div' } ) );
editor.setData(
'
'
);
expectModel(
'
foo
' +
'
' +
'
bar
'
);
} );
it( 'should be possible to overwrite table conversion', () => {
editor.model.schema.register( 'fooTable', {
allowWhere: '$block',
allowAttributes: [ 'headingRows' ],
isObject: true
} );
editor.model.schema.register( 'fooCell', {
allowIn: 'fooRow',
isObject: true
} );
editor.model.schema.register( 'fooRow', {
allowIn: 'fooTable',
isObject: true
} );
editor.conversion.elementToElement( { model: 'fooTable', view: 'table', converterPriority: 'high' } );
editor.conversion.elementToElement( { model: 'fooRow', view: 'tr', converterPriority: 'high' } );
editor.conversion.elementToElement( { model: 'fooCell', view: 'td', converterPriority: 'high' } );
editor.conversion.elementToElement( { model: 'fooCell', view: 'th', converterPriority: 'high' } );
editor.setData(
'
'
);
expectModel(
'
'
);
} );
describe( 'headingColumns', () => {
it( 'should properly calculate heading columns', () => {
editor.setData(
'
' +
'' +
// This row starts with 1 th (3 total).
'| 21 | 22 | 23 | 24 |
' +
// This row starts with 2 th (2 total). This one has max number of heading columns: 2.
'| 31 | 32 | 33 | 34 |
' +
// This row starts with 1 th (1 total).
'| 41 | 42 | 43 | 44 |
' +
// This row starts with 0 th (3 total).
'| 51 | 52 | 53 | 54 |
' +
'' +
'' +
// This row has 4 ths but it is a thead.
'| 11 | 12 | 13 | 14 |
' +
'' +
'
'
);
expectModel(
'
' +
'' +
'11' +
'12' +
'13' +
'14' +
'' +
'' +
'21' +
'22' +
'23' +
'24' +
'' +
'' +
'31' +
'32' +
'33' +
'34' +
'' +
'' +
'41' +
'42' +
'43' +
'44' +
'' +
'' +
'51' +
'52' +
'53' +
'54' +
'' +
'
'
);
} );
it( 'should calculate heading columns of cells with colspan', () => {
editor.setData(
'
' +
'' +
// This row has colspan of 3 so it should be the whole table should have 3 heading columns.
'| 21 | 22 | 23 | 24 |
' +
'| 31 | 33 | 34 |
' +
'' +
'' +
// This row has 4 ths but it is a thead.
'| 11 | 12 | 13 | 14 |
' +
'' +
'
'
);
expectModel(
'
' +
'' +
'11' +
'12' +
'13' +
'14' +
'' +
'' +
'21' +
'22' +
'23' +
'24' +
'' +
'' +
'31' +
'33' +
'34' +
'' +
'
'
);
} );
} );
} );