a76f8dd836de77a079113e79edfdf9adf1157a1ad28c8f0c685716505e28b513cc4d087794dc22f8530c52e0f8f28effefd94ffa07159f86cb0a5911244723 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* @flow */
  2. import {
  3. tip,
  4. hasOwn,
  5. isDef,
  6. isUndef,
  7. hyphenate,
  8. formatComponentName
  9. } from 'core/util/index'
  10. export function extractPropsFromVNodeData (
  11. data: VNodeData,
  12. Ctor: Class<Component>,
  13. tag?: string
  14. ): ?Object {
  15. // we are only extracting raw values here.
  16. // validation and default values are handled in the child
  17. // component itself.
  18. const propOptions = Ctor.options.props
  19. if (isUndef(propOptions)) {
  20. return
  21. }
  22. const res = {}
  23. const { attrs, props } = data
  24. if (isDef(attrs) || isDef(props)) {
  25. for (const key in propOptions) {
  26. const altKey = hyphenate(key)
  27. if (process.env.NODE_ENV !== 'production') {
  28. const keyInLowerCase = key.toLowerCase()
  29. if (
  30. key !== keyInLowerCase &&
  31. attrs && hasOwn(attrs, keyInLowerCase)
  32. ) {
  33. tip(
  34. `Prop "${keyInLowerCase}" is passed to component ` +
  35. `${formatComponentName(tag || Ctor)}, but the declared prop name is` +
  36. ` "${key}". ` +
  37. `Note that HTML attributes are case-insensitive and camelCased ` +
  38. `props need to use their kebab-case equivalents when using in-DOM ` +
  39. `templates. You should probably use "${altKey}" instead of "${key}".`
  40. )
  41. }
  42. }
  43. checkProp(res, props, key, altKey, true) ||
  44. checkProp(res, attrs, key, altKey, false)
  45. }
  46. }
  47. return res
  48. }
  49. function checkProp (
  50. res: Object,
  51. hash: ?Object,
  52. key: string,
  53. altKey: string,
  54. preserve: boolean
  55. ): boolean {
  56. if (isDef(hash)) {
  57. if (hasOwn(hash, key)) {
  58. res[key] = hash[key]
  59. if (!preserve) {
  60. delete hash[key]
  61. }
  62. return true
  63. } else if (hasOwn(hash, altKey)) {
  64. res[key] = hash[altKey]
  65. if (!preserve) {
  66. delete hash[altKey]
  67. }
  68. return true
  69. }
  70. }
  71. return false
  72. }