1
1
import { isArray , isString , isObject , hyphenate } from './'
2
2
import { isNoUnitNumericStyleProp } from './domAttrConfig'
3
3
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 {
7
7
if ( isArray ( value ) ) {
8
8
const res : Record < string , string | number > = { }
9
9
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
+ )
12
14
if ( normalized ) {
13
15
for ( const key in normalized ) {
14
16
res [ key ] = normalized [ key ]
@@ -21,21 +23,21 @@ export function normalizeStyle(
21
23
}
22
24
}
23
25
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
+ } )
33
37
return ret
34
38
}
35
39
36
- export function stringifyStyle (
37
- styles : Record < string , string | number > | undefined
38
- ) : string {
40
+ export function stringifyStyle ( styles : NormalizedStyle | undefined ) : string {
39
41
let ret = ''
40
42
if ( ! styles ) {
41
43
return ret
0 commit comments