Skip to content

Commit 0635caf

Browse files
Alfred-Skybluesxzz
authored andcommitted
chore: extend HTMLElement with Symbol as key
1 parent d53bf8a commit 0635caf

File tree

5 files changed

+18
-13
lines changed

5 files changed

+18
-13
lines changed

packages/runtime-dom/__tests__/patchClass.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { patchProp } from '../src/patchProp'
2-
import { ElementWithTransition } from '../src/components/Transition'
2+
import { ElementWithTransition, vtcKey } from '../src/components/Transition'
33
import { svgNS } from '../src/nodeOps'
44

55
describe('runtime-dom: class patching', () => {
@@ -13,12 +13,12 @@ describe('runtime-dom: class patching', () => {
1313

1414
test('transition class', () => {
1515
const el = document.createElement('div') as ElementWithTransition
16-
el._vtc = new Set(['bar', 'baz'])
16+
el[vtcKey] = new Set(['bar', 'baz'])
1717
patchProp(el, 'class', null, 'foo')
1818
expect(el.className).toBe('foo bar baz')
1919
patchProp(el, 'class', null, null)
2020
expect(el.className).toBe('bar baz')
21-
delete el._vtc
21+
delete el[vtcKey]
2222
patchProp(el, 'class', null, 'foo')
2323
expect(el.className).toBe('foo')
2424
})

packages/runtime-dom/src/components/Transition.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ export interface TransitionProps extends BaseTransitionProps<Element> {
3232
leaveToClass?: string
3333
}
3434

35+
export const vtcKey = Symbol('_vtc')
36+
3537
export interface ElementWithTransition extends HTMLElement {
3638
// _vtc = Vue Transition Classes.
3739
// Store the temporarily-added transition classes on the element
3840
// so that we can avoid overwriting them if the element's class is patched
3941
// during the transition.
40-
_vtc?: Set<string>
42+
[vtcKey]?: Set<string>
4143
}
4244

4345
// DOM Transition is a higher-order-component based on the platform-agnostic
@@ -295,18 +297,18 @@ function NumberOf(val: unknown): number {
295297
export function addTransitionClass(el: Element, cls: string) {
296298
cls.split(/\s+/).forEach(c => c && el.classList.add(c))
297299
;(
298-
(el as ElementWithTransition)._vtc ||
299-
((el as ElementWithTransition)._vtc = new Set())
300+
(el as ElementWithTransition)[vtcKey] ||
301+
((el as ElementWithTransition)[vtcKey] = new Set())
300302
).add(cls)
301303
}
302304

303305
export function removeTransitionClass(el: Element, cls: string) {
304306
cls.split(/\s+/).forEach(c => c && el.classList.remove(c))
305-
const { _vtc } = el as ElementWithTransition
307+
const _vtc = (el as ElementWithTransition)[vtcKey]
306308
if (_vtc) {
307309
_vtc.delete(cls)
308310
if (!_vtc!.size) {
309-
;(el as ElementWithTransition)._vtc = undefined
311+
;(el as ElementWithTransition)[vtcKey] = undefined
310312
}
311313
}
312314
}

packages/runtime-dom/src/components/TransitionGroup.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
getTransitionInfo,
77
resolveTransitionProps,
88
TransitionPropsValidators,
9-
forceReflow
9+
forceReflow,
10+
vtcKey
1011
} from './Transition'
1112
import {
1213
Fragment,
@@ -199,7 +200,7 @@ function hasCSSTransform(
199200
// all other transition classes applied to ensure only the move class
200201
// is applied.
201202
const clone = el.cloneNode() as HTMLElement
202-
const _vtc = el._vtc
203+
const _vtc = el[vtcKey]
203204
if (_vtc) {
204205
_vtc.forEach(cls => {
205206
cls.split(/\s+/).forEach(c => c && clone.classList.remove(c))

packages/runtime-dom/src/modules/class.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { ElementWithTransition } from '../components/Transition'
1+
import { ElementWithTransition, vtcKey } from '../components/Transition'
22

33
// compiler should normalize class + :class bindings on the same element
44
// into a single binding ['staticClass', dynamic]
55
export function patchClass(el: Element, value: string | null, isSVG: boolean) {
66
// directly setting className should be faster than setAttribute in theory
77
// if this is an element during a transition, take the temporary transition
88
// classes into account.
9-
const transitionClasses = (el as ElementWithTransition)._vtc
9+
const transitionClasses = (el as ElementWithTransition)[vtcKey]
1010
if (transitionClasses) {
1111
value = (
1212
value ? [value, ...transitionClasses] : [...transitionClasses]

packages/vue/__tests__/svgNamespace.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// - runtime-core/src/renderer.ts
66
// - compiler-core/src/transforms/transformElement.ts
77

8+
import { vtcKey } from '../../runtime-dom/src/components/Transition'
89
import { render, h, ref, nextTick } from '../src'
910

1011
describe('SVG support', () => {
@@ -54,7 +55,8 @@ describe('SVG support', () => {
5455

5556
// set a transition class on the <div> - which is only respected on non-svg
5657
// patches
57-
;(f2 as any)._vtc = ['baz']
58+
;(f2 as any)[vtcKey] = ['baz']
59+
console.log('我打印了f2', f2)
5860
cls.value = 'bar'
5961
await nextTick()
6062
expect(f1.getAttribute('class')).toBe('bar')

0 commit comments

Comments
 (0)