Skip to content

Commit

Permalink
feat(ViewEncapsulation): default ViewEncapsulation to configurable
Browse files Browse the repository at this point in the history
BREAKING CHANGES:

DirectiveNormalizer takes new constructor arguments, `config:CompilerConfig`.

Closes #7883
  • Loading branch information
laco0416 authored and mhevery committed May 26, 2016
1 parent c3fafa0 commit f93512b
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 12 deletions.
2 changes: 1 addition & 1 deletion modules/@angular/compiler/src/compile_metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 : [];
Expand Down
10 changes: 9 additions & 1 deletion modules/@angular/compiler/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -16,6 +20,10 @@ export class CompilerConfig {
interpolateRegexp = DEFAULT_INTERPOLATE_REGEXP;
}
this.interpolateRegexp = interpolateRegexp;
if (isBlank(defaultEncapsulation)) {
defaultEncapsulation = ViewEncapsulation.Emulated;
}
this.defaultEncapsulation = defaultEncapsulation;
}
}

Expand Down
8 changes: 6 additions & 2 deletions modules/@angular/compiler/src/directive_normalizer.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -24,14 +24,15 @@ import {
htmlVisitAll
} from './html_ast';
import {HtmlParser} from './html_parser';
import {CompilerConfig} from './config'

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<CompileDirectiveMetadata> {
if (!directive.isComponent) {
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 0 additions & 4 deletions modules/@angular/compiler/test/compile_metadata_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
34 changes: 34 additions & 0 deletions modules/@angular/compiler/test/directive_normalizer_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion modules/@angular/compiler/test/offline_compiler_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(), []),
Expand Down
2 changes: 1 addition & 1 deletion modules/@angular/compiler_cli/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, []);
Expand Down
2 changes: 1 addition & 1 deletion modules/@angular/core/src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,7 @@ export var ContentChildren: ContentChildrenMetadataFactory =
* }
* }
* ```
*
*
* ```html
* <container #container_ref>
* <item>a</item>
Expand Down
3 changes: 2 additions & 1 deletion tools/public_api_guard/public_api_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1046,9 +1046,10 @@ const COMPILER = [
'CompileQueryMetadata.selectors:Array<CompileTokenMetadata>',
'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[]})',
Expand Down

0 comments on commit f93512b

Please sign in to comment.