| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835 |
- /**
- * @vitest-environment jsdom
- */
- // using DOM renderer because this case is mostly DOM-specific
- import {
- Fragment,
- type FunctionalComponent,
- Teleport,
- createBlock,
- createCommentVNode,
- createElementBlock,
- createElementVNode,
- defineComponent,
- h,
- mergeProps,
- nextTick,
- onUpdated,
- openBlock,
- ref,
- render,
- withModifiers,
- } from '@vue/runtime-dom'
- import { createApp } from 'vue'
- import { PatchFlags } from '@vue/shared'
- describe('attribute fallthrough', () => {
- it('should allow attrs to fallthrough', async () => {
- const click = vi.fn()
- const childUpdated = vi.fn()
- const Hello = {
- setup() {
- const count = ref(0)
- function inc() {
- count.value++
- click()
- }
- return () =>
- h(Child, {
- foo: count.value + 1,
- id: 'test',
- class: 'c' + count.value,
- style: { color: count.value ? 'red' : 'green' },
- onClick: inc,
- 'data-id': count.value + 1,
- })
- },
- }
- const Child = {
- setup(props: any) {
- onUpdated(childUpdated)
- return () =>
- h(
- 'div',
- {
- class: 'c2',
- style: { fontWeight: 'bold' },
- },
- 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('2')
- expect(node.getAttribute('class')).toBe('c2 c1')
- expect(node.style.color).toBe('red')
- expect(node.style.fontWeight).toBe('bold')
- expect(node.dataset.id).toBe('2')
- })
- it('should only allow whitelisted fallthrough on functional component with optional props', async () => {
- const click = vi.fn()
- const childUpdated = vi.fn()
- const count = ref(0)
- function inc() {
- count.value++
- click()
- }
- const Hello = () =>
- h(Child, {
- foo: count.value + 1,
- id: 'test',
- class: 'c' + count.value,
- style: { color: count.value ? 'red' : 'green' },
- onClick: inc,
- })
- const Child = (props: any) => {
- childUpdated()
- return h(
- 'div',
- {
- class: 'c2',
- style: { fontWeight: 'bold' },
- },
- props.foo,
- )
- }
- const root = document.createElement('div')
- document.body.appendChild(root)
- render(h(Hello), root)
- const node = root.children[0] as HTMLElement
- // not whitelisted
- expect(node.getAttribute('id')).toBe(null)
- expect(node.getAttribute('foo')).toBe(null)
- // whitelisted: style, class, event listeners
- expect(node.getAttribute('class')).toBe('c2 c0')
- expect(node.style.color).toBe('green')
- expect(node.style.fontWeight).toBe('bold')
- node.dispatchEvent(new CustomEvent('click'))
- expect(click).toHaveBeenCalled()
- await nextTick()
- expect(childUpdated).toHaveBeenCalled()
- expect(node.getAttribute('id')).toBe(null)
- expect(node.getAttribute('foo')).toBe(null)
- expect(node.getAttribute('class')).toBe('c2 c1')
- expect(node.style.color).toBe('red')
- expect(node.style.fontWeight).toBe('bold')
- })
- it('should allow all attrs on functional component with declared props', async () => {
- const click = vi.fn()
- const childUpdated = vi.fn()
- const count = ref(0)
- function inc() {
- count.value++
- click()
- }
- const Hello = () =>
- h(Child, {
- foo: count.value + 1,
- id: 'test',
- class: 'c' + count.value,
- style: { color: count.value ? 'red' : 'green' },
- onClick: inc,
- })
- const Child = (props: { foo: number }) => {
- childUpdated()
- return h(
- 'div',
- {
- class: 'c2',
- style: { fontWeight: 'bold' },
- },
- props.foo,
- )
- }
- Child.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(null) // declared as prop
- expect(node.getAttribute('class')).toBe('c2 c0')
- expect(node.style.color).toBe('green')
- expect(node.style.fontWeight).toBe('bold')
- node.dispatchEvent(new CustomEvent('click'))
- expect(click).toHaveBeenCalled()
- await nextTick()
- expect(childUpdated).toHaveBeenCalled()
- expect(node.getAttribute('id')).toBe('test')
- expect(node.getAttribute('foo')).toBe(null)
- expect(node.getAttribute('class')).toBe('c2 c1')
- expect(node.style.color).toBe('red')
- expect(node.style.fontWeight).toBe('bold')
- })
- it('should fallthrough for nested components', async () => {
- const click = vi.fn()
- const childUpdated = vi.fn()
- const grandChildUpdated = vi.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 = {
- setup(props: any) {
- 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)
- },
- }
- const GrandChild = defineComponent({
- props: {
- id: String,
- foo: Number,
- },
- setup(props) {
- onUpdated(grandChildUpdated)
- return () =>
- h(
- 'div',
- {
- id: props.id,
- class: 'c2',
- style: { fontWeight: 'bold' },
- },
- props.foo,
- )
- },
- })
- const root = document.createElement('div')
- document.body.appendChild(root)
- render(h(Hello), root)
- 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('class')).toBe('c2 c0')
- expect(node.style.color).toBe('green')
- expect(node.style.fontWeight).toBe('bold')
- node.dispatchEvent(new CustomEvent('click'))
- expect(click).toHaveBeenCalled()
- // ...while declared ones remain props
- expect(node.hasAttribute('foo')).toBe(false)
- await nextTick()
- expect(childUpdated).toHaveBeenCalled()
- expect(grandChildUpdated).toHaveBeenCalled()
- expect(node.getAttribute('id')).toBe('test')
- expect(node.getAttribute('class')).toBe('c2 c1')
- expect(node.style.color).toBe('red')
- expect(node.style.fontWeight).toBe('bold')
- expect(node.hasAttribute('foo')).toBe(false)
- })
- it('should not fallthrough with inheritAttrs: false', () => {
- const Parent = {
- render() {
- return h(Child, { foo: 1, class: 'parent' })
- },
- }
- const Child = defineComponent({
- props: ['foo'],
- inheritAttrs: false,
- render() {
- return h('div', this.foo)
- },
- })
- const root = document.createElement('div')
- document.body.appendChild(root)
- render(h(Parent), root)
- // should not contain class
- expect(root.innerHTML).toMatch(`<div>1</div>`)
- })
- // #3741
- it('should not fallthrough with inheritAttrs: false from mixins', () => {
- const Parent = {
- render() {
- return h(Child, { foo: 1, class: 'parent' })
- },
- }
- const mixin = {
- inheritAttrs: false,
- }
- const Child = defineComponent({
- mixins: [mixin],
- props: ['foo'],
- render() {
- return h('div', this.foo)
- },
- })
- const root = document.createElement('div')
- document.body.appendChild(root)
- render(h(Parent), root)
- // should not contain class
- expect(root.innerHTML).toMatch(`<div>1</div>`)
- })
- it('explicit spreading with inheritAttrs: false', () => {
- const Parent = {
- render() {
- return h(Child, { foo: 1, class: 'parent' })
- },
- }
- const Child = defineComponent({
- props: ['foo'],
- inheritAttrs: false,
- render() {
- return h(
- 'div',
- mergeProps(
- {
- class: 'child',
- },
- this.$attrs,
- ),
- this.foo,
- )
- },
- })
- const root = document.createElement('div')
- document.body.appendChild(root)
- render(h(Parent), root)
- // should merge parent/child classes
- expect(root.innerHTML).toMatch(`<div class="child parent">1</div>`)
- })
- it('should warn when fallthrough fails on non-single-root', () => {
- const Parent = {
- render() {
- return h(Child, { foo: 1, class: 'parent', onBar: () => {} })
- },
- }
- const Child = defineComponent({
- props: ['foo'],
- render() {
- return [h('div'), h('div')]
- },
- })
- const root = document.createElement('div')
- document.body.appendChild(root)
- render(h(Parent), root)
- expect(`Extraneous non-props attributes (class)`).toHaveBeenWarned()
- expect(`Extraneous non-emits event listeners`).toHaveBeenWarned()
- })
- it('should warn when fallthrough fails on teleport root node', () => {
- const Parent = {
- render() {
- return h(Child, { class: 'parent' })
- },
- }
- const root = document.createElement('div')
- const Child = defineComponent({
- render() {
- return h(Teleport, { to: root }, h('div'))
- },
- })
- document.body.appendChild(root)
- render(h(Parent), root)
- expect(`Extraneous non-props attributes (class)`).toHaveBeenWarned()
- })
- it('should dedupe same listeners when $attrs is used during render', () => {
- const click = vi.fn()
- const count = ref(0)
- function inc() {
- count.value++
- click()
- }
- const Parent = {
- render() {
- return h(Child, { onClick: inc })
- },
- }
- const Child = defineComponent({
- render() {
- return h(
- 'div',
- mergeProps(
- {
- onClick: withModifiers(() => {}, ['prevent', 'stop']),
- },
- this.$attrs,
- ),
- )
- },
- })
- const root = document.createElement('div')
- document.body.appendChild(root)
- render(h(Parent), root)
- const node = root.children[0] as HTMLElement
- node.dispatchEvent(new CustomEvent('click'))
- expect(click).toHaveBeenCalledTimes(1)
- expect(count.value).toBe(1)
- })
- it('should not warn when $attrs is used during render', () => {
- const Parent = {
- render() {
- return h(Child, { foo: 1, class: 'parent', onBar: () => {} })
- },
- }
- const Child = defineComponent({
- props: ['foo'],
- render() {
- return [h('div'), h('div', this.$attrs)]
- },
- })
- const root = document.createElement('div')
- document.body.appendChild(root)
- render(h(Parent), root)
- expect(`Extraneous non-props attributes`).not.toHaveBeenWarned()
- expect(`Extraneous non-emits event listeners`).not.toHaveBeenWarned()
- expect(root.innerHTML).toBe(`<div></div><div class="parent"></div>`)
- })
- it('should not warn when context.attrs is used during render', () => {
- const Parent = {
- render() {
- return h(Child, { foo: 1, class: 'parent', onBar: () => {} })
- },
- }
- const Child = defineComponent({
- props: ['foo'],
- setup(_props, { attrs }) {
- return () => [h('div'), h('div', attrs)]
- },
- })
- const root = document.createElement('div')
- document.body.appendChild(root)
- render(h(Parent), root)
- expect(`Extraneous non-props attributes`).not.toHaveBeenWarned()
- expect(`Extraneous non-emits event listeners`).not.toHaveBeenWarned()
- expect(root.innerHTML).toBe(`<div></div><div class="parent"></div>`)
- })
- it('should not warn when context.attrs is used during render (functional)', () => {
- const Parent = {
- render() {
- return h(Child, { foo: 1, class: 'parent', onBar: () => {} })
- },
- }
- const Child: FunctionalComponent = (_, { attrs }) => [
- h('div'),
- h('div', attrs),
- ]
- Child.props = ['foo']
- const root = document.createElement('div')
- document.body.appendChild(root)
- render(h(Parent), root)
- expect(`Extraneous non-props attributes`).not.toHaveBeenWarned()
- expect(`Extraneous non-emits event listeners`).not.toHaveBeenWarned()
- expect(root.innerHTML).toBe(`<div></div><div class="parent"></div>`)
- })
- it('should not warn when functional component has optional props', () => {
- const Parent = {
- render() {
- return h(Child, { foo: 1, class: 'parent', onBar: () => {} })
- },
- }
- const Child = (props: any) => [h('div'), h('div', { class: props.class })]
- const root = document.createElement('div')
- document.body.appendChild(root)
- render(h(Parent), root)
- expect(`Extraneous non-props attributes`).not.toHaveBeenWarned()
- expect(`Extraneous non-emits event listeners`).not.toHaveBeenWarned()
- expect(root.innerHTML).toBe(`<div></div><div class="parent"></div>`)
- })
- it('should warn when functional component has props and does not use attrs', () => {
- const Parent = {
- render() {
- return h(Child, { foo: 1, class: 'parent', onBar: () => {} })
- },
- }
- const Child: FunctionalComponent = () => [h('div'), h('div')]
- Child.props = ['foo']
- const root = document.createElement('div')
- document.body.appendChild(root)
- render(h(Parent), root)
- expect(`Extraneous non-props attributes`).toHaveBeenWarned()
- expect(`Extraneous non-emits event listeners`).toHaveBeenWarned()
- expect(root.innerHTML).toBe(`<div></div><div></div>`)
- })
- // #677
- it('should update merged dynamic attrs on optimized child root', async () => {
- const aria = ref('true')
- const cls = ref('bar')
- const Parent = {
- render() {
- return h(Child, { 'aria-hidden': aria.value, class: cls.value })
- },
- }
- const Child = {
- props: [],
- render() {
- return (openBlock(), createBlock('div'))
- },
- }
- const root = document.createElement('div')
- document.body.appendChild(root)
- render(h(Parent), root)
- expect(root.innerHTML).toBe(`<div aria-hidden="true" class="bar"></div>`)
- aria.value = 'false'
- await nextTick()
- expect(root.innerHTML).toBe(`<div aria-hidden="false" class="bar"></div>`)
- cls.value = 'barr'
- await nextTick()
- expect(root.innerHTML).toBe(`<div aria-hidden="false" class="barr"></div>`)
- })
- it('should not let listener fallthrough when declared in emits (stateful)', () => {
- const Child = defineComponent({
- emits: ['click'],
- render() {
- return h(
- 'button',
- {
- onClick: () => {
- this.$emit('click', 'custom')
- },
- },
- 'hello',
- )
- },
- })
- const onClick = vi.fn()
- const App = {
- render() {
- return h(Child, {
- onClick,
- })
- },
- }
- const root = document.createElement('div')
- document.body.appendChild(root)
- render(h(App), root)
- const node = root.children[0] as HTMLElement
- node.dispatchEvent(new CustomEvent('click'))
- expect(onClick).toHaveBeenCalledTimes(1)
- expect(onClick).toHaveBeenCalledWith('custom')
- })
- it('should not let listener fallthrough when declared in emits (functional)', () => {
- const Child: FunctionalComponent<{}, { click: any }> = (_, { emit }) => {
- // should not be in props
- expect((_ as any).onClick).toBeUndefined()
- return h('button', {
- onClick: () => {
- emit('click', 'custom')
- },
- })
- }
- Child.emits = ['click']
- const onClick = vi.fn()
- const App = {
- render() {
- return h(Child, {
- onClick,
- })
- },
- }
- const root = document.createElement('div')
- document.body.appendChild(root)
- render(h(App), root)
- const node = root.children[0] as HTMLElement
- node.dispatchEvent(new CustomEvent('click'))
- expect(onClick).toHaveBeenCalledTimes(1)
- expect(onClick).toHaveBeenCalledWith('custom')
- })
- it('should support fallthrough for fragments with single element + comments', () => {
- const click = vi.fn()
- const Hello = {
- setup() {
- return () => h(Child, { class: 'foo', onClick: click })
- },
- }
- const Child = {
- setup() {
- return () => (
- openBlock(),
- createBlock(
- Fragment,
- null,
- [
- createCommentVNode('hello'),
- h('button'),
- createCommentVNode('world'),
- ],
- PatchFlags.STABLE_FRAGMENT | PatchFlags.DEV_ROOT_FRAGMENT,
- )
- )
- },
- }
- const root = document.createElement('div')
- document.body.appendChild(root)
- render(h(Hello), root)
- expect(root.innerHTML).toBe(
- `<!--hello--><button class="foo"></button><!--world-->`,
- )
- const button = root.children[0] as HTMLElement
- button.dispatchEvent(new CustomEvent('click'))
- expect(click).toHaveBeenCalled()
- })
- it('should support fallthrough for nested dev root fragments', async () => {
- const toggle = ref(false)
- const Child = {
- setup() {
- return () => (
- openBlock(),
- createElementBlock(
- Fragment,
- null,
- [
- createCommentVNode(' comment A '),
- toggle.value
- ? (openBlock(), createElementBlock('span', { key: 0 }, 'Foo'))
- : (openBlock(),
- createElementBlock(
- Fragment,
- { key: 1 },
- [
- createCommentVNode(' comment B '),
- createElementVNode('div', null, 'Bar'),
- ],
- PatchFlags.STABLE_FRAGMENT | PatchFlags.DEV_ROOT_FRAGMENT,
- )),
- ],
- PatchFlags.STABLE_FRAGMENT | PatchFlags.DEV_ROOT_FRAGMENT,
- )
- )
- },
- }
- const Root = {
- setup() {
- return () => (openBlock(), createBlock(Child, { class: 'red' }))
- },
- }
- const root = document.createElement('div')
- document.body.appendChild(root)
- render(h(Root), root)
- expect(root.innerHTML).toBe(
- `<!-- comment A --><!-- comment B --><div class="red">Bar</div>`,
- )
- toggle.value = true
- await nextTick()
- expect(root.innerHTML).toBe(
- `<!-- comment A --><span class=\"red\">Foo</span>`,
- )
- })
- // #1989
- it('should not fallthrough v-model listeners with corresponding declared prop', () => {
- let textFoo = ''
- let textBar = ''
- const click = vi.fn()
- const App = defineComponent({
- setup() {
- return () =>
- h(Child, {
- modelValue: textFoo,
- 'onUpdate:modelValue': (val: string) => {
- textFoo = val
- },
- })
- },
- })
- const Child = defineComponent({
- props: ['modelValue'],
- setup(_props, { emit }) {
- return () =>
- h(GrandChild, {
- modelValue: textBar,
- 'onUpdate:modelValue': (val: string) => {
- textBar = val
- emit('update:modelValue', 'from Child')
- },
- })
- },
- })
- const GrandChild = defineComponent({
- props: ['modelValue'],
- setup(_props, { emit }) {
- return () =>
- h('button', {
- onClick() {
- click()
- emit('update:modelValue', 'from GrandChild')
- },
- })
- },
- })
- const root = document.createElement('div')
- document.body.appendChild(root)
- render(h(App), root)
- const node = root.children[0] as HTMLElement
- node.dispatchEvent(new CustomEvent('click'))
- expect(click).toHaveBeenCalled()
- expect(textBar).toBe('from GrandChild')
- expect(textFoo).toBe('from Child')
- })
- // covers uncaught regression #10710
- it('should track this.$attrs access in slots', async () => {
- const GrandChild = {
- template: `<slot/>`,
- }
- const Child = {
- components: { GrandChild },
- template: `<div><GrandChild>{{ $attrs.foo }}</GrandChild></div>`,
- }
- const obj = ref(1)
- const App = {
- render() {
- return h(Child, { foo: obj.value })
- },
- }
- const root = document.createElement('div')
- createApp(App).mount(root)
- expect(root.innerHTML).toBe('<div foo="1">1</div>')
- obj.value = 2
- await nextTick()
- expect(root.innerHTML).toBe('<div foo="2">2</div>')
- })
- })
|