From 0342eba67550e904bbdbe545b1341d458d7c05df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotrek=20Koszuli=C5=84ski?= Date: Fri, 26 Oct 2018 14:00:14 +0200 Subject: [PATCH] Minor refactoring. --- src/view/renderer.js | 49 ++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/src/view/renderer.js b/src/view/renderer.js index 31c55ec9d..934ed80a0 100644 --- a/src/view/renderer.js +++ b/src/view/renderer.js @@ -756,29 +756,9 @@ export default class Renderer { domSelection.collapse( anchor.parent, anchor.offset ); domSelection.extend( focus.parent, focus.offset ); - // The following is a Firefox–specific hack (https://github.com/ckeditor/ckeditor5-engine/issues/1439). - // When the native DOM selection is at the end of the block and preceded by
e.g. - // - //

foo
[]

- // - // which happens a lot when using the soft line break, the browser fails to (visually) move the - // caret to the new line. A quick fix is as simple as force–refreshing the selection with the same range. + // Firefox–specific hack (https://github.com/ckeditor/ckeditor5-engine/issues/1439). if ( env.isGecko ) { - const parent = focus.parent; - - // This fix works only when the focus point is at the very end of an element. - // There is no point in running it in cases unrelated to the browser bug. - if ( parent.nodeType != Node.ELEMENT_NODE || focus.offset != parent.childNodes.length - 1 ) { - return; - } - - const childAtOffset = parent.childNodes[ focus.offset ]; - - // To stay on the safe side, the fix being as specific as possible, it targets only the - // selection which is at the very end of the element and preceded by
. - if ( childAtOffset && childAtOffset.tagName == 'BR' ) { - domSelection.addRange( domSelection.getRangeAt( 0 ) ); - } + fixGeckoSelectionAfterBr( focus, domSelection ); } } @@ -951,3 +931,28 @@ function sameNodes( blockFiller, actualDomChild, expectedDomChild ) { // Not matching types. return false; } + +// The following is a Firefox–specific hack (https://github.com/ckeditor/ckeditor5-engine/issues/1439). +// When the native DOM selection is at the end of the block and preceded by
e.g. +// +//

foo
[]

+// +// which happens a lot when using the soft line break, the browser fails to (visually) move the +// caret to the new line. A quick fix is as simple as force–refreshing the selection with the same range. +function fixGeckoSelectionAfterBr( focus, domSelection ) { + const parent = focus.parent; + + // This fix works only when the focus point is at the very end of an element. + // There is no point in running it in cases unrelated to the browser bug. + if ( parent.nodeType != Node.ELEMENT_NODE || focus.offset != parent.childNodes.length - 1 ) { + return; + } + + const childAtOffset = parent.childNodes[ focus.offset ]; + + // To stay on the safe side, the fix being as specific as possible, it targets only the + // selection which is at the very end of the element and preceded by
. + if ( childAtOffset && childAtOffset.tagName == 'BR' ) { + domSelection.addRange( domSelection.getRangeAt( 0 ) ); + } +}