UNPKG

angular2

Version:

Angular 2 - a web framework for modern web apps

78 lines (77 loc) 3.91 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; import { CompileIdentifierMetadata } from './compile_metadata'; import * as o from './output/output_ast'; import { ViewEncapsulation } from 'angular2/src/core/metadata/view'; import { ShadowCss } from 'angular2/src/compiler/shadow_css'; import { UrlResolver } from 'angular2/src/compiler/url_resolver'; import { extractStyleUrls } from './style_url_resolver'; import { Injectable } from 'angular2/src/core/di'; import { isPresent } from 'angular2/src/facade/lang'; const COMPONENT_VARIABLE = '%COMP%'; const HOST_ATTR = `_nghost-${COMPONENT_VARIABLE}`; const CONTENT_ATTR = `_ngcontent-${COMPONENT_VARIABLE}`; export class StylesCompileDependency { constructor(sourceUrl, isShimmed, valuePlaceholder) { this.sourceUrl = sourceUrl; this.isShimmed = isShimmed; this.valuePlaceholder = valuePlaceholder; } } export class StylesCompileResult { constructor(statements, stylesVar, dependencies) { this.statements = statements; this.stylesVar = stylesVar; this.dependencies = dependencies; } } export let StyleCompiler = class StyleCompiler { constructor(_urlResolver) { this._urlResolver = _urlResolver; this._shadowCss = new ShadowCss(); } compileComponent(comp) { var shim = comp.template.encapsulation === ViewEncapsulation.Emulated; return this._compileStyles(getStylesVarName(comp), comp.template.styles, comp.template.styleUrls, shim); } compileStylesheet(stylesheetUrl, cssText, isShimmed) { var styleWithImports = extractStyleUrls(this._urlResolver, stylesheetUrl, cssText); return this._compileStyles(getStylesVarName(null), [styleWithImports.style], styleWithImports.styleUrls, isShimmed); } _compileStyles(stylesVar, plainStyles, absUrls, shim) { var styleExpressions = plainStyles.map(plainStyle => o.literal(this._shimIfNeeded(plainStyle, shim))); var dependencies = []; for (var i = 0; i < absUrls.length; i++) { var identifier = new CompileIdentifierMetadata({ name: getStylesVarName(null) }); dependencies.push(new StylesCompileDependency(absUrls[i], shim, identifier)); styleExpressions.push(new o.ExternalExpr(identifier)); } // styles variable contains plain strings and arrays of other styles arrays (recursive), // so we set its type to dynamic. var stmt = o.variable(stylesVar) .set(o.literalArr(styleExpressions, new o.ArrayType(o.DYNAMIC_TYPE, [o.TypeModifier.Const]))) .toDeclStmt(null, [o.StmtModifier.Final]); return new StylesCompileResult([stmt], stylesVar, dependencies); } _shimIfNeeded(style, shim) { return shim ? this._shadowCss.shimCssText(style, CONTENT_ATTR, HOST_ATTR) : style; } }; StyleCompiler = __decorate([ Injectable(), __metadata('design:paramtypes', [UrlResolver]) ], StyleCompiler); function getStylesVarName(component) { var result = `styles`; if (isPresent(component)) { result += `_${component.type.name}`; } return result; }