From ef1ab8e94eb8116c1d288dc337472e1240393ad1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maksymilian=20Barna=C5=9B?= Date: Tue, 12 Jan 2021 16:30:36 +0100 Subject: [PATCH 1/2] Stop conversion early when model element is not available. --- .../src/imagestyle/converters.js | 5 +++-- .../tests/imagestyle/imagestyleediting.js | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/ckeditor5-image/src/imagestyle/converters.js b/packages/ckeditor5-image/src/imagestyle/converters.js index aefa0e94207..8de6df27d49 100644 --- a/packages/ckeditor5-image/src/imagestyle/converters.js +++ b/packages/ckeditor5-image/src/imagestyle/converters.js @@ -57,8 +57,9 @@ export function viewToModelStyleAttribute( styles ) { const viewFigureElement = data.viewItem; const modelImageElement = first( data.modelRange.getItems() ); - // Check if `imageStyle` attribute is allowed for current element. - if ( !conversionApi.schema.checkAttribute( modelImageElement, 'imageStyle' ) ) { + // Check if `modelImageElement` exists (see: #8270) and `imageStyle` attribute is allowed + // for that element, otherwise stop conversion early. + if ( modelImageElement && !conversionApi.schema.checkAttribute( modelImageElement, 'imageStyle' ) ) { return; } diff --git a/packages/ckeditor5-image/tests/imagestyle/imagestyleediting.js b/packages/ckeditor5-image/tests/imagestyle/imagestyleediting.js index cf7b4ef5b35..02c50d54ca8 100644 --- a/packages/ckeditor5-image/tests/imagestyle/imagestyleediting.js +++ b/packages/ckeditor5-image/tests/imagestyle/imagestyleediting.js @@ -4,6 +4,7 @@ */ import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor'; +import ImageResizeEditing from '../../src/imageresize/imageresizeediting'; import ImageStyleEditing from '../../src/imagestyle/imagestyleediting'; import ImageEditing from '../../src/image/imageediting'; import ImageStyleCommand from '../../src/imagestyle/imagestylecommand'; @@ -249,6 +250,24 @@ describe( 'ImageStyleEditing', () => { '
' ); } ); + + // See: https://github.com/ckeditor/ckeditor5/issues/8270. + it( 'should stop conversion when model element is not found', () => { + return VirtualTestEditor + .create( { + plugins: [ ImageEditing, ImageResizeEditing, ImageStyleEditing ] + } ) + .then( newEditor => { + editor = newEditor; + + expect( + () => editor.setData( '
' ) + ).not.to.throw(); + + // No conversion has been done. + expect( editor.getData() ).to.equal( '' ); + } ); + } ); } ); describe( 'config', () => { From 5fc60259b47404041ddeb73ddc56fffe4181866d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maksymilian=20Barna=C5=9B?= Date: Thu, 14 Jan 2021 10:40:10 +0100 Subject: [PATCH 2/2] Review fixes. --- packages/ckeditor5-image/src/imagestyle/converters.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/ckeditor5-image/src/imagestyle/converters.js b/packages/ckeditor5-image/src/imagestyle/converters.js index 8de6df27d49..79116a2f1a1 100644 --- a/packages/ckeditor5-image/src/imagestyle/converters.js +++ b/packages/ckeditor5-image/src/imagestyle/converters.js @@ -57,8 +57,8 @@ export function viewToModelStyleAttribute( styles ) { const viewFigureElement = data.viewItem; const modelImageElement = first( data.modelRange.getItems() ); - // Check if `modelImageElement` exists (see: #8270) and `imageStyle` attribute is allowed - // for that element, otherwise stop conversion early. + // Check if `modelImageElement` exists (see: https://github.com/ckeditor/ckeditor5/issues/8270) + // and `imageStyle` attribute is allowed for that element, otherwise stop conversion early. if ( modelImageElement && !conversionApi.schema.checkAttribute( modelImageElement, 'imageStyle' ) ) { return; }