Skip to content

fix: detect iframe DOM nodes#768

Open
PinkChampagne17 wants to merge 1 commit into
react-component:masterfrom
PinkChampagne17:fix-iframe-dom-nodes
Open

fix: detect iframe DOM nodes#768
PinkChampagne17 wants to merge 1 commit into
react-component:masterfrom
PinkChampagne17:fix-iframe-dom-nodes

Conversation

@PinkChampagne17
Copy link
Copy Markdown

@PinkChampagne17 PinkChampagne17 commented Jun 2, 2026

Fix #460.

Summary by CodeRabbit

发布说明

  • Bug Fixes

    • 改进了跨 iframe/portal 等跨文档场景下的元素识别和可见性判断,提升兼容性与可靠性,减少渲染与交互异常。
  • Tests

    • 新增跨 iframe 场景的单元测试,覆盖元素识别与可见性判定,增强对复杂 DOM 环境的验证和回归保护。

@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 2, 2026

@PinkChampagne17 is attempting to deploy a commit to the React Component Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 2, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7449e88a-59b8-42d4-83c6-8f5b16f4e496

📥 Commits

Reviewing files that changed from the base of the PR and between 826453f and 7750d4d.

📒 Files selected for processing (4)
  • src/Dom/findDOMNode.ts
  • src/Dom/isVisible.ts
  • tests/findDOMNode.test.tsx
  • tests/isVisible.test.ts
🚧 Files skipped from review as they are similar to previous changes (4)
  • src/Dom/findDOMNode.ts
  • tests/isVisible.test.ts
  • tests/findDOMNode.test.tsx
  • src/Dom/isVisible.ts

漫步

该 PR 修改 DOM 节点检测机制,将基于同源构造函数的 instanceof 检查替换为 nodeType === 1,并为 findDOMNodeisVisible 添加跨 iframe 的测试覆盖,验证跨文档节点的识别与可见性判断。

变更

iframe 跨文档 DOM 识别

Layer / File(s) Summary
findDOMNode 跨 iframe 检测实现
src/Dom/findDOMNode.ts, tests/findDOMNode.test.tsx
isDOM 函数改用 nodeType === 1 判定元素节点,替代 instanceof HTMLElement/SVGElementReact 导入改为类型导入;新增测试用例验证 iframe 内 DOM 节点被正确识别并由 getDOM/findDOMNode 返回。
isVisible 节点检测一致性更新
src/Dom/isVisible.ts, tests/isVisible.test.ts
isVisible 的前置元素检查由 instanceof Element 改为 nodeType === 1,并新增跨 iframe 场景测试(包含对 getBoundingClientRect 的 mock),验证可见性判断在跨文档元素上的行为。
sequenceDiagram
  participant Test
  participant findDOMNode
  participant isDOM
  participant isVisible
  participant iframeElem as iframe 内元素

  Test->>iframeElem: 在 iframe 中创建 div 并发起测试调用
  Test->>findDOMNode: 调用 findDOMNode/getDOM with node
  findDOMNode->>isDOM: 调用 isDOM(node)
  isDOM->>iframeElem: 检查 nodeType === 1
  iframeElem-->>isDOM: true
  isDOM-->>findDOMNode: true
  findDOMNode-->>Test: 返回 iframe 内 div

  Test->>isVisible: 调用 isVisible(node)
  isVisible->>iframeElem: 检查 nodeType === 1
  isVisible->>iframeElem: 调用 getBoundingClientRect(被 mock)
  iframeElem-->>isVisible: 返回尺寸数据
  isVisible-->>Test: 返回 true
Loading

预估代码审查工作量

🎯 2 (Simple) | ⏱️ ~12 分钟

可能相关的 PR

建议的审查者

  • zombieJ

🐰 跨帧的叶子轻轻摇,
instanceof 走散天涯遥;
nodeType 一数真分明,
iframe 内外皆招呼,
小兔鼓掌乐逍遥!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed Pull request title clearly and concisely summarizes the main change: fixing iframe DOM node detection, which aligns with the changeset modifications.
Linked Issues check ✅ Passed The PR successfully addresses issue #460 by replacing instanceof checks with nodeType === 1 for cross-frame safe DOM detection, with comprehensive test coverage for iframe scenarios.
Out of Scope Changes check ✅ Passed All code changes are directly related to fixing iframe DOM detection and include appropriate test coverage; no out-of-scope modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

src/Dom/findDOMNode.ts

ESLint skipped: missing config or dependency (missing-dependency). The ESLint configuration references a package that is not available in the sandbox.

src/Dom/isVisible.ts

ESLint skipped: the ESLint configuration for this file references a package that is not available in the sandbox.

tests/findDOMNode.test.tsx

ESLint skipped: the ESLint configuration for this file references a package that is not available in the sandbox.

  • 1 others

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the DOM element checks in findDOMNode and isVisible to use nodeType === 1 instead of instanceof checks, ensuring cross-frame compatibility (e.g., with iframes). Corresponding unit tests have been added to verify this behavior. A review comment suggests making the isDOM check more robust by explicitly verifying that the node is an object before checking its nodeType property.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread src/Dom/findDOMNode.ts Outdated
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/Dom/isVisible.ts (1)

6-24: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

加强 isVisible 的运行时输入校验,避免 nodeType 伪对象误判

src/Dom/isVisible.ts 入口目前只用 element.nodeType === 1 来放行后续的 offsetParent / getBBox / getBoundingClientRect 判断;如果运行时传入的不是单纯的真实 Element(哪怕具备 nodeType: 1 并挂了同名方法/字段),就可能被误判为可见。建议在入口加入更稳的“真实 Element 特征”校验(例如 nodeName 等),与 src/Dom/findDOMNode.ts 里“跨 frame 不能用 instanceof”的做法保持一致但仍收紧运行时契约。

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/Dom/isVisible.ts` around lines 6 - 24, The isVisible function currently
trusts element.nodeType === 1 alone, which allows pseudo-objects with the same
property names to slip through; tighten the runtime check before using
offsetParent/getBBox/getBoundingClientRect by ensuring the value is a real DOM
element — e.g., verify typeof element === 'object' && element !== null &&
element.nodeType === 1 && typeof element.nodeName === 'string' and/or presence
of ownerDocument/documentElement properties (mirroring the safer,
frame-crossing-safe checks used in findDOMNode.ts) so only genuine Elements
reach the offsetParent/getBBox/getBoundingClientRect branches; update isVisible
to perform this gate check and early-return false for invalid inputs.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/Dom/findDOMNode.ts`:
- Around line 3-7: isDOM currently only checks nodeType===1 and can misclassify
plain objects like {nodeType:1}; update isDOM to more robustly detect real DOM
elements by ensuring node is a non-null object, nodeType===1, and at least one
real DOM property exists (e.g. typeof node.nodeName === "string" and typeof
(node as any).ownerDocument === "object") or when available use the safe
cross-frame pattern (try checking typeof Element !== "undefined" && node
instanceof Element). Adjust the type predicate to a narrower Element (instead of
HTMLElement|SVGElement) so getDOM and findDOMNode return a true DOM Element and
callers won't be handed plain objects; update getDOM/findDOMNode uses
accordingly.

---

Outside diff comments:
In `@src/Dom/isVisible.ts`:
- Around line 6-24: The isVisible function currently trusts element.nodeType ===
1 alone, which allows pseudo-objects with the same property names to slip
through; tighten the runtime check before using
offsetParent/getBBox/getBoundingClientRect by ensuring the value is a real DOM
element — e.g., verify typeof element === 'object' && element !== null &&
element.nodeType === 1 && typeof element.nodeName === 'string' and/or presence
of ownerDocument/documentElement properties (mirroring the safer,
frame-crossing-safe checks used in findDOMNode.ts) so only genuine Elements
reach the offsetParent/getBBox/getBoundingClientRect branches; update isVisible
to perform this gate check and early-return false for invalid inputs.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 400f1c01-9c70-4ec5-9f3b-aadbcf18ceb4

📥 Commits

Reviewing files that changed from the base of the PR and between b7731c3 and 826453f.

📒 Files selected for processing (4)
  • src/Dom/findDOMNode.ts
  • src/Dom/isVisible.ts
  • tests/findDOMNode.test.tsx
  • tests/isVisible.test.ts

Comment thread src/Dom/findDOMNode.ts Outdated
Co-authored-by: Codex <codex@openai.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Function isDom not working inside iframe

1 participant