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

Decorator downcasting should ignore elements without links. #8739

Merged
merged 5 commits into from
Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions packages/ckeditor5-link/src/linkimageediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ function downcastImageLinkManualDecorator( manualDecorators, decorator ) {
const viewFigure = conversionApi.mapper.toViewElement( data.item );
const linkInImage = Array.from( viewFigure.getChildren() ).find( child => child.name === 'a' );

// The unlink command is removing the link by the time this dispatcher is ran.
maxbarnas marked this conversation as resolved.
Show resolved Hide resolved
if ( !linkInImage ) {
return;
}

for ( const [ key, val ] of toMap( attributes ) ) {
conversionApi.writer.setAttribute( key, val, linkInImage );
}
Expand Down
28 changes: 28 additions & 0 deletions packages/ckeditor5-link/tests/linkimageediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,34 @@ describe( 'LinkImageEditing', () => {
'</p>'
);
} );

it( 'should downcast without error if the image already has no link', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add two more tests. In the one you adde you check whether what the command does works. But what if someone operates on the attributes themselves? Then they can do things in a different order and also they can mix those operations with other, potentially conflicting operations (e.g. set decorator attr, remove linkHref). I actually don't know what will happen in this case in the model as I don't know if there's some postfixer that cleans up decorator attrs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just added another test for reversed order of downcasters. Will the first test be enough to cover the "regular" order, or should I explicitly add it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it makes sense to add another test with an explicit, "default" order of operations. Just in case the default behavior changes.

setModelData( model,
'[<image alt="bar" src="sample.jpg"></image>]'
);

editor.execute( 'link', 'https://cksource.com', {
linkIsDownloadable: true,
linkIsExternal: true,
linkIsGallery: true
} );

// Attributes will be removed along with the link, but the downcast will be fired.
// The lack of link should not affect the downcasting. #8401.
expect( () => {
editor.execute( 'unlink', 'https://cksource.com', {
linkIsDownloadable: true,
linkIsExternal: true,
linkIsGallery: true
} );
} ).to.not.throw();

expect( editor.getData() ).to.equal(
'<figure class="image">' +
'<img src="sample.jpg" alt="bar">' +
'</figure>'
);
} );
} );
} );
} );