style_spec.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. var _ = require('../../../../src/util')
  2. var def = require('../../../../src/directives/style')
  3. var Vue = require('../../../../src/vue')
  4. function checkPrefixedProp (prop) {
  5. var el = document.createElement('div')
  6. var upper = prop.charAt(0).toUpperCase() + prop.slice(1)
  7. if (!(prop in el.style)) {
  8. var prefixes = ['Webkit', 'Moz', 'ms']
  9. var i = prefixes.length
  10. while (i--) {
  11. if ((prefixes[i] + upper) in el.style) {
  12. prop = prefixes[i] + upper
  13. }
  14. }
  15. }
  16. return prop
  17. }
  18. if (_.inBrowser) {
  19. describe('v-style', function () {
  20. var el, dir
  21. beforeEach(function () {
  22. el = document.createElement('div')
  23. dir = { el: el }
  24. _.extend(dir, def)
  25. })
  26. it('normal with arg', function () {
  27. dir.arg = 'color'
  28. dir.update('red')
  29. expect(el.style.color).toBe('red')
  30. })
  31. it('normal no arg', function () {
  32. dir.update('color:red;')
  33. expect(el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
  34. })
  35. it('!important', function () {
  36. dir.arg = 'color'
  37. dir.update('red !important;')
  38. expect(el.style.getPropertyPriority('color')).toBe('important')
  39. })
  40. it('camel case', function () {
  41. dir.arg = 'marginLeft'
  42. dir.update('30px')
  43. expect(el.style.marginLeft).toBe('30px')
  44. })
  45. it('remove on falsy value', function () {
  46. el.style.color = 'red'
  47. dir.arg = 'color'
  48. dir.update(null)
  49. expect(el.style.color).toBe('')
  50. })
  51. it('ignore unsupported property', function () {
  52. dir.arg = 'unsupported'
  53. dir.update('test')
  54. expect(el.style.unsupported).not.toBe('test')
  55. })
  56. it('auto prefixing', function () {
  57. var prop = checkPrefixedProp('transform')
  58. dir.arg = 'transform'
  59. var val = 'scale(0.5)'
  60. dir.update(val)
  61. expect(el.style[prop]).toBe(val)
  62. })
  63. it('update with object', function () {
  64. el.style.padding = '10px'
  65. dir.update({color: 'red', marginRight: '30px'})
  66. expect(el.style.getPropertyValue('color')).toBe('red')
  67. expect(el.style.getPropertyValue('margin-right')).toBe('30px')
  68. expect(el.style.getPropertyValue('padding')).toBe('10px')
  69. dir.update({color: 'blue', padding: null })
  70. expect(el.style.getPropertyValue('color')).toBe('blue')
  71. expect(el.style.getPropertyValue('margin-right')).toBeFalsy()
  72. expect(el.style.getPropertyValue('padding')).toBeFalsy()
  73. })
  74. it('update with object and auto prefix', function () {
  75. var prop = checkPrefixedProp('transform')
  76. var val = 'scale(0.5)'
  77. dir.update({transform: val})
  78. expect(el.style[prop]).toBe(val)
  79. })
  80. it('updates object deep', function (done) {
  81. el.setAttribute('v-style', 'divStyling')
  82. var vm = new Vue({
  83. el: el,
  84. data: {divStyling: { display: 'none'}}
  85. })
  86. expect(el.style.display).toBe('none')
  87. vm.divStyling.display = 'block'
  88. _.nextTick(function () {
  89. expect(el.style.display).toBe('block')
  90. done()
  91. })
  92. })
  93. })
  94. }