style.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* @flow */
  2. import { cached, camelize, toObject, extend } from 'shared/util'
  3. const prefixes = ['Webkit', 'Moz', 'ms']
  4. let testEl
  5. const normalize = cached(function (prop) {
  6. testEl = testEl || document.createElement('div')
  7. prop = camelize(prop)
  8. if (prop !== 'filter' && (prop in testEl.style)) {
  9. return prop
  10. }
  11. const upper = prop.charAt(0).toUpperCase() + prop.slice(1)
  12. for (let i = 0; i < prefixes.length; i++) {
  13. const prefixed = prefixes[i] + upper
  14. if (prefixed in testEl.style) {
  15. return prefixed
  16. }
  17. }
  18. })
  19. function updateStyle (oldVnode: VNodeWithData, vnode: VNodeWithData) {
  20. if (!oldVnode.data.style && !vnode.data.style) {
  21. return
  22. }
  23. let cur, name
  24. const elm: any = vnode.elm
  25. const oldStyle: any = oldVnode.data.style || {}
  26. let style = vnode.data.style || {}
  27. // handle array syntax
  28. if (Array.isArray(style)) {
  29. style = vnode.data.style = toObject(style)
  30. }
  31. for (name in oldStyle) {
  32. if (!style[name]) {
  33. elm.style[normalize(name)] = ''
  34. }
  35. }
  36. for (name in style) {
  37. cur = style[name]
  38. if (cur !== oldStyle[name]) {
  39. elm.style[normalize(name)] = cur
  40. }
  41. }
  42. // clone the style for future updates,
  43. // in case the user mutates the style object in-place.
  44. vnode.data.style = extend({}, style)
  45. }
  46. export default {
  47. create: updateStyle,
  48. update: updateStyle
  49. }