8
8
import {
9
9
SerializedTemplateInfo ,
10
10
TemplateAnalysis as OptimizationTemplateAnalysis ,
11
- TemplateInfo ,
12
11
TemplateInfoFactory ,
13
12
TemplateIntegrationOptions ,
14
13
TemplateTypes ,
@@ -23,6 +22,7 @@ import { BlockFactory } from "../BlockParser";
23
22
import { Block , Style } from "../BlockTree" ;
24
23
import { ResolvedConfiguration } from "../configuration" ;
25
24
25
+ import { Analyzer } from "./Analyzer" ;
26
26
import { ElementAnalysis , SerializedElementAnalysis } from "./ElementAnalysis" ;
27
27
import { TemplateValidator , TemplateValidatorOptions } from "./validations" ;
28
28
@@ -47,10 +47,11 @@ export interface SerializedAnalysis {
47
47
* 2. Call [[addExclusiveStyle addExclusiveStyle(alwaysPresent, ...style)]] for all the styles used that are mutually exclusive on the current html element.
48
48
* 3. Call [[endElement endElement()]] when done adding styles for the current element.
49
49
*/
50
- export class Analysis {
50
+ export class Analysis < K extends keyof TemplateTypes > {
51
51
52
- template : TemplateInfo < keyof TemplateTypes > ;
53
52
idGenerator : IdentGenerator ;
53
+ parent ?: Analyzer < K > ;
54
+ template : TemplateTypes [ K ] ;
54
55
55
56
/**
56
57
* A map from a local name for the block to the [[Block]].
@@ -59,13 +60,53 @@ export class Analysis {
59
60
*/
60
61
blocks : ObjectDictionary < Block > ;
61
62
63
+
62
64
/**
63
- * Return the number of blocks discovered in this Template.
65
+ * A per-element correlation of styles used. The current correlation is added
66
+ * to this list when [[endElement]] is called.
67
+ */
68
+ // tslint:disable-next-line:prefer-whatever-to-any
69
+ elements : Map < string , ElementAnalysis < any , any , any > > ;
70
+
71
+ /**
72
+ * The current element, created when calling [[startElement]].
73
+ * The current element is unset after calling [[endElement]].
64
74
*/
65
- blockCount ( ) : number {
66
- return Object . keys ( this . blocks ) . length ;
75
+ // tslint:disable-next-line:prefer-whatever-to-any
76
+ currentElement : ElementAnalysis < any , any , any > | undefined ;
77
+
78
+ /**
79
+ * Template validator instance to verify blocks applied to an element.
80
+ */
81
+ validator : TemplateValidator ;
82
+
83
+ /**
84
+ * @param template The template being analyzed.
85
+ */
86
+ constructor ( template : TemplateTypes [ K ] , options ?: TemplateValidatorOptions , parent ?: Analyzer < K > , ) {
87
+ this . idGenerator = new IdentGenerator ( ) ;
88
+ this . parent = parent ;
89
+ this . template = template ;
90
+ this . blocks = { } ;
91
+ this . elements = new Map ( ) ;
92
+ this . validator = new TemplateValidator ( options ) ;
67
93
}
68
94
95
+ /**
96
+ * Return the number of blocks discovered in this Template.
97
+ */
98
+ blockCount ( ) : number { return Object . keys ( this . blocks ) . length ; }
99
+
100
+ /**
101
+ * Convenience setter for adding a block to the template scope.
102
+ */
103
+ addBlock ( name : string , block : Block ) { this . blocks [ name ] = block ; }
104
+
105
+ /**
106
+ * Convenience getter for fetching a block from the template scope.
107
+ */
108
+ getBlock ( name : string ) : Block { return this . blocks [ name ] ; }
109
+
69
110
/**
70
111
* Return the number of styles discovered in this Analysis' Template.
71
112
* This is slow.
@@ -83,26 +124,10 @@ export class Analysis {
83
124
return c ;
84
125
}
85
126
86
- /**
87
- * A per-element correlation of styles used. The current correlation is added
88
- * to this list when [[endElement]] is called.
89
- */
90
- // tslint:disable-next-line:prefer-whatever-to-any
91
- elements : Map < string , ElementAnalysis < any , any , any > > ;
92
-
93
- /**
94
- * The current element, created when calling [[startElement]].
95
- * The current element is unset after calling [[endElement]].
96
- */
97
- // tslint:disable-next-line:prefer-whatever-to-any
98
- currentElement : ElementAnalysis < any , any , any > | undefined ;
99
-
100
127
/**
101
128
* Return the number of elements discovered in this Analysis.
102
129
*/
103
- elementCount ( ) : number {
104
- return this . elements . size ;
105
- }
130
+ elementCount ( ) : number { return this . elements . size ; }
106
131
107
132
/**
108
133
* Get the nth element discovered in this Analysis.
@@ -139,22 +164,6 @@ export class Analysis {
139
164
return this . elements . get ( id ) ;
140
165
}
141
166
142
- /**
143
- * Template validator instance to verify blocks applied to an element.
144
- */
145
- validator : TemplateValidator ;
146
-
147
- /**
148
- * @param template The template being analyzed.
149
- */
150
- constructor ( template : TemplateInfo < keyof TemplateTypes > , options ?: TemplateValidatorOptions ) {
151
- this . idGenerator = new IdentGenerator ( ) ;
152
- this . template = template ;
153
- this . blocks = { } ;
154
- this . elements = new Map ( ) ;
155
- this . validator = new TemplateValidator ( options ) ;
156
- }
157
-
158
167
/**
159
168
* @param block The block for which the local name should be returned.
160
169
* @return The local name of the given block.
@@ -218,6 +227,14 @@ export class Analysis {
218
227
}
219
228
this . validator . validate ( this , element ) ;
220
229
this . elements . set ( element . id , element ) ;
230
+ if ( this . parent ) {
231
+ for ( let s of [ ...element . classesFound ( false ) , ...element . attributesFound ( false ) ] ) {
232
+ this . parent . saveStaticStyle ( s , this ) ;
233
+ }
234
+ for ( let s of [ ...element . classesFound ( true ) , ...element . attributesFound ( true ) ] ) {
235
+ this . parent . saveDynamicStyle ( s , this ) ;
236
+ }
237
+ }
221
238
}
222
239
223
240
/**
@@ -322,10 +339,11 @@ export class Analysis {
322
339
static async deserialize (
323
340
serializedAnalysis : SerializedAnalysis ,
324
341
blockFactory : BlockFactory ,
325
- ) : Promise < Analysis > {
342
+ parent : Analyzer < keyof TemplateTypes >
343
+ ) : Promise < Analysis < keyof TemplateTypes > > {
326
344
let blockNames = Object . keys ( serializedAnalysis . blocks ) ;
327
345
let info = TemplateInfoFactory . deserialize < keyof TemplateTypes > ( serializedAnalysis . template ) ;
328
- let analysis = new Analysis ( info ) ;
346
+ let analysis = new Analysis ( info , { } , parent ) ;
329
347
let blockPromises = new Array < Promise < { name : string ; block : Block } > > ( ) ;
330
348
blockNames . forEach ( n => {
331
349
let blockIdentifier = serializedAnalysis . blocks [ n ] ;
0 commit comments