Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat(ViewEncapsulation): default ViewEncapsulation to configurable
BREAKING CHANGES:

DirectiveNormalizer takes new constructor arguments, `config:CompilerConfig`.
  • Loading branch information
laco0416
laco0416 committed May 26, 2016
commit cbb15ea8e9723c388a4f3f8ff5f5d22ca677f003
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
9 changes: 8 additions & 1 deletion modules/@angular/compiler/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

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
7 changes: 4 additions & 3 deletions modules/@angular/compiler/test/offline_compiler_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -79,4 +80,4 @@ export class SimpleJsImportGenerator implements ImportGenerator {
return importedUrlStr;
}
}
}
}
5 changes: 3 additions & 2 deletions modules/@angular/compiler_cli/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
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 @@ -1044,8 +1044,9 @@ const COMPILER = [
'CompileQueryMetadata.selectors:Array<CompileTokenMetadata>',
'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[]})',
Expand Down