Skip to content

fix(runtime): 修复逻辑索引未命中时事件目标信息丢失的问题 - #19528

Merged
Single-Dancer merged 1 commit into
NervJS:mainfrom
sep2:fix/runtime-event-target-fallback
Oct 16, 2026
Merged

Single-Dancer merged 1 commit into
NervJS:mainfrom
sep2:fix/runtime-event-target-fallback

Conversation

@sep2

@sep2 sep2 commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

这个 PR 做了什么?

修复 TaroEvent.target / currentTarget 在逻辑节点索引未命中时的数据丢失和目标混淆。

问题与复现

事件派发时接收节点可以正常命中索引,但处理器在首次读取事件目标前移除节点,会清除对应索引。此时原有 currentTarget getter 因为 element === null 直接返回 this.target,把接收事件的父节点错误替换成触发事件的子节点;target getter 也会用空对象替换平台仍然提供的 dataset。

下面的运行时复现使用普通的单参数 eventHandler(event):

import { document, eventHandler } from '@tarojs/runtime'

const holder = document.createElement('view')
const parent = document.createElement('view')
const child = document.createElement('view')
parent.id = 'parent'
child.id = 'child'
holder.appendChild(parent)
parent.appendChild(child)

parent.addEventListener('tap', event => {
  holder.removeChild(parent)
  console.log(event.currentTarget.id) // 期望 parent,修复前为 child
}, false)

eventHandler({
  type: 'tap',
  target: { id: child.id, dataset: { sid: child.sid } },
  currentTarget: { id: parent.id, dataset: { sid: parent.sid } },
  detail: {}
})

修改

  • 索引命中时继续使用逻辑 dataset,保留对象值身份以及支付宝 dataset / targetDataset 的合并优先级。
  • 索引未命中时,分别使用对应的平台 dataset 作为兜底。
  • 只有已知逻辑节点相同,或未命中时原始目标对象相同,才复用 target 包装对象;空 ID、相同的未解析 ID 或两个空查询结果不作为身份相同的依据。
  • 保留手动构造事件的行为及现有惰性缓存。

目标 dataset 继续使用已有的浅复制/合并流程,成本随来源字段数变化;未命中时现在复制实际平台数据,而不是空对象。索引查询次数保持不变。兜底只能保留平台事件已经携带的信息。

测试

新增 14 项运行时回归,覆盖处理器内移除节点、空/未解析 ID 与 sid、原始对象及逻辑节点身份、数据优先级、支付宝混合命中情况、缺省 dataset 和缓存查询次数。13 项在修复前失败,修复后全部通过。

本地验证:

  • pnpm --filter @tarojs/shared build
  • pnpm --filter @tarojs/runtime build
  • pnpm --filter @tarojs/runtime test:ci:12 个测试文件通过,148 项通过、1 项既有跳过。
  • pnpm --filter @tarojs/runtime exec tsc --noEmit
  • pnpm exec eslint packages/taro-runtime/src/dom/event.ts packages/taro-runtime/tests/event-target.spec.ts
  • 额外 TypeScript 检查覆盖新增测试文件。

验证环境:macOS arm64、Node.js 26.5.0、pnpm 10.0.0。以上为运行时及构建验证;设备行为尚待验证。

这个 PR 是什么类型?

  • 错误修复 (Bugfix)
  • 新功能 (Feature)
  • 代码重构 (Refactor)
  • TypeScript 类型定义修改 (Types)
  • 文档修改 (Docs)
  • 代码风格更新 (Code style update)
  • 构建优化 (Chore)
  • 其他,请描述 (Other, please describe)

这个 PR 涉及以下平台:

  • 所有小程序(使用 TaroEvent 的共享事件路径)

Summary by CodeRabbit

  • Bug 修复
    • 修正事件目标与当前目标的数据集回退和合并行为,确保节点被移除或目标信息缺失时,事件数据仍能正确保留。
    • 修正无法解析事件目标时对目标节点与接收节点的区分处理。

@coderabbitai

coderabbitai Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a757aaec-7769-4013-9b37-fd9a15461528
📥 Commits

Reviewing files that changed from the base of the PR and between a45e835 and 19df00a.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 26df7c94-81bd-4850-ac14-0b70dee90549
📥 Commits

Reviewing files that changed from the base of the PR and between c48bec6 and a45e835.

📒 Files selected for processing: 2
  • packages/taro-runtime/src/dom/event.ts
  • packages/taro-runtime/tests/event-target.spec.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Walkthrough

调整 TaroEvent.target 和 TaroEvent.currentTarget 的 dataset 回退逻辑及对象复用判断。新增测试覆盖节点移除、数据合并、无法解析的节点和惰性缓存。

Changes

事件目标处理

层 / 文件 摘要
目标数据回退与验证
packages/taro-runtime/src/dom/event.ts, packages/taro-runtime/tests/event-target.spec.ts
调整 target 与 currentTarget 的 dataset 数据来源及对象复用条件。新增测试覆盖节点移除、索引节点数据优先级、原生数据回退、包装对象共享和惰性缓存。

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to a45e8

No merge-blocking issue was identified. The existing CI build supplies the test prerequisite; device behavior remains unverified.

Pre-merge checks: Passed 4 · Failed 1

Failed 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%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
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.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 标题准确概括了逻辑索引未命中时保留事件目标信息这一主要变更,与代码修改、测试和 PR 目标一致。
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

我是兔子,蹦到事件旁,
看见 target 留住数据行。
currentTarget 分清节点样,
索引数据与原生数据各有担当。
缓存安静,不多查一趟,
我竖起耳朵,为测试鼓掌。

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

@sep2 sep2 changed the title fix(runtime): preserve event targets when logical indexes miss fix(runtime): 修复逻辑索引未命中时事件目标信息丢失的问题 Oct 15, 2026
sep2 added a commit to sep2/vite-plugin-taro that referenced this pull request Oct 15, 2026
Preserve platform target data when the logical event index is unavailable, while retaining indexed datasets, Alipay merge order and lazy caches. Cover the behavior across WX, ZFB and TT template runtimes.

Upstream: NervJS/taro#19528

Retire 05-event-target-data after upgrading the pinned @tarojs/runtime to a release containing the upstream fix; retain the regression tests.
@Single-Dancer Single-Dancer added this to the 4.3.1 milestone Oct 16, 2026
@Single-Dancer
Single-Dancer force-pushed the fix/runtime-event-target-fallback branch from a45e835 to 19df00a Compare October 16, 2026 07:29
@codecov

codecov Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 55.35%. Comparing base (91cff98) to head (19df00a).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #19528      +/-   ##
==========================================
+ Coverage   55.32%   55.35%   +0.02%     
==========================================
  Files         467      467              
  Lines       25650    25650              
  Branches     6428     6421       -7     
==========================================
+ Hits        14192    14199       +7     
- Misses       9463     9484      +21     
+ Partials     1995     1967      -28     
Flag Coverage Δ
taro-cli 60.27% <ø> (ø)
taro-runtime 58.57% <100.00%> (+0.21%) ⬆️
taro-web 57.39% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
packages/taro-runtime/src/dom/event.ts 92.07% <100.00%> (+0.99%) ⬆️

... and 10 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Single-Dancer
Single-Dancer merged commit 1cf1bbf into NervJS:main Oct 16, 2026
25 checks passed
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.

2 participants