Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/taro-runtime/src/dom/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ export class TaroElement extends TaroNode {
return
}
delete this.props[qualifiedName]
if (qualifiedName.startsWith('data-')) {
delete this.dataset[toCamelCase(qualifiedName.replace(/^data-/, ''))]
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

这里还有个边界情况:目前 data-user-id 和 data-user--id 都会映射到 dataset.userId,直接删除这个键会把另一个仍存在的属性值一起清掉。

node.setAttribute('data-user-id', 'old')
node.setAttribute('data-user--id', 'keep')
node.removeAttribute('data-user-id')
// data-user--id 还在,但 dataset.userId 变成了 undefined

这个场景在 base 中会保留 'keep',本次改动后会丢失,事件回调里的 dataset 也会受影响。建议处理一下仍存在的同名映射,并补个回归用例。

@Jarvis636431 Jarvis636431 Oct 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

感谢指出,已在 2f84963 修复并推送。

现在删除 data-* 属性后,会从剩余属性中查找映射到同一 dataset 键的属性:存在则恢复其值,不存在才删除该键。这样既保留你示例中的 'keep',也覆盖删除最后写入的属性时恢复另一个属性值的情况。

新增 4 个回归用例,覆盖两种属性写入顺序下的两种删除顺序、删除最后一个映射时清理键,以及事件 target.dataset / currentTarget.dataset(同一或不同节点)的结果。新增用例在修复前全部失败,修复后 data-attributes 的 14 个用例全部通过;runtime 全量 test:ci(--maxWorkers=2)、修改文件 ESLint、git diff --check 均通过。

辛苦再看下,谢谢。

}
}

// Serialization
Expand Down
112 changes: 112 additions & 0 deletions packages/taro-runtime/tests/data-attributes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { hooks } from '@tarojs/shared'
import { describe, expect, test, vi } from 'vitest'

import { createEvent, document, MutationObserver } from '../src/index'

describe('removing data attributes', () => {
test.each([
['data-test', 'test'],
['data-user-id', 'userId'],
['data-order-item-id', 'orderItemId']
])('removes %s from attributes and dataset', (attribute, key) => {
const node = document.createElement('view')
node.setAttribute(attribute, 'old')
node.setAttribute('data-keep', 'retained')

node.removeAttribute(attribute)

expect(node.hasAttribute(attribute)).toBe(false)
expect(Object.prototype.hasOwnProperty.call(node.dataset, key)).toBe(false)
expect(node.dataset).toEqual({ keep: 'retained' })
})

test('supports repeated removal and setting the attribute again', () => {
const node = document.createElement('view')
node.setAttribute('data-user-id', 'old')
node.removeAttribute('data-user-id')
node.removeAttribute('data-user-id')
expect(node.dataset).toEqual({})

node.setAttribute('data-user-id', 'new')
expect(node.getAttribute('data-user-id')).toBe('new')
expect(node.dataset.userId).toBe('new')
})

test('removing an absent attribute preserves the shared empty dataset', () => {
const node = document.createElement('view')
const other = document.createElement('view')
const dataset = node.dataset
node.removeAttribute('data-missing')
expect(node.dataset).toBe(dataset)
expect(other.dataset).toBe(dataset)
expect(dataset).toEqual({})
})

test('removing an ordinary attribute preserves dataset entries', () => {
const node = document.createElement('view')
node.setAttribute('data-user-id', 'retained')
node.setAttribute('user-id', 'ordinary')
node.removeAttribute('user-id')
expect(node.hasAttribute('user-id')).toBe(false)
expect(node.dataset).toEqual({ userId: 'retained' })
})

test('preserves the attribute and dataset when a platform hook interrupts removal', () => {
const node = document.createElement('view')
node.setAttribute('data-user-id', 'retained')
const interrupt = vi.fn(() => true)
hooks.tap('onRemoveAttribute', interrupt)
try {
node.removeAttribute('data-user-id')
expect(interrupt).toHaveBeenCalledWith(node, 'data-user-id')
expect(node.getAttribute('data-user-id')).toBe('retained')
expect(node.dataset.userId).toBe('retained')
} finally {
hooks.off('onRemoveAttribute', interrupt)
}
})

test('keeps the existing attribute mutation notification', async () => {
const node = document.createElement('view')
node.setAttribute('data-user-id', 'old')
const callback = vi.fn()
const observer = new MutationObserver(callback)
observer.observe(node, { attributes: true, attributeOldValue: true })
try {
node.removeAttribute('data-user-id')
await Promise.resolve()
expect(callback).toHaveBeenCalledTimes(1)
expect(callback.mock.calls[0][0]).toEqual([
expect.objectContaining({ target: node, type: 'attributes', attributeName: 'data-user-id', oldValue: 'old' })
])
expect(node.dataset).toEqual({})
} finally {
observer.disconnect()
}
})

test.each([false, true])('events omit removed data with a distinct currentTarget: %s', (distinct) => {
const target = document.createElement('view')
const currentTarget = distinct ? document.createElement('view') : target
if (distinct) currentTarget.appendChild(target)
for (const node of new Set([target, currentTarget])) {
node.setAttribute('data-user-id', 'old')
node.setAttribute('data-keep', 'retained')
node.removeAttribute('data-user-id')
}
const listener = vi.fn(event => {
expect(event.target.dataset).toEqual({ keep: 'retained' })
expect(event.currentTarget.dataset).toEqual({ keep: 'retained' })
})
currentTarget.addEventListener('tap', listener)
const event = createEvent({
type: 'tap',
detail: {},
target: { dataset: { sid: target.sid, userId: 'old' }, id: '' },
currentTarget: { dataset: { sid: currentTarget.sid, userId: 'old' }, id: '' }
}, currentTarget)

currentTarget.dispatchEvent(event)
expect(listener).toHaveBeenCalledTimes(1)
})
})
Loading