-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
Commit
Ignores values that are not string or numbers, and append px as default unit to appropriate properties. There will still be certain cases where the user simply provides an invalid string value to a property which will be too costly to detect (it's possible by using the `cssstyle` package, but very heavy). But in such cases the user would already notice the style is not working on the client, so it's not really worth it for the perf implications. fix #9231
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* @flow */ | ||
|
||
import { escape } from '../util' | ||
import { escape, noUnitNumericStyleProps } from '../util' | ||
import { hyphenate } from 'shared/util' | ||
import { getStyle } from 'web/util/style' | ||
|
||
|
@@ -11,15 +11,34 @@ export function genStyle (style: Object): string { | |
const hyphenatedKey = hyphenate(key) | ||
if (Array.isArray(value)) { | ||
for (let i = 0, len = value.length; i < len; i++) { | ||
styleText += `${hyphenatedKey}:${value[i]};` | ||
styleText += normalizeValue(hyphenatedKey, value[i]) | ||
} | ||
} else { | ||
styleText += `${hyphenatedKey}:${value};` | ||
styleText += normalizeValue(hyphenatedKey, value) | ||
} | ||
} | ||
return styleText | ||
} | ||
|
||
function normalizeValue(key: string, value: any): string { | ||
if (typeof value === 'string') { | ||
return `${key}:${value};` | ||
} else if (typeof value === 'number') { | ||
// Handle numeric values. | ||
// Turns out all evergreen browsers plus IE11 already support setting plain | ||
// numbers on the style object and will automatically convert it to px if | ||
// applicable, so we should support that on the server too. | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
yyx990803
Author
Member
|
||
if (noUnitNumericStyleProps[key]) { | ||
return `${key}:${value};` | ||
} else { | ||
return `${key}:${value}px;` | ||
} | ||
} else { | ||
// invalid values | ||
return `` | ||
} | ||
} | ||
|
||
export default function renderStyle (vnode: VNodeWithData): ?string { | ||
const styleText = genStyle(getStyle(vnode, false)) | ||
if (styleText !== '') { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,4 +69,3 @@ export function getStyle (vnode: VNodeWithData, checkChild: boolean): Object { | |
} | ||
return res | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1499,6 +1499,47 @@ describe('SSR: renderToString', () => { | |
done() | ||
}) | ||
}) | ||
|
||
it('invalid style value', done => { | ||
renderVmWithOptions({ | ||
template: '<div :style="style"><p :style="style2"/></div>', | ||
data: { | ||
// all invalid, should not even have "style" attribute | ||
style: { | ||
opacity: {}, | ||
color: null | ||
}, | ||
// mix of valid and invalid | ||
style2: { | ||
opacity: 0, | ||
color: null | ||
} | ||
} | ||
}, result => { | ||
expect(result).toContain( | ||
'<div data-server-rendered="true"><p style="opacity:0;"></p></div>' | ||
) | ||
done() | ||
}) | ||
}) | ||
|
||
it('numeric style value', done => { | ||
renderVmWithOptions({ | ||
template: '<div :style="style"></div>', | ||
data: { | ||
style: { | ||
opacity: 0, // opacity should display as-is | ||
top: 0, // top and margin should be converted to '0px' | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
TheAlexLichter
Contributor
|
||
marginTop: 10 | ||
} | ||
} | ||
}, result => { | ||
expect(result).toContain( | ||
'<div data-server-rendered="true" style="opacity:0;top:0px;margin-top:10px;"></div>' | ||
) | ||
done() | ||
}) | ||
}) | ||
}) | ||
|
||
function renderVmWithOptions (options, cb) { | ||
|
It doesn't seems to be so...?