1
- import { Attributes , DOM , IFiber } from " ./type"
2
- import { isStr , LANE } from " ./reconcile"
1
+ import { Attributes , DOM , IFiber } from ' ./type'
2
+ import { isStr , LANE } from ' ./reconcile'
3
3
4
- const hasOwnProperty = Object . prototype . hasOwnProperty ;
5
-
6
- const defaultObj = { } as const ;
4
+ const defaultObj = { } as const
7
5
8
6
const jointIter = < P extends Attributes > (
9
7
aProps = defaultObj as P ,
10
8
bProps = defaultObj as P ,
11
9
callback : ( name : string , a : any , b : any ) => void
12
10
) => {
13
- for ( const name in aProps ) {
14
- if ( hasOwnProperty . call ( aProps , name ) ) {
15
- callback ( name , aProps [ name ] , bProps [ name ] ) ;
16
- }
17
- }
18
- for ( const name in bProps ) {
19
- if ( hasOwnProperty . call ( bProps , name ) && ! hasOwnProperty . call ( aProps , name ) ) {
20
- callback ( name , undefined , bProps [ name ] ) ;
21
- }
22
- }
11
+ if ( ! aProps || ! bProps ) return
12
+ Object . keys ( aProps ) . forEach ( k => callback ( k , aProps [ k ] , bProps [ k ] ) )
13
+ Object . keys ( bProps ) . forEach ( k => callback ( k , undefined , bProps [ k ] ) )
23
14
}
24
15
25
16
export const updateElement = < P extends Attributes > (
@@ -28,19 +19,19 @@ export const updateElement = <P extends Attributes>(
28
19
bProps : P
29
20
) => {
30
21
jointIter ( aProps , bProps , ( name , a , b ) => {
31
- if ( a === b || name === " children" ) {
32
- } else if ( name === " style" && ! isStr ( b ) ) {
22
+ if ( a === b || name === ' children' ) {
23
+ } else if ( name === ' style' && ! isStr ( b ) ) {
33
24
jointIter ( a , b , ( styleKey , aStyle , bStyle ) => {
34
25
if ( aStyle !== bStyle ) {
35
- ; ( dom as any ) [ name ] [ styleKey ] = bStyle || ""
26
+ ; ( dom as any ) [ name ] [ styleKey ] = bStyle || ''
36
27
}
37
28
} )
38
- } else if ( name [ 0 ] === "o" && name [ 1 ] === "n" ) {
29
+ } else if ( name [ 0 ] === 'o' && name [ 1 ] === 'n' ) {
39
30
name = name . slice ( 2 ) . toLowerCase ( ) as Extract < keyof P , string >
40
31
if ( a ) dom . removeEventListener ( name , a )
41
32
dom . addEventListener ( name , b )
42
33
} else if ( name in dom && ! ( dom instanceof SVGElement ) ) {
43
- ; ( dom as any ) [ name ] = b || ''
34
+ ; ( dom as any ) [ name ] = b || ''
44
35
} else if ( b == null || b === false ) {
45
36
dom . removeAttribute ( name )
46
37
} else {
@@ -51,14 +42,14 @@ export const updateElement = <P extends Attributes>(
51
42
52
43
export const createElement = < P = Attributes > ( fiber : IFiber ) => {
53
44
const dom =
54
- fiber . type === ""
55
- ? document . createTextNode ( "" )
45
+ fiber . type === ''
46
+ ? document . createTextNode ( '' )
56
47
: fiber . lane & LANE . SVG
57
- ? document . createElementNS (
58
- " http://www.w3.org/2000/svg" ,
48
+ ? document . createElementNS (
49
+ ' http://www.w3.org/2000/svg' ,
59
50
fiber . type as string
60
51
)
61
- : document . createElement ( fiber . type as string )
52
+ : document . createElement ( fiber . type as string )
62
53
updateElement ( dom as DOM , { } as P , fiber . props as P )
63
54
return dom
64
55
}
0 commit comments