-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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 #16892 from ckeditor/ck/16806
Fix (editor-multi-root): No longer lose selection on clicking editable containing only one block element. Closes #16806
- Loading branch information
Showing
10 changed files
with
470 additions
and
51 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
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
44 changes: 44 additions & 0 deletions
44
packages/ckeditor5-utils/src/dom/getrangefrommouseevent.ts
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,44 @@ | ||
/** | ||
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved. | ||
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license | ||
*/ | ||
|
||
/** | ||
* @module utils/dom/getrangefrommouseevent | ||
*/ | ||
|
||
/** | ||
* Returns a DOM range from a given point specified by a mouse event. | ||
* | ||
* @param domEvent The mouse event. | ||
* @returns The DOM range. | ||
*/ | ||
export default function getRangeFromMouseEvent( | ||
domEvent: MouseEvent & { | ||
rangeParent?: HTMLElement; | ||
rangeOffset?: number; | ||
} | ||
): Range | null { | ||
if ( !domEvent.target ) { | ||
return null; | ||
} | ||
|
||
const domDoc = ( domEvent.target as HTMLElement ).ownerDocument; | ||
const x = domEvent.clientX; | ||
const y = domEvent.clientY; | ||
let domRange = null; | ||
|
||
// Webkit & Blink. | ||
if ( domDoc.caretRangeFromPoint && domDoc.caretRangeFromPoint( x, y ) ) { | ||
domRange = domDoc.caretRangeFromPoint( x, y ); | ||
} | ||
|
||
// FF. | ||
else if ( domEvent.rangeParent ) { | ||
domRange = domDoc.createRange(); | ||
domRange.setStart( domEvent.rangeParent, domEvent.rangeOffset! ); | ||
domRange.collapse( true ); | ||
} | ||
|
||
return domRange; | ||
} |
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
76 changes: 76 additions & 0 deletions
76
packages/ckeditor5-utils/tests/dom/getrangefrommouseevent.js
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,76 @@ | ||
/** | ||
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved. | ||
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license | ||
*/ | ||
|
||
import getRangeFromMouseEvent from '../../src/dom/getrangefrommouseevent.js'; | ||
|
||
describe( 'getRangeFromMouseEvent()', () => { | ||
it( 'should use Document#caretRangeFromPoint method to obtain range on Webkit & Blink', () => { | ||
const fakeRange = { | ||
startOffset: 0, | ||
endOffset: 0 | ||
}; | ||
|
||
const caretRangeFromPointSpy = sinon.stub().returns( fakeRange ); | ||
const evt = { | ||
clientX: 10, | ||
clientY: 11, | ||
target: { | ||
ownerDocument: { | ||
caretRangeFromPoint: caretRangeFromPointSpy | ||
} | ||
} | ||
}; | ||
|
||
expect( getRangeFromMouseEvent( evt ) ).to.be.equal( fakeRange ); | ||
expect( caretRangeFromPointSpy ).to.be.calledWith( 10, 11 ); | ||
} ); | ||
|
||
it( 'should use Document#createRange method to obtain range on Firefox', () => { | ||
const fakeRange = { | ||
startOffset: 0, | ||
endOffset: 0, | ||
setStart: sinon.stub(), | ||
collapse: sinon.stub() | ||
}; | ||
|
||
const evt = { | ||
clientX: 10, | ||
clientY: 11, | ||
rangeOffset: 13, | ||
rangeParent: { parent: true }, | ||
target: { | ||
ownerDocument: { | ||
createRange: sinon.stub().returns( fakeRange ) | ||
} | ||
} | ||
}; | ||
|
||
expect( getRangeFromMouseEvent( evt ) ).to.be.equal( fakeRange ); | ||
|
||
expect( fakeRange.collapse ).to.be.calledWith( true ); | ||
expect( fakeRange.setStart ).to.be.calledWith( evt.rangeParent, evt.rangeOffset ); | ||
} ); | ||
|
||
it( 'should return null if event target is null', () => { | ||
const evt = { | ||
target: null | ||
}; | ||
|
||
expect( getRangeFromMouseEvent( evt ) ).to.be.null; | ||
} ); | ||
|
||
it( 'should return null if event target is not null but it\'s not possible to create range on document', () => { | ||
const evt = { | ||
target: { | ||
ownerDocument: { | ||
createRange: null, | ||
caretRangeFromPoint: null | ||
} | ||
} | ||
}; | ||
|
||
expect( getRangeFromMouseEvent( evt ) ).to.be.null; | ||
} ); | ||
} ); |
Oops, something went wrong.