/**
* @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 Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import ImageEditing from '@ckeditor/ckeditor5-image/src/image/imageediting';
import Widget from '@ckeditor/ckeditor5-widget/src/widget';
import { defaultConversion, defaultSchema, formatTable, modelTable } from '../_utils/utils';
describe( 'upcastTable()', () => {
let editor, model;
beforeEach( () => {
return VirtualTestEditor
.create( {
plugins: [ Paragraph, ImageEditing, Widget ]
} )
.then( newEditor => {
editor = newEditor;
model = editor.model;
defaultSchema( model.schema, false );
defaultConversion( editor.conversion, true );
// 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(
'
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(
'
'
);
} );
it( 'should strip table in table', () => {
editor.setData(
'
' +
'' +
'' +
'' +
'' +
'| tableception | ' +
' ' +
' ' +
' | ' +
'
' +
'
'
);
expectModel(
'
' +
'' +
'' +
'tableception' +
'' +
'' +
'
'
);
} );
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' +
'' +
'
'
);
} );
} );
describe( 'block contents', () => {
it( 'should upcast table with empty table cell to paragraph', () => {
editor.setData(
'
' +
'' +
'' +
'| foo | ' +
'
' +
'' +
'
'
);
expectModel( modelTable( [
[ 'foo' ]
] ) );
} );
it( 'should upcast table with
in table cell', () => {
editor.setData(
'
' +
'' +
'' +
'foo | ' +
'
' +
'' +
'
'
);
expectModel( modelTable( [
[ 'foo' ]
] ) );
} );
it( 'should upcast table with multiple
in table cell', () => {
editor.setData(
'
' +
'' +
'' +
'| ' +
' foo ' +
'bar ' +
'baz ' +
' | ' +
'
' +
'' +
'
'
);
expectModel( modelTable( [
[ '
foobarbaz' ]
] ) );
} );
it( 'should upcast table with
![]()
in table cell to empty table cell', () => {
editor.setData(
'
' +
'' +
'' +
' | ' +
'
' +
'' +
'
'
);
expectModel( modelTable( [
[ '' ]
] ) );
} );
} );
} );