component.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. var _ = require('./index')
  2. /**
  3. * Check if an element is a component, if yes return its
  4. * component id.
  5. *
  6. * @param {Element} el
  7. * @param {Object} options
  8. * @return {String|undefined}
  9. */
  10. exports.commonTagRE = /^(div|p|span|img|a|b|i|br|ul|ol|li|h1|h2|h3|h4|h5|h6|code|pre|table|th|td|tr|form|label|input|select|option)$/
  11. exports.checkComponent = function (el, options) {
  12. var tag = el.tagName.toLowerCase()
  13. if (tag === 'component') {
  14. // dynamic syntax
  15. var exp = el.getAttribute('is')
  16. if (exp != null) {
  17. el.removeAttribute('is')
  18. } else {
  19. exp = el.getAttribute('bind-is')
  20. if (exp != null) {
  21. // leverage literal dynamic for now.
  22. // TODO: make this cleaner
  23. exp = '{{' + exp + '}}'
  24. el.removeAttribute('bind-is')
  25. }
  26. }
  27. return exp
  28. } else if (!exports.commonTagRE.test(tag)) {
  29. if (_.resolveAsset(options, 'components', tag)) {
  30. return tag
  31. } else if (process.env.NODE_ENV !== 'production') {
  32. if (tag.indexOf('-') > -1 ||
  33. /HTMLUnknownElement/.test(Object.prototype.toString.call(el))) {
  34. _.warn(
  35. 'Unknown custom element: <' + tag + '> - did you ' +
  36. 'register the component correctly?'
  37. )
  38. }
  39. }
  40. }
  41. /* eslint-disable no-cond-assign */
  42. if (tag = _.attr(el, 'component')) {
  43. /* eslint-enable no-cond-assign */
  44. return tag
  45. }
  46. }
  47. /**
  48. * Set a prop's initial value on a vm and its data object.
  49. *
  50. * @param {Vue} vm
  51. * @param {Object} prop
  52. * @param {*} value
  53. */
  54. exports.initProp = function (vm, prop, value) {
  55. if (exports.assertProp(prop, value)) {
  56. var key = prop.path
  57. vm[key] = vm._data[key] = value
  58. }
  59. }
  60. /**
  61. * Assert whether a prop is valid.
  62. *
  63. * @param {Object} prop
  64. * @param {*} value
  65. */
  66. exports.assertProp = function (prop, value) {
  67. // if a prop is not provided and is not required,
  68. // skip the check.
  69. if (prop.raw === null && !prop.required) {
  70. return true
  71. }
  72. var options = prop.options
  73. var type = options.type
  74. var valid = true
  75. var expectedType
  76. if (type) {
  77. if (type === String) {
  78. expectedType = 'string'
  79. valid = typeof value === expectedType
  80. } else if (type === Number) {
  81. expectedType = 'number'
  82. valid = typeof value === 'number'
  83. } else if (type === Boolean) {
  84. expectedType = 'boolean'
  85. valid = typeof value === 'boolean'
  86. } else if (type === Function) {
  87. expectedType = 'function'
  88. valid = typeof value === 'function'
  89. } else if (type === Object) {
  90. expectedType = 'object'
  91. valid = _.isPlainObject(value)
  92. } else if (type === Array) {
  93. expectedType = 'array'
  94. valid = _.isArray(value)
  95. } else {
  96. valid = value instanceof type
  97. }
  98. }
  99. if (!valid) {
  100. process.env.NODE_ENV !== 'production' && _.warn(
  101. 'Invalid prop: type check failed for ' +
  102. prop.path + '="' + prop.raw + '".' +
  103. ' Expected ' + formatType(expectedType) +
  104. ', got ' + formatValue(value) + '.'
  105. )
  106. return false
  107. }
  108. var validator = options.validator
  109. if (validator) {
  110. if (!validator.call(null, value)) {
  111. process.env.NODE_ENV !== 'production' && _.warn(
  112. 'Invalid prop: custom validator check failed for ' +
  113. prop.path + '="' + prop.raw + '"'
  114. )
  115. return false
  116. }
  117. }
  118. return true
  119. }
  120. function formatType (val) {
  121. return val
  122. ? val.charAt(0).toUpperCase() + val.slice(1)
  123. : 'custom type'
  124. }
  125. function formatValue (val) {
  126. return Object.prototype.toString.call(val).slice(8, -1)
  127. }