Skip to content

Commit

Permalink
fix(hydration): improve attr hydration mismatch check for boolean attrs
Browse files Browse the repository at this point in the history
close #10057
close #10060
  • Loading branch information
yyx990803 committed Jan 10, 2024
1 parent d60a575 commit 972face
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
15 changes: 15 additions & 0 deletions packages/runtime-core/__tests__/hydration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1489,5 +1489,20 @@ describe('SSR hydration', () => {
mountWithHydration(`<div id="bar"></div>`, () => h('div', { id: 'foo' }))
expect(`Hydration attribute mismatch`).toHaveBeenWarnedTimes(2)
})

test('boolean attr handling', () => {
mountWithHydration(`<input />`, () => h('input', { readonly: false }))
expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()

mountWithHydration(`<input readonly />`, () =>
h('input', { readonly: true }),
)
expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()

mountWithHydration(`<input readonly="readonly" />`, () =>
h('input', { readonly: true }),
)
expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
})
})
})
25 changes: 12 additions & 13 deletions packages/runtime-core/src/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,19 +754,18 @@ function propHasMismatch(
(el instanceof SVGElement && isKnownSvgAttr(key)) ||
(el instanceof HTMLElement && (isBooleanAttr(key) || isKnownHtmlAttr(key)))
) {
// #10000 some attrs such as textarea.value can't be get by `hasAttribute`
actual = el.hasAttribute(key)
? el.getAttribute(key)
: key in el
? el[key as keyof typeof el]
: ''
expected = isBooleanAttr(key)
? includeBooleanAttr(clientValue)
? ''
: false
: clientValue == null
? ''
: String(clientValue)
if (isBooleanAttr(key)) {
actual = el.hasAttribute(key)
expected = includeBooleanAttr(clientValue)
} else {
// #10000 some attrs such as textarea.value can't be get by `hasAttribute`
actual = el.hasAttribute(key)
? el.getAttribute(key)
: key in el
? el[key as keyof typeof el]
: ''
expected = clientValue == null ? '' : String(clientValue)
}
if (actual !== expected) {
mismatchType = `attribute`
mismatchKey = key
Expand Down

0 comments on commit 972face

Please sign in to comment.