style_spec.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. var _ = require('src/util')
  2. var def = require('src/directives/internal/style')
  3. var Vue = require('src')
  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. describe(':style', function () {
  19. var el, dir
  20. beforeEach(function () {
  21. el = document.createElement('div')
  22. dir = { el: el }
  23. _.extend(dir, def)
  24. })
  25. it('plain CSS string', function () {
  26. dir.update('color:red;')
  27. expect(el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
  28. })
  29. it('!important', function () {
  30. dir.update({
  31. color: 'red !important;'
  32. })
  33. expect(el.style.getPropertyPriority('color')).toBe('important')
  34. })
  35. it('camel case', function () {
  36. dir.update({
  37. marginLeft: '30px'
  38. })
  39. expect(el.style.marginLeft).toBe('30px')
  40. })
  41. it('remove on falsy value', function () {
  42. el.style.color = 'red'
  43. dir.update({
  44. color: null
  45. })
  46. expect(el.style.color).toBe('')
  47. })
  48. it('ignore unsupported property', function () {
  49. dir.update({
  50. unsupported: 'test'
  51. })
  52. expect(el.style.unsupported).not.toBe('test')
  53. })
  54. it('auto prefixing', function () {
  55. var prop = checkPrefixedProp('transform')
  56. var val = 'scale(0.5)'
  57. dir.update({
  58. transform: val
  59. })
  60. expect(el.style[prop]).toBe(val)
  61. })
  62. it('object with multiple fields', function () {
  63. el.style.padding = '10px'
  64. dir.update({
  65. color: 'red',
  66. marginRight: '30px'
  67. })
  68. expect(el.style.getPropertyValue('color')).toBe('red')
  69. expect(el.style.getPropertyValue('margin-right')).toBe('30px')
  70. expect(el.style.getPropertyValue('padding')).toBe('10px')
  71. dir.update({
  72. color: 'blue',
  73. padding: null
  74. })
  75. expect(el.style.getPropertyValue('color')).toBe('blue')
  76. expect(el.style.getPropertyValue('margin-right')).toBeFalsy()
  77. expect(el.style.getPropertyValue('padding')).toBeFalsy()
  78. // handle falsy value
  79. dir.update(null)
  80. expect(el.style.getPropertyValue('color')).toBeFalsy()
  81. expect(el.style.getPropertyValue('margin-right')).toBeFalsy()
  82. expect(el.style.getPropertyValue('padding')).toBeFalsy()
  83. })
  84. it('array of objects', function () {
  85. el.style.padding = '10px'
  86. dir.update([{color: 'red'}, {marginRight: '30px'}])
  87. expect(el.style.getPropertyValue('color')).toBe('red')
  88. expect(el.style.getPropertyValue('margin-right')).toBe('30px')
  89. expect(el.style.getPropertyValue('padding')).toBe('10px')
  90. dir.update([{color: 'blue'}, {padding: null}])
  91. expect(el.style.getPropertyValue('color')).toBe('blue')
  92. expect(el.style.getPropertyValue('margin-right')).toBeFalsy()
  93. expect(el.style.getPropertyValue('padding')).toBeFalsy()
  94. })
  95. it('updates object deep', function (done) {
  96. el.setAttribute(':style', 'divStyling')
  97. var vm = new Vue({
  98. el: el,
  99. data: {divStyling: { display: 'none' }}
  100. })
  101. expect(el.style.display).toBe('none')
  102. vm.divStyling.display = 'block'
  103. _.nextTick(function () {
  104. expect(el.style.display).toBe('block')
  105. done()
  106. })
  107. })
  108. // #2654
  109. it('background size with only one value', function () {
  110. dir.update({ backgroundSize: '100%' })
  111. expect(el.style.cssText.replace(/\s/g, '')).toMatch(/background-size:100%(auto)?;/)
  112. })
  113. })