Skip to content

Commit

Permalink
fix(runtime-dom/style): normalize string when merging styles (#1127)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzz-Z committed May 6, 2020
1 parent 4d9b651 commit 2d9f136
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
28 changes: 28 additions & 0 deletions packages/runtime-core/__tests__/vnode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,34 @@ describe('vnode', () => {
}
})
})
test('style', () => {
let props1: Data = {
style: 'width:100px;right:10;top:10'
}
let props2: Data = {
style: [
{
color: 'blue',
width: '200px'
},
{
width: '300px',
height: '300px',
fontSize: 30
}
]
}
expect(mergeProps(props1, props2)).toMatchObject({
style: {
color: 'blue',
width: '300px',
height: '300px',
fontSize: 30,
right: 10,
top: 10
}
})
})

test('handlers', () => {
let clickHandler1 = function() {}
Expand Down
15 changes: 14 additions & 1 deletion packages/shared/src/normalizeProp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export function normalizeStyle(
if (isArray(value)) {
const res: Record<string, string | number> = {}
for (let i = 0; i < value.length; i++) {
const normalized = normalizeStyle(value[i])
const styles = isString(value[i]) ? strStyleToObj(value[i]) : value[i]
const normalized = normalizeStyle(styles)
if (normalized) {
for (const key in normalized) {
res[key] = normalized[key]
Expand All @@ -20,6 +21,18 @@ export function normalizeStyle(
}
}

function strStyleToObj(style: string) {
const ret: Record<string, string | number> = {}
style
.replace(/\s*/g, '')

This comment has been minimized.

Copy link
@tbl0605

tbl0605 May 6, 2020

Hi,
I'm maybe wrong, but I'm not sure this will work correctly when there are spaces between multiple values of same css property (because all spaces are removed everywhere):

style: 'padding: 10 20'

And why not use \s+ instead of \s* ?

This comment has been minimized.

Copy link
@yyx990803

yyx990803 May 6, 2020

Member

see 8df6bc0

.split(';')
.forEach((item: string) => {
const [key, val] = item.split(':')
ret[key] = isNaN(Number(val)) ? val : Number(val)
})
return ret
}

export function stringifyStyle(
styles: Record<string, string | number> | undefined
): string {
Expand Down

0 comments on commit 2d9f136

Please sign in to comment.