Skip to content
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

[RFC] fix #4055, generate style on custom component, Don't Merge #4076

Merged
merged 4 commits into from
Nov 4, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 34 additions & 15 deletions src/platforms/web/server/modules/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,43 @@

import { hyphenate, toObject } from 'shared/util'

export default function renderStyle (node: VNodeWithData): ?string {
function concatStyleString (former: string, latter: string) {
if (former === '' || latter === '' || former.charAt(former.length - 1) === ';') {
return former + latter
}
return former + ';' + latter
}

function generateStyleText (node) {
const staticStyle = node.data.attrs && node.data.attrs.style
if (node.data.style || staticStyle) {
let styles = node.data.style
let res = ''
if (styles) {
if (typeof styles === 'string') {
res += styles
} else {
if (Array.isArray(styles)) {
styles = toObject(styles)
}
for (const key in styles) {
res += `${hyphenate(key)}:${styles[key]};`
}
res += staticStyle || ''
let styles = node.data.style
const parentStyle = node.parent ? generateStyleText(node.parent) : ''

if (!styles && !staticStyle) {
return parentStyle
}

let dynamicStyle = ''
if (styles) {
if (typeof styles === 'string') {
dynamicStyle += styles
} else {
if (Array.isArray(styles)) {
styles = toObject(styles)
}
for (const key in styles) {
dynamicStyle += `${hyphenate(key)}:${styles[key]};`
}
}
}

dynamicStyle = concatStyleString(parentStyle, dynamicStyle)
return concatStyleString(dynamicStyle, staticStyle || '')
}

export default function renderStyle (node: VNodeWithData): ?string {
const res = generateStyleText(node)
if (res) {
return ` style=${JSON.stringify(res)}`
}
}
11 changes: 11 additions & 0 deletions src/server/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,19 @@ export function createRenderFunction (
}
}

function hasAncestorData (node: VNode) {
const parentNode = node.parent
return parentNode && (parentNode.data || hasAncestorData(parentNode))
}

function renderStartingTag (node: VNode) {
let markup = `<${node.tag}`

// construct synthetic data for module processing
// because modules like style also produce code by parent VNode data
if (!node.data && hasAncestorData(node)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a synthetic data property if VNode's ancestor has data. I don't this is good enough...

node.data = {}
}
if (node.data) {
// check directives
const dirs = node.data.directives
Expand Down
81 changes: 81 additions & 0 deletions test/ssr/ssr-string.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,87 @@ describe('SSR: renderToString', () => {
})
})

it('custom component style', done => {
renderVmWithOptions({
template: '<section><comp :style="style"></comp></section>',
data: {
style: 'color:red'
},
components: {
comp: {
template: '<div></div>'
}
}
}, result => {
expect(result).toContain(
'<section server-rendered="true"><div style="color:red"></div></section>'
)
done()
})
})

it('nested custom component style', done => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yyx990803 By "multi-nested components", do you mean this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, the generateStyleText call is recursive, nice.

renderVmWithOptions({
template: '<comp :style="style"></comp>',
data: {
style: 'color:red'
},
components: {
comp: {
template: '<nested style="font-size:520rem"></nested>',
components: {
nested: {
template: '<div></div>'
}
}
}
}
}, result => {
expect(result).toContain(
'<div server-rendered="true" style="color:red;font-size:520rem"></div>'
)
done()
})
})

it('component style not passed to child', done => {
renderVmWithOptions({
template: '<comp :style="style"></comp>',
data: {
style: 'color:red'
},
components: {
comp: {
template: '<div><div></div></div>'
}
}
}, result => {
expect(result).toContain(
'<div server-rendered="true" style="color:red"><div></div></div>'
)
done()
})
})

it('component style not passed to slot', done => {
renderVmWithOptions({
template: '<comp :style="style"><span style="color:black"></span></comp>',
data: {
style: 'color:red'
},
components: {
comp: {
template: '<div><slot></slot></div>'
}
}
}, result => {
expect(result).toContain(
'<div server-rendered="true" style="color:red"><span style="color:black"></span></div>'
)
done()
})
})

it('text interpolation', done => {
renderVmWithOptions({
template: '<div>{{ foo }} side {{ bar }}</div>',
Expand Down