Skip to content

fix(swipe): guard document.addEventListener in Taro env - #3514

Open
xiaoyatong wants to merge 2 commits into
feat_v3.x_cppfrom
fix/swipe-taro-document-guard
Open

fix(swipe): guard document.addEventListener in Taro env#3514
xiaoyatong wants to merge 2 commits into
feat_v3.x_cppfrom
fix/swipe-taro-document-guard

Conversation

@xiaoyatong

@xiaoyatong xiaoyatong commented Aug 26, 2026

Copy link
Copy Markdown
Collaborator

在 Taro Android/iOS 运行时 document.addEventListener 可能不是函数, 在调用前增加环境检测,避免抛出 not a function 错误。

🤔 这个变动的性质是?

  • 新特性提交
  • 日常 bug 修复
  • 站点、文档改进
  • 演示代码改进
  • 组件样式/交互改进
  • TypeScript 定义更新
  • 包体积优化
  • 性能优化
  • 功能增强
  • 国际化改进
  • 重构
  • 代码风格优化
  • 测试用例
  • 分支合并
  • 其他改动(是关于什么的改动?)

🔗 相关 Issue

💡 需求背景和解决方案

☑️ 请求合并前的自查清单

⚠️ 请自检并全部勾选全部选项⚠️

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • TypeScript 定义已补充或无须补充
  • fork仓库代码是否为最新避免文件冲突
  • Files changed 没有 package.json lock 等无关文件

Summary by CodeRabbit

  • Bug 修复
    • 优化滑动、弹幕、图片预览和气泡提示在非浏览器环境下的兼容性。
    • 在缺少文档操作或事件监听能力时,自动跳过相关操作,避免运行时错误。
    • 提升相关组件在服务端渲染及其他非浏览器场景中的稳定性。

在 Taro Android/iOS 运行时 document.addEventListener 可能不是函数,
在调用前增加环境检测,避免抛出 not a function 错误。

Co-Authored-By: Claude <noreply@anthropic.com>
@github-actions github-actions Bot added the action:review This PR needs more reviews (less than 2 approvals) label Aug 26, 2026
@coderabbitai

coderabbitai Bot commented Aug 26, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: be167978-63df-4ae4-bf56-63588e344c7c

📥 Commits

Reviewing files that changed from the base of the PR and between 39584b9 and 12a451d.

📒 Files selected for processing (3)
  • src/packages/barrage/barrage.taro.tsx
  • src/packages/imagepreview/imagepreview.taro.tsx
  • src/packages/popover/popover.taro.tsx

Included review availability: Your plan provides up to 4 included reviews per hour; 2 remain after this review.


Walkthrough

Swipe、Barrage、ImagePreview 和 Popover 组件在访问 document API 前增加环境检查。非浏览器环境会跳过事件注册、停止 DOM 创建流程,或使用 null 继续位置计算。

Changes

Taro DOM 环境保护

Layer / File(s) Summary
Document API 检查
src/packages/swipe/swipe.taro.tsx, src/packages/barrage/barrage.taro.tsx, src/packages/imagepreview/imagepreview.taro.tsx, src/packages/popover/popover.taro.tsx
SwipeImagePreview 在注册事件前检查 document.addEventListenerBarrage 在创建元素前检查 document.createElementPopover 在查询目标元素前检查 document

Estimated code review effort: 2 (简单) | ~10 分钟

Merge Risk: ⚪ Minimal · up to 12a45

The PR adds an environment guard before document.addEventListener in Taro runtimes to prevent a not-a-function error. No actionable merge-blocking risk remains after normal checks and review.

Poem

小兔检查 document,
发现 API 是否可用。
事件监听安全注册,
DOM 不可用时及时返回,
四个组件安静运行。

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning 描述说明了问题和总体解决方案,但模板中的变更性质未勾选,相关 Issue、需求背景和解决方案部分为空,自查清单也未完成。 请勾选“日常 bug 修复”或“其他改动”,补充相关 Issue 信息;在“需求背景和解决方案”中说明受影响的组件及环境检测方式;完成合并前自查清单。
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed 标题准确描述了 swipe 中为 Taro 环境增加 document.addEventListener 守卫的变更,但未覆盖 barrageimagepreviewpopover 的相关修复。该标题仍与主要变更直接相关。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 4…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 4 files.

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/swipe-taro-document-guard

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.

imagepreview/barrage/popover 在 Taro Android/iOS 运行时直接访问
document API 会抛出 not a function,补充 typeof 守卫与 swipe 保持一致。

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action:review This PR needs more reviews (less than 2 approvals) size/S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant