wip: more tests for v2 compat

This commit is contained in:
Evan You
2021-04-29 14:45:22 -04:00
parent d3d9355c5a
commit 3963f2e963
5 changed files with 289 additions and 14 deletions

View File

@@ -1,4 +1,9 @@
import { isSpecialBooleanAttr } from '@vue/shared'
import { isSpecialBooleanAttr, makeMap, NOOP } from '@vue/shared'
import {
compatUtils,
ComponentInternalInstance,
DeprecationTypes
} from '@vue/runtime-core'
export const xlinkNS = 'http://www.w3.org/1999/xlink'
@@ -6,7 +11,8 @@ export function patchAttr(
el: Element,
key: string,
value: any,
isSVG: boolean
isSVG: boolean,
instance?: ComponentInternalInstance | null
) {
if (isSVG && key.startsWith('xlink:')) {
if (value == null) {
@@ -15,7 +21,7 @@ export function patchAttr(
el.setAttributeNS(xlinkNS, key, value)
}
} else {
if (__COMPAT__ && compatCoerceAttr(el, key, value)) {
if (__COMPAT__ && compatCoerceAttr(el, key, value, instance)) {
return
}
@@ -31,9 +37,6 @@ export function patchAttr(
}
// 2.x compat
import { makeMap, NOOP } from '@vue/shared'
import { compatUtils, DeprecationTypes } from '@vue/runtime-core'
const isEnumeratedAttr = __COMPAT__
? /*#__PURE__*/ makeMap('contenteditable,draggable,spellcheck')
: NOOP
@@ -41,7 +44,8 @@ const isEnumeratedAttr = __COMPAT__
export function compatCoerceAttr(
el: Element,
key: string,
value: unknown
value: unknown,
instance: ComponentInternalInstance | null = null
): boolean {
if (isEnumeratedAttr(key)) {
const v2CocercedValue =
@@ -54,7 +58,7 @@ export function compatCoerceAttr(
v2CocercedValue &&
compatUtils.softAssertCompatEnabled(
DeprecationTypes.ATTR_ENUMERATED_COERSION,
null,
instance,
key,
value,
v2CocercedValue
@@ -68,7 +72,7 @@ export function compatCoerceAttr(
!isSpecialBooleanAttr(key) &&
compatUtils.softAssertCompatEnabled(
DeprecationTypes.ATTR_FALSE_VALUE,
null,
instance,
key
)
) {

View File

@@ -2,7 +2,7 @@
// Reason: potentially setting innerHTML.
// This can come from explicit usage of v-html or innerHTML as a prop in render
import { warn } from '@vue/runtime-core'
import { warn, DeprecationTypes, compatUtils } from '@vue/runtime-core'
// functions. The user is responsible for using them with only trusted content.
export function patchDOMProp(
@@ -55,6 +55,28 @@ export function patchDOMProp(
}
}
if (
__COMPAT__ &&
value === false &&
compatUtils.isCompatEnabled(
DeprecationTypes.ATTR_FALSE_VALUE,
parentComponent
)
) {
const type = typeof el[key]
if (type === 'string' || type === 'number') {
__DEV__ &&
compatUtils.warnDeprecation(
DeprecationTypes.ATTR_FALSE_VALUE,
parentComponent,
key
)
el[key] = type === 'number' ? 0 : ''
el.removeAttribute(key)
return
}
}
// some properties perform value validation and throw
try {
el[key] = value

View File

@@ -58,7 +58,7 @@ export const patchProp: DOMRendererOptions['patchProp'] = (
} else if (key === 'false-value') {
;(el as any)._falseValue = nextValue
}
patchAttr(el, key, nextValue, isSVG)
patchAttr(el, key, nextValue, isSVG, parentComponent)
}
break
}