390b224f62a69ccb27301cd7317325c4e66c0bb76d19d56be420bf4b0c0166d94bcd6b7ed807c70465a2cfd62bf65be17b38c9e0ce4530e65bee143f3d1fe3-exec 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* @flow */
  2. import { extend } from 'shared/util'
  3. function updateAttrs (oldVnode: VNodeWithData, vnode: VNodeWithData) {
  4. if (!oldVnode.data.attrs && !vnode.data.attrs) {
  5. return
  6. }
  7. let key, cur, old
  8. const elm = vnode.elm
  9. const oldAttrs = oldVnode.data.attrs || {}
  10. let attrs = vnode.data.attrs || {}
  11. // clone observed objects, as the user probably wants to mutate it
  12. if (attrs.__ob__) {
  13. attrs = vnode.data.attrs = extend({}, attrs)
  14. }
  15. const supportBatchUpdate = typeof elm.setAttrs === 'function'
  16. const batchedAttrs = {}
  17. for (key in attrs) {
  18. cur = attrs[key]
  19. old = oldAttrs[key]
  20. if (old !== cur) {
  21. supportBatchUpdate
  22. ? (batchedAttrs[key] = cur)
  23. : elm.setAttr(key, cur)
  24. }
  25. }
  26. for (key in oldAttrs) {
  27. if (attrs[key] == null) {
  28. supportBatchUpdate
  29. ? (batchedAttrs[key] = undefined)
  30. : elm.setAttr(key)
  31. }
  32. }
  33. if (supportBatchUpdate) {
  34. elm.setAttrs(batchedAttrs)
  35. }
  36. }
  37. export default {
  38. create: updateAttrs,
  39. update: updateAttrs
  40. }