2
0

style.spec.ts 11 KB

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