fix(runtime-dom/ssr): properly handle xlink and boolean attributes

This commit is contained in:
Evan You 2020-01-28 22:03:53 -05:00
parent 6f43c4b516
commit e6e2c58234
3 changed files with 38 additions and 22 deletions

View File

@ -1,4 +1,6 @@
// TODO explain why we are no longer checking boolean/enumerated here import { isSpecialBooleanAttr } from '@vue/shared'
const xlinkNS = 'http://www.w3.org/1999/xlink'
export function patchAttr( export function patchAttr(
el: Element, el: Element,
@ -7,12 +9,19 @@ export function patchAttr(
isSVG: boolean isSVG: boolean
) { ) {
if (isSVG && key.indexOf('xlink:') === 0) { if (isSVG && key.indexOf('xlink:') === 0) {
// TODO handle xlink if (value == null) {
} else if (value == null) { el.removeAttributeNS(xlinkNS, key)
} else {
el.setAttributeNS(xlinkNS, key, value)
}
} else {
// note we are only checking boolean attributes that don't have a
// correspoding dom prop of the same name here.
const isBoolean = isSpecialBooleanAttr(key)
if (value == null || (isBoolean && value === false)) {
el.removeAttribute(key) el.removeAttribute(key)
} else { } else {
// TODO in dev mode, warn against incorrect values for boolean or el.setAttribute(key, isBoolean ? '' : value)
// enumerated attributes }
el.setAttribute(key, value)
} }
} }

View File

@ -30,7 +30,9 @@ export function renderProps(
? key ? key
: propsToAttrMap[key] || key.toLowerCase() : propsToAttrMap[key] || key.toLowerCase()
if (isBooleanAttr(attrKey)) { if (isBooleanAttr(attrKey)) {
ret += ` ${attrKey}=""` if (value !== false) {
ret += ` ${attrKey}`
}
} else if (isSSRSafeAttrName(attrKey)) { } else if (isSSRSafeAttrName(attrKey)) {
ret += ` ${attrKey}="${escape(value)}"` ret += ` ${attrKey}="${escape(value)}"`
} }

View File

@ -1,18 +1,23 @@
import { makeMap } from './makeMap' import { makeMap } from './makeMap'
// TODO validate this list! // On the client we only need to offer special cases for boolean attributes that
// on the client, most of these probably has corresponding prop // have different names from their corresponding dom properties:
// or, like allowFullscreen on iframe, although case is different, the attr // - itemscope -> N/A
// affects the property properly... // - allowfullscreen -> allowFullscreen
// Basically, we can skip this check on the client // - formnovalidate -> formNoValidate
// but they are still needed during SSR to produce correct initial markup // - ismap -> isMap
export const isBooleanAttr = makeMap( // - nomodule -> noModule
'allowfullscreen,async,autofocus,autoplay,checked,compact,controls,declare,' + // - novalidate -> noValidate
'default,defaultchecked,defaultmuted,defaultselected,defer,disabled,' + // - readonly -> readOnly
'enabled,formnovalidate,hidden,indeterminate,inert,ismap,itemscope,loop,multiple,' + const specialBooleanAttrs = `itemscope,allowfullscreen,formnovalidate,ismap,nomodule,novalidate,readonly`
'muted,nohref,noresize,noshade,novalidate,nowrap,open,pauseonexit,readonly,' + export const isSpecialBooleanAttr = /*#__PURE__*/ makeMap(specialBooleanAttrs)
'required,reversed,scoped,seamless,selected,sortable,translate,' +
'truespeed,typemustmatch,visible' // The full list is needed during SSR to produce the correct initial markup.
export const isBooleanAttr = /*#__PURE__*/ makeMap(
specialBooleanAttrs +
`,async,autofocus,autoplay,controls,default,defer,disabled,hidden,ismap,` +
`loop,nomodule,open,required,reversed,scoped,seamless,` +
`checked,muted,multiple,selected`
) )
const unsafeAttrCharRE = /[>/="'\u0009\u000a\u000c\u0020]/ const unsafeAttrCharRE = /[>/="'\u0009\u000a\u000c\u0020]/
@ -37,7 +42,7 @@ export const propsToAttrMap: Record<string, string | undefined> = {
} }
// CSS properties that accept plain numbers // CSS properties that accept plain numbers
export const isNoUnitNumericStyleProp = makeMap( export const isNoUnitNumericStyleProp = /*#__PURE__*/ makeMap(
`animation-iteration-count,border-image-outset,border-image-slice,` + `animation-iteration-count,border-image-outset,border-image-slice,` +
`border-image-width,box-flex,box-flex-group,box-ordinal-group,column-count,` + `border-image-width,box-flex,box-flex-group,box-ordinal-group,column-count,` +
`columns,flex,flex-grow,flex-positive,flex-shrink,flex-negative,flex-order,` + `columns,flex,flex-grow,flex-positive,flex-shrink,flex-negative,flex-order,` +