diff --git a/modules/@angular/compiler/src/compile_metadata.ts b/modules/@angular/compiler/src/compile_metadata.ts index ae7ad7df47c7e..8541d75dec194 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 fe29bfa7f4af5..365b36d5e96a5 100644 --- a/modules/@angular/compiler/src/config.ts +++ b/modules/@angular/compiler/src/config.ts @@ -2,15 +2,22 @@ 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 defaultEncapsulation: ViewEncapsulation; constructor(public genDebugInfo: boolean, public logBindingUpdate: boolean, - public useJit: boolean, renderTypes: RenderTypes = null) { + public useJit: boolean, renderTypes: RenderTypes = null, + defaultEncapsulation: ViewEncapsulation = null) { if (isBlank(renderTypes)) { renderTypes = new DefaultRenderTypes(); } this.renderTypes = renderTypes; + 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 4100dd77bc1d2..e042b72b8068b 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 6e18b08d4e3fd..feff538356e59 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 ed75fd4f48acf..5a5bf6bcf5892 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 dd1fbc6847953..af015a5638768 100644 --- a/modules/@angular/compiler/test/offline_compiler_util.ts +++ b/modules/@angular/compiler/test/offline_compiler_util.ts @@ -49,11 +49,12 @@ 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()), new MockSchemaRegistry({}, {}), htmlParser, new Console(), []), - new StyleCompiler(urlResolver), new ViewCompiler(new CompilerConfig(true, true, true)), + new StyleCompiler(urlResolver), new ViewCompiler(config), emitter, xhr); } @@ -79,4 +80,4 @@ export class SimpleJsImportGenerator implements ImportGenerator { return importedUrlStr; } } -} \ No newline at end of file +} diff --git a/modules/@angular/compiler_cli/src/codegen.ts b/modules/@angular/compiler_cli/src/codegen.ts index 1eaac182361be..670800601664a 100644 --- a/modules/@angular/compiler_cli/src/codegen.ts +++ b/modules/@angular/compiler_cli/src/codegen.ts @@ -156,13 +156,14 @@ 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()); const tmplParser = new TemplateParser(parser, new DomElementSchemaRegistry(), htmlParser, /*console*/ null, []); const offlineCompiler = new compiler.OfflineCompiler( normalizer, tmplParser, new StyleCompiler(urlResolver), - new ViewCompiler(new compiler.CompilerConfig(true, true, true)), + new ViewCompiler(config), new TypeScriptEmitter(reflectorHost), xhr); const resolver = new CompileMetadataResolver( new compiler.DirectiveResolver(staticReflector), new compiler.PipeResolver(staticReflector), diff --git a/modules/@angular/core/src/metadata.ts b/modules/@angular/core/src/metadata.ts index 7755040aa8f04..a4559b48e1afe 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 8bbbd31b474fd..cdf2280d7a3df 100644 --- a/tools/public_api_guard/public_api_spec.ts +++ b/tools/public_api_guard/public_api_spec.ts @@ -1044,8 +1044,9 @@ const COMPILER = [ 'CompileQueryMetadata.selectors:Array', 'CompileQueryMetadata.toJson():{[key:string]:any}', 'CompilerConfig', - 'CompilerConfig.constructor(genDebugInfo:boolean, logBindingUpdate:boolean, useJit:boolean, renderTypes:RenderTypes)', + 'CompilerConfig.constructor(genDebugInfo:boolean, logBindingUpdate:boolean, useJit:boolean, renderTypes:RenderTypes, defaultEncapsulation:ViewEncapsulation)', 'CompilerConfig.renderTypes:RenderTypes', + '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[]})',