Skip to content

Commit

Permalink
Support auto-prefixed style value as array (client/ssr) (#5460)
Browse files Browse the repository at this point in the history
* support auto-prefixed style value as array (client/ssr)

* adjust test case
  • Loading branch information
fnlctrl authored and yyx990803 committed Apr 17, 2017
1 parent 5a617cc commit 38810d8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
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

0 comments on commit 38810d8

Please sign in to comment.