Skip to content

Commit 8df6bc0

Browse files
committed
refactor: reuse parseStringStyle across compiler and runtime
1 parent 2d9f136 commit 8df6bc0

File tree

3 files changed

+28
-33
lines changed

3 files changed

+28
-33
lines changed

packages/compiler-dom/src/transforms/transformStyle.ts

+5-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
SimpleExpressionNode,
66
SourceLocation
77
} from '@vue/compiler-core'
8+
import { parseStringStyle } from '@vue/shared'
89

910
// Parse inline CSS strings for static style attributes into an object.
1011
// This is a NodeTransform since it works on the static `style` attribute and
@@ -30,19 +31,10 @@ export const transformStyle: NodeTransform = (node, context) => {
3031
}
3132
}
3233

33-
const listDelimiterRE = /;(?![^(]*\))/g
34-
const propertyDelimiterRE = /:(.+)/
35-
36-
function parseInlineCSS(
34+
const parseInlineCSS = (
3735
cssText: string,
3836
loc: SourceLocation
39-
): SimpleExpressionNode {
40-
const res: Record<string, string> = {}
41-
cssText.split(listDelimiterRE).forEach(item => {
42-
if (item) {
43-
const tmp = item.split(propertyDelimiterRE)
44-
tmp.length > 1 && (res[tmp[0].trim()] = tmp[1].trim())
45-
}
46-
})
47-
return createSimpleExpression(JSON.stringify(res), false, loc, true)
37+
): SimpleExpressionNode => {
38+
const normalized = parseStringStyle(cssText)
39+
return createSimpleExpression(JSON.stringify(normalized), false, loc, true)
4840
}

packages/runtime-core/__tests__/vnode.spec.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@ describe('vnode', () => {
258258
}
259259
})
260260
})
261-
test('style', () => {
261+
262+
test('style w/ strings', () => {
262263
let props1: Data = {
263264
style: 'width:100px;right:10;top:10'
264265
}
@@ -281,8 +282,8 @@ describe('vnode', () => {
281282
width: '300px',
282283
height: '300px',
283284
fontSize: 30,
284-
right: 10,
285-
top: 10
285+
right: '10',
286+
top: '10'
286287
}
287288
})
288289
})

packages/shared/src/normalizeProp.ts

+19-17
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { isArray, isString, isObject, hyphenate } from './'
22
import { isNoUnitNumericStyleProp } from './domAttrConfig'
33

4-
export function normalizeStyle(
5-
value: unknown
6-
): Record<string, string | number> | undefined {
4+
export type NormalizedStyle = Record<string, string | number>
5+
6+
export function normalizeStyle(value: unknown): NormalizedStyle | undefined {
77
if (isArray(value)) {
88
const res: Record<string, string | number> = {}
99
for (let i = 0; i < value.length; i++) {
10-
const styles = isString(value[i]) ? strStyleToObj(value[i]) : value[i]
11-
const normalized = normalizeStyle(styles)
10+
const item = value[i]
11+
const normalized = normalizeStyle(
12+
isString(item) ? parseStringStyle(item) : item
13+
)
1214
if (normalized) {
1315
for (const key in normalized) {
1416
res[key] = normalized[key]
@@ -21,21 +23,21 @@ export function normalizeStyle(
2123
}
2224
}
2325

24-
function strStyleToObj(style: string) {
25-
const ret: Record<string, string | number> = {}
26-
style
27-
.replace(/\s*/g, '')
28-
.split(';')
29-
.forEach((item: string) => {
30-
const [key, val] = item.split(':')
31-
ret[key] = isNaN(Number(val)) ? val : Number(val)
32-
})
26+
const listDelimiterRE = /;(?![^(]*\))/g
27+
const propertyDelimiterRE = /:(.+)/
28+
29+
export function parseStringStyle(cssText: string): NormalizedStyle {
30+
const ret: NormalizedStyle = {}
31+
cssText.split(listDelimiterRE).forEach(item => {
32+
if (item) {
33+
const tmp = item.split(propertyDelimiterRE)
34+
tmp.length > 1 && (ret[tmp[0].trim()] = tmp[1].trim())
35+
}
36+
})
3337
return ret
3438
}
3539

36-
export function stringifyStyle(
37-
styles: Record<string, string | number> | undefined
38-
): string {
40+
export function stringifyStyle(styles: NormalizedStyle | undefined): string {
3941
let ret = ''
4042
if (!styles) {
4143
return ret

0 commit comments

Comments
 (0)