3
3
* @license MIT
4
4
* @see https://github.com/alexsasharegan/vue-functional-data-merge
5
5
*/
6
-
6
+ /* eslint-disable max-statements */
7
7
import { VNodeData } from 'vue'
8
+ import { camelize } from './helpers'
9
+
10
+ const pattern = {
11
+ styleList : / ; (? ! [ ^ ( ] * \) ) / g,
12
+ styleProp : / : ( .* ) / ,
13
+ } as const
14
+
15
+ function parseStyle ( style : string ) {
16
+ const styleMap : Dictionary < any > = { }
17
+
18
+ for ( const s of style . split ( pattern . styleList ) ) {
19
+ let [ key , val ] = s . split ( pattern . styleProp )
20
+ key = key . trim ( )
21
+ if ( ! key ) {
22
+ continue
23
+ }
24
+ // May be undefined if the `key: value` pair is incomplete.
25
+ if ( typeof val === 'string' ) {
26
+ val = val . trim ( )
27
+ }
28
+ styleMap [ camelize ( key ) ] = val
29
+ }
30
+
31
+ return styleMap
32
+ }
8
33
9
34
/**
10
35
* Intelligently merges data for createElement.
@@ -13,7 +38,7 @@ import { VNodeData } from 'vue'
13
38
*/
14
39
export default function mergeData ( ...vNodeData : VNodeData [ ] ) : VNodeData
15
40
export default function mergeData ( ) : VNodeData {
16
- const mergeTarget : VNodeData & { [ key : string ] : any } = { }
41
+ const mergeTarget : VNodeData & Dictionary < any > = { }
17
42
let i : number = arguments . length
18
43
let prop : string
19
44
let event : string
@@ -31,6 +56,23 @@ export default function mergeData (): VNodeData {
31
56
if ( ! Array . isArray ( mergeTarget [ prop ] ) ) {
32
57
mergeTarget [ prop ] = [ ]
33
58
}
59
+
60
+ if ( prop === 'style' ) {
61
+ let style : any [ ]
62
+ if ( Array . isArray ( arguments [ i ] . style ) ) {
63
+ style = arguments [ i ] . style
64
+ } else {
65
+ style = [ arguments [ i ] . style ]
66
+ }
67
+ for ( let j = 0 ; j < style . length ; j ++ ) {
68
+ const s = style [ j ]
69
+ if ( typeof s === 'string' ) {
70
+ style [ j ] = parseStyle ( s )
71
+ }
72
+ }
73
+ arguments [ i ] . style = style
74
+ }
75
+
34
76
// Repackaging in an array allows Vue runtime
35
77
// to merge class/style bindings regardless of type.
36
78
mergeTarget [ prop ] = mergeTarget [ prop ] . concat ( arguments [ i ] [ prop ] )
0 commit comments