Skip to content

Commit

Permalink
Additional tests + fixes after the review.
Browse files Browse the repository at this point in the history
  • Loading branch information
Magdalena Chrześcian committed Jan 25, 2021
1 parent 51408da commit 37e74ec
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/ckeditor5-media-embed/src/mediaembedcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,5 @@ function isAllowedInParent( selection, model ) {
// @returns {Boolean}
function isMediaSelected( selection ) {
const element = selection.getSelectedElement();
return element && element.name === 'media' || false;
return !!element && element.name === 'media';
}
4 changes: 2 additions & 2 deletions packages/ckeditor5-widget/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export function findOptimalInsertionPosition( selection, model ) {
}

/**
* Checks if the selection is on object.
* Checks if the selection is on an object.
*
* @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection} selection
* @param {module:engine/model/schema~Schema} schema
Expand All @@ -314,7 +314,7 @@ export function findOptimalInsertionPosition( selection, model ) {
export function checkSelectionOnObject( selection, schema ) {
const selectedElement = selection.getSelectedElement();

return selectedElement && schema.isObject( selectedElement ) || false;
return !!selectedElement && schema.isObject( selectedElement );
}

/**
Expand Down
26 changes: 24 additions & 2 deletions packages/ckeditor5-widget/tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,10 @@ describe( 'widget utils', () => {
allowIn: '$root',
isSelectable: true
} );

model.schema.extend( '$text', {
allowIn: 'image'
} );
} );

it( 'should return false if no element is selected', () => {
Expand All @@ -536,7 +540,7 @@ describe( 'widget utils', () => {
expect( isSelectionOnObject ).to.be.false;
} );

it( 'should return false if the selection is not on the object', () => {
it( 'should return false if the selection is not on an object', () => {
setData( model, '[<element></element>]' );

const selection = model.document.selection;
Expand All @@ -545,14 +549,32 @@ describe( 'widget utils', () => {
expect( isSelectionOnObject ).to.be.false;
} );

it( 'should return true if the selection is on the object', () => {
it( 'should return true if the selection is on an object', () => {
setData( model, '<paragraph></paragraph>[<image></image>]' );

const selection = model.document.selection;
const isSelectionOnObject = checkSelectionOnObject( selection, model.schema );

expect( isSelectionOnObject ).to.be.true;
} );

it( 'should return false if the selection contains an object', () => {
setData( model, '<paragraph>fo[o</paragraph><image></image><paragraph>ba]r</paragraph>' );

const selection = model.document.selection;
const isSelectionOnObject = checkSelectionOnObject( selection, model.schema );

expect( isSelectionOnObject ).to.be.false;
} );

it( 'should return false if the selection is nested in an object', () => {
setData( model, '<image>[foo]</image>' );

const selection = model.document.selection;
const isSelectionOnObject = checkSelectionOnObject( selection, model.schema );

expect( isSelectionOnObject ).to.be.false;
} );
} );

describe( 'viewToModelPositionOutsideModelElement()', () => {
Expand Down

0 comments on commit 37e74ec

Please sign in to comment.