style_spec.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. dir.update({color: 'red', marginRight: '30px'})
  65. expect(el.style.getPropertyValue('color')).toBe('red')
  66. expect(el.style.getPropertyValue('margin-right')).toBe('30px')
  67. })
  68. it('update with object and auto prefix', function () {
  69. var prop = checkPrefixedProp('transform')
  70. var val = 'scale(0.5)';
  71. dir.update({transform: val})
  72. expect(el.style[prop]).toBe(val)
  73. })
  74. it('updates object deep', function (done) {
  75. el.setAttribute('v-style', 'divStyling')
  76. var vm = new Vue({
  77. el: el,
  78. data: {divStyling: { display: 'none'}}
  79. })
  80. expect(el.style.display).toBe('none')
  81. vm.divStyling.display = 'block'
  82. _.nextTick(function () {
  83. expect(el.style.display).toBe('block')
  84. done()
  85. })
  86. })
  87. })
  88. }