From 5a42e99e880fffc893ad704caf212339b7e6dfdf Mon Sep 17 00:00:00 2001 From: Aleksander Nowodzinski Date: Tue, 23 May 2017 11:46:16 +0200 Subject: [PATCH] Fix: FocusTracker should remain in sync when multiple 'blur' events are followed by the 'focus'. Closes #159. --- src/focustracker.js | 2 ++ tests/focustracker.js | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/focustracker.js b/src/focustracker.js index 108c5c8..d9481b8 100644 --- a/src/focustracker.js +++ b/src/focustracker.js @@ -115,6 +115,8 @@ export default class FocusTracker { * @fires blur */ _blur() { + clearTimeout( this._nextEventLoopTimeout ); + this._nextEventLoopTimeout = setTimeout( () => { this.focusedElement = null; this.isFocused = false; diff --git a/tests/focustracker.js b/tests/focustracker.js index d877065..8fcfbdc 100644 --- a/tests/focustracker.js +++ b/tests/focustracker.js @@ -115,6 +115,19 @@ describe( 'FocusTracker', () => { expect( focusTracker.isFocused ).to.true; expect( changeSpy.notCalled ).to.true; } ); + + // https://github.com/ckeditor/ckeditor5-utils/issues/159 + it( 'should keep `isFocused` synced when multiple blur events are followed by the focus', () => { + focusTracker.add( container ); + focusTracker.isFocused = true; + + container.dispatchEvent( new Event( 'blur' ) ); + containerFirstInput.dispatchEvent( new Event( 'blur' ) ); + containerSecondInput.dispatchEvent( new Event( 'focus' ) ); + testUtils.sinon.clock.tick( 0 ); + + expect( focusTracker.isFocused ).to.be.true; + } ); } ); } );