Skip to content

Commit

Permalink
Merge pull request #8466 from ckeditor/i/7892
Browse files Browse the repository at this point in the history
Fix (engine): Editor should not crash while selecting an Image from bottom to top. Closes #7892.
  • Loading branch information
niegowski committed Nov 23, 2020
2 parents 3d86dad + 185fbc2 commit 5dd05b9
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,10 @@ function mergeIntersectingRanges( ranges ) {
for ( const range of ranges ) {
const previousRange = nonIntersectingRanges.pop();

if ( range.isIntersecting( previousRange ) ) {
if ( range.isEqual( previousRange ) ) {
// Use only one of two identical ranges.
nonIntersectingRanges.push( previousRange );
} else if ( range.isIntersecting( previousRange ) ) {
// Get the sum of two ranges.
const start = previousRange.start.isAfter( range.start ) ? range.start : previousRange.start;
const end = previousRange.end.isAfter( range.end ) ? previousRange.end : range.end;
Expand Down
7 changes: 7 additions & 0 deletions packages/ckeditor5-engine/tests/manual/tickets/7892/1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div id="editor">
<p>Finish the selection HERE.</p>
<figure class="image">
<img src="sample.jpg" alt="sample image" />
</figure>
<p>Start the selection HERE.</p>
</div>
25 changes: 25 additions & 0 deletions packages/ckeditor5-engine/tests/manual/tickets/7892/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

/* globals window, document, console */

import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';

const config = {
plugins: [ ArticlePluginSet ],
toolbar: [
'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote', 'undo', 'redo'
]
};

ClassicEditor
.create( document.querySelector( '#editor' ), config )
.then( editor => {
window.editor = editor;
} )
.catch( error => {
console.error( error.stack );
} );
9 changes: 9 additions & 0 deletions packages/ckeditor5-engine/tests/manual/tickets/7892/1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### Selecting an Image from bottom to top throws exceptions [#7892](https://github.com/ckeditor/ckeditor5/issues/7892)

1. Open Firefox.
2. Start the selection below the image using mouse.
3. Finish the selection above the image.

**Expected result**: there is no error in the console.

**Unexpected result**: there is an `Uncaught CKEditorError: model-selection-range-intersects: Trying to add a range that intersects with another range in the selection` error in the console.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,35 @@ describe( 'Selection post-fixer', () => {
'[<figure></figure>]'
);
} );

it( 'should fix multi-range selection with equal ranges', () => {
// It may happen that multi-range selection contains equal ranges.
// Duplicated ranges should be ommited from the final (merged) selection.
// https://github.com/ckeditor/ckeditor5/issues/7892
model.change( writer => {
const firstRange = writer.createRange(
writer.createPositionAt( modelRoot.getChild( 1 ).getChild( 0 ), 3 )
);

const duplicatedRange = firstRange.clone();

const otherRange = writer.createRange(
writer.createPositionAt( modelRoot.getChild( 1 ).getChild( 0 ), 3 ),
writer.createPositionAt( modelRoot.getChild( 2 ), 0 )
);

// <paragraph>foo</paragraph><image><caption>xxx[][][</caption></image><paragraph>]bar</paragraph>
writer.setSelection( [ firstRange, duplicatedRange, otherRange ] );
} );

expect( getModelData( model ) ).to.equal(
'<paragraph>foo</paragraph>' +
'[<image>' +
'<caption>xxx</caption>' +
'</image>' +
'<paragraph>]bar</paragraph>'
);
} );
} );

describe( 'non-collapsed selection - other scenarios', () => {
Expand Down

0 comments on commit 5dd05b9

Please sign in to comment.