@@ -9,21 +9,12 @@ import {UrlResolver} from 'angular2/src/compiler/url_resolver';
99import { extractStyleUrls } from './style_url_resolver' ;
1010import {
1111 escapeSingleQuoteString ,
12- codeGenConcatArray ,
13- codeGenMapArray ,
14- codeGenReplaceAll ,
1512 codeGenExportVariable ,
1613 codeGenToString ,
1714 MODULE_SUFFIX
1815} from './util' ;
1916import { Injectable } from 'angular2/src/core/di' ;
20-
21- const COMPONENT_VARIABLE = '%COMP%' ;
22- var COMPONENT_REGEX = / % C O M P % / g;
23- const HOST_ATTR = `_nghost-${ COMPONENT_VARIABLE } ` ;
24- const HOST_ATTR_EXPR = `'_nghost-'+${ COMPONENT_VARIABLE } ` ;
25- const CONTENT_ATTR = `_ngcontent-${ COMPONENT_VARIABLE } ` ;
26- const CONTENT_ATTR_EXPR = `'_ngcontent-'+${ COMPONENT_VARIABLE } ` ;
17+ import { COMPONENT_VARIABLE , HOST_ATTR , CONTENT_ATTR } from 'angular2/src/core/render/view_factory' ;
2718
2819@Injectable ( )
2920export class StyleCompiler {
@@ -32,46 +23,33 @@ export class StyleCompiler {
3223
3324 constructor ( private _xhr : XHR , private _urlResolver : UrlResolver ) { }
3425
35- compileComponentRuntime ( appId : string , templateId : number ,
36- template : CompileTemplateMetadata ) : Promise < string [ ] > {
26+ compileComponentRuntime ( template : CompileTemplateMetadata ) : Promise < Array < string | any [ ] > > {
3727 var styles = template . styles ;
3828 var styleAbsUrls = template . styleUrls ;
3929 return this . _loadStyles ( styles , styleAbsUrls ,
40- template . encapsulation === ViewEncapsulation . Emulated )
41- . then ( styles => styles . map ( style => StringWrapper . replaceAll (
42- style , COMPONENT_REGEX , componentId ( appId , templateId ) ) ) ) ;
30+ template . encapsulation === ViewEncapsulation . Emulated ) ;
4331 }
4432
45- compileComponentCodeGen ( appIdExpression : string , templateIdExpression : string ,
46- template : CompileTemplateMetadata ) : SourceExpression {
33+ compileComponentCodeGen ( template : CompileTemplateMetadata ) : SourceExpression {
4734 var shim = template . encapsulation === ViewEncapsulation . Emulated ;
48- var suffix ;
49- if ( shim ) {
50- suffix = codeGenMapArray (
51- [ 'style' ] ,
52- `style${ codeGenReplaceAll ( COMPONENT_VARIABLE , componentIdExpression ( appIdExpression , templateIdExpression ) ) } ` ) ;
53- } else {
54- suffix = '' ;
55- }
56- return this . _styleCodeGen ( template . styles , template . styleUrls , shim , suffix ) ;
35+ return this . _styleCodeGen ( template . styles , template . styleUrls , shim ) ;
5736 }
5837
5938 compileStylesheetCodeGen ( stylesheetUrl : string , cssText : string ) : SourceModule [ ] {
6039 var styleWithImports = extractStyleUrls ( this . _urlResolver , stylesheetUrl , cssText ) ;
6140 return [
6241 this . _styleModule (
6342 stylesheetUrl , false ,
64- this . _styleCodeGen ( [ styleWithImports . style ] , styleWithImports . styleUrls , false , '' ) ) ,
65- this . _styleModule (
66- stylesheetUrl , true ,
67- this . _styleCodeGen ( [ styleWithImports . style ] , styleWithImports . styleUrls , true , '' ) )
43+ this . _styleCodeGen ( [ styleWithImports . style ] , styleWithImports . styleUrls , false ) ) ,
44+ this . _styleModule ( stylesheetUrl , true , this . _styleCodeGen ( [ styleWithImports . style ] ,
45+ styleWithImports . styleUrls , true ) )
6846 ] ;
6947 }
7048
7149 clearCache ( ) { this . _styleCache . clear ( ) ; }
7250
7351 private _loadStyles ( plainStyles : string [ ] , absUrls : string [ ] ,
74- encapsulate : boolean ) : Promise < string [ ] > {
52+ encapsulate : boolean ) : Promise < Array < string | any [ ] > > {
7553 var promises = absUrls . map ( ( absUrl ) => {
7654 var cacheKey = `${ absUrl } ${ encapsulate ? '.shim' : '' } ` ;
7755 var result = this . _styleCache . get ( cacheKey ) ;
@@ -86,22 +64,23 @@ export class StyleCompiler {
8664 return result ;
8765 } ) ;
8866 return PromiseWrapper . all ( promises ) . then ( ( nestedStyles : string [ ] [ ] ) => {
89- var result = plainStyles . map ( plainStyle => this . _shimIfNeeded ( plainStyle , encapsulate ) ) ;
90- nestedStyles . forEach ( styles => styles . forEach ( style => result . push ( style ) ) ) ;
67+ var result : Array < string | any [ ] > =
68+ plainStyles . map ( plainStyle => this . _shimIfNeeded ( plainStyle , encapsulate ) ) ;
69+ nestedStyles . forEach ( styles => result . push ( styles ) ) ;
9170 return result ;
9271 } ) ;
9372 }
9473
95- private _styleCodeGen ( plainStyles : string [ ] , absUrls : string [ ] , shim : boolean ,
96- suffix : string ) : SourceExpression {
97- var expressionSource = `(` ;
98- expressionSource +=
99- `[ ${ plainStyles . map ( plainStyle => escapeSingleQuoteString ( this . _shimIfNeeded ( plainStyle , shim ) ) ) . join ( ',' ) } ]` ;
74+ private _styleCodeGen ( plainStyles : string [ ] , absUrls : string [ ] , shim : boolean ) : SourceExpression {
75+ var arrayPrefix = IS_DART ? `const` : '' ;
76+ var styleExpressions = plainStyles . map (
77+ plainStyle => escapeSingleQuoteString ( this . _shimIfNeeded ( plainStyle , shim ) ) ) ;
78+
10079 for ( var i = 0 ; i < absUrls . length ; i ++ ) {
10180 var moduleUrl = this . _createModuleUrl ( absUrls [ i ] , shim ) ;
102- expressionSource += codeGenConcatArray ( `${ moduleRef ( moduleUrl ) } STYLES` ) ;
81+ styleExpressions . push ( `${ moduleRef ( moduleUrl ) } STYLES` ) ;
10382 }
104- expressionSource + = `) ${ suffix } ` ;
83+ var expressionSource = `${ arrayPrefix } [ ${ styleExpressions . join ( ',' ) } ] ` ;
10584 return new SourceExpression ( [ ] , expressionSource ) ;
10685 }
10786
@@ -122,29 +101,3 @@ export class StyleCompiler {
122101 return shim ? `${ stylesheetUrl } .shim${ MODULE_SUFFIX } ` : `${ stylesheetUrl } ${ MODULE_SUFFIX } ` ;
123102 }
124103}
125-
126- export function shimContentAttribute ( appId : string , templateId : number ) : string {
127- return StringWrapper . replaceAll ( CONTENT_ATTR , COMPONENT_REGEX , componentId ( appId , templateId ) ) ;
128- }
129-
130- export function shimContentAttributeExpr ( appIdExpr : string , templateIdExpr : string ) : string {
131- return StringWrapper . replaceAll ( CONTENT_ATTR_EXPR , COMPONENT_REGEX ,
132- componentIdExpression ( appIdExpr , templateIdExpr ) ) ;
133- }
134-
135- export function shimHostAttribute ( appId : string , templateId : number ) : string {
136- return StringWrapper . replaceAll ( HOST_ATTR , COMPONENT_REGEX , componentId ( appId , templateId ) ) ;
137- }
138-
139- export function shimHostAttributeExpr ( appIdExpr : string , templateIdExpr : string ) : string {
140- return StringWrapper . replaceAll ( HOST_ATTR_EXPR , COMPONENT_REGEX ,
141- componentIdExpression ( appIdExpr , templateIdExpr ) ) ;
142- }
143-
144- function componentId ( appId : string , templateId : number ) : string {
145- return `${ appId } -${ templateId } ` ;
146- }
147-
148- function componentIdExpression ( appIdExpression : string , templateIdExpression : string ) : string {
149- return `${ appIdExpression } +'-'+${ codeGenToString ( templateIdExpression ) } ` ;
150- }
0 commit comments