2
0

style.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* @flow */
  2. import { cached, extend, camelize, toObject } 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: any = vnode.data.style || {}
  27. // handle string
  28. if (typeof style === 'string') {
  29. elm.setAttribute('style', style)
  30. return
  31. }
  32. const needClone = style.__ob__
  33. // handle array syntax
  34. if (Array.isArray(style)) {
  35. style = vnode.data.style = toObject(style)
  36. }
  37. // clone the style for future updates,
  38. // in case the user mutates the style object in-place.
  39. if (needClone) {
  40. style = vnode.data.style = extend({}, style)
  41. }
  42. for (name in oldStyle) {
  43. if (!style[name]) {
  44. elm.style[normalize(name)] = ''
  45. }
  46. }
  47. for (name in style) {
  48. cur = style[name]
  49. if (cur !== oldStyle[name]) {
  50. // ie9 setting to null has no effect, must use empty string
  51. elm.style[normalize(name)] = cur || ''
  52. }
  53. }
  54. }
  55. export default {
  56. create: updateStyle,
  57. update: updateStyle
  58. }