style.spec.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. import Vue from 'vue'
  2. function checkPrefixedProp (prop) {
  3. var el = document.createElement('div')
  4. var upper = prop.charAt(0).toUpperCase() + prop.slice(1)
  5. if (!(prop in el.style)) {
  6. var prefixes = ['Webkit', 'Moz', 'ms']
  7. var i = prefixes.length
  8. while (i--) {
  9. if ((prefixes[i] + upper) in el.style) {
  10. prop = prefixes[i] + upper
  11. }
  12. }
  13. }
  14. return prop
  15. }
  16. describe('Directive v-bind:style', () => {
  17. let vm
  18. beforeEach(() => {
  19. vm = new Vue({
  20. template: '<div :style="styles"></div>',
  21. data () {
  22. return {
  23. styles: {},
  24. fontSize: 16
  25. }
  26. }
  27. }).$mount()
  28. })
  29. it('string', done => {
  30. vm.styles = 'color:red;'
  31. waitForUpdate(() => {
  32. expect(vm.$el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
  33. }).then(done)
  34. })
  35. it('falsy number', done => {
  36. vm.styles = { opacity: 0 }
  37. waitForUpdate(() => {
  38. expect(vm.$el.style.opacity).toBe('0')
  39. }).then(done)
  40. })
  41. it('plain object', done => {
  42. vm.styles = { color: 'red' }
  43. waitForUpdate(() => {
  44. expect(vm.$el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
  45. }).then(done)
  46. })
  47. it('camelCase', done => {
  48. vm.styles = { marginRight: '10px' }
  49. waitForUpdate(() => {
  50. expect(vm.$el.style.marginRight).toBe('10px')
  51. }).then(done)
  52. })
  53. it('remove if falsy value', done => {
  54. vm.$el.style.color = 'red'
  55. waitForUpdate(() => {
  56. vm.styles = { color: null }
  57. }).then(() => {
  58. expect(vm.$el.style.color).toBe('')
  59. }).then(done)
  60. })
  61. it('ignore unsupported property', done => {
  62. vm.styles = { foo: 'bar' }
  63. waitForUpdate(() => {
  64. expect(vm.$el.style.foo).not.toBe('bar')
  65. }).then(done)
  66. })
  67. it('auto prefix', done => {
  68. const prop = checkPrefixedProp('transform')
  69. const val = 'scale(0.5)'
  70. vm.styles = { transform: val }
  71. waitForUpdate(() => {
  72. expect(vm.$el.style[prop]).toBe(val)
  73. }).then(done)
  74. })
  75. it('auto-prefixed style value as array', done => {
  76. vm.styles = { display: ['-webkit-box', '-ms-flexbox', 'flex'] }
  77. const testEl = document.createElement('div')
  78. vm.styles.display.forEach(value => {
  79. testEl.style.display = value
  80. })
  81. waitForUpdate(() => {
  82. expect(vm.$el.style.display).toBe(testEl.style.display)
  83. }).then(done)
  84. })
  85. it('!important', done => {
  86. vm.styles = { display: 'block !important' }
  87. waitForUpdate(() => {
  88. expect(vm.$el.style.getPropertyPriority('display')).toBe('important')
  89. }).then(done)
  90. })
  91. it('object with multiple entries', done => {
  92. vm.$el.style.color = 'red'
  93. vm.styles = {
  94. marginLeft: '10px',
  95. marginRight: '15px'
  96. }
  97. waitForUpdate(() => {
  98. expect(vm.$el.style.getPropertyValue('color')).toBe('red')
  99. expect(vm.$el.style.getPropertyValue('margin-left')).toBe('10px')
  100. expect(vm.$el.style.getPropertyValue('margin-right')).toBe('15px')
  101. vm.styles = {
  102. color: 'blue',
  103. padding: null
  104. }
  105. }).then(() => {
  106. expect(vm.$el.style.getPropertyValue('color')).toBe('blue')
  107. expect(vm.$el.style.getPropertyValue('padding')).toBeFalsy()
  108. expect(vm.$el.style.getPropertyValue('margin-left')).toBeFalsy()
  109. expect(vm.$el.style.getPropertyValue('margin-right')).toBeFalsy()
  110. // handle falsy value
  111. vm.styles = null
  112. }).then(() => {
  113. expect(vm.$el.style.getPropertyValue('color')).toBeFalsy()
  114. expect(vm.$el.style.getPropertyValue('padding')).toBeFalsy()
  115. expect(vm.$el.style.getPropertyValue('margin-left')).toBeFalsy()
  116. expect(vm.$el.style.getPropertyValue('margin-right')).toBeFalsy()
  117. }).then(done)
  118. })
  119. it('array of objects', done => {
  120. vm.$el.style.padding = '10px'
  121. vm.styles = [{ color: 'red' }, { marginRight: '20px' }]
  122. waitForUpdate(() => {
  123. expect(vm.$el.style.getPropertyValue('color')).toBe('red')
  124. expect(vm.$el.style.getPropertyValue('margin-right')).toBe('20px')
  125. expect(vm.$el.style.getPropertyValue('padding')).toBe('10px')
  126. vm.styles = [{ color: 'blue' }, { padding: null }]
  127. }).then(() => {
  128. expect(vm.$el.style.getPropertyValue('color')).toBe('blue')
  129. expect(vm.$el.style.getPropertyValue('margin-right')).toBeFalsy()
  130. expect(vm.$el.style.getPropertyValue('padding')).toBeFalsy()
  131. }).then(done)
  132. })
  133. it('updates objects deeply', done => {
  134. vm.styles = { display: 'none' }
  135. waitForUpdate(() => {
  136. expect(vm.$el.style.display).toBe('none')
  137. vm.styles.display = 'block'
  138. }).then(() => {
  139. expect(vm.$el.style.display).toBe('block')
  140. }).then(done)
  141. })
  142. it('background size with only one value', done => {
  143. vm.styles = { backgroundSize: '100%' }
  144. waitForUpdate(() => {
  145. expect(vm.$el.style.cssText.replace(/\s/g, '')).toMatch(/background-size:100%(auto)?;/)
  146. }).then(done)
  147. })
  148. it('should work with interpolation', done => {
  149. vm.styles = { fontSize: `${vm.fontSize}px` }
  150. waitForUpdate(() => {
  151. expect(vm.$el.style.fontSize).toBe('16px')
  152. }).then(done)
  153. })
  154. const supportCssVariable = () => {
  155. const el = document.createElement('div')
  156. el.style.setProperty('--color', 'red')
  157. return el.style.getPropertyValue('--color') === 'red'
  158. }
  159. if (supportCssVariable()) {
  160. it('CSS variables', done => {
  161. vm.styles = { '--color': 'red' }
  162. waitForUpdate(() => {
  163. expect(vm.$el.style.getPropertyValue('--color')).toBe('red')
  164. }).then(done)
  165. })
  166. }
  167. it('should merge static style with binding style', () => {
  168. const vm = new Vue({
  169. template: '<div style="background: url(https://vuejs.org/images/logo.png);color: blue" :style="test"></div>',
  170. data: {
  171. test: { color: 'red', fontSize: '12px' }
  172. }
  173. }).$mount()
  174. const style = vm.$el.style
  175. expect(style.getPropertyValue('background-image')).toMatch('https://vuejs.org/images/logo.png')
  176. expect(style.getPropertyValue('color')).toBe('red')
  177. expect(style.getPropertyValue('font-size')).toBe('12px')
  178. })
  179. it('should merge between parent and child', done => {
  180. const vm = new Vue({
  181. template: '<child style="text-align: left;margin-right:20px" :style="test"></child>',
  182. data: {
  183. test: { color: 'red', fontSize: '12px' }
  184. },
  185. components: {
  186. child: {
  187. template: '<div style="margin-right:10px;" :style="{marginLeft: marginLeft}"></div>',
  188. data: () => ({ marginLeft: '16px' })
  189. }
  190. }
  191. }).$mount()
  192. const style = vm.$el.style
  193. const child = vm.$children[0]
  194. const css = style.cssText.replace(/\s/g, '')
  195. expect(css).toContain('margin-right:20px;')
  196. expect(css).toContain('margin-left:16px;')
  197. expect(css).toContain('text-align:left;')
  198. expect(css).toContain('color:red;')
  199. expect(css).toContain('font-size:12px;')
  200. expect(style.color).toBe('red')
  201. expect(style.marginRight).toBe('20px')
  202. vm.test.color = 'blue'
  203. waitForUpdate(() => {
  204. expect(style.color).toBe('blue')
  205. child.marginLeft = '30px'
  206. }).then(() => {
  207. expect(style.marginLeft).toBe('30px')
  208. child.fontSize = '30px'
  209. }).then(() => {
  210. expect(style.fontSize).toBe('12px')
  211. }).then(done)
  212. })
  213. it('should not pass to child root element', () => {
  214. const vm = new Vue({
  215. template: '<child :style="test"></child>',
  216. data: {
  217. test: { color: 'red', fontSize: '12px' }
  218. },
  219. components: {
  220. child: {
  221. template: '<div><nested ref="nested" style="color: blue;text-align:left"></nested></div>',
  222. components: {
  223. nested: {
  224. template: '<div></div>'
  225. }
  226. }
  227. }
  228. }
  229. }).$mount()
  230. const style = vm.$el.style
  231. expect(style.color).toBe('red')
  232. expect(style.textAlign).toBe('')
  233. expect(style.fontSize).toBe('12px')
  234. expect(vm.$children[0].$refs.nested.$el.style.color).toBe('blue')
  235. })
  236. it('should merge between nested components', (done) => {
  237. const vm = new Vue({
  238. template: '<child :style="test"></child>',
  239. data: {
  240. test: { color: 'red', fontSize: '12px' }
  241. },
  242. components: {
  243. child: {
  244. template: '<nested style="color: blue;text-align:left"></nested>',
  245. components: {
  246. nested: {
  247. template: '<div style="margin-left: 12px;" :style="nestedStyle"></div>',
  248. data: () => ({ nestedStyle: { marginLeft: '30px' }})
  249. }
  250. }
  251. }
  252. }
  253. }).$mount()
  254. const style = vm.$el.style
  255. const child = vm.$children[0].$children[0]
  256. expect(style.color).toBe('red')
  257. expect(style.marginLeft).toBe('30px')
  258. expect(style.textAlign).toBe('left')
  259. expect(style.fontSize).toBe('12px')
  260. vm.test.color = 'yellow'
  261. waitForUpdate(() => {
  262. child.nestedStyle.marginLeft = '60px'
  263. }).then(() => {
  264. expect(style.marginLeft).toBe('60px')
  265. child.nestedStyle = {
  266. fontSize: '14px',
  267. marginLeft: '40px'
  268. }
  269. }).then(() => {
  270. expect(style.fontSize).toBe('12px')
  271. expect(style.marginLeft).toBe('40px')
  272. }).then(done)
  273. })
  274. it('should not merge for different adjacent elements', (done) => {
  275. const vm = new Vue({
  276. template:
  277. '<div>' +
  278. '<section style="color: blue" :style="style" v-if="!bool"></section>' +
  279. '<div></div>' +
  280. '<section style="margin-top: 12px" v-if="bool"></section>' +
  281. '</div>',
  282. data: {
  283. bool: false,
  284. style: {
  285. fontSize: '12px'
  286. }
  287. }
  288. }).$mount()
  289. const style = vm.$el.children[0].style
  290. expect(style.fontSize).toBe('12px')
  291. expect(style.color).toBe('blue')
  292. waitForUpdate(() => {
  293. vm.bool = true
  294. }).then(() => {
  295. expect(style.color).toBe('')
  296. expect(style.fontSize).toBe('')
  297. expect(style.marginTop).toBe('12px')
  298. }).then(done)
  299. })
  300. it('should not merge for v-if, v-else-if and v-else elements', (done) => {
  301. const vm = new Vue({
  302. template:
  303. '<div>' +
  304. '<section style="color: blue" :style="style" v-if="foo"></section>' +
  305. '<section style="margin-top: 12px" v-else-if="bar"></section>' +
  306. '<section style="margin-bottom: 24px" v-else></section>' +
  307. '<div></div>' +
  308. '</div>',
  309. data: {
  310. foo: true,
  311. bar: false,
  312. style: {
  313. fontSize: '12px'
  314. }
  315. }
  316. }).$mount()
  317. const style = vm.$el.children[0].style
  318. expect(style.fontSize).toBe('12px')
  319. expect(style.color).toBe('blue')
  320. waitForUpdate(() => {
  321. vm.foo = false
  322. }).then(() => {
  323. expect(style.color).toBe('')
  324. expect(style.fontSize).toBe('')
  325. expect(style.marginBottom).toBe('24px')
  326. vm.bar = true
  327. }).then(() => {
  328. expect(style.color).toBe('')
  329. expect(style.fontSize).toBe('')
  330. expect(style.marginBottom).toBe('')
  331. expect(style.marginTop).toBe('12px')
  332. }).then(done)
  333. })
  334. // #5318
  335. it('should work for elements passed down as a slot', done => {
  336. const vm = new Vue({
  337. template: `<test><div :style="style"/></test>`,
  338. data: {
  339. style: { color: 'red' }
  340. },
  341. components: {
  342. test: {
  343. template: `<div><slot/></div>`
  344. }
  345. }
  346. }).$mount()
  347. expect(vm.$el.children[0].style.color).toBe('red')
  348. vm.style.color = 'green'
  349. waitForUpdate(() => {
  350. expect(vm.$el.children[0].style.color).toBe('green')
  351. }).then(done)
  352. })
  353. })