/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import LinkImageEditing from '../src/linkimageediting';
import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml';
import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
import ImageCaptionEditing from '@ckeditor/ckeditor5-image/src/imagecaption/imagecaptionediting';
describe( 'LinkImageEditing', () => {
let editor, model, view;
beforeEach( () => {
return VirtualTestEditor
.create( {
plugins: [ Paragraph, LinkImageEditing ]
} )
.then( newEditor => {
editor = newEditor;
model = editor.model;
view = editor.editing.view;
} );
} );
afterEach( () => {
return editor.destroy();
} );
it( 'should have pluginName', () => {
expect( LinkImageEditing.pluginName ).to.equal( 'LinkImageEditing' );
} );
it( 'should be loaded', () => {
expect( editor.plugins.get( LinkImageEditing ) ).to.be.instanceOf( LinkImageEditing );
} );
it( 'should set proper schema rules', () => {
expect( model.schema.checkAttribute( [ '$root', 'image' ], 'linkHref' ) ).to.be.true;
} );
describe( 'conversion in data pipeline', () => {
describe( 'model to view', () => {
it( 'should convert an image with a link', () => {
setModelData( model, '' );
expect( editor.getData() ).to.equal(
'
'
);
} );
it( 'should convert an image with a link and without alt attribute', () => {
setModelData( model, '' );
expect( editor.getData() ).to.equal(
'
'
);
} );
it( 'should convert srcset attribute to srcset and sizes attribute wrapped into a link', () => {
setModelData( model,
'' +
''
);
expect( normalizeHtml( editor.getData() ) ).to.equal(
'' +
'' +
'
' +
'' +
''
);
} );
} );
describe( 'view to model', () => {
describe( 'figure > a > img', () => {
it( 'should convert a link in an image figure', () => {
editor.setData(
'
'
);
expect( getModelData( model, { withoutSelection: true } ) )
.to.equal( '' );
} );
it( 'should convert an image with a link and without alt attribute', () => {
editor.setData( '
' );
expect( getModelData( model, { withoutSelection: true } ) )
.to.equal( '' );
} );
it( 'should not convert without src attribute', () => {
editor.setData( '
' );
expect( getModelData( model, { withoutSelection: true } ) )
.to.equal( '' );
} );
it( 'should not convert in wrong context', () => {
model.schema.register( 'div', { inheritAllFrom: '$block' } );
model.schema.addChildCheck( ( ctx, childDef ) => {
if ( ctx.endsWith( '$root' ) && childDef.name == 'image' ) {
return false;
}
} );
editor.conversion.elementToElement( { model: 'div', view: 'div' } );
editor.setData(
'
' );
expect( getModelData( model, { withoutSelection: true } ) )
.to.equal( '' );
} );
it( 'should not convert "a" element if is already consumed', () => {
editor.data.upcastDispatcher.on( 'element:a', ( evt, data, conversionApi ) => {
conversionApi.consumable.consume( data.viewItem, { attributes: [ 'href' ] } );
}, { priority: 'highest' } );
editor.setData(
'
'
);
expect( editor.getData() ).to.equal( '
' );
} );
it( 'should not convert if a link misses "href" attribute', () => {
editor.setData(
'
'
);
expect( getModelData( model, { withoutSelection: true } ) )
.to.equal( '' );
} );
it( 'should convert a link without an image to a paragraph with the link', () => {
editor.setData(
'Foo'
);
expect( getModelData( model, { withoutSelection: true } ) )
.to.equal( '<$text linkHref="http://ckeditor.com">Foo$text>' );
} );
} );
describe( 'a > img', () => {
it( 'should convert a link in an image figure', () => {
editor.setData(
'
'
);
expect( getModelData( model, { withoutSelection: true } ) )
.to.equal( '' );
} );
it( 'should convert an image with a link and without alt attribute', () => {
editor.setData( '
' );
expect( getModelData( model, { withoutSelection: true } ) )
.to.equal( '' );
} );
it( 'should not convert without src attribute', () => {
editor.setData( '
' );
expect( getModelData( model, { withoutSelection: true } ) )
.to.equal( '' );
} );
it( 'should not convert in wrong context', () => {
model.schema.register( 'div', { inheritAllFrom: '$block' } );
model.schema.addChildCheck( ( ctx, childDef ) => {
if ( ctx.endsWith( '$root' ) && childDef.name == 'image' ) {
return false;
}
} );
editor.conversion.elementToElement( { model: 'div', view: 'div' } );
editor.setData(
'' );
expect( getModelData( model, { withoutSelection: true } ) )
.to.equal( '' );
} );
it( 'should not convert "a" element if is already consumed', () => {
editor.data.upcastDispatcher.on( 'element:a', ( evt, data, conversionApi ) => {
conversionApi.consumable.consume( data.viewItem, { attributes: [ 'href' ] } );
}, { priority: 'highest' } );
editor.setData(
'
'
);
expect( editor.getData() ).to.equal( '
' );
} );
it( 'should not convert if a link misses "href" attribute', () => {
editor.setData(
'
'
);
expect( getModelData( model, { withoutSelection: true } ) )
.to.equal( '' );
} );
} );
describe( 'figure > a > img + figcaption', () => {
it( 'should convert a link and the caption element', () => {
return VirtualTestEditor
.create( {
plugins: [ Paragraph, LinkImageEditing, ImageCaptionEditing ]
} )
.then( editor => {
editor.setData(
'' +
'' +
'
' +
'' +
'' +
'Foo Bar.' +
'' +
''
);
expect( getModelData( editor.model, { withoutSelection: true } ) ).to.equal(
'' +
'Foo Bar.' +
''
);
return editor.destroy();
} );
} );
} );
} );
} );
describe( 'conversion in editing pipeline', () => {
describe( 'model to view', () => {
it( 'should convert the image element', () => {
setModelData( model, '' );
expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
'' +
'' +
'
' +
'' +
''
);
} );
it( 'should convert attribute change', () => {
setModelData( model, '' );
const image = model.document.getRoot().getChild( 0 );
model.change( writer => {
writer.setAttribute( 'linkHref', 'https://ckeditor.com/why-ckeditor/', image );
} );
expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
'' +
'' +
'
' +
'' +
''
);
} );
it( 'should convert attribute removal', () => {
setModelData( model, '' );
const image = model.document.getRoot().getChild( 0 );
model.change( writer => {
writer.removeAttribute( 'linkHref', image );
} );
expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
'' +
'
' +
''
);
} );
} );
describe( 'figure > a > img + figcaption', () => {
it( 'should convert a link and the caption element', () => {
return VirtualTestEditor
.create( {
plugins: [ Paragraph, LinkImageEditing, ImageCaptionEditing ]
} )
.then( editor => {
setModelData( editor.model,
'' +
'Foo Bar.' +
''
);
expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal(
'' +
'' +
'
' +
'' +
'' +
'Foo Bar.' +
'' +
''
);
return editor.destroy();
} );
} );
} );
} );
} );