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
Prev Previous commit
fix(runtime): restore dataset mappings by attribute write order
  • Loading branch information
Jarvis636431 committed Oct 27, 2026
commit f7a80349f92d65d8aa53b632b7984383a68549e3
6 changes: 6 additions & 0 deletions packages/taro-runtime/src/dom-external/node.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { DATASET, OBJECT, PROPS, STYLE } from '../constants'
import { copyDatasetAttributeOrder } from '../dom/dataset'
import { NodeType } from '../dom/node_types'
import { parser } from '../dom-external/inner-html/parser'

import type { TaroElement } from '../dom/element'
import type { TaroNode } from '../dom/node'

export type IPosition = 'beforebegin' | 'afterbegin' | 'beforeend' | 'afterend'
Expand Down Expand Up @@ -67,6 +69,10 @@ export function cloneNode (this: TaroNode, isDeep = false) {
newNode.childNodes = this.childNodes.map(node => (node as any).cloneNode(true))
}

if (this.nodeType === NodeType.ELEMENT_NODE) {
copyDatasetAttributeOrder(this as TaroElement, newNode)
}

return newNode
}

Expand Down
39 changes: 39 additions & 0 deletions packages/taro-runtime/src/dom/dataset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { toCamelCase } from '@tarojs/shared'

import type { TaroElement } from './element'

// Object property order does not track writes to existing attributes.
const attributeOrder = new WeakMap<TaroElement, Set<string>>()

export function recordDatasetAttribute (element: TaroElement, name: string) {
let order = attributeOrder.get(element)
if (!order) {
order = new Set()
attributeOrder.set(element, order)
}
order.delete(name)
order.add(name)
}

export function removeDatasetAttribute (element: TaroElement, name: string) {
const key = toCamelCase(name.replace(/^data-/, ''))
const order = attributeOrder.get(element)
order?.delete(name)
let remainingAttribute: string | undefined
order?.forEach(attribute => {
if (toCamelCase(attribute.replace(/^data-/, '')) === key) {
remainingAttribute = attribute
}
})
if (remainingAttribute !== undefined) {
element.dataset[key] = element.props[remainingAttribute]
} else {
delete element.dataset[key]
}
if (order?.size === 0) attributeOrder.delete(element)
}

export function copyDatasetAttributeOrder (source: TaroElement, target: TaroElement) {
const order = attributeOrder.get(source)
if (order) attributeOrder.set(target, new Set(order))
}
12 changes: 3 additions & 9 deletions packages/taro-runtime/src/dom/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { MutationObserver, MutationRecordType } from '../dom-external/mutation-observer'
import { extend, getComponentsAlias, isElement, isHasExtractProp, shortcutAttr } from '../utils'
import { ClassList } from './class-list'
import { recordDatasetAttribute, removeDatasetAttribute } from './dataset'
import { eventSource } from './event-source'
import { TaroNode } from './node'
import { NodeType } from './node_types'
Expand Down Expand Up @@ -172,6 +173,7 @@ export class TaroElement extends TaroNode {
this.dataset = Object.create(null)
}
this.dataset[toCamelCase(qualifiedName.replace(/^data-/, ''))] = value
recordDatasetAttribute(this, qualifiedName)
}
break
}
Expand Down Expand Up @@ -247,15 +249,7 @@ export class TaroElement extends TaroNode {
}
delete this.props[qualifiedName]
if (qualifiedName.startsWith('data-')) {
const datasetKey = toCamelCase(qualifiedName.replace(/^data-/, ''))
const remainingAttribute = Object.keys(this.props).find(
name => name.startsWith('data-') && toCamelCase(name.replace(/^data-/, '')) === datasetKey
)
if (remainingAttribute !== undefined) {
this.dataset[datasetKey] = this.props[remainingAttribute]
} else {
delete this.dataset[datasetKey]
}
removeDatasetAttribute(this, qualifiedName)
}
}

Expand Down
127 changes: 126 additions & 1 deletion packages/taro-runtime/tests/data-attributes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,127 @@ import { describe, expect, test, vi } from 'vitest'
import { createEvent, document, MutationObserver } from '../src/index'

describe('removing data attributes', () => {
const collidingAttributes = ['data-user-id', 'data-user--id', 'data-user---id']
const removalOrders = [
[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]
]

test.each(removalOrders)('preserves the latest surviving write when removing attributes in order %s, %s, %s', (...order) => {
for (const rewritten of [-1, 0, 1, 2]) {
const node = document.createElement('view')
collidingAttributes.forEach((attribute, index) => node.setAttribute(attribute, ['old', 'middle', 'keep'][index]))
if (rewritten !== -1) node.setAttribute(collidingAttributes[rewritten], 'rewritten')
const writes = rewritten === -1 ? [0, 1, 2] : [0, 1, 2].filter(index => index !== rewritten).concat(rewritten)
const remaining = new Set([0, 1, 2])

for (const index of order) {
node.removeAttribute(collidingAttributes[index])
remaining.delete(index)
const latest = writes.filter(index => remaining.has(index)).pop()
if (latest === undefined) {
expect(Object.prototype.hasOwnProperty.call(node.dataset, 'userId')).toBe(false)
} else {
expect(node.dataset.userId).toBe(latest === rewritten ? 'rewritten' : ['old', 'middle', 'keep'][latest])
}
}
}
})

test('treats a removed and recreated mapping as the latest write without reordering attributes', () => {
const node = document.createElement('view')
collidingAttributes.forEach((attribute, index) => node.setAttribute(attribute, index))
node.setAttribute(collidingAttributes[0], 'updated')
expect(node.attributes.map(attribute => attribute.name)).toEqual(collidingAttributes)
node.removeAttribute(collidingAttributes[0])
expect(node.dataset.userId).toBe(2)
node.setAttribute(collidingAttributes[0], 'recreated')
node.removeAttribute(collidingAttributes[1])
expect(node.dataset.userId).toBe('recreated')
node.removeAttribute(collidingAttributes[0])
expect(node.dataset.userId).toBe(2)
})

test.each([undefined, null, false, 0, '', { nested: true }])('restores the actual remaining value: %j', (value) => {
const node = document.createElement('view')
node.setAttribute('data-user-id', value)
node.setAttribute('data-user--id', 'removed')
node.removeAttribute('data-user--id')
expect(Object.prototype.hasOwnProperty.call(node.dataset, 'userId')).toBe(true)
expect(node.dataset.userId).toBe(value)
node.removeAttribute('data-user-id')
expect(Object.prototype.hasOwnProperty.call(node.dataset, 'userId')).toBe(false)
})

test.each([false, true])('clones write order independently with deep cloning: %s', (deep) => {
const parent = document.createElement('view')
const source = document.createElement('view')
parent.appendChild(source)
collidingAttributes.forEach((attribute, index) => source.setAttribute(attribute, index))
source.setAttribute(collidingAttributes[0], 'latest')
const clone = deep ? parent.cloneNode(true).firstChild! : source.cloneNode()

clone.removeAttribute(collidingAttributes[2])
expect(clone.dataset.userId).toBe('latest')
clone.setAttribute(collidingAttributes[1], 'clone-only')
source.removeAttribute(collidingAttributes[0])
expect(source.dataset.userId).toBe(2)
expect(clone.dataset.userId).toBe('clone-only')
clone.removeAttribute(collidingAttributes[1])
expect(clone.dataset.userId).toBe('latest')
expect(source.getAttribute(collidingAttributes[1])).toBe(1)
})

test('leaves write order intact when removal is absent or interrupted', () => {
const node = document.createElement('view')
collidingAttributes.forEach((attribute, index) => node.setAttribute(attribute, index))
node.removeAttribute('data-user----id')
expect(node.dataset.userId).toBe(2)
const interrupt = () => true
hooks.tap('onRemoveAttribute', interrupt)
try {
node.removeAttribute(collidingAttributes[2])
expect(node.getAttribute(collidingAttributes[2])).toBe(2)
expect(node.dataset.userId).toBe(2)
} finally {
hooks.off('onRemoveAttribute', interrupt)
}
node.removeAttribute(collidingAttributes[1])
expect(node.dataset.userId).toBe(2)
node.removeAttribute(collidingAttributes[2])
expect(node.dataset.userId).toBe(0)
node.removeAttribute(collidingAttributes[0])
node.setAttribute(collidingAttributes[1], 'new')
expect(node.dataset.userId).toBe('new')
})

test('tracks parsed attributes and preserves write order through repeated cloning', () => {
const parent = document.createElement('view')
parent.innerHTML = '<view data-user-id="old" data-user--id="middle" data-user---id="keep"></view>'
const node = parent.firstChild!
node.setAttribute('data-user-id', 'updated')
const clone = node.cloneNode().cloneNode()
clone.removeAttribute('data-user-id')
expect(clone.dataset.userId).toBe('keep')
clone.removeAttribute('data-user---id')
expect(clone.dataset.userId).toBe('middle')
expect(node.dataset.userId).toBe('updated')
})

test.each([
['data-', ''],
['data-__proto__', '__proto__'],
['data-constructor', 'constructor'],
['data-to-string', 'toString']
])('supports special dataset keys for %s', (attribute, key) => {
const node = document.createElement('view')
node.setAttribute(attribute, 'old')
node.setAttribute(attribute + '-', 'keep')
node.removeAttribute(attribute)
expect(node.dataset[key]).toBe('keep')
node.removeAttribute(attribute + '-')
expect(Object.prototype.hasOwnProperty.call(node.dataset, key)).toBe(false)
})

test.each([
['data-test', 'test'],
['data-user-id', 'userId'],
Expand Down Expand Up @@ -34,7 +155,9 @@ describe('removing data attributes', () => {

test.each([
['data-user-id', 'data-user--id'],
['data-user--id', 'data-user-id']
['data-user--id', 'data-user-id'],
['data-userId', 'data-user-id'],
['data-user-id', 'data-user-id-']
])('preserves the shared dataset key for %s and %s in either removal order', (first, second) => {
for (const removed of [first, second]) {
const node = document.createElement('view')
Expand All @@ -61,6 +184,8 @@ describe('removing data attributes', () => {
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', 'retained')
node.setAttribute('data-user---id', 'middle')
node.setAttribute('data-user--id', 'retained')
node.setAttribute('data-user-id', 'removed')
node.removeAttribute('data-user-id')
Expand Down
Loading