Skip to content

Commit

Permalink
fix(runtime-dom): should set <input list="..."> as attribute
Browse files Browse the repository at this point in the history
fix #1526
  • Loading branch information
yyx990803 committed Jul 6, 2020
1 parent 31e37b4 commit 441c236
Showing 1 changed file with 44 additions and 20 deletions.
64 changes: 44 additions & 20 deletions packages/runtime-dom/src/patchProp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { patchDOMProp } from './modules/props'
import { patchEvent } from './modules/events'
import { isOn, isString, isFunction } from '@vue/shared'
import { RendererOptions } from '@vue/runtime-core'
import { chdir } from 'process'

This comment has been minimized.

Copy link
@bigopon

bigopon Jul 6, 2020

should this line be removed?


const nativeOnRE = /^on[a-z]/

Expand Down Expand Up @@ -38,26 +39,7 @@ export const patchProp: DOMRendererOptions['patchProp'] = (
if (!key.startsWith('onUpdate:')) {
patchEvent(el, key, prevValue, nextValue, parentComponent)
}
} else if (
// spellcheck and draggable are numerated attrs, however their
// corresponding DOM properties are actually booleans - this leads to
// setting it with a string "false" value leading it to be coerced to
// `true`, so we need to always treat them as attributes.
// Note that `contentEditable` doesn't have this problem: its DOM
// property is also enumerated string values.
key !== 'spellcheck' &&
key !== 'draggable' &&
(isSVG
? // most keys must be set as attribute on svg elements to work
// ...except innerHTML
key === 'innerHTML' ||
// or native onclick with function values
(key in el && nativeOnRE.test(key) && isFunction(nextValue))
: // for normal html elements, set as a property if it exists
key in el &&
// except native onclick with string values
!(nativeOnRE.test(key) && isString(nextValue)))
) {
} else if (shouldSetAsProp(el, key, nextValue, isSVG)) {
patchDOMProp(
el,
key,
Expand All @@ -82,3 +64,45 @@ export const patchProp: DOMRendererOptions['patchProp'] = (
break
}
}

function shouldSetAsProp(
el: Element,
key: string,
value: unknown,
isSVG: boolean
) {
if (isSVG) {
// most keys must be set as attribute on svg elements to work
// ...except innerHTML
if (key === 'innerHTML') {
return true
}
// or native onclick with function values
if (key in el && nativeOnRE.test(key) && isFunction(value)) {
return true
}
return false
}

// spellcheck and draggable are numerated attrs, however their
// corresponding DOM properties are actually booleans - this leads to
// setting it with a string "false" value leading it to be coerced to
// `true`, so we need to always treat them as attributes.
// Note that `contentEditable` doesn't have this problem: its DOM
// property is also enumerated string values.
if (key === 'spellcheck' || key === 'draggable') {
return false
}

// #1526 <input list> must be set as attribute
if (key === 'list' && el.tagName === 'INPUT') {
return false
}

// native onclick with string value, must be set as attribute
if (nativeOnRE.test(key) && isString(value)) {
return false
}

return key in el
}

0 comments on commit 441c236

Please sign in to comment.