@@ -42,6 +42,8 @@ export abstract class BlockObject<StyleType extends Style, ContainerType extends
42
42
private _resolvedStyles : Set < Style > | undefined ;
43
43
/** cache for the block getter. */
44
44
private _block : Block | undefined ;
45
+ /** cache for the block getter. */
46
+ private _base : StyleType | null ;
45
47
46
48
/**
47
49
* Save name, parent container, and create the PropertyContainer for this data object.
@@ -67,10 +69,36 @@ export abstract class BlockObject<StyleType extends Style, ContainerType extends
67
69
}
68
70
69
71
/**
70
- * Get the inherited block object for this block object of the same name and type.
71
- * @returns The base inherited block object .
72
+ * Given a parent that is a base class of this style, retrieve this style's
73
+ * base style from it, if it exists. This method does not traverse into base styles .
72
74
*/
73
- public abstract get base ( ) : StyleType | undefined ;
75
+ protected abstract getBaseFromBaseParent ( baseParent : ContainerType ) : StyleType | undefined ;
76
+
77
+ /**
78
+ * Get the style that this style inherits from, if any.
79
+ *
80
+ * This walks down the declared styles of the parent's inheritance chain,
81
+ * and attempts to find a matching directly declared style on each.
82
+ *
83
+ * The result is cached because it never changes and is decidable as soon
84
+ * as the style is instantiated.
85
+ */
86
+ public get base ( ) : StyleType | undefined {
87
+ if ( this . _base !== undefined ) {
88
+ return this . _base || undefined ;
89
+ }
90
+ let baseParent : ContainerType | undefined = < ContainerType | undefined > this . parent . base ;
91
+ while ( baseParent ) {
92
+ let cls = this . getBaseFromBaseParent ( baseParent ) ;
93
+ if ( cls ) {
94
+ this . _base = cls ;
95
+ return cls ;
96
+ }
97
+ baseParent = < ContainerType | undefined > baseParent . base ;
98
+ }
99
+ this . _base = null ;
100
+ return undefined ;
101
+ }
74
102
75
103
/**
76
104
* Return the local identifier for this `Style`.
@@ -309,6 +337,9 @@ export class Block
309
337
return new Array ( ...this . _dependencies ) ;
310
338
}
311
339
340
+ /**
341
+ * Get the block that this block inherits from.
342
+ */
312
343
get base ( ) : Block | undefined {
313
344
return this . _base ;
314
345
}
@@ -339,7 +370,7 @@ export class Block
339
370
}
340
371
341
372
/**
342
- * Validate that this block implements all foreign selectors from blocks it impelemnts .
373
+ * Validate that this block implements all foreign selectors from blocks it implements .
343
374
* @param b The block to check implementation against.
344
375
* @returns The Styles from b that are missing in the block.
345
376
*/
@@ -779,7 +810,6 @@ export type StyleParent = Block | BlockClass | State | undefined;
779
810
* Represents a Class present in the Block.
780
811
*/
781
812
export class BlockClass extends BlockObject < BlockClass , Block > {
782
- private _base : BlockClass | null | undefined ;
783
813
private _sourceAttribute : Attribute ;
784
814
private _states : StateMap = { } ;
785
815
@@ -792,29 +822,12 @@ export class BlockClass extends BlockObject<BlockClass, Block> {
792
822
super ( name , parent ) ;
793
823
}
794
824
795
- get parent ( ) : Block {
796
- return < Block > super . parent ;
797
- }
798
-
799
825
get isRoot ( ) : boolean {
800
826
return this . name === "root" ;
801
827
}
802
828
803
- get base ( ) : BlockClass | undefined {
804
- if ( this . _base !== undefined ) {
805
- return this . _base || undefined ;
806
- }
807
- let base = this . block . base ;
808
- while ( base ) {
809
- let cls = base . getClass ( this . name ) ;
810
- if ( cls ) {
811
- this . _base = cls ;
812
- return cls ;
813
- }
814
- base = base . base ;
815
- }
816
- this . _base = null ;
817
- return undefined ;
829
+ protected getBaseFromBaseParent ( baseParent : Block ) : BlockClass | undefined {
830
+ return baseParent . getClass ( this . name ) ;
818
831
}
819
832
820
833
localName ( ) : string {
@@ -1073,7 +1086,6 @@ export class State extends BlockObject<State, BlockClass> {
1073
1086
isGlobal = false ;
1074
1087
1075
1088
private _hasSubStates : boolean ;
1076
- private _base : State | null | undefined ;
1077
1089
private _subStates : undefined | ObjectDictionary < SubState > ;
1078
1090
private _sourceAttributes : Attr [ ] ;
1079
1091
@@ -1226,24 +1238,8 @@ export class State extends BlockObject<State, BlockClass> {
1226
1238
}
1227
1239
}
1228
1240
1229
- /**
1230
- * Get the state that this state inherits from.
1231
- */
1232
- get base ( ) : State | undefined {
1233
- if ( this . _base !== undefined ) {
1234
- return this . _base || undefined ;
1235
- }
1236
- let baseClass : BlockClass | undefined = this . _container . base ;
1237
- while ( baseClass ) {
1238
- let state = baseClass . getState ( this . _name ) ;
1239
- if ( state ) {
1240
- this . _base = state ;
1241
- return state ;
1242
- }
1243
- baseClass = baseClass . base ;
1244
- }
1245
- this . _base = null ;
1246
- return undefined ;
1241
+ getBaseFromBaseParent ( baseParent : BlockClass ) : State | undefined {
1242
+ return baseParent . getState ( this . _name ) || undefined ;
1247
1243
}
1248
1244
1249
1245
/**
@@ -1253,30 +1249,14 @@ export class State extends BlockObject<State, BlockClass> {
1253
1249
all ( ) : Style [ ] {
1254
1250
return [ this ] ;
1255
1251
}
1256
-
1257
1252
}
1258
1253
1259
1254
export class SubState extends BlockObject < SubState , State > {
1260
- private _base : SubState | null | undefined ;
1261
-
1262
1255
_sourceAttributes : Array < AttributeNS > ;
1263
1256
isGlobal = false ;
1264
1257
1265
- get base ( ) : SubState | undefined {
1266
- if ( this . _base !== undefined ) {
1267
- return this . _base || undefined ;
1268
- }
1269
- let baseState : State | undefined = this . _container . base ;
1270
- while ( baseState ) {
1271
- let subState = baseState . getSubState ( this . _name ) ;
1272
- if ( subState ) {
1273
- this . _base = subState ;
1274
- return subState ;
1275
- }
1276
- baseState = baseState . base ;
1277
- }
1278
- this . _base = null ;
1279
- return undefined ;
1258
+ getBaseFromBaseParent ( baseParent : State ) : SubState | undefined {
1259
+ return baseParent . getSubState ( this . _name ) || undefined ;
1280
1260
}
1281
1261
1282
1262
localName ( ) : string {
0 commit comments