This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1286 from ckeditor/t/1281
Fix: `DocumenSelection#change:range` event will be fired only once after multiple selection live ranges have changed. Closes #1281.
- Loading branch information
Showing
3 changed files
with
274 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
/** | ||
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
/* globals document */ | ||
|
||
import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor'; | ||
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; | ||
import Position from '../../src/model/position'; | ||
|
||
import { setData as setModelData, getData as getModelData } from '../../src/dev-utils/model'; | ||
|
||
describe( 'Bug ckeditor5-engine#1281', () => { | ||
let element, editor, model; | ||
|
||
beforeEach( () => { | ||
element = document.createElement( 'div' ); | ||
document.body.appendChild( element ); | ||
|
||
return ClassicTestEditor | ||
.create( element, { plugins: [ Paragraph ] } ) | ||
.then( newEditor => { | ||
editor = newEditor; | ||
model = editor.model; | ||
} ); | ||
} ); | ||
|
||
afterEach( () => { | ||
element.remove(); | ||
|
||
return editor.destroy(); | ||
} ); | ||
|
||
it( 'loads content that contains multi-range selection', () => { | ||
setModelData( model, | ||
'<paragraph>Paragraph 1.</paragraph>' + | ||
'<paragraph>Paragraph 2.</paragraph>' + | ||
'<paragraph>[Paragraph 3.]</paragraph>' + | ||
'<paragraph>[Paragraph 4.]</paragraph>' | ||
); | ||
|
||
const root = model.document.getRoot(); | ||
const thirdParagraph = root.getNodeByPath( [ 2 ] ); | ||
const fourthParagraph = root.getNodeByPath( [ 3 ] ); | ||
const selRanges = Array.from( model.document.selection.getRanges() ); | ||
|
||
expect( selRanges.length ).to.equal( 2 ); | ||
|
||
assertPositions( Position.createAt( thirdParagraph ), selRanges[ 0 ].start ); | ||
assertPositions( Position.createAt( thirdParagraph, 'end' ), selRanges[ 0 ].end ); | ||
|
||
assertPositions( Position.createAt( fourthParagraph ), selRanges[ 1 ].start ); | ||
assertPositions( Position.createAt( fourthParagraph, 'end' ), selRanges[ 1 ].end ); | ||
} ); | ||
|
||
it( 'does not throw an error when content before the selection is being removed (last element is selected)', () => { | ||
setModelData( model, | ||
'<paragraph>Paragraph 1.</paragraph>' + | ||
'<paragraph>Paragraph 2.</paragraph>' + | ||
'<paragraph>[Paragraph 3.]</paragraph>' + | ||
'<paragraph>[Paragraph 4.]</paragraph>' | ||
); | ||
|
||
model.change( writer => { | ||
const root = model.document.getRoot(); | ||
const firstParagraph = root.getNodeByPath( [ 0 ] ); | ||
|
||
expect( () => { | ||
writer.remove( firstParagraph ); | ||
} ).to.not.throw(); | ||
|
||
assertOutput( | ||
'<paragraph>Paragraph 2.</paragraph>' + | ||
'<paragraph>[Paragraph 3.]</paragraph>' + | ||
'<paragraph>[Paragraph 4.]</paragraph>' | ||
); | ||
} ); | ||
} ); | ||
|
||
it( 'does not throw an error when content before the selection is being removed (last element is not selected)', () => { | ||
setModelData( model, | ||
'<paragraph>Paragraph 1.</paragraph>' + | ||
'<paragraph>Paragraph 2.</paragraph>' + | ||
'<paragraph>[Paragraph 3.]</paragraph>' + | ||
'<paragraph>[Paragraph 4.]</paragraph>' + | ||
'<paragraph>Paragraph 5.</paragraph>' | ||
); | ||
|
||
model.change( writer => { | ||
const root = model.document.getRoot(); | ||
const firstParagraph = root.getNodeByPath( [ 0 ] ); | ||
|
||
expect( () => { | ||
writer.remove( firstParagraph ); | ||
} ).to.not.throw(); | ||
|
||
assertOutput( | ||
'<paragraph>Paragraph 2.</paragraph>' + | ||
'<paragraph>[Paragraph 3.]</paragraph>' + | ||
'<paragraph>[Paragraph 4.]</paragraph>' + | ||
'<paragraph>Paragraph 5.</paragraph>' | ||
); | ||
} ); | ||
} ); | ||
|
||
it( 'does not throw an error when content after the selection is being removed (first element is selected)', () => { | ||
setModelData( model, | ||
'<paragraph>[Paragraph 1.]</paragraph>' + | ||
'<paragraph>Paragraph 2.</paragraph>' + | ||
'<paragraph>Paragraph 3.</paragraph>' + | ||
'<paragraph>[Paragraph 4.]</paragraph>' + | ||
'<paragraph>Paragraph 5.</paragraph>' | ||
); | ||
|
||
model.change( writer => { | ||
const root = model.document.getRoot(); | ||
const lastParagraph = root.getNodeByPath( [ 4 ] ); | ||
|
||
expect( () => { | ||
writer.remove( lastParagraph ); | ||
} ).to.not.throw(); | ||
|
||
assertOutput( | ||
'<paragraph>[Paragraph 1.]</paragraph>' + | ||
'<paragraph>Paragraph 2.</paragraph>' + | ||
'<paragraph>Paragraph 3.</paragraph>' + | ||
'<paragraph>[Paragraph 4.]</paragraph>' | ||
); | ||
} ); | ||
} ); | ||
|
||
it( 'does not throw an error when content after the selection is being removed (first element is not selected)', () => { | ||
setModelData( model, | ||
'<paragraph>Paragraph 1.</paragraph>' + | ||
'<paragraph>Paragraph 2.</paragraph>' + | ||
'<paragraph>[Paragraph 3.]</paragraph>' + | ||
'<paragraph>[Paragraph 4.]</paragraph>' + | ||
'<paragraph>Paragraph 5.</paragraph>' | ||
); | ||
|
||
model.change( writer => { | ||
const root = model.document.getRoot(); | ||
const lastParagraph = root.getNodeByPath( [ 4 ] ); | ||
|
||
expect( () => { | ||
writer.remove( lastParagraph ); | ||
} ).to.not.throw(); | ||
|
||
assertOutput( | ||
'<paragraph>Paragraph 1.</paragraph>' + | ||
'<paragraph>Paragraph 2.</paragraph>' + | ||
'<paragraph>[Paragraph 3.]</paragraph>' + | ||
'<paragraph>[Paragraph 4.]</paragraph>' | ||
); | ||
} ); | ||
} ); | ||
|
||
it( 'does not throw an error when content between the selection\'s ranges is being removed (last element is selected)', () => { | ||
setModelData( model, | ||
'<paragraph>Paragraph 1.</paragraph>' + | ||
'<paragraph>[Paragraph 2.]</paragraph>' + | ||
'<paragraph>Paragraph 3.</paragraph>' + | ||
'<paragraph>[Paragraph 4.]</paragraph>' | ||
); | ||
|
||
model.change( writer => { | ||
const root = model.document.getRoot(); | ||
const thirdParagraph = root.getNodeByPath( [ 2 ] ); | ||
|
||
expect( () => { | ||
writer.remove( thirdParagraph ); | ||
} ).to.not.throw(); | ||
|
||
assertOutput( | ||
'<paragraph>Paragraph 1.</paragraph>' + | ||
'<paragraph>[Paragraph 2.]</paragraph>' + | ||
'<paragraph>[Paragraph 4.]</paragraph>' | ||
); | ||
} ); | ||
} ); | ||
|
||
it( 'does not throw an error when content between the selection\'s ranges is being removed (last element is not selected)', () => { | ||
setModelData( model, | ||
'<paragraph>Paragraph 1.</paragraph>' + | ||
'<paragraph>[Paragraph 2.]</paragraph>' + | ||
'<paragraph>Paragraph 3.</paragraph>' + | ||
'<paragraph>[Paragraph 4.]</paragraph>' + | ||
'<paragraph>Paragraph 5.</paragraph>' | ||
); | ||
|
||
model.change( writer => { | ||
const root = model.document.getRoot(); | ||
const thirdParagraph = root.getNodeByPath( [ 2 ] ); | ||
|
||
expect( () => { | ||
writer.remove( thirdParagraph ); | ||
} ).to.not.throw(); | ||
|
||
assertOutput( | ||
'<paragraph>Paragraph 1.</paragraph>' + | ||
'<paragraph>[Paragraph 2.]</paragraph>' + | ||
'<paragraph>[Paragraph 4.]</paragraph>' + | ||
'<paragraph>Paragraph 5.</paragraph>' | ||
); | ||
} ); | ||
} ); | ||
|
||
function assertPositions( firstPosition, secondPosition ) { | ||
expect( firstPosition.isEqual( secondPosition ) ).to.be.true; | ||
} | ||
|
||
function assertOutput( output ) { | ||
expect( getModelData( model ) ).to.equal( output ); | ||
} | ||
} ); |