Skip to content

Commit

Permalink
refactor: use shared isAttrRenderable logic between ssr and hydration
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 18, 2024
1 parent 492a720 commit 81d307a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
9 changes: 4 additions & 5 deletions packages/runtime-core/src/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import {
isBooleanAttr,
isKnownHtmlAttr,
isKnownSvgAttr,
isObject,
isOn,
isRenderableAttrValue,
isReservedProp,
isString,
normalizeClass,
Expand Down Expand Up @@ -770,10 +770,9 @@ function propHasMismatch(
} else {
actual = false
}
expected =
isObject(clientValue) || clientValue == null
? false
: String(clientValue)
expected = isRenderableAttrValue(clientValue)
? String(clientValue)
: false
}
if (actual !== expected) {
mismatchType = `attribute`
Expand Down
19 changes: 8 additions & 11 deletions packages/server-renderer/src/helpers/ssrRenderAttrs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { escapeHtml, isSVGTag, stringifyStyle } from '@vue/shared'
import {
escapeHtml,
isRenderableAttrValue,
isSVGTag,
stringifyStyle,
} from '@vue/shared'
import {
includeBooleanAttr,
isBooleanAttr,
Expand Down Expand Up @@ -47,7 +52,7 @@ export function ssrRenderDynamicAttr(
value: unknown,
tag?: string,
): string {
if (!isRenderableValue(value)) {
if (!isRenderableAttrValue(value)) {
return ``
}
const attrKey =
Expand All @@ -69,20 +74,12 @@ export function ssrRenderDynamicAttr(
// Render a v-bind attr with static key. The key is pre-processed at compile
// time and we only need to check and escape value.
export function ssrRenderAttr(key: string, value: unknown): string {
if (!isRenderableValue(value)) {
if (!isRenderableAttrValue(value)) {
return ``
}
return ` ${key}="${escapeHtml(value)}"`
}

function isRenderableValue(value: unknown): boolean {
if (value == null) {
return false
}
const type = typeof value
return type === 'string' || type === 'number' || type === 'boolean'
}

export function ssrRenderClass(raw: unknown): string {
return escapeHtml(normalizeClass(raw))
}
Expand Down
11 changes: 11 additions & 0 deletions packages/shared/src/domAttrConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,14 @@ export const isKnownSvgAttr = /*#__PURE__*/ makeMap(
`xlink:href,xlink:role,xlink:show,xlink:title,xlink:type,xmlns:xlink,xml:base,xml:lang,` +
`xml:space,y,y1,y2,yChannelSelector,z,zoomAndPan`,
)

/**
* Shared between server-renderer and runtime-core hydration logic
*/
export function isRenderableAttrValue(value: unknown): boolean {
if (value == null) {
return false
}
const type = typeof value
return type === 'string' || type === 'number' || type === 'boolean'
}

0 comments on commit 81d307a

Please sign in to comment.