Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core): fix resolving inheritAttrs from mixins #3742

Merged
merged 4 commits into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 28 additions & 0 deletions packages/runtime-core/__tests__/rendererAttrsFallthrough.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,34 @@ describe('attribute fallthrough', () => {
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() {
Expand Down
9 changes: 9 additions & 0 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@ export interface ComponentInternalInstance {
*/
emitsOptions: ObjectEmitsOptions | null

/**
* resolved inheritAttrs options
* @internal
*/
inheritAttrs?: boolean

// the rest are only for stateful components ---------------------------------

// main proxy that serves as the public instance (`this`)
Expand Down Expand Up @@ -464,6 +470,9 @@ export function createComponentInstance(
// props default value
propsDefaults: EMPTY_OBJ,

// inheritAttrs
inheritAttrs: type.inheritAttrs,

// state
ctx: EMPTY_OBJ,
data: EMPTY_OBJ,
Expand Down
39 changes: 23 additions & 16 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,17 +556,14 @@ export function applyOptions(
renderTriggered,
errorCaptured,
// public API
expose
expose,
inheritAttrs
} = options

const publicThis = instance.proxy!
const ctx = instance.ctx
const globalMixins = instance.appContext.mixins

if (asMixin && render && instance.render === NOOP) {
instance.render = render as InternalRenderFunction
}

// applyOptions is called non-as-mixin once per instance
if (!asMixin) {
shouldCacheAccess = false
Expand Down Expand Up @@ -744,17 +741,6 @@ export function applyOptions(
})
}

// asset options.
// To reduce memory usage, only components with mixins or extends will have
// resolved asset registry attached to instance.
if (asMixin) {
resolveInstanceAssets(instance, options, COMPONENTS)
resolveInstanceAssets(instance, options, DIRECTIVES)
if (__COMPAT__ && isCompatEnabled(DeprecationTypes.FILTERS, instance)) {
resolveInstanceAssets(instance, options, FILTERS)
}
}

// lifecycle options
if (!asMixin) {
callSyncHook(
Expand Down Expand Up @@ -828,6 +814,27 @@ export function applyOptions(
warn(`The \`expose\` option is ignored when used in mixins.`)
}
}

// options that are handled when creating the instance but also need to be
// applied from mixins
if (asMixin) {
if (render && instance.render === NOOP) {
instance.render = render as InternalRenderFunction
}

if (inheritAttrs != null && instance.type.inheritAttrs == null) {
instance.inheritAttrs = inheritAttrs
}

// asset options.
// To reduce memory usage, only components with mixins or extends will have
// resolved asset registry attached to instance.
resolveInstanceAssets(instance, options, COMPONENTS)
resolveInstanceAssets(instance, options, DIRECTIVES)
if (__COMPAT__ && isCompatEnabled(DeprecationTypes.FILTERS, instance)) {
resolveInstanceAssets(instance, options, FILTERS)
}
}
}

function resolveInstanceAssets(
Expand Down
7 changes: 4 additions & 3 deletions packages/runtime-core/src/componentRenderUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ export function renderComponentRoot(
renderCache,
data,
setupState,
ctx
ctx,
inheritAttrs
} = instance

let result
Expand Down Expand Up @@ -123,7 +124,7 @@ export function renderComponentRoot(
;[root, setRoot] = getChildRoot(result)
}

if (fallthroughAttrs && Component.inheritAttrs !== false) {
if (fallthroughAttrs && inheritAttrs !== false) {
const keys = Object.keys(fallthroughAttrs)
const { shapeFlag } = root
if (keys.length) {
Expand Down Expand Up @@ -190,7 +191,7 @@ export function renderComponentRoot(
) {
const { class: cls, style } = vnode.props || {}
if (cls || style) {
if (__DEV__ && Component.inheritAttrs === false) {
if (__DEV__ && inheritAttrs === false) {
warnDeprecation(
DeprecationTypes.INSTANCE_ATTRS_CLASS_STYLE,
instance,
Expand Down
3 changes: 1 addition & 2 deletions packages/server-renderer/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ function renderComponentSubTree(
if (ssrRender) {
// optimized
// resolve fallthrough attrs
let attrs =
instance.type.inheritAttrs !== false ? instance.attrs : undefined
let attrs = instance.inheritAttrs !== false ? instance.attrs : undefined
let hasCloned = false

let cur = instance
Expand Down