diff --git a/modules/@angular/compiler/src/compile_metadata.ts b/modules/@angular/compiler/src/compile_metadata.ts index ae7ad7df47..8541d75dec 100644 --- a/modules/@angular/compiler/src/compile_metadata.ts +++ b/modules/@angular/compiler/src/compile_metadata.ts @@ -656,7 +656,7 @@ export class CompileTemplateMetadata { ngContentSelectors?: string[], animations?: CompileAnimationEntryMetadata[] } = {}) { - this.encapsulation = isPresent(encapsulation) ? encapsulation : ViewEncapsulation.Emulated; + this.encapsulation = encapsulation; this.template = template; this.templateUrl = templateUrl; this.styles = isPresent(styles) ? styles : []; diff --git a/modules/@angular/compiler/src/config.ts b/modules/@angular/compiler/src/config.ts index 0232928542..4427f59733 100644 --- a/modules/@angular/compiler/src/config.ts +++ b/modules/@angular/compiler/src/config.ts @@ -2,12 +2,16 @@ import {isBlank} from '../src/facade/lang'; import {unimplemented} from '../src/facade/exceptions'; import {Identifiers} from './identifiers'; import {CompileIdentifierMetadata} from './compile_metadata'; +import {ViewEncapsulation} from '@angular/core'; export class CompilerConfig { public renderTypes: RenderTypes; public interpolateRegexp: RegExp; + public defaultEncapsulation: ViewEncapsulation; + constructor(public genDebugInfo: boolean, public logBindingUpdate: boolean, - public useJit: boolean, renderTypes: RenderTypes = null, interpolateRegexp: RegExp = null) { + public useJit: boolean, renderTypes: RenderTypes = null, + interpolateRegexp: RegExp = null, defaultEncapsulation: ViewEncapsulation = null) { if (isBlank(renderTypes)) { renderTypes = new DefaultRenderTypes(); } @@ -16,6 +20,10 @@ export class CompilerConfig { interpolateRegexp = DEFAULT_INTERPOLATE_REGEXP; } this.interpolateRegexp = interpolateRegexp; + if (isBlank(defaultEncapsulation)) { + defaultEncapsulation = ViewEncapsulation.Emulated; + } + this.defaultEncapsulation = defaultEncapsulation; } } diff --git a/modules/@angular/compiler/src/directive_normalizer.ts b/modules/@angular/compiler/src/directive_normalizer.ts index 4100dd77bc..e042b72b80 100644 --- a/modules/@angular/compiler/src/directive_normalizer.ts +++ b/modules/@angular/compiler/src/directive_normalizer.ts @@ -1,6 +1,6 @@ import {Injectable, ViewEncapsulation} from '@angular/core'; -import {isPresent} from '../src/facade/lang'; +import {isPresent, isBlank} from '../src/facade/lang'; import {BaseException} from '../src/facade/exceptions'; import {PromiseWrapper} from '../src/facade/async'; @@ -24,6 +24,7 @@ import { htmlVisitAll } from './html_ast'; import {HtmlParser} from './html_parser'; +import {CompilerConfig} from './config' import {preparseElement, PreparsedElementType} from './template_preparser'; @@ -31,7 +32,7 @@ import {preparseElement, PreparsedElementType} from './template_preparser'; @Injectable() export class DirectiveNormalizer { constructor(private _xhr: XHR, private _urlResolver: UrlResolver, - private _htmlParser: HtmlParser) {} + private _htmlParser: HtmlParser, private _config: CompilerConfig) {} normalizeDirective(directive: CompileDirectiveMetadata): Promise { if (!directive.isComponent) { @@ -99,6 +100,9 @@ export class DirectiveNormalizer { }); var encapsulation = templateMeta.encapsulation; + if (isBlank(encapsulation)) { + encapsulation = this._config.defaultEncapsulation; + } if (encapsulation === ViewEncapsulation.Emulated && allResolvedStyles.length === 0 && allStyleAbsUrls.length === 0) { encapsulation = ViewEncapsulation.None; diff --git a/modules/@angular/compiler/test/compile_metadata_spec.ts b/modules/@angular/compiler/test/compile_metadata_spec.ts index 6e18b08d4e..feff538356 100644 --- a/modules/@angular/compiler/test/compile_metadata_spec.ts +++ b/modules/@angular/compiler/test/compile_metadata_spec.ts @@ -172,10 +172,6 @@ export function main() { }); describe('TemplateMetadata', () => { - it('should use ViewEncapsulation.Emulated by default', () => { - expect(new CompileTemplateMetadata({encapsulation: null}).encapsulation) - .toBe(ViewEncapsulation.Emulated); - }); it('should serialize with full data', () => { expect(CompileTemplateMetadata.fromJson(fullTemplateMeta.toJson())) diff --git a/modules/@angular/compiler/test/directive_normalizer_spec.ts b/modules/@angular/compiler/test/directive_normalizer_spec.ts index ed75fd4f48..5a5bf6bcf5 100644 --- a/modules/@angular/compiler/test/directive_normalizer_spec.ts +++ b/modules/@angular/compiler/test/directive_normalizer_spec.ts @@ -12,6 +12,7 @@ import { import {AsyncTestCompleter} from '@angular/core/testing/testing_internal'; import {CompileTypeMetadata, CompileTemplateMetadata} from '@angular/compiler/src/compile_metadata'; import {ViewEncapsulation} from '@angular/core/src/metadata/view'; +import {CompilerConfig} from "@angular/compiler/src/config"; import {DirectiveNormalizer} from '@angular/compiler/src/directive_normalizer'; import {XHR} from '@angular/compiler/src/xhr'; @@ -81,6 +82,39 @@ export function main() { async.done(); }); })); + + it('should use ViewEncapsulation.Emulated by default', + inject([AsyncTestCompleter, DirectiveNormalizer], + (async, normalizer: DirectiveNormalizer) => { + normalizer.normalizeTemplate(dirType, new CompileTemplateMetadata({ + encapsulation: null, + template: '', + templateUrl: null, + styles: [], + styleUrls: ['test.css'] + })) + .then((template: CompileTemplateMetadata) => { + expect(template.encapsulation).toEqual(ViewEncapsulation.Emulated); + async.done(); + }); + })); + + it('should use default encapsulation provided by CompilerConfig', + inject([AsyncTestCompleter, CompilerConfig , DirectiveNormalizer], + (async, config: CompilerConfig, normalizer: DirectiveNormalizer) => { + config.defaultEncapsulation = ViewEncapsulation.None; + normalizer.normalizeTemplate(dirType, new CompileTemplateMetadata({ + encapsulation: null, + template: '', + templateUrl: null, + styles: [], + styleUrls: ['test.css'] + })) + .then((template: CompileTemplateMetadata) => { + expect(template.encapsulation).toEqual(ViewEncapsulation.None); + async.done(); + }); + })); }); describe('templateUrl', () => { diff --git a/modules/@angular/compiler/test/offline_compiler_util.ts b/modules/@angular/compiler/test/offline_compiler_util.ts index a4dd8dec2c..12ac08fd8b 100644 --- a/modules/@angular/compiler/test/offline_compiler_util.ts +++ b/modules/@angular/compiler/test/offline_compiler_util.ts @@ -49,8 +49,8 @@ function _createOfflineCompiler(xhr: MockXHR, emitter: OutputEmitter): OfflineCo var urlResolver = createOfflineCompileUrlResolver(); xhr.when(`${THIS_MODULE_PATH}/offline_compiler_compa.html`, 'Hello World {{user}}!'); var htmlParser = new HtmlParser(); - var normalizer = new DirectiveNormalizer(xhr, urlResolver, htmlParser); var config = new CompilerConfig(true, true, true); + var normalizer = new DirectiveNormalizer(xhr, urlResolver, htmlParser, config); return new OfflineCompiler( normalizer, new TemplateParser(new Parser(new Lexer(), config), new MockSchemaRegistry({}, {}), htmlParser, new Console(), []), diff --git a/modules/@angular/compiler_cli/src/codegen.ts b/modules/@angular/compiler_cli/src/codegen.ts index fb40de747a..2985f6c047 100644 --- a/modules/@angular/compiler_cli/src/codegen.ts +++ b/modules/@angular/compiler_cli/src/codegen.ts @@ -156,8 +156,8 @@ export class CodeGenerator { const staticReflector = new StaticReflector(reflectorHost); StaticAndDynamicReflectionCapabilities.install(staticReflector); const htmlParser = new HtmlParser(); - const normalizer = new DirectiveNormalizer(xhr, urlResolver, htmlParser); const config = new compiler.CompilerConfig(true, true, true); + const normalizer = new DirectiveNormalizer(xhr, urlResolver, htmlParser, config); const parser = new Parser(new Lexer(), config); const tmplParser = new TemplateParser(parser, new DomElementSchemaRegistry(), htmlParser, /*console*/ null, []); diff --git a/modules/@angular/core/src/metadata.ts b/modules/@angular/core/src/metadata.ts index 7755040aa8..a4559b48e1 100644 --- a/modules/@angular/core/src/metadata.ts +++ b/modules/@angular/core/src/metadata.ts @@ -1120,7 +1120,7 @@ export var ContentChildren: ContentChildrenMetadataFactory = * } * } * ``` - * + * * ```html * * a diff --git a/tools/public_api_guard/public_api_spec.ts b/tools/public_api_guard/public_api_spec.ts index 7720f37482..d22435ee4e 100644 --- a/tools/public_api_guard/public_api_spec.ts +++ b/tools/public_api_guard/public_api_spec.ts @@ -1046,9 +1046,10 @@ const COMPILER = [ 'CompileQueryMetadata.selectors:Array', 'CompileQueryMetadata.toJson():{[key:string]:any}', 'CompilerConfig', - 'CompilerConfig.constructor(genDebugInfo:boolean, logBindingUpdate:boolean, useJit:boolean, renderTypes:RenderTypes, interpolateRegexp:RegExp)', + 'CompilerConfig.constructor(genDebugInfo:boolean, logBindingUpdate:boolean, useJit:boolean, renderTypes:RenderTypes, interpolateRegexp:RegExp, defaultEncapsulation:ViewEncapsulation)', 'CompilerConfig.renderTypes:RenderTypes', 'CompilerConfig.interpolateRegexp:RegExp', + 'CompilerConfig.defaultEncapsulation:ViewEncapsulation', 'CompileTemplateMetadata', 'CompileTemplateMetadata.animations:CompileAnimationEntryMetadata[]', 'CompileTemplateMetadata.constructor({encapsulation,template,templateUrl,styles,styleUrls,animations,ngContentSelectors}:{encapsulation?:ViewEncapsulation, template?:string, templateUrl?:string, styles?:string[], styleUrls?:string[], ngContentSelectors?:string[], animations?:CompileAnimationEntryMetadata[]})',