Skip to content

Commit 38810d8

Browse files
fnlctrlyyx990803
authored andcommitted
Support auto-prefixed style value as array (client/ssr) (vuejs#5460)
* support auto-prefixed style value as array (client/ssr) * adjust test case
1 parent 5a617cc commit 38810d8

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

src/platforms/web/runtime/modules/style.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,17 @@ const setProp = (el, name, val) => {
1212
} else if (importantRE.test(val)) {
1313
el.style.setProperty(name, val.replace(importantRE, ''), 'important')
1414
} else {
15-
el.style[normalize(name)] = val
15+
const normalizedName = normalize(name)
16+
if (Array.isArray(val)) {
17+
// Support values array created by autoprefixer, e.g.
18+
// {display: ["-webkit-box", "-ms-flexbox", "flex"]}
19+
// Set them one by one, and the browser will only set those it can recognize
20+
for (let i = 0, len = val.length; i < len; i++) {
21+
el.style[normalizedName] = val[i]
22+
}
23+
} else {
24+
el.style[normalizedName] = val
25+
}
1626
}
1727
}
1828

src/platforms/web/server/modules/style.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ function genStyleText (vnode: VNode): string {
88
let styleText = ''
99
const style = getStyle(vnode, false)
1010
for (const key in style) {
11-
styleText += `${hyphenate(key)}:${style[key]};`
11+
const value = style[key]
12+
const hyphenatedKey = hyphenate(key)
13+
if (Array.isArray(value)) {
14+
for (let i = 0, len = value.length; i < len; i++) {
15+
styleText += `${hyphenatedKey}:${value[i]};`
16+
}
17+
} else {
18+
styleText += `${hyphenatedKey}:${value};`
19+
}
1220
}
1321
return styleText
1422
}

test/ssr/ssr-string.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,22 @@ describe('SSR: renderToString', () => {
123123
})
124124
})
125125

126+
it('auto-prefixed style value as array', done => {
127+
renderVmWithOptions({
128+
template: '<div :style="style"></div>',
129+
data: {
130+
style: {
131+
display: ['-webkit-box', '-ms-flexbox', 'flex']
132+
}
133+
}
134+
}, result => {
135+
expect(result).toContain(
136+
'<div data-server-rendered="true" style="display:-webkit-box;display:-ms-flexbox;display:flex;"></div>'
137+
)
138+
done()
139+
})
140+
})
141+
126142
it('custom component style', done => {
127143
renderVmWithOptions({
128144
template: '<section><comp :style="style"></comp></section>',

test/unit/features/directives/style.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ describe('Directive v-bind:style', () => {
8383
}).then(done)
8484
})
8585

86+
it('auto-prefixed style value as array', done => {
87+
vm.styles = { display: ['-webkit-box', '-ms-flexbox', 'flex'] }
88+
const testEl = document.createElement('div')
89+
vm.styles.display.forEach(value => {
90+
testEl.style.display = value
91+
})
92+
waitForUpdate(() => {
93+
expect(vm.$el.style.display).toBe(testEl.style.display)
94+
}).then(done)
95+
})
96+
8697
it('!important', done => {
8798
vm.styles = { display: 'block !important' }
8899
waitForUpdate(() => {

0 commit comments

Comments
 (0)