88import {
99 SerializedTemplateInfo ,
1010 TemplateAnalysis as OptimizationTemplateAnalysis ,
11- TemplateInfo ,
1211 TemplateInfoFactory ,
1312 TemplateIntegrationOptions ,
1413 TemplateTypes ,
@@ -23,6 +22,7 @@ import { BlockFactory } from "../BlockParser";
2322import { Block , Style } from "../BlockTree" ;
2423import { ResolvedConfiguration } from "../configuration" ;
2524
25+ import { Analyzer } from "./Analyzer" ;
2626import { ElementAnalysis , SerializedElementAnalysis } from "./ElementAnalysis" ;
2727import { TemplateValidator , TemplateValidatorOptions } from "./validations" ;
2828
@@ -47,10 +47,11 @@ export interface SerializedAnalysis {
4747 * 2. Call [[addExclusiveStyle addExclusiveStyle(alwaysPresent, ...style)]] for all the styles used that are mutually exclusive on the current html element.
4848 * 3. Call [[endElement endElement()]] when done adding styles for the current element.
4949 */
50- export class Analysis {
50+ export class Analysis < K extends keyof TemplateTypes > {
5151
52- template : TemplateInfo < keyof TemplateTypes > ;
5352 idGenerator : IdentGenerator ;
53+ parent ?: Analyzer < K > ;
54+ template : TemplateTypes [ K ] ;
5455
5556 /**
5657 * A map from a local name for the block to the [[Block]].
@@ -59,13 +60,53 @@ export class Analysis {
5960 */
6061 blocks : ObjectDictionary < Block > ;
6162
63+
6264 /**
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]].
6474 */
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 ) ;
6793 }
6894
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+
69110 /**
70111 * Return the number of styles discovered in this Analysis' Template.
71112 * This is slow.
@@ -83,26 +124,10 @@ export class Analysis {
83124 return c ;
84125 }
85126
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-
100127 /**
101128 * Return the number of elements discovered in this Analysis.
102129 */
103- elementCount ( ) : number {
104- return this . elements . size ;
105- }
130+ elementCount ( ) : number { return this . elements . size ; }
106131
107132 /**
108133 * Get the nth element discovered in this Analysis.
@@ -139,22 +164,6 @@ export class Analysis {
139164 return this . elements . get ( id ) ;
140165 }
141166
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-
158167 /**
159168 * @param block The block for which the local name should be returned.
160169 * @return The local name of the given block.
@@ -218,6 +227,14 @@ export class Analysis {
218227 }
219228 this . validator . validate ( this , element ) ;
220229 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+ }
221238 }
222239
223240 /**
@@ -322,10 +339,11 @@ export class Analysis {
322339 static async deserialize (
323340 serializedAnalysis : SerializedAnalysis ,
324341 blockFactory : BlockFactory ,
325- ) : Promise < Analysis > {
342+ parent : Analyzer < keyof TemplateTypes >
343+ ) : Promise < Analysis < keyof TemplateTypes > > {
326344 let blockNames = Object . keys ( serializedAnalysis . blocks ) ;
327345 let info = TemplateInfoFactory . deserialize < keyof TemplateTypes > ( serializedAnalysis . template ) ;
328- let analysis = new Analysis ( info ) ;
346+ let analysis = new Analysis ( info , { } , parent ) ;
329347 let blockPromises = new Array < Promise < { name : string ; block : Block } > > ( ) ;
330348 blockNames . forEach ( n => {
331349 let blockIdentifier = serializedAnalysis . blocks [ n ] ;
0 commit comments