Skip to content

Commit

Permalink
fix type error
Browse files Browse the repository at this point in the history
  • Loading branch information
canalun committed Mar 4, 2024
1 parent e41dd47 commit 8267de0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
9 changes: 6 additions & 3 deletions src/game/object/blocks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { assert } from "~game/utils/dom"

import { getBlockElements } from "./getBlockElements"

// A block is "remained" until the ball hits it,
Expand Down Expand Up @@ -61,8 +63,9 @@ function addFrameOffsetToRect(
right: number
}
) {
let currentOwnerDocument = element.ownerDocument
while (currentOwnerDocument !== window.top.document) {
let currentOwnerDocument: Document | null = element.ownerDocument
assert(!!window.top, "window.top not found")
while (currentOwnerDocument && currentOwnerDocument !== window.top.document) {
const _srcFrameRect =
currentOwnerDocument.defaultView?.frameElement?.getBoundingClientRect()
if (!_srcFrameRect) {
Expand All @@ -75,7 +78,7 @@ function addFrameOffsetToRect(
rect.right += _srcFrameRect.left

currentOwnerDocument =
currentOwnerDocument.defaultView?.frameElement?.ownerDocument
currentOwnerDocument.defaultView?.frameElement?.ownerDocument || null
}
return rect
}
31 changes: 17 additions & 14 deletions src/game/object/getBlockElements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,22 @@ function collectBlockElements(
if (element.shadowRoot) {
collectBlockElements(element.shadowRoot, canBeBlock, blockElements)
}
if (isFrameElement(element)) {
if (isPenetrableFrame(element)) {
element.contentWindow.document.readyState === "complete"
? collectBlockElements(
element.contentDocument,
canBeBlock,
blockElements
)
: element.addEventListener("load", () => {
if (isFrameElement(element) && isPenetrableFrame(element)) {
element.contentWindow &&
element.contentWindow.document.readyState === "complete"
? collectBlockElements(
element.contentWindow.document,
canBeBlock,
blockElements
)
: element.addEventListener("load", () => {
element.contentWindow &&
collectBlockElements(
element.contentDocument,
element.contentWindow.document,
canBeBlock,
blockElements
)
})
}
})
}
}
}
Expand Down Expand Up @@ -171,7 +171,10 @@ function isVisible(element: Element): boolean {
function hasNoTextNode(element: Element): boolean {
const childNodes = Array.from(element.childNodes)
const result = !childNodes.some(
(node) => node.nodeType === Node.TEXT_NODE && node.textContent.trim()
(node) =>
node.nodeType === Node.TEXT_NODE &&
node.textContent &&
node.textContent.trim()
)
result &&
process.env.NODE_ENV === "development" &&
Expand Down Expand Up @@ -329,7 +332,7 @@ function hasVisibleChildNodes(element: Element): boolean {
case Node.ELEMENT_NODE:
return isVisibleWithCache(node as Element)
case Node.TEXT_NODE:
return node.textContent.trim() !== ""
return node.textContent && node.textContent.trim() !== ""
default:
return false
}
Expand Down
6 changes: 4 additions & 2 deletions src/game/start/freezePage.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { veilZIndex } from "../configuration/settings"
import { isFrameElement, isPenetrableFrame } from "../utils/dom"
import { assert, isFrameElement, isPenetrableFrame } from "../utils/dom"

export function freezePage() {
setVeil()
clearTimeoutAll()
assert(!!window.top, "window.top is not available")
preventScroll(window.top)
}

Expand Down Expand Up @@ -47,12 +48,13 @@ function preventScroll(window: Window) {

Array.from(window.document.querySelectorAll("iframe, frame")).forEach(
(frame) => {
isFrameElement(frame) && isPenetrableFrame(frame)
isFrameElement(frame) && isPenetrableFrame(frame) && frame.contentWindow
? preventScroll(frame.contentWindow)
: frame.addEventListener("load", () => {
// fire preventScroll when iframe is reloaded
isFrameElement(frame) &&
isPenetrableFrame(frame) &&
frame.contentWindow &&
preventScroll(frame.contentWindow)
})
}
Expand Down
1 change: 1 addition & 0 deletions src/game/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function isPenetrableFrame(
frame: HTMLIFrameElement | HTMLFrameElement
): boolean {
try {
// @ts-ignore: intentional access to frame's document
frame.contentWindow.document
} catch (e) {
return false
Expand Down

0 comments on commit 8267de0

Please sign in to comment.