@@ -25,7 +25,7 @@ export const OBJ_REF_SPLITTER = (s: string): [string, string] | undefined => {
25
25
} ;
26
26
27
27
export type Style = BlockClass | State | SubState ;
28
- export type StyleContainer = Block | BlockClass | State | null ;
28
+ export type StyleContainer = Block | BlockClass | State ;
29
29
30
30
/**
31
31
* Abstract class that serves as the base for all Styles. Contains basic
@@ -40,6 +40,8 @@ export abstract class BlockObject<StyleType extends Style, ContainerType extends
40
40
41
41
/** cache of resolveStyles() */
42
42
private _resolvedStyles : Set < Style > | undefined ;
43
+ /** cache for the block getter. */
44
+ private _block : Block | undefined ;
43
45
44
46
/**
45
47
* Save name, parent container, and create the PropertyContainer for this data object.
@@ -95,10 +97,20 @@ export abstract class BlockObject<StyleType extends Style, ContainerType extends
95
97
public abstract cssClass ( opts : OptionsReader ) : string ;
96
98
97
99
/**
98
- * Crawl up the container tree and return the base block object.
100
+ * traverse parents and return the base block object.
99
101
* @returns The base block in this container tree.
100
102
*/
101
- public abstract get block ( ) : Block ;
103
+ public get block ( ) : Block {
104
+ if ( this . _block !== undefined ) {
105
+ return this . _block ;
106
+ }
107
+ let p : StyleContainer = this . parent ;
108
+ if ( isBlock ( p ) ) {
109
+ return this . _block = p ;
110
+ } else {
111
+ return this . _block = p . block ;
112
+ }
113
+ }
102
114
103
115
/**
104
116
* Returns all the classes needed to represent this block object
@@ -178,6 +190,8 @@ export abstract class BlockObject<StyleType extends Style, ContainerType extends
178
190
return `${ this . asSource ( ) } => ${ this . cssClasses ( opts ) . map ( n => `.${ n } ` ) . join ( " " ) } ` ;
179
191
}
180
192
193
+ // TypeScript can't figure out that `this` is the `StyleType` so this private
194
+ // method casts it in a few places where it's needed.
181
195
private asStyle ( ) : StyleType {
182
196
return < StyleType > < any > this ;
183
197
}
@@ -778,10 +792,6 @@ export class BlockClass extends BlockObject<BlockClass, Block> {
778
792
super ( name , parent ) ;
779
793
}
780
794
781
- get block ( ) : Block {
782
- return this . parent ;
783
- }
784
-
785
795
get parent ( ) : Block {
786
796
return < Block > super . parent ;
787
797
}
@@ -1142,10 +1152,6 @@ export class State extends BlockObject<State, BlockClass> {
1142
1152
return this . _subStates && this . _subStates [ name ] || null ;
1143
1153
}
1144
1154
1145
- get block ( ) : Block {
1146
- return this . blockClass . block ;
1147
- }
1148
-
1149
1155
/**
1150
1156
* Retrieve the BlockClass that this state belongs to, if applicable.
1151
1157
* @returns The parent block class, or null.
@@ -1273,10 +1279,6 @@ export class SubState extends BlockObject<SubState, State> {
1273
1279
return undefined ;
1274
1280
}
1275
1281
1276
- get block ( ) : Block {
1277
- return this . _container . block ;
1278
- }
1279
-
1280
1282
localName ( ) : string {
1281
1283
return `${ this . _container . localName ( ) } -${ this . name } ` ;
1282
1284
}
0 commit comments