Skip to content

Commit

Permalink
Merge pull request #12065 from ckeditor/ck/11972
Browse files Browse the repository at this point in the history
Internal (image): Prevent getClosestSelectedImageWidget() from throwing when view selection is empty. Closes #11972.

Internal (table): Prevent getTableWidgetAncestor() from throwing when view selection is empty. Closes #11972.
  • Loading branch information
arkflpc authored Jul 15, 2022
2 parents 8856154 + 6930fdd commit 9f36712
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
8 changes: 7 additions & 1 deletion packages/ckeditor5-image/src/imageutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,19 @@ export default class ImageUtils extends Plugin {
* @returns {module:engine/view/element~Element|null}
*/
getClosestSelectedImageWidget( selection ) {
const selectionPosition = selection.getFirstPosition();

if ( !selectionPosition ) {
return null;
}

const viewElement = selection.getSelectedElement();

if ( viewElement && this.isImageWidget( viewElement ) ) {
return viewElement;
}

let parent = selection.getFirstPosition().parent;
let parent = selectionPosition.parent;

while ( parent ) {
if ( parent.is( 'element' ) && this.isImageWidget( parent ) ) {
Expand Down
7 changes: 7 additions & 0 deletions packages/ckeditor5-image/tests/imageutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ describe( 'ImageUtils plugin', () => {

expect( imageUtils.getClosestSelectedImageWidget( selection ) ).to.be.null;
} );

// See https://github.com/ckeditor/ckeditor5/issues/11972.
it( 'should return null if view selection is empty', () => {
const selection = writer.createSelection();

expect( imageUtils.getClosestSelectedImageWidget( selection ) ).to.be.null;
} );
} );

describe( 'getClosestSelectedImageElement()', () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/ckeditor5-table/src/utils/ui/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ export function getSelectedTableWidget( selection ) {
* @returns {module:engine/view/element~Element|null}
*/
export function getTableWidgetAncestor( selection ) {
let parent = selection.getFirstPosition().parent;
const selectionPosition = selection.getFirstPosition();

if ( !selectionPosition ) {
return null;
}

let parent = selectionPosition.parent;

while ( parent ) {
if ( parent.is( 'element' ) && isTableWidget( parent ) ) {
Expand Down
20 changes: 20 additions & 0 deletions packages/ckeditor5-table/tests/utils/ui/widget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

import ViewSelection from '@ckeditor/ckeditor5-engine/src/view/selection';
import { getTableWidgetAncestor } from '../../../src/utils/ui/widget';

describe( 'table utils', () => {
describe( 'widget', () => {
describe( 'getTableWidgetAncestor()', () => {
// See https://github.com/ckeditor/ckeditor5/issues/11972.
it( 'should return null if view selection is empty', () => {
const selection = new ViewSelection();

expect( getTableWidgetAncestor( selection ) ).to.be.null;
} );
} );
} );
} );

0 comments on commit 9f36712

Please sign in to comment.