Repository navigation
fix(runtime): 删除 data-* 属性时同步清理 dataset #19569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
96c3499
9d211a4
dcf71ef
2f84963
f7a8034
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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-/, ''))] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 也会受影响。建议处理一下仍存在的同名映射,并补个回归用例。
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
| 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) | ||
| }) | ||
| }) |
Uh oh!
There was an error while loading. Please reload this page.