Tiago Temporin 7 роки тому
батько
коміт
1cdc5093bf
1 змінених файлів з 33 додано та 25 видалено
  1. 33 25
      packages/ckeditor5-utils/src/dom/scroll.js

+ 33 - 25
packages/ckeditor5-utils/src/dom/scroll.js

@@ -45,32 +45,36 @@ export function scrollViewportToShowTarget( { target, viewportOffset = 0 } ) {
 			firstAncestorToScroll = getParentElement( currentFrame );
 		}
 
-		// Scroll the target's ancestors first. Once done, scrolling the viewport is easy.
-		scrollAncestorsToShowRect( firstAncestorToScroll, () => {
-			// Note: If the target does not belong to the current window **directly**,
-			// i.e. it resides in an iframe belonging to the window, obtain the target's rect
-			// in the coordinates of the current window. By default, a Rect returns geometry
-			// relative to the current window's viewport. To make it work in a parent window,
-			// it must be shifted.
-			return getRectRelativeToWindow( target, currentWindow );
-		} );
-
-		// Obtain the rect of the target after it has been scrolled within its ancestors.
-		// It's time to scroll the viewport.
-		const targetRect = getRectRelativeToWindow( target, currentWindow );
-
-		scrollWindowToShowRect( currentWindow, targetRect, viewportOffset );
-
-		if ( currentWindow.parent != currentWindow ) {
-			// Keep the reference to the <iframe> element the "previous current window" was
-			// rendered within. It will be useful to re–calculate the rect of the target
-			// in the parent window's relative geometry. The target's rect must be shifted
-			// by it's iframe's position.
-			currentFrame = currentWindow.frameElement;
-			currentWindow = currentWindow.parent;
+		if (firstAncestorToScroll) {
+			// Scroll the target's ancestors first. Once done, scrolling the viewport is easy.
+			scrollAncestorsToShowRect(firstAncestorToScroll, () => {
+				// Note: If the target does not belong to the current window **directly**,
+				// i.e. it resides in an iframe belonging to the window, obtain the target's rect
+				// in the coordinates of the current window. By default, a Rect returns geometry
+				// relative to the current window's viewport. To make it work in a parent window,
+				// it must be shifted.
+				return getRectRelativeToWindow(target, currentWindow);
+			});
+
+			// Obtain the rect of the target after it has been scrolled within its ancestors.
+			// It's time to scroll the viewport.
+			const targetRect = getRectRelativeToWindow(target, currentWindow);
+
+			scrollWindowToShowRect(currentWindow, targetRect, viewportOffset);
+
+			if (currentWindow.parent != currentWindow) {
+				// Keep the reference to the <iframe> element the "previous current window" was
+				// rendered within. It will be useful to re–calculate the rect of the target
+				// in the parent window's relative geometry. The target's rect must be shifted
+				// by it's iframe's position.
+				currentFrame = currentWindow.frameElement;
+				currentWindow = currentWindow.parent;
+			} else {
+				currentWindow = null;
+			}
 		} else {
 			currentWindow = null;
-		}
+		}	
 	}
 }
 
@@ -249,7 +253,11 @@ function getWindow( elementOrRange ) {
 // @private
 // @param {HTMLElement|Range} firstRect
 // @returns {HTMLelement}
-function getParentElement( elementOrRange ) {
+function getParentElement(elementOrRange) {
+	if (!elementOrRange) {
+		return null;
+	}
+
 	if ( isRange( elementOrRange ) ) {
 		let parent = elementOrRange.commonAncestorContainer;