-
Notifications
You must be signed in to change notification settings - Fork 40
T/629b Alternative fix infinite selection loop. #671
Changes from 17 commits
f4c0d25
b9d6bc2
1b39bfb
d2d1e73
a656829
d80b367
317d3dc
f220109
a83c2ab
6b6da0c
3768f04
3dc704c
a8e53ca
dcf65b8
0de2bf7
25b8ba2
a3690d5
b1e0e1f
adcdbdc
6a2418b
7ccaa13
bf9fc9f
bf87840
6a9306e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -283,33 +283,39 @@ export default class Selection { | |
} | ||
|
||
/** | ||
* Checks whether, this selection is equal to given selection. Selections equal if they have the same ranges and directions. | ||
* Checks whether, this selection is equal to given selection. Selections are equal if they have same directions, | ||
* same number of ranges and all ranges from one selection equal to a range from other selection. | ||
* | ||
* @param {engine.view.Selection} otherSelection Selection to compare with. | ||
* @returns {Boolean} `true` if selections are equal, `false` otherwise. | ||
*/ | ||
isEqual( otherSelection ) { | ||
const rangeCount = this.rangeCount; | ||
|
||
if ( rangeCount != otherSelection.rangeCount ) { | ||
if ( this.isFake != otherSelection.isFake ) { | ||
return false; | ||
} | ||
|
||
if ( this.isFake != otherSelection.isFake ) { | ||
if ( this.isFake && this.fakeSelectionLabel != otherSelection.fakeSelectionLabel ) { | ||
return false; | ||
} | ||
|
||
if ( this.isFake && this.fakeSelectionLabel != otherSelection.fakeSelectionLabel ) { | ||
if ( this.rangeCount != otherSelection.rangeCount ) { | ||
return false; | ||
} else if ( this.rangeCount === 0 ) { | ||
return true; | ||
} | ||
|
||
for ( let i = 0; i < this.rangeCount; i++ ) { | ||
if ( !this._ranges[ i ].isEqual( otherSelection._ranges[ i ] ) ) { | ||
return false; | ||
} | ||
if ( !this.anchor.isEqual( otherSelection.anchor ) || !this.focus.isEqual( otherSelection.focus ) ) { | ||
return false; | ||
} | ||
|
||
return this._lastRangeBackward === otherSelection._lastRangeBackward; | ||
// Every range from this selection... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do I see correctly that the ranges can be in a different order? Why so? Then, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Backward/forward selection check is indirectly implemented when we check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was rather asking why is this method trying to find a range in the other selection which matches range from this selection... for each range in this selection? Moreover, this implementation is very ineffective, cause it creates new array on every call of Therefore, I've been asking about the order of ranges (whether it shouldn't be the same in both selections) because the easiest possible implementation is to loop through both selections' ranges and compare the pairs. Not only this will be much faster, but also cleaner. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I see. Ranges are not sorted upon insertion. This means that we have to check whether every range in selection A has a matching range in selection B the way we do. If the ranges are ordered that would be easier. It is a matter of us deciding whether the order range is important. Being strict, I think that we should not care about the ranges order, because from "outside" selection: I can change the implementation but... To be honest selection will usually have just one range, except of tables where it may be more but in 99% scenarios it will will be less than 10-20 ranges. I agree with changing so the arrays are not created. If you want to change "order logic" I'm fine with that as well, just confirm please whether we are doing it or leaving it as is.
So, instead one operation we have three? And it is me who get complaints about premature optimization? :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
BTW. I don't know if you haven't got too confused... For each range in selection A, we check if there is a matching range in selection B. The only way we can "speed" this up is if we already have sorted arrays... (or care about the order). |
||
return Array.from( this.getRanges() ).every( ( rangeA ) => { | ||
// ... has a range in other selection... | ||
return Array.from( otherSelection.getRanges() ).some( ( rangeB ) => { | ||
// ... which it is equal to. | ||
return rangeA.isEqual( rangeB ); | ||
} ); | ||
} ); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,18 +28,16 @@ import { getData as getViewData } from 'ckeditor5/engine/dev-utils/view.js'; | |
|
||
describe( 'EditingController', () => { | ||
describe( 'constructor()', () => { | ||
let model, editing; | ||
|
||
beforeEach( () => { | ||
model = new ModelDocument(); | ||
editing = new EditingController( model ); | ||
} ); | ||
|
||
it( 'should create controller with properties', () => { | ||
const model = new ModelDocument(); | ||
const editing = new EditingController( model ); | ||
|
||
expect( editing ).to.have.property( 'model' ).that.equals( model ); | ||
expect( editing ).to.have.property( 'view' ).that.is.instanceof( ViewDocument ); | ||
expect( editing ).to.have.property( 'mapper' ).that.is.instanceof( Mapper ); | ||
expect( editing ).to.have.property( 'modelToView' ).that.is.instanceof( ModelConversionDispatcher ); | ||
|
||
editing.destroy(); | ||
} ); | ||
} ); | ||
|
||
|
@@ -54,6 +52,10 @@ describe( 'EditingController', () => { | |
editing = new EditingController( model ); | ||
} ); | ||
|
||
afterEach( () => { | ||
editing.destroy(); | ||
} ); | ||
|
||
it( 'should create root', () => { | ||
const domRoot = createElement( document, 'div', null, createElement( document, 'p' ) ); | ||
|
||
|
@@ -128,6 +130,7 @@ describe( 'EditingController', () => { | |
after( () => { | ||
document.body.removeChild( domRoot ); | ||
listener.stopListening(); | ||
editing.destroy(); | ||
} ); | ||
|
||
beforeEach( () => { | ||
|
@@ -274,6 +277,8 @@ describe( 'EditingController', () => { | |
} ); | ||
|
||
expect( spy.called ).to.be.false; | ||
|
||
editing.destroy(); | ||
} ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I miss a test that destroy() destroys the view. |
||
} ); | ||
} ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deserves some comment that you empirically tested that making more than 50 selection changes in 2s is not possible. BTW. What about making selections by dragging over the text?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The selection has to be same as previous or pre-previous selection, so you would have to drag precisly, which means that it would be probably impossible to do 50 changes in 2 secs.