|
@@ -15,7 +15,7 @@ import { mockWarn } from '@vue/shared'
|
|
|
describe('attribute fallthrough', () => {
|
|
describe('attribute fallthrough', () => {
|
|
|
mockWarn()
|
|
mockWarn()
|
|
|
|
|
|
|
|
- it('everything should be in props when component has no declared props', async () => {
|
|
|
|
|
|
|
+ it('should allow whitelisted attrs to fallthrough', async () => {
|
|
|
const click = jest.fn()
|
|
const click = jest.fn()
|
|
|
const childUpdated = jest.fn()
|
|
const childUpdated = jest.fn()
|
|
|
|
|
|
|
@@ -42,87 +42,19 @@ describe('attribute fallthrough', () => {
|
|
|
|
|
|
|
|
const Child = {
|
|
const Child = {
|
|
|
setup(props: any) {
|
|
setup(props: any) {
|
|
|
- onUpdated(childUpdated)
|
|
|
|
|
- return () =>
|
|
|
|
|
- h(
|
|
|
|
|
- 'div',
|
|
|
|
|
- mergeProps(
|
|
|
|
|
- {
|
|
|
|
|
- class: 'c2',
|
|
|
|
|
- style: { fontWeight: 'bold' }
|
|
|
|
|
- },
|
|
|
|
|
- props
|
|
|
|
|
- ),
|
|
|
|
|
- props.foo
|
|
|
|
|
- )
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- const root = document.createElement('div')
|
|
|
|
|
- document.body.appendChild(root)
|
|
|
|
|
- render(h(Hello), root)
|
|
|
|
|
-
|
|
|
|
|
- const node = root.children[0] as HTMLElement
|
|
|
|
|
-
|
|
|
|
|
- expect(node.getAttribute('id')).toBe('test')
|
|
|
|
|
- expect(node.getAttribute('foo')).toBe('1')
|
|
|
|
|
- expect(node.getAttribute('class')).toBe('c2 c0')
|
|
|
|
|
- expect(node.style.color).toBe('green')
|
|
|
|
|
- expect(node.style.fontWeight).toBe('bold')
|
|
|
|
|
- expect(node.dataset.id).toBe('1')
|
|
|
|
|
- node.dispatchEvent(new CustomEvent('click'))
|
|
|
|
|
- expect(click).toHaveBeenCalled()
|
|
|
|
|
-
|
|
|
|
|
- await nextTick()
|
|
|
|
|
- expect(childUpdated).toHaveBeenCalled()
|
|
|
|
|
- expect(node.getAttribute('id')).toBe('test')
|
|
|
|
|
- expect(node.getAttribute('foo')).toBe('1')
|
|
|
|
|
- expect(node.getAttribute('class')).toBe('c2 c1')
|
|
|
|
|
- expect(node.style.color).toBe('red')
|
|
|
|
|
- expect(node.style.fontWeight).toBe('bold')
|
|
|
|
|
- })
|
|
|
|
|
-
|
|
|
|
|
- it('should implicitly fallthrough on single root nodes', async () => {
|
|
|
|
|
- const click = jest.fn()
|
|
|
|
|
- const childUpdated = jest.fn()
|
|
|
|
|
-
|
|
|
|
|
- const Hello = {
|
|
|
|
|
- setup() {
|
|
|
|
|
- const count = ref(0)
|
|
|
|
|
-
|
|
|
|
|
- function inc() {
|
|
|
|
|
- count.value++
|
|
|
|
|
- click()
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return () =>
|
|
|
|
|
- h(Child, {
|
|
|
|
|
- foo: 1,
|
|
|
|
|
- id: 'test',
|
|
|
|
|
- class: 'c' + count.value,
|
|
|
|
|
- style: { color: count.value ? 'red' : 'green' },
|
|
|
|
|
- onClick: inc
|
|
|
|
|
- })
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- const Child = defineComponent({
|
|
|
|
|
- props: {
|
|
|
|
|
- foo: Number
|
|
|
|
|
- },
|
|
|
|
|
- setup(props) {
|
|
|
|
|
onUpdated(childUpdated)
|
|
onUpdated(childUpdated)
|
|
|
return () =>
|
|
return () =>
|
|
|
h(
|
|
h(
|
|
|
'div',
|
|
'div',
|
|
|
{
|
|
{
|
|
|
|
|
+ id: props.id, // id is not whitelisted
|
|
|
class: 'c2',
|
|
class: 'c2',
|
|
|
style: { fontWeight: 'bold' }
|
|
style: { fontWeight: 'bold' }
|
|
|
},
|
|
},
|
|
|
props.foo
|
|
props.foo
|
|
|
)
|
|
)
|
|
|
}
|
|
}
|
|
|
- })
|
|
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
const root = document.createElement('div')
|
|
const root = document.createElement('div')
|
|
|
document.body.appendChild(root)
|
|
document.body.appendChild(root)
|
|
@@ -130,25 +62,22 @@ describe('attribute fallthrough', () => {
|
|
|
|
|
|
|
|
const node = root.children[0] as HTMLElement
|
|
const node = root.children[0] as HTMLElement
|
|
|
|
|
|
|
|
- // with declared props, any parent attr that isn't a prop falls through
|
|
|
|
|
- expect(node.getAttribute('id')).toBe('test')
|
|
|
|
|
|
|
+ expect(node.getAttribute('id')).toBe('test') // id is not whitelisted, but explicitly bound
|
|
|
|
|
+ expect(node.getAttribute('foo')).toBe(null) // foo is not whitelisted
|
|
|
expect(node.getAttribute('class')).toBe('c2 c0')
|
|
expect(node.getAttribute('class')).toBe('c2 c0')
|
|
|
expect(node.style.color).toBe('green')
|
|
expect(node.style.color).toBe('green')
|
|
|
expect(node.style.fontWeight).toBe('bold')
|
|
expect(node.style.fontWeight).toBe('bold')
|
|
|
|
|
+ expect(node.dataset.id).toBe('1')
|
|
|
node.dispatchEvent(new CustomEvent('click'))
|
|
node.dispatchEvent(new CustomEvent('click'))
|
|
|
expect(click).toHaveBeenCalled()
|
|
expect(click).toHaveBeenCalled()
|
|
|
|
|
|
|
|
- // ...while declared ones remain props
|
|
|
|
|
- expect(node.hasAttribute('foo')).toBe(false)
|
|
|
|
|
-
|
|
|
|
|
await nextTick()
|
|
await nextTick()
|
|
|
expect(childUpdated).toHaveBeenCalled()
|
|
expect(childUpdated).toHaveBeenCalled()
|
|
|
expect(node.getAttribute('id')).toBe('test')
|
|
expect(node.getAttribute('id')).toBe('test')
|
|
|
|
|
+ expect(node.getAttribute('foo')).toBe(null)
|
|
|
expect(node.getAttribute('class')).toBe('c2 c1')
|
|
expect(node.getAttribute('class')).toBe('c2 c1')
|
|
|
expect(node.style.color).toBe('red')
|
|
expect(node.style.color).toBe('red')
|
|
|
expect(node.style.fontWeight).toBe('bold')
|
|
expect(node.style.fontWeight).toBe('bold')
|
|
|
-
|
|
|
|
|
- expect(node.hasAttribute('foo')).toBe(false)
|
|
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('should fallthrough for nested components', async () => {
|
|
it('should fallthrough for nested components', async () => {
|
|
@@ -179,12 +108,16 @@ describe('attribute fallthrough', () => {
|
|
|
const Child = {
|
|
const Child = {
|
|
|
setup(props: any) {
|
|
setup(props: any) {
|
|
|
onUpdated(childUpdated)
|
|
onUpdated(childUpdated)
|
|
|
|
|
+ // HOC simply passing props down.
|
|
|
|
|
+ // this will result in merging the same attrs, but should be deduped by
|
|
|
|
|
+ // `mergeProps`.
|
|
|
return () => h(GrandChild, props)
|
|
return () => h(GrandChild, props)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const GrandChild = defineComponent({
|
|
const GrandChild = defineComponent({
|
|
|
props: {
|
|
props: {
|
|
|
|
|
+ id: String,
|
|
|
foo: Number
|
|
foo: Number
|
|
|
},
|
|
},
|
|
|
setup(props) {
|
|
setup(props) {
|
|
@@ -193,6 +126,7 @@ describe('attribute fallthrough', () => {
|
|
|
h(
|
|
h(
|
|
|
'div',
|
|
'div',
|
|
|
{
|
|
{
|
|
|
|
|
+ id: props.id,
|
|
|
class: 'c2',
|
|
class: 'c2',
|
|
|
style: { fontWeight: 'bold' }
|
|
style: { fontWeight: 'bold' }
|
|
|
},
|
|
},
|
|
@@ -351,11 +285,11 @@ describe('attribute fallthrough', () => {
|
|
|
|
|
|
|
|
// #677
|
|
// #677
|
|
|
it('should update merged dynamic attrs on optimized child root', async () => {
|
|
it('should update merged dynamic attrs on optimized child root', async () => {
|
|
|
- const id = ref('foo')
|
|
|
|
|
|
|
+ const aria = ref('true')
|
|
|
const cls = ref('bar')
|
|
const cls = ref('bar')
|
|
|
const Parent = {
|
|
const Parent = {
|
|
|
render() {
|
|
render() {
|
|
|
- return h(Child, { id: id.value, class: cls.value })
|
|
|
|
|
|
|
+ return h(Child, { 'aria-hidden': aria.value, class: cls.value })
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -370,14 +304,14 @@ describe('attribute fallthrough', () => {
|
|
|
document.body.appendChild(root)
|
|
document.body.appendChild(root)
|
|
|
render(h(Parent), root)
|
|
render(h(Parent), root)
|
|
|
|
|
|
|
|
- expect(root.innerHTML).toBe(`<div id="foo" class="bar"></div>`)
|
|
|
|
|
|
|
+ expect(root.innerHTML).toBe(`<div aria-hidden="true" class="bar"></div>`)
|
|
|
|
|
|
|
|
- id.value = 'fooo'
|
|
|
|
|
|
|
+ aria.value = 'false'
|
|
|
await nextTick()
|
|
await nextTick()
|
|
|
- expect(root.innerHTML).toBe(`<div id="fooo" class="bar"></div>`)
|
|
|
|
|
|
|
+ expect(root.innerHTML).toBe(`<div aria-hidden="false" class="bar"></div>`)
|
|
|
|
|
|
|
|
cls.value = 'barr'
|
|
cls.value = 'barr'
|
|
|
await nextTick()
|
|
await nextTick()
|
|
|
- expect(root.innerHTML).toBe(`<div id="fooo" class="barr"></div>`)
|
|
|
|
|
|
|
+ expect(root.innerHTML).toBe(`<div aria-hidden="false" class="barr"></div>`)
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|