Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No longer crash editor after removing link from image when LinkConfig#addTargetToExternalLinks is enabled #17281

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/ckeditor5-link/src/utils/automaticdecorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ export default class AutomaticDecorators {
const linkInImage = Array.from( viewFigure.getChildren() )
.find( ( child ): child is ViewElement => child.is( 'element', 'a' ) )!;

// It's not guaranteed that the anchor is present in the image block during execution of this dispatcher.
// It might have been removed during the execution of unlink command that runs the image link downcast dispatcher
// that is executed before this one and removes the anchor from the image block.
if ( !linkInImage ) {
return;
}

for ( const item of this._definitions ) {
const attributes = toMap( item.attributes );

Expand Down
43 changes: 43 additions & 0 deletions packages/ckeditor5-link/tests/unlinkcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

import { global } from '@ckeditor/ckeditor5-utils';
import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor.js';
import UnlinkCommand from '../src/unlinkcommand.js';
import LinkEditing from '../src/linkediting.js';
import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model.js';
import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils.js';
import LinkImageEditing from '../src/linkimageediting.js';
import Image from '@ckeditor/ckeditor5-image/src/image.js';
import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor.js';

describe( 'UnlinkCommand', () => {
let editor, model, document, command;
Expand Down Expand Up @@ -508,4 +512,43 @@ describe( 'UnlinkCommand', () => {
expect( getData( model ) ).to.equal( '<paragraph>[<linkableInline></linkableInline>]</paragraph>' );
} );
} );

describe( '`Image` plugin integration', () => {
let editorElement;

beforeEach( async () => {
await editor.destroy();

editorElement = global.document.body.appendChild(
global.document.createElement( 'div' )
);

return ClassicTestEditor.create( editorElement, {
extraPlugins: [ LinkEditing, LinkImageEditing, Image ],
link: {
addTargetToExternalLinks: true
}
} )
.then( newEditor => {
editor = newEditor;
model = editor.model;
document = model.document;
} );
} );

afterEach( async () => {
await editor.destroy();
editorElement.remove();
} );

it( 'should not crash during removal of external `linkHref` from `imageBlock` when `Image` plugin is present', () => {
setData( model, '[<imageBlock linkHref="url"></imageBlock>]' );

expect( () => {
editor.execute( 'unlink' );
} ).not.to.throw();

expect( getData( model ) ).to.equal( '[<imageBlock></imageBlock>]' );
} );
} );
} );