| 
									
										
										
										
											2016-06-23 09:47:54 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-15 09:12:40 -08:00
										 |  |  | import {ViewEncapsulation} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2016-11-23 09:42:19 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import {CompileDirectiveMetadata, CompileIdentifierMetadata, CompileStylesheetMetadata, identifierModuleUrl, identifierName} from './compile_metadata'; | 
					
						
							| 
									
										
										
										
											2016-12-15 09:12:40 -08:00
										 |  |  | import {CompilerInjectable} from './injectable'; | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  | import * as o from './output/output_ast'; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {ShadowCss} from './shadow_css'; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | import {UrlResolver} from './url_resolver'; | 
					
						
							| 
									
										
										
										
											2015-12-02 10:35:51 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const COMPONENT_VARIABLE = '%COMP%'; | 
					
						
							| 
									
										
										
										
											2016-07-13 11:01:32 -07:00
										 |  |  | const HOST_ATTR = `_nghost-${COMPONENT_VARIABLE}`; | 
					
						
							|  |  |  | const CONTENT_ATTR = `_ngcontent-${COMPONENT_VARIABLE}`; | 
					
						
							| 
									
										
										
										
											2015-09-02 15:07:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  | export class StylesCompileDependency { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   constructor( | 
					
						
							| 
									
										
										
										
											2016-11-23 09:42:19 -08:00
										 |  |  |       public name: string, public moduleUrl: string, public isShimmed: boolean, | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       public valuePlaceholder: CompileIdentifierMetadata) {} | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class StylesCompileResult { | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  |   constructor( | 
					
						
							|  |  |  |       public componentStylesheet: CompiledStylesheet, | 
					
						
							|  |  |  |       public externalStylesheets: CompiledStylesheet[]) {} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class CompiledStylesheet { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   constructor( | 
					
						
							|  |  |  |       public statements: o.Statement[], public stylesVar: string, | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  |       public dependencies: StylesCompileDependency[], public isShimmed: boolean, | 
					
						
							|  |  |  |       public meta: CompileStylesheetMetadata) {} | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-15 09:12:40 -08:00
										 |  |  | @CompilerInjectable() | 
					
						
							| 
									
										
										
										
											2015-09-02 15:07:31 -07:00
										 |  |  | export class StyleCompiler { | 
					
						
							|  |  |  |   private _shadowCss: ShadowCss = new ShadowCss(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  |   constructor(private _urlResolver: UrlResolver) {} | 
					
						
							| 
									
										
										
										
											2015-09-02 15:07:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  |   compileComponent(comp: CompileDirectiveMetadata): StylesCompileResult { | 
					
						
							| 
									
										
										
										
											2016-07-09 10:12:39 -07:00
										 |  |  |     const externalStylesheets: CompiledStylesheet[] = []; | 
					
						
							|  |  |  |     const componentStylesheet: CompiledStylesheet = this._compileStyles( | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  |         comp, new CompileStylesheetMetadata({ | 
					
						
							|  |  |  |           styles: comp.template.styles, | 
					
						
							|  |  |  |           styleUrls: comp.template.styleUrls, | 
					
						
							| 
									
										
										
										
											2016-11-23 09:42:19 -08:00
										 |  |  |           moduleUrl: identifierModuleUrl(comp.type) | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  |         }), | 
					
						
							|  |  |  |         true); | 
					
						
							|  |  |  |     comp.template.externalStylesheets.forEach((stylesheetMeta) => { | 
					
						
							| 
									
										
										
										
											2016-07-09 10:12:39 -07:00
										 |  |  |       const compiledStylesheet = this._compileStyles(comp, stylesheetMeta, false); | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  |       externalStylesheets.push(compiledStylesheet); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     return new StylesCompileResult(componentStylesheet, externalStylesheets); | 
					
						
							| 
									
										
										
										
											2015-09-02 15:07:31 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   private _compileStyles( | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  |       comp: CompileDirectiveMetadata, stylesheet: CompileStylesheetMetadata, | 
					
						
							|  |  |  |       isComponentStylesheet: boolean): CompiledStylesheet { | 
					
						
							| 
									
										
										
										
											2016-07-09 10:12:39 -07:00
										 |  |  |     const shim = comp.template.encapsulation === ViewEncapsulation.Emulated; | 
					
						
							|  |  |  |     const styleExpressions = | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  |         stylesheet.styles.map(plainStyle => o.literal(this._shimIfNeeded(plainStyle, shim))); | 
					
						
							| 
									
										
										
										
											2016-07-09 10:12:39 -07:00
										 |  |  |     const dependencies: StylesCompileDependency[] = []; | 
					
						
							|  |  |  |     for (let i = 0; i < stylesheet.styleUrls.length; i++) { | 
					
						
							| 
									
										
										
										
											2016-11-30 10:52:51 -08:00
										 |  |  |       const identifier: CompileIdentifierMetadata = {reference: null}; | 
					
						
							| 
									
										
										
										
											2016-11-23 09:42:19 -08:00
										 |  |  |       dependencies.push(new StylesCompileDependency( | 
					
						
							|  |  |  |           getStylesVarName(null), stylesheet.styleUrls[i], shim, identifier)); | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  |       styleExpressions.push(new o.ExternalExpr(identifier)); | 
					
						
							| 
									
										
										
										
											2015-09-02 15:07:31 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  |     // styles variable contains plain strings and arrays of other styles arrays (recursive),
 | 
					
						
							|  |  |  |     // so we set its type to dynamic.
 | 
					
						
							| 
									
										
										
										
											2016-07-09 10:12:39 -07:00
										 |  |  |     const stylesVar = getStylesVarName(isComponentStylesheet ? comp : null); | 
					
						
							|  |  |  |     const stmt = o.variable(stylesVar) | 
					
						
							|  |  |  |                      .set(o.literalArr( | 
					
						
							|  |  |  |                          styleExpressions, new o.ArrayType(o.DYNAMIC_TYPE, [o.TypeModifier.Const]))) | 
					
						
							|  |  |  |                      .toDeclStmt(null, [o.StmtModifier.Final]); | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  |     return new CompiledStylesheet([stmt], stylesVar, dependencies, shim, stylesheet); | 
					
						
							| 
									
										
										
										
											2015-09-02 15:07:31 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private _shimIfNeeded(style: string, shim: boolean): string { | 
					
						
							|  |  |  |     return shim ? this._shadowCss.shimCssText(style, CONTENT_ATTR, HOST_ATTR) : style; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-09-02 15:07:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  | function getStylesVarName(component: CompileDirectiveMetadata): string { | 
					
						
							| 
									
										
										
										
											2016-07-09 10:12:39 -07:00
										 |  |  |   let result = `styles`; | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  |   if (component) { | 
					
						
							| 
									
										
										
										
											2016-11-23 09:42:19 -08:00
										 |  |  |     result += `_${identifierName(component.type)}`; | 
					
						
							| 
									
										
										
										
											2015-09-02 15:07:31 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  |   return result; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | } |