Skip to content

Support auto-prefixed style value as array (client/ssr) #5460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/platforms/web/runtime/modules/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@ const setProp = (el, name, val) => {
} else if (importantRE.test(val)) {
el.style.setProperty(name, val.replace(importantRE, ''), 'important')
} else {
el.style[normalize(name)] = val
const normalizedName = normalize(name)
if (Array.isArray(val)) {
// Support values array created by autoprefixer, e.g.
// {display: ["-webkit-box", "-ms-flexbox", "flex"]}
// Set them one by one, and the browser will only set those it can recognize
for (let i = 0, len = val.length; i < len; i++) {
el.style[normalizedName] = val[i]
}
} else {
el.style[normalizedName] = val
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/platforms/web/server/modules/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ function genStyleText (vnode: VNode): string {
let styleText = ''
const style = getStyle(vnode, false)
for (const key in style) {
styleText += `${hyphenate(key)}:${style[key]};`
const value = style[key]
const hyphenatedKey = hyphenate(key)
if (Array.isArray(value)) {
for (let i = 0, len = value.length; i < len; i++) {
styleText += `${hyphenatedKey}:${value[i]};`
}
} else {
styleText += `${hyphenatedKey}:${value};`
}
}
return styleText
}
Expand Down
16 changes: 16 additions & 0 deletions test/ssr/ssr-string.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,22 @@ describe('SSR: renderToString', () => {
})
})

it('auto-prefixed style value as array', done => {
renderVmWithOptions({
template: '<div :style="style"></div>',
data: {
style: {
display: ['-webkit-box', '-ms-flexbox', 'flex']
}
}
}, result => {
expect(result).toContain(
'<div data-server-rendered="true" style="display:-webkit-box;display:-ms-flexbox;display:flex;"></div>'
)
done()
})
})

it('custom component style', done => {
renderVmWithOptions({
template: '<section><comp :style="style"></comp></section>',
Expand Down
11 changes: 11 additions & 0 deletions test/unit/features/directives/style.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ describe('Directive v-bind:style', () => {
}).then(done)
})

it('auto-prefixed style value as array', done => {
vm.styles = { display: ['-webkit-box', '-ms-flexbox', 'flex'] }
const testEl = document.createElement('div')
vm.styles.display.forEach(value => {
testEl.style.display = value
})
waitForUpdate(() => {
expect(vm.$el.style.display).toBe(testEl.style.display)
}).then(done)
})

it('!important', done => {
vm.styles = { display: 'block !important' }
waitForUpdate(() => {
Expand Down