feat(compiler): Add a enableLegacyTemplate
option to support <template>
When the `enableLegacyTemplate` is set to `false`, `<template>` tags and the `template` attribute are no more used to define angular templates but are treated as regular tag and attribute. The default value is `true`. In order to define a template, you have to use the `<ng-template>` tag. This option applies to your application and all the libraries it uses. That is you should make sure none of them rely on the legacy way to defined templates when this option is turned off (`false`).
This commit is contained in:

committed by
Igor Minar

parent
bf8eb41248
commit
e99d721612
@ -61,7 +61,8 @@ export function createAotCompiler(compilerHost: AotCompilerHost, options: AotCom
|
||||
defaultEncapsulation: ViewEncapsulation.Emulated,
|
||||
logBindingUpdate: false,
|
||||
useJit: false,
|
||||
useViewEngine: options.useViewEngine
|
||||
useViewEngine: options.useViewEngine,
|
||||
enableLegacyTemplate: options.enableLegacyTemplate !== false,
|
||||
});
|
||||
const normalizer = new DirectiveNormalizer(
|
||||
{get: (url: string) => compilerHost.loadResource(url)}, urlResolver, htmlParser, config);
|
||||
|
@ -12,4 +12,5 @@ export interface AotCompilerOptions {
|
||||
i18nFormat?: string;
|
||||
translations?: string;
|
||||
useViewEngine?: boolean;
|
||||
enableLegacyTemplate?: boolean;
|
||||
}
|
||||
|
@ -23,22 +23,28 @@ export const USE_VIEW_ENGINE = new InjectionToken<boolean>('UseViewEngine');
|
||||
export class CompilerConfig {
|
||||
public renderTypes: RenderTypes;
|
||||
public defaultEncapsulation: ViewEncapsulation;
|
||||
private _genDebugInfo: boolean;
|
||||
private _logBindingUpdate: boolean;
|
||||
// Whether to support the `<template>` tag and the `template` attribute to define angular
|
||||
// templates. They have been deprecated in 4.x, `<ng-template>` should be used instead.
|
||||
public enableLegacyTemplate: boolean;
|
||||
public useJit: boolean;
|
||||
public useViewEngine: boolean;
|
||||
public missingTranslation: MissingTranslationStrategy;
|
||||
|
||||
private _genDebugInfo: boolean;
|
||||
private _logBindingUpdate: boolean;
|
||||
|
||||
constructor(
|
||||
{renderTypes = new DefaultRenderTypes(), defaultEncapsulation = ViewEncapsulation.Emulated,
|
||||
genDebugInfo, logBindingUpdate, useJit = true, missingTranslation, useViewEngine}: {
|
||||
genDebugInfo, logBindingUpdate, useJit = true, missingTranslation, useViewEngine,
|
||||
enableLegacyTemplate}: {
|
||||
renderTypes?: RenderTypes,
|
||||
defaultEncapsulation?: ViewEncapsulation,
|
||||
genDebugInfo?: boolean,
|
||||
logBindingUpdate?: boolean,
|
||||
useJit?: boolean,
|
||||
missingTranslation?: MissingTranslationStrategy,
|
||||
useViewEngine?: boolean
|
||||
useViewEngine?: boolean,
|
||||
enableLegacyTemplate?: boolean,
|
||||
} = {}) {
|
||||
this.renderTypes = renderTypes;
|
||||
this.defaultEncapsulation = defaultEncapsulation;
|
||||
@ -47,6 +53,7 @@ export class CompilerConfig {
|
||||
this.useJit = useJit;
|
||||
this.missingTranslation = missingTranslation;
|
||||
this.useViewEngine = true;
|
||||
this.enableLegacyTemplate = enableLegacyTemplate !== false;
|
||||
}
|
||||
|
||||
get genDebugInfo(): boolean {
|
||||
|
@ -104,7 +104,6 @@ export const COMPILER_PROVIDERS: Array<any|Type<any>|{[k: string]: any}|any[]> =
|
||||
AnimationParser,
|
||||
];
|
||||
|
||||
|
||||
@CompilerInjectable()
|
||||
export class JitCompilerFactory implements CompilerFactory {
|
||||
private _defaultOptions: CompilerOptions[];
|
||||
@ -136,7 +135,8 @@ export class JitCompilerFactory implements CompilerFactory {
|
||||
// from the app providers
|
||||
defaultEncapsulation: opts.defaultEncapsulation,
|
||||
logBindingUpdate: opts.useDebug,
|
||||
missingTranslation: opts.missingTranslation, useViewEngine
|
||||
missingTranslation: opts.missingTranslation, useViewEngine,
|
||||
enableLegacyTemplate: opts.enableLegacyTemplate,
|
||||
});
|
||||
},
|
||||
deps: [USE_VIEW_ENGINE]
|
||||
|
@ -273,7 +273,7 @@ class TemplateParseVisitor implements html.Visitor {
|
||||
let hasInlineTemplates = false;
|
||||
const attrs: AttrAst[] = [];
|
||||
const isTemplateElement = isTemplate(
|
||||
element,
|
||||
element, this.config.enableLegacyTemplate,
|
||||
(m: string, span: ParseSourceSpan) => this._reportError(m, span, ParseErrorLevel.WARNING));
|
||||
|
||||
element.attrs.forEach(attr => {
|
||||
@ -285,7 +285,7 @@ class TemplateParseVisitor implements html.Visitor {
|
||||
let prefixToken: string|undefined;
|
||||
let normalizedName = this._normalizeAttributeName(attr.name);
|
||||
|
||||
if (normalizedName == TEMPLATE_ATTR) {
|
||||
if (this.config.enableLegacyTemplate && normalizedName == TEMPLATE_ATTR) {
|
||||
this._reportError(
|
||||
`The template attribute is deprecated. Use an ng-template element instead.`,
|
||||
attr.sourceSpan, ParseErrorLevel.WARNING);
|
||||
@ -905,16 +905,19 @@ function isEmptyExpression(ast: AST): boolean {
|
||||
|
||||
// `template` is deprecated in 4.x
|
||||
function isTemplate(
|
||||
el: html.Element, reportDeprecation: (m: string, span: ParseSourceSpan) => void): boolean {
|
||||
el: html.Element, enableLegacyTemplate: boolean,
|
||||
reportDeprecation: (m: string, span: ParseSourceSpan) => void): boolean {
|
||||
const tagNoNs = splitNsName(el.name)[1];
|
||||
// `<ng-template>` is an angular construct and is lower case
|
||||
if (tagNoNs === NG_TEMPLATE_ELEMENT) return true;
|
||||
// `<template>` is HTML and case insensitive
|
||||
if (tagNoNs.toLowerCase() === TEMPLATE_ELEMENT) {
|
||||
reportDeprecation(
|
||||
`The <template> element is deprecated. Use <ng-template> instead`, el.sourceSpan);
|
||||
return true;
|
||||
}
|
||||
if (enableLegacyTemplate && tagNoNs.toLowerCase() === TEMPLATE_ELEMENT) {
|
||||
reportDeprecation(
|
||||
`The <template> element is deprecated. Use <ng-template> instead`, el.sourceSpan);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user