Skip to content

Commit

Permalink
account dor document.defaultView being null
Browse files Browse the repository at this point in the history
  • Loading branch information
KingSora committed Apr 12, 2024
1 parent 3399587 commit 3fc6823
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
Expand Up @@ -22,6 +22,7 @@ import {
stopPropagation,
isBodyElement,
getFocusedElement,
wnd,
} from '~/support';
import {
dataAttributeHost,
Expand Down Expand Up @@ -63,10 +64,10 @@ export interface StructureSetupElementsObj {
// ctx ----
_isTextarea: boolean;
_isBody: boolean;
_windowElm: Window;
_documentElm: Document;
_targetIsElm: boolean;
_viewportIsTarget: boolean;
_windowElm: () => Window;
_viewportHasClass: (viewportAttributeClassName: string) => boolean;
_viewportAddRemoveClass: (viewportAttributeClassName: string, add?: boolean) => void;
}
Expand Down Expand Up @@ -98,7 +99,7 @@ export const createStructureSetupElements = (
const isTextarea = is(targetElement, 'textarea');
const ownerDocument = targetElement.ownerDocument;
const docElement = ownerDocument.documentElement;
const docWnd = ownerDocument.defaultView as Window;
const getDocumentWindow = () => ownerDocument.defaultView || wnd;
const focusElm = (customActiveElm: Element | null) => {
if (customActiveElm && (customActiveElm as HTMLElement).focus) {
(customActiveElm as HTMLElement).focus();
Expand Down Expand Up @@ -151,12 +152,12 @@ export const createStructureSetupElements = (
_scrollOffsetElement: viewportIsTargetBody ? docElement : viewportElement,
_scrollEventElement: viewportIsTargetBody ? ownerDocument : viewportElement,
_originalScrollOffsetElement: isBody ? docElement : originalNonBodyScrollOffsetElement,
_windowElm: docWnd,
_documentElm: ownerDocument,
_isTextarea: isTextarea,
_isBody: isBody,
_targetIsElm: targetIsElm,
_viewportIsTarget: viewportIsTarget,
_windowElm: getDocumentWindow,
_viewportHasClass: (viewportAttributeClassName: string) =>
hasAttrClass(
viewportElement,
Expand Down Expand Up @@ -193,6 +194,7 @@ export const createStructureSetupElements = (
const contentSlot = viewportIsTargetBody ? _target : _content || _viewport;
const destroy = bind(runEachAndClear, destroyFns);
const appendElements = () => {
const docWnd = getDocumentWindow();
const initActiveElm = getFocusedElement();
const unwrap = (elm: HTMLElement | false | null | undefined) => {
appendChildren(parent(elm), contents(elm));
Expand Down Expand Up @@ -273,7 +275,9 @@ export const createStructureSetupElements = (

// focus viewport if previously focused element was target, otherwise focus previously focused element
focusElm(
!viewportIsTarget && docWnd.top === docWnd && initActiveElm === targetElement
!viewportIsTarget &&
initActiveElm === targetElement &&
(!docWnd || (docWnd && docWnd.top === docWnd))
? _viewport
: initActiveElm
);
Expand Down
Expand Up @@ -57,8 +57,8 @@ export const createOverflowUpdateSegment: CreateStructureUpdateSegment = (
_padding,
_viewport,
_viewportIsTarget,
_viewportAddRemoveClass,
_isBody,
_viewportAddRemoveClass,
_windowElm,
} = structureSetupElements;
const { _nativeScrollbarsHiding } = env;
Expand Down Expand Up @@ -177,7 +177,7 @@ export const createOverflowUpdateSegment: CreateStructureUpdateSegment = (
// arrangedViewportScrollSize = scrollSize(_viewport);
}

const windowInnerSize = windowSize(_windowElm);
const windowInnerSize = windowSize(_windowElm());
const overflowAmountScrollSize = {
w: max0(mathMax(viewportScrollSize.w, arrangedViewportScrollSize.w) + sizeFraction.w),
h: max0(mathMax(viewportScrollSize.h, arrangedViewportScrollSize.h) + sizeFraction.h),
Expand Down
Expand Up @@ -249,7 +249,7 @@ const assertCorrectSetupElements = (

expect(_isTextarea).toBe(isTextarea);
expect(_isBody).toBe(isBody);
expect(_windowElm).toBe(document.defaultView);
expect(_windowElm()).toBe(document.defaultView);
expect(_documentElm).toBe(document);

expect(typeof destroy).toBe('function');
Expand Down

0 comments on commit 3fc6823

Please sign in to comment.