Skip to content

Commit e1660f4

Browse files
committed
refactor(runtime-core): adjust attr fallthrough behavior
BREAKING CHANGE: adjust attr fallthrough behavior Updated per pending RFC vuejs/rfcs#137 - Implicit fallthrough now by default only applies for a whitelist of attributes (class, style, event listeners, a11y attributes, and data attributes). - Fallthrough is now applied regardless of whether the component has explicitly declared props. (close #749)
1 parent 06d3447 commit e1660f4

File tree

4 files changed

+52
-95
lines changed

4 files changed

+52
-95
lines changed

packages/runtime-core/__tests__/rendererAttrsFallthrough.spec.ts

+18-84
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { mockWarn } from '@vue/shared'
1515
describe('attribute fallthrough', () => {
1616
mockWarn()
1717

18-
it('everything should be in props when component has no declared props', async () => {
18+
it('should allow whitelisted attrs to fallthrough', async () => {
1919
const click = jest.fn()
2020
const childUpdated = jest.fn()
2121

@@ -42,113 +42,42 @@ describe('attribute fallthrough', () => {
4242

4343
const Child = {
4444
setup(props: any) {
45-
onUpdated(childUpdated)
46-
return () =>
47-
h(
48-
'div',
49-
mergeProps(
50-
{
51-
class: 'c2',
52-
style: { fontWeight: 'bold' }
53-
},
54-
props
55-
),
56-
props.foo
57-
)
58-
}
59-
}
60-
61-
const root = document.createElement('div')
62-
document.body.appendChild(root)
63-
render(h(Hello), root)
64-
65-
const node = root.children[0] as HTMLElement
66-
67-
expect(node.getAttribute('id')).toBe('test')
68-
expect(node.getAttribute('foo')).toBe('1')
69-
expect(node.getAttribute('class')).toBe('c2 c0')
70-
expect(node.style.color).toBe('green')
71-
expect(node.style.fontWeight).toBe('bold')
72-
expect(node.dataset.id).toBe('1')
73-
node.dispatchEvent(new CustomEvent('click'))
74-
expect(click).toHaveBeenCalled()
75-
76-
await nextTick()
77-
expect(childUpdated).toHaveBeenCalled()
78-
expect(node.getAttribute('id')).toBe('test')
79-
expect(node.getAttribute('foo')).toBe('1')
80-
expect(node.getAttribute('class')).toBe('c2 c1')
81-
expect(node.style.color).toBe('red')
82-
expect(node.style.fontWeight).toBe('bold')
83-
})
84-
85-
it('should implicitly fallthrough on single root nodes', async () => {
86-
const click = jest.fn()
87-
const childUpdated = jest.fn()
88-
89-
const Hello = {
90-
setup() {
91-
const count = ref(0)
92-
93-
function inc() {
94-
count.value++
95-
click()
96-
}
97-
98-
return () =>
99-
h(Child, {
100-
foo: 1,
101-
id: 'test',
102-
class: 'c' + count.value,
103-
style: { color: count.value ? 'red' : 'green' },
104-
onClick: inc
105-
})
106-
}
107-
}
108-
109-
const Child = defineComponent({
110-
props: {
111-
foo: Number
112-
},
113-
setup(props) {
11445
onUpdated(childUpdated)
11546
return () =>
11647
h(
11748
'div',
11849
{
50+
id: props.id, // id is not whitelisted
11951
class: 'c2',
12052
style: { fontWeight: 'bold' }
12153
},
12254
props.foo
12355
)
12456
}
125-
})
57+
}
12658

12759
const root = document.createElement('div')
12860
document.body.appendChild(root)
12961
render(h(Hello), root)
13062

13163
const node = root.children[0] as HTMLElement
13264

133-
// with declared props, any parent attr that isn't a prop falls through
134-
expect(node.getAttribute('id')).toBe('test')
65+
expect(node.getAttribute('id')).toBe('test') // id is not whitelisted, but explicitly bound
66+
expect(node.getAttribute('foo')).toBe(null) // foo is not whitelisted
13567
expect(node.getAttribute('class')).toBe('c2 c0')
13668
expect(node.style.color).toBe('green')
13769
expect(node.style.fontWeight).toBe('bold')
70+
expect(node.dataset.id).toBe('1')
13871
node.dispatchEvent(new CustomEvent('click'))
13972
expect(click).toHaveBeenCalled()
14073

141-
// ...while declared ones remain props
142-
expect(node.hasAttribute('foo')).toBe(false)
143-
14474
await nextTick()
14575
expect(childUpdated).toHaveBeenCalled()
14676
expect(node.getAttribute('id')).toBe('test')
77+
expect(node.getAttribute('foo')).toBe(null)
14778
expect(node.getAttribute('class')).toBe('c2 c1')
14879
expect(node.style.color).toBe('red')
14980
expect(node.style.fontWeight).toBe('bold')
150-
151-
expect(node.hasAttribute('foo')).toBe(false)
15281
})
15382

15483
it('should fallthrough for nested components', async () => {
@@ -179,12 +108,16 @@ describe('attribute fallthrough', () => {
179108
const Child = {
180109
setup(props: any) {
181110
onUpdated(childUpdated)
111+
// HOC simply passing props down.
112+
// this will result in merging the same attrs, but should be deduped by
113+
// `mergeProps`.
182114
return () => h(GrandChild, props)
183115
}
184116
}
185117

186118
const GrandChild = defineComponent({
187119
props: {
120+
id: String,
188121
foo: Number
189122
},
190123
setup(props) {
@@ -193,6 +126,7 @@ describe('attribute fallthrough', () => {
193126
h(
194127
'div',
195128
{
129+
id: props.id,
196130
class: 'c2',
197131
style: { fontWeight: 'bold' }
198132
},
@@ -351,11 +285,11 @@ describe('attribute fallthrough', () => {
351285

352286
// #677
353287
it('should update merged dynamic attrs on optimized child root', async () => {
354-
const id = ref('foo')
288+
const aria = ref('true')
355289
const cls = ref('bar')
356290
const Parent = {
357291
render() {
358-
return h(Child, { id: id.value, class: cls.value })
292+
return h(Child, { 'aria-hidden': aria.value, class: cls.value })
359293
}
360294
}
361295

@@ -370,14 +304,14 @@ describe('attribute fallthrough', () => {
370304
document.body.appendChild(root)
371305
render(h(Parent), root)
372306

373-
expect(root.innerHTML).toBe(`<div id="foo" class="bar"></div>`)
307+
expect(root.innerHTML).toBe(`<div aria-hidden="true" class="bar"></div>`)
374308

375-
id.value = 'fooo'
309+
aria.value = 'false'
376310
await nextTick()
377-
expect(root.innerHTML).toBe(`<div id="fooo" class="bar"></div>`)
311+
expect(root.innerHTML).toBe(`<div aria-hidden="false" class="bar"></div>`)
378312

379313
cls.value = 'barr'
380314
await nextTick()
381-
expect(root.innerHTML).toBe(`<div id="fooo" class="barr"></div>`)
315+
expect(root.innerHTML).toBe(`<div aria-hidden="false" class="barr"></div>`)
382316
})
383317
})

packages/runtime-core/src/componentProps.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ export function resolveProps(
214214
lock()
215215

216216
instance.props = props
217-
instance.attrs = options ? attrs || EMPTY_OBJ : props
217+
instance.attrs = attrs || EMPTY_OBJ
218218
instance.vnodeHooks = vnodeHooks || EMPTY_OBJ
219219
}
220220

packages/runtime-core/src/componentRenderUtils.ts

+24-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
cloneVNode
1212
} from './vnode'
1313
import { handleError, ErrorCodes } from './errorHandling'
14-
import { PatchFlags, ShapeFlags, EMPTY_OBJ } from '@vue/shared'
14+
import { PatchFlags, ShapeFlags, EMPTY_OBJ, isOn } from '@vue/shared'
1515
import { warn } from './warning'
1616

1717
// mark the current rendering instance for asset resolution (e.g.
@@ -79,25 +79,26 @@ export function renderComponentRoot(
7979
}
8080

8181
// attr merging
82+
let fallthroughAttrs
8283
if (
83-
Component.props != null &&
8484
Component.inheritAttrs !== false &&
8585
attrs !== EMPTY_OBJ &&
86-
Object.keys(attrs).length
86+
(fallthroughAttrs = getFallthroughAttrs(attrs))
8787
) {
8888
if (
8989
result.shapeFlag & ShapeFlags.ELEMENT ||
9090
result.shapeFlag & ShapeFlags.COMPONENT
9191
) {
92-
result = cloneVNode(result, attrs)
92+
result = cloneVNode(result, fallthroughAttrs)
9393
// If the child root node is a compiler optimized vnode, make sure it
9494
// force update full props to account for the merged attrs.
9595
if (result.dynamicChildren !== null) {
9696
result.patchFlag |= PatchFlags.FULL_PROPS
9797
}
9898
} else if (__DEV__ && !accessedAttrs && result.type !== Comment) {
9999
warn(
100-
`Extraneous non-props attributes (${Object.keys(attrs).join(',')}) ` +
100+
`Extraneous non-props attributes (` +
101+
`${Object.keys(attrs).join(', ')}) ` +
101102
`were passed to component but could not be automatically inherited ` +
102103
`because component renders fragment or text root nodes.`
103104
)
@@ -142,7 +143,24 @@ export function renderComponentRoot(
142143
return result
143144
}
144145

145-
function isElementRoot(vnode: VNode) {
146+
const getFallthroughAttrs = (attrs: Data): Data | undefined => {
147+
let res: Data | undefined
148+
for (const key in attrs) {
149+
if (
150+
key === 'class' ||
151+
key === 'style' ||
152+
key === 'role' ||
153+
isOn(key) ||
154+
key.indexOf('aria-') === 0 ||
155+
key.indexOf('data-') === 0
156+
) {
157+
;(res || (res = {}))[key] = attrs[key]
158+
}
159+
}
160+
return res
161+
}
162+
163+
const isElementRoot = (vnode: VNode) => {
146164
return (
147165
vnode.shapeFlag & ShapeFlags.COMPONENT ||
148166
vnode.shapeFlag & ShapeFlags.ELEMENT ||

packages/runtime-core/src/vnode.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -399,15 +399,20 @@ export function mergeProps(...args: (Data & VNodeProps)[]) {
399399
const toMerge = args[i]
400400
for (const key in toMerge) {
401401
if (key === 'class') {
402-
ret.class = normalizeClass([ret.class, toMerge.class])
402+
if (ret.class !== toMerge.class) {
403+
ret.class = normalizeClass([ret.class, toMerge.class])
404+
}
403405
} else if (key === 'style') {
404406
ret.style = normalizeStyle([ret.style, toMerge.style])
405407
} else if (handlersRE.test(key)) {
406408
// on*, vnode*
407409
const existing = ret[key]
408-
ret[key] = existing
409-
? [].concat(existing as any, toMerge[key] as any)
410-
: toMerge[key]
410+
const incoming = toMerge[key]
411+
if (existing !== incoming) {
412+
ret[key] = existing
413+
? [].concat(existing as any, toMerge[key] as any)
414+
: incoming
415+
}
411416
} else {
412417
ret[key] = toMerge[key]
413418
}

0 commit comments

Comments
 (0)