feat(ViewEncapsulation): default ViewEncapsulation to configurable

BREAKING CHANGES:

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

Closes #7883
This commit is contained in:
laco0416 2016-04-03 08:34:44 +09:00 committed by Misko Hevery
parent c3fafa0651
commit f93512bf27
9 changed files with 55 additions and 12 deletions

View File

@ -656,7 +656,7 @@ export class CompileTemplateMetadata {
ngContentSelectors?: string[], ngContentSelectors?: string[],
animations?: CompileAnimationEntryMetadata[] animations?: CompileAnimationEntryMetadata[]
} = {}) { } = {}) {
this.encapsulation = isPresent(encapsulation) ? encapsulation : ViewEncapsulation.Emulated; this.encapsulation = encapsulation;
this.template = template; this.template = template;
this.templateUrl = templateUrl; this.templateUrl = templateUrl;
this.styles = isPresent(styles) ? styles : []; this.styles = isPresent(styles) ? styles : [];

View File

@ -2,12 +2,16 @@ import {isBlank} from '../src/facade/lang';
import {unimplemented} from '../src/facade/exceptions'; import {unimplemented} from '../src/facade/exceptions';
import {Identifiers} from './identifiers'; import {Identifiers} from './identifiers';
import {CompileIdentifierMetadata} from './compile_metadata'; import {CompileIdentifierMetadata} from './compile_metadata';
import {ViewEncapsulation} from '@angular/core';
export class CompilerConfig { export class CompilerConfig {
public renderTypes: RenderTypes; public renderTypes: RenderTypes;
public interpolateRegexp: RegExp; public interpolateRegexp: RegExp;
public defaultEncapsulation: ViewEncapsulation;
constructor(public genDebugInfo: boolean, public logBindingUpdate: boolean, 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)) { if (isBlank(renderTypes)) {
renderTypes = new DefaultRenderTypes(); renderTypes = new DefaultRenderTypes();
} }
@ -16,6 +20,10 @@ export class CompilerConfig {
interpolateRegexp = DEFAULT_INTERPOLATE_REGEXP; interpolateRegexp = DEFAULT_INTERPOLATE_REGEXP;
} }
this.interpolateRegexp = interpolateRegexp; this.interpolateRegexp = interpolateRegexp;
if (isBlank(defaultEncapsulation)) {
defaultEncapsulation = ViewEncapsulation.Emulated;
}
this.defaultEncapsulation = defaultEncapsulation;
} }
} }

View File

@ -1,6 +1,6 @@
import {Injectable, ViewEncapsulation} from '@angular/core'; 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 {BaseException} from '../src/facade/exceptions';
import {PromiseWrapper} from '../src/facade/async'; import {PromiseWrapper} from '../src/facade/async';
@ -24,6 +24,7 @@ import {
htmlVisitAll htmlVisitAll
} from './html_ast'; } from './html_ast';
import {HtmlParser} from './html_parser'; import {HtmlParser} from './html_parser';
import {CompilerConfig} from './config'
import {preparseElement, PreparsedElementType} from './template_preparser'; import {preparseElement, PreparsedElementType} from './template_preparser';
@ -31,7 +32,7 @@ import {preparseElement, PreparsedElementType} from './template_preparser';
@Injectable() @Injectable()
export class DirectiveNormalizer { export class DirectiveNormalizer {
constructor(private _xhr: XHR, private _urlResolver: UrlResolver, constructor(private _xhr: XHR, private _urlResolver: UrlResolver,
private _htmlParser: HtmlParser) {} private _htmlParser: HtmlParser, private _config: CompilerConfig) {}
normalizeDirective(directive: CompileDirectiveMetadata): Promise<CompileDirectiveMetadata> { normalizeDirective(directive: CompileDirectiveMetadata): Promise<CompileDirectiveMetadata> {
if (!directive.isComponent) { if (!directive.isComponent) {
@ -99,6 +100,9 @@ export class DirectiveNormalizer {
}); });
var encapsulation = templateMeta.encapsulation; var encapsulation = templateMeta.encapsulation;
if (isBlank(encapsulation)) {
encapsulation = this._config.defaultEncapsulation;
}
if (encapsulation === ViewEncapsulation.Emulated && allResolvedStyles.length === 0 && if (encapsulation === ViewEncapsulation.Emulated && allResolvedStyles.length === 0 &&
allStyleAbsUrls.length === 0) { allStyleAbsUrls.length === 0) {
encapsulation = ViewEncapsulation.None; encapsulation = ViewEncapsulation.None;

View File

@ -172,10 +172,6 @@ export function main() {
}); });
describe('TemplateMetadata', () => { describe('TemplateMetadata', () => {
it('should use ViewEncapsulation.Emulated by default', () => {
expect(new CompileTemplateMetadata({encapsulation: null}).encapsulation)
.toBe(ViewEncapsulation.Emulated);
});
it('should serialize with full data', () => { it('should serialize with full data', () => {
expect(CompileTemplateMetadata.fromJson(fullTemplateMeta.toJson())) expect(CompileTemplateMetadata.fromJson(fullTemplateMeta.toJson()))

View File

@ -12,6 +12,7 @@ import {
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal'; import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
import {CompileTypeMetadata, CompileTemplateMetadata} from '@angular/compiler/src/compile_metadata'; import {CompileTypeMetadata, CompileTemplateMetadata} from '@angular/compiler/src/compile_metadata';
import {ViewEncapsulation} from '@angular/core/src/metadata/view'; import {ViewEncapsulation} from '@angular/core/src/metadata/view';
import {CompilerConfig} from "@angular/compiler/src/config";
import {DirectiveNormalizer} from '@angular/compiler/src/directive_normalizer'; import {DirectiveNormalizer} from '@angular/compiler/src/directive_normalizer';
import {XHR} from '@angular/compiler/src/xhr'; import {XHR} from '@angular/compiler/src/xhr';
@ -81,6 +82,39 @@ export function main() {
async.done(); 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', () => { describe('templateUrl', () => {

View File

@ -49,8 +49,8 @@ function _createOfflineCompiler(xhr: MockXHR, emitter: OutputEmitter): OfflineCo
var urlResolver = createOfflineCompileUrlResolver(); var urlResolver = createOfflineCompileUrlResolver();
xhr.when(`${THIS_MODULE_PATH}/offline_compiler_compa.html`, 'Hello World {{user}}!'); xhr.when(`${THIS_MODULE_PATH}/offline_compiler_compa.html`, 'Hello World {{user}}!');
var htmlParser = new HtmlParser(); var htmlParser = new HtmlParser();
var normalizer = new DirectiveNormalizer(xhr, urlResolver, htmlParser);
var config = new CompilerConfig(true, true, true); var config = new CompilerConfig(true, true, true);
var normalizer = new DirectiveNormalizer(xhr, urlResolver, htmlParser, config);
return new OfflineCompiler( return new OfflineCompiler(
normalizer, new TemplateParser(new Parser(new Lexer(), config), new MockSchemaRegistry({}, {}), normalizer, new TemplateParser(new Parser(new Lexer(), config), new MockSchemaRegistry({}, {}),
htmlParser, new Console(), []), htmlParser, new Console(), []),

View File

@ -156,8 +156,8 @@ export class CodeGenerator {
const staticReflector = new StaticReflector(reflectorHost); const staticReflector = new StaticReflector(reflectorHost);
StaticAndDynamicReflectionCapabilities.install(staticReflector); StaticAndDynamicReflectionCapabilities.install(staticReflector);
const htmlParser = new HtmlParser(); const htmlParser = new HtmlParser();
const normalizer = new DirectiveNormalizer(xhr, urlResolver, htmlParser);
const config = new compiler.CompilerConfig(true, true, true); const config = new compiler.CompilerConfig(true, true, true);
const normalizer = new DirectiveNormalizer(xhr, urlResolver, htmlParser, config);
const parser = new Parser(new Lexer(), config); const parser = new Parser(new Lexer(), config);
const tmplParser = new TemplateParser(parser, new DomElementSchemaRegistry(), htmlParser, const tmplParser = new TemplateParser(parser, new DomElementSchemaRegistry(), htmlParser,
/*console*/ null, []); /*console*/ null, []);

View File

@ -1120,7 +1120,7 @@ export var ContentChildren: ContentChildrenMetadataFactory =
* } * }
* } * }
* ``` * ```
* *
* ```html * ```html
* <container #container_ref> * <container #container_ref>
* <item>a</item> * <item>a</item>

View File

@ -1046,9 +1046,10 @@ const COMPILER = [
'CompileQueryMetadata.selectors:Array<CompileTokenMetadata>', 'CompileQueryMetadata.selectors:Array<CompileTokenMetadata>',
'CompileQueryMetadata.toJson():{[key:string]:any}', 'CompileQueryMetadata.toJson():{[key:string]:any}',
'CompilerConfig', '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.renderTypes:RenderTypes',
'CompilerConfig.interpolateRegexp:RegExp', 'CompilerConfig.interpolateRegexp:RegExp',
'CompilerConfig.defaultEncapsulation:ViewEncapsulation',
'CompileTemplateMetadata', 'CompileTemplateMetadata',
'CompileTemplateMetadata.animations:CompileAnimationEntryMetadata[]', '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[]})', 'CompileTemplateMetadata.constructor({encapsulation,template,templateUrl,styles,styleUrls,animations,ngContentSelectors}:{encapsulation?:ViewEncapsulation, template?:string, templateUrl?:string, styles?:string[], styleUrls?:string[], ngContentSelectors?:string[], animations?:CompileAnimationEntryMetadata[]})',