2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2017-03-02 12:37:01 -05:00
|
|
|
import {ViewEncapsulation, ɵstringify as stringify} from '@angular/core';
|
2017-03-24 12:59:58 -04:00
|
|
|
|
2017-03-14 12:16:15 -04:00
|
|
|
import {CompileAnimationEntryMetadata, CompileDirectiveMetadata, CompileStylesheetMetadata, CompileTemplateMetadata, templateSourceUrl} from './compile_metadata';
|
2016-05-26 19:43:15 -04:00
|
|
|
import {CompilerConfig} from './config';
|
2016-12-15 12:12:40 -05:00
|
|
|
import {CompilerInjectable} from './injectable';
|
2016-08-01 15:19:09 -04:00
|
|
|
import * as html from './ml_parser/ast';
|
|
|
|
import {HtmlParser} from './ml_parser/html_parser';
|
|
|
|
import {InterpolationConfig} from './ml_parser/interpolation_config';
|
2016-08-17 12:24:44 -04:00
|
|
|
import {ResourceLoader} from './resource_loader';
|
2016-06-24 11:46:43 -04:00
|
|
|
import {extractStyleUrls, isStyleUrlResolvable} from './style_url_resolver';
|
2016-07-21 14:41:25 -04:00
|
|
|
import {PreparsedElementType, preparseElement} from './template_parser/template_preparser';
|
2016-06-24 11:46:43 -04:00
|
|
|
import {UrlResolver} from './url_resolver';
|
2017-05-17 18:39:08 -04:00
|
|
|
import {SyncAsync, isDefined, syntaxError} from './util';
|
2015-08-25 18:36:02 -04:00
|
|
|
|
2016-11-10 17:07:30 -05:00
|
|
|
export interface PrenormalizedTemplateMetadata {
|
2017-03-14 12:16:15 -04:00
|
|
|
ngModuleType: any;
|
2016-11-10 17:07:30 -05:00
|
|
|
componentType: any;
|
|
|
|
moduleUrl: string;
|
2017-03-24 12:59:58 -04:00
|
|
|
template: string|null;
|
|
|
|
templateUrl: string|null;
|
|
|
|
styles: string[];
|
|
|
|
styleUrls: string[];
|
|
|
|
interpolation: [string, string]|null;
|
|
|
|
encapsulation: ViewEncapsulation|null;
|
|
|
|
animations: CompileAnimationEntryMetadata[];
|
2016-11-10 17:07:30 -05:00
|
|
|
}
|
|
|
|
|
2016-12-15 12:12:40 -05:00
|
|
|
@CompilerInjectable()
|
2016-01-06 17:13:44 -05:00
|
|
|
export class DirectiveNormalizer {
|
2017-05-17 18:39:08 -04:00
|
|
|
private _resourceLoaderCache = new Map<string, SyncAsync<string>>();
|
2016-06-24 11:46:43 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
constructor(
|
2016-08-17 12:24:44 -04:00
|
|
|
private _resourceLoader: ResourceLoader, private _urlResolver: UrlResolver,
|
|
|
|
private _htmlParser: HtmlParser, private _config: CompilerConfig) {}
|
2015-08-25 18:36:02 -04:00
|
|
|
|
2016-12-11 05:43:51 -05:00
|
|
|
clearCache(): void { this._resourceLoaderCache.clear(); }
|
2016-06-24 11:46:43 -04:00
|
|
|
|
2016-12-11 05:43:51 -05:00
|
|
|
clearCacheFor(normalizedDirective: CompileDirectiveMetadata): void {
|
2016-06-24 11:46:43 -04:00
|
|
|
if (!normalizedDirective.isComponent) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-24 12:59:58 -04:00
|
|
|
const template = normalizedDirective.template !;
|
|
|
|
this._resourceLoaderCache.delete(template.templateUrl !);
|
|
|
|
template.externalStylesheets.forEach(
|
|
|
|
(stylesheet) => { this._resourceLoaderCache.delete(stylesheet.moduleUrl !); });
|
2016-06-24 11:46:43 -04:00
|
|
|
}
|
|
|
|
|
2017-05-17 18:39:08 -04:00
|
|
|
private _fetch(url: string): SyncAsync<string> {
|
2016-11-12 08:08:58 -05:00
|
|
|
let result = this._resourceLoaderCache.get(url);
|
2016-06-24 11:46:43 -04:00
|
|
|
if (!result) {
|
2017-05-17 18:39:08 -04:00
|
|
|
result = this._resourceLoader.get(url);
|
2016-08-17 12:24:44 -04:00
|
|
|
this._resourceLoaderCache.set(url, result);
|
2016-06-24 11:46:43 -04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-11-10 17:07:30 -05:00
|
|
|
normalizeTemplate(prenormData: PrenormalizedTemplateMetadata):
|
2017-05-17 18:39:08 -04:00
|
|
|
SyncAsync<CompileTemplateMetadata> {
|
2017-03-24 12:59:58 -04:00
|
|
|
if (isDefined(prenormData.template)) {
|
|
|
|
if (isDefined(prenormData.templateUrl)) {
|
2017-03-29 13:26:48 -04:00
|
|
|
throw syntaxError(
|
|
|
|
`'${stringify(prenormData.componentType)}' component cannot define both template and templateUrl`);
|
|
|
|
}
|
2016-12-11 05:49:03 -05:00
|
|
|
if (typeof prenormData.template !== 'string') {
|
2017-01-27 16:19:00 -05:00
|
|
|
throw syntaxError(
|
2016-12-11 05:49:03 -05:00
|
|
|
`The template specified for component ${stringify(prenormData.componentType)} is not a string`);
|
|
|
|
}
|
2017-03-24 12:59:58 -04:00
|
|
|
} else if (isDefined(prenormData.templateUrl)) {
|
2016-12-11 05:49:03 -05:00
|
|
|
if (typeof prenormData.templateUrl !== 'string') {
|
2017-01-27 16:19:00 -05:00
|
|
|
throw syntaxError(
|
2016-12-11 05:49:03 -05:00
|
|
|
`The templateUrl specified for component ${stringify(prenormData.componentType)} is not a string`);
|
|
|
|
}
|
2016-06-24 11:46:43 -04:00
|
|
|
} else {
|
2017-01-27 16:19:00 -05:00
|
|
|
throw syntaxError(
|
2016-11-10 17:07:30 -05:00
|
|
|
`No template specified for component ${stringify(prenormData.componentType)}`);
|
2016-06-24 11:46:43 -04:00
|
|
|
}
|
2017-05-17 18:39:08 -04:00
|
|
|
return SyncAsync.then(
|
|
|
|
this.normalizeTemplateOnly(prenormData),
|
|
|
|
(result: CompileTemplateMetadata) => this.normalizeExternalStylesheets(result));
|
|
|
|
}
|
2016-11-10 17:07:30 -05:00
|
|
|
|
2017-05-17 18:39:08 -04:00
|
|
|
normalizeTemplateOnly(prenomData: PrenormalizedTemplateMetadata):
|
|
|
|
SyncAsync<CompileTemplateMetadata> {
|
|
|
|
let template: SyncAsync<string>;
|
|
|
|
let templateUrl: string;
|
|
|
|
if (prenomData.template != null) {
|
|
|
|
template = prenomData.template;
|
|
|
|
templateUrl = prenomData.moduleUrl;
|
2016-06-24 11:46:43 -04:00
|
|
|
} else {
|
2017-05-17 18:39:08 -04:00
|
|
|
templateUrl = this._urlResolver.resolve(prenomData.moduleUrl, prenomData.templateUrl !);
|
|
|
|
template = this._fetch(templateUrl);
|
2016-06-24 11:46:43 -04:00
|
|
|
}
|
2017-05-17 18:39:08 -04:00
|
|
|
return SyncAsync.then(
|
|
|
|
template, (template) => this.normalizeLoadedTemplate(prenomData, template, templateUrl));
|
2015-08-25 18:36:02 -04:00
|
|
|
}
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
normalizeLoadedTemplate(
|
2017-03-14 12:16:15 -04:00
|
|
|
prenormData: PrenormalizedTemplateMetadata, template: string,
|
2016-06-08 19:38:52 -04:00
|
|
|
templateAbsUrl: string): CompileTemplateMetadata {
|
2017-03-14 12:16:15 -04:00
|
|
|
const isInline = !!prenormData.template;
|
2017-03-24 12:59:58 -04:00
|
|
|
const interpolationConfig = InterpolationConfig.fromArray(prenormData.interpolation !);
|
2016-11-10 17:07:30 -05:00
|
|
|
const rootNodesAndErrors = this._htmlParser.parse(
|
2017-03-14 12:16:15 -04:00
|
|
|
template,
|
|
|
|
templateSourceUrl(
|
|
|
|
{reference: prenormData.ngModuleType}, {type: {reference: prenormData.componentType}},
|
|
|
|
{isInline, templateUrl: templateAbsUrl}),
|
|
|
|
true, interpolationConfig);
|
2015-10-07 12:34:21 -04:00
|
|
|
if (rootNodesAndErrors.errors.length > 0) {
|
2016-07-09 13:12:39 -04:00
|
|
|
const errorString = rootNodesAndErrors.errors.join('\n');
|
2017-01-27 16:19:00 -05:00
|
|
|
throw syntaxError(`Template parse errors:\n${errorString}`);
|
2015-10-07 12:34:21 -04:00
|
|
|
}
|
2016-12-22 13:17:41 -05:00
|
|
|
|
2016-07-09 13:12:39 -04:00
|
|
|
const templateMetadataStyles = this.normalizeStylesheet(new CompileStylesheetMetadata({
|
2017-03-14 12:16:15 -04:00
|
|
|
styles: prenormData.styles,
|
|
|
|
styleUrls: prenormData.styleUrls,
|
|
|
|
moduleUrl: prenormData.moduleUrl
|
2016-06-24 11:46:43 -04:00
|
|
|
}));
|
2015-10-07 12:34:21 -04:00
|
|
|
|
2016-07-09 13:12:39 -04:00
|
|
|
const visitor = new TemplatePreparseVisitor();
|
2016-07-21 16:56:58 -04:00
|
|
|
html.visitAll(visitor, rootNodesAndErrors.rootNodes);
|
2016-07-09 13:12:39 -04:00
|
|
|
const templateStyles = this.normalizeStylesheet(new CompileStylesheetMetadata(
|
2016-06-24 11:46:43 -04:00
|
|
|
{styles: visitor.styles, styleUrls: visitor.styleUrls, moduleUrl: templateAbsUrl}));
|
2015-09-14 18:59:09 -04:00
|
|
|
|
2017-03-14 12:16:15 -04:00
|
|
|
let encapsulation = prenormData.encapsulation;
|
2016-12-11 05:43:51 -05:00
|
|
|
if (encapsulation == null) {
|
2016-04-02 19:34:44 -04:00
|
|
|
encapsulation = this._config.defaultEncapsulation;
|
|
|
|
}
|
2016-09-20 17:14:57 -04:00
|
|
|
|
|
|
|
const styles = templateMetadataStyles.styles.concat(templateStyles.styles);
|
|
|
|
const styleUrls = templateMetadataStyles.styleUrls.concat(templateStyles.styleUrls);
|
|
|
|
|
|
|
|
if (encapsulation === ViewEncapsulation.Emulated && styles.length === 0 &&
|
|
|
|
styleUrls.length === 0) {
|
2015-09-18 13:33:23 -04:00
|
|
|
encapsulation = ViewEncapsulation.None;
|
|
|
|
}
|
2016-09-20 17:14:57 -04:00
|
|
|
|
2015-09-18 13:33:23 -04:00
|
|
|
return new CompileTemplateMetadata({
|
2016-06-22 20:25:42 -04:00
|
|
|
encapsulation,
|
2016-09-20 17:14:57 -04:00
|
|
|
template,
|
|
|
|
templateUrl: templateAbsUrl, styles, styleUrls,
|
2016-05-25 15:46:22 -04:00
|
|
|
ngContentSelectors: visitor.ngContentSelectors,
|
2017-03-14 12:16:15 -04:00
|
|
|
animations: prenormData.animations,
|
2017-03-24 12:59:58 -04:00
|
|
|
interpolation: prenormData.interpolation, isInline,
|
|
|
|
externalStylesheets: []
|
2015-08-25 18:36:02 -04:00
|
|
|
});
|
|
|
|
}
|
2016-06-24 11:46:43 -04:00
|
|
|
|
|
|
|
normalizeExternalStylesheets(templateMeta: CompileTemplateMetadata):
|
2017-05-17 18:39:08 -04:00
|
|
|
SyncAsync<CompileTemplateMetadata> {
|
|
|
|
return SyncAsync.then(
|
|
|
|
this._loadMissingExternalStylesheets(templateMeta.styleUrls),
|
|
|
|
(externalStylesheets) => new CompileTemplateMetadata({
|
|
|
|
encapsulation: templateMeta.encapsulation,
|
|
|
|
template: templateMeta.template,
|
|
|
|
templateUrl: templateMeta.templateUrl,
|
|
|
|
styles: templateMeta.styles,
|
|
|
|
styleUrls: templateMeta.styleUrls,
|
|
|
|
externalStylesheets: externalStylesheets,
|
|
|
|
ngContentSelectors: templateMeta.ngContentSelectors,
|
|
|
|
animations: templateMeta.animations,
|
|
|
|
interpolation: templateMeta.interpolation,
|
|
|
|
isInline: templateMeta.isInline,
|
|
|
|
}));
|
2016-06-24 11:46:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private _loadMissingExternalStylesheets(
|
|
|
|
styleUrls: string[],
|
|
|
|
loadedStylesheets:
|
|
|
|
Map<string, CompileStylesheetMetadata> = new Map<string, CompileStylesheetMetadata>()):
|
2017-05-17 18:39:08 -04:00
|
|
|
SyncAsync<CompileStylesheetMetadata[]> {
|
|
|
|
return SyncAsync.then(
|
|
|
|
SyncAsync.all(styleUrls.filter((styleUrl) => !loadedStylesheets.has(styleUrl))
|
|
|
|
.map(
|
|
|
|
styleUrl => SyncAsync.then(
|
|
|
|
this._fetch(styleUrl),
|
|
|
|
(loadedStyle) => {
|
|
|
|
const stylesheet =
|
|
|
|
this.normalizeStylesheet(new CompileStylesheetMetadata(
|
|
|
|
{styles: [loadedStyle], moduleUrl: styleUrl}));
|
|
|
|
loadedStylesheets.set(styleUrl, stylesheet);
|
|
|
|
return this._loadMissingExternalStylesheets(
|
|
|
|
stylesheet.styleUrls, loadedStylesheets);
|
|
|
|
}))),
|
|
|
|
(_) => Array.from(loadedStylesheets.values()));
|
2016-06-24 11:46:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
normalizeStylesheet(stylesheet: CompileStylesheetMetadata): CompileStylesheetMetadata {
|
2017-03-24 12:59:58 -04:00
|
|
|
const moduleUrl = stylesheet.moduleUrl !;
|
2016-11-12 08:08:58 -05:00
|
|
|
const allStyleUrls = stylesheet.styleUrls.filter(isStyleUrlResolvable)
|
2017-03-24 12:59:58 -04:00
|
|
|
.map(url => this._urlResolver.resolve(moduleUrl, url));
|
2016-06-24 11:46:43 -04:00
|
|
|
|
2016-11-12 08:08:58 -05:00
|
|
|
const allStyles = stylesheet.styles.map(style => {
|
2017-03-24 12:59:58 -04:00
|
|
|
const styleWithImports = extractStyleUrls(this._urlResolver, moduleUrl, style);
|
2016-06-24 11:46:43 -04:00
|
|
|
allStyleUrls.push(...styleWithImports.styleUrls);
|
|
|
|
return styleWithImports.style;
|
|
|
|
});
|
|
|
|
|
|
|
|
return new CompileStylesheetMetadata(
|
2017-03-24 12:59:58 -04:00
|
|
|
{styles: allStyles, styleUrls: allStyleUrls, moduleUrl: moduleUrl});
|
2016-06-24 11:46:43 -04:00
|
|
|
}
|
2015-08-25 18:36:02 -04:00
|
|
|
}
|
|
|
|
|
2016-07-21 16:56:58 -04:00
|
|
|
class TemplatePreparseVisitor implements html.Visitor {
|
2015-08-25 18:36:02 -04:00
|
|
|
ngContentSelectors: string[] = [];
|
|
|
|
styles: string[] = [];
|
|
|
|
styleUrls: string[] = [];
|
2015-09-18 13:33:23 -04:00
|
|
|
ngNonBindableStackCount: number = 0;
|
2015-08-25 18:36:02 -04:00
|
|
|
|
2016-07-21 16:56:58 -04:00
|
|
|
visitElement(ast: html.Element, context: any): any {
|
2016-11-12 08:08:58 -05:00
|
|
|
const preparsedElement = preparseElement(ast);
|
2015-09-18 13:33:23 -04:00
|
|
|
switch (preparsedElement.type) {
|
|
|
|
case PreparsedElementType.NG_CONTENT:
|
2015-09-18 13:33:23 -04:00
|
|
|
if (this.ngNonBindableStackCount === 0) {
|
|
|
|
this.ngContentSelectors.push(preparsedElement.selectAttr);
|
|
|
|
}
|
2015-09-18 13:33:23 -04:00
|
|
|
break;
|
|
|
|
case PreparsedElementType.STYLE:
|
2016-11-12 08:08:58 -05:00
|
|
|
let textContent = '';
|
2015-09-18 13:33:23 -04:00
|
|
|
ast.children.forEach(child => {
|
2016-07-21 16:56:58 -04:00
|
|
|
if (child instanceof html.Text) {
|
2016-07-13 14:01:32 -04:00
|
|
|
textContent += child.value;
|
2015-09-18 13:33:23 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
this.styles.push(textContent);
|
|
|
|
break;
|
|
|
|
case PreparsedElementType.STYLESHEET:
|
|
|
|
this.styleUrls.push(preparsedElement.hrefAttr);
|
|
|
|
break;
|
2016-01-25 20:57:29 -05:00
|
|
|
default:
|
|
|
|
break;
|
2015-08-25 18:36:02 -04:00
|
|
|
}
|
2015-09-18 13:33:23 -04:00
|
|
|
if (preparsedElement.nonBindable) {
|
|
|
|
this.ngNonBindableStackCount++;
|
|
|
|
}
|
2016-07-21 16:56:58 -04:00
|
|
|
html.visitAll(this, ast.children);
|
2015-09-18 13:33:23 -04:00
|
|
|
if (preparsedElement.nonBindable) {
|
|
|
|
this.ngNonBindableStackCount--;
|
2015-08-25 18:36:02 -04:00
|
|
|
}
|
2015-09-18 13:33:23 -04:00
|
|
|
return null;
|
2015-08-25 18:36:02 -04:00
|
|
|
}
|
2016-08-12 00:00:35 -04:00
|
|
|
|
2016-12-22 13:17:41 -05:00
|
|
|
visitExpansion(ast: html.Expansion, context: any): any { html.visitAll(this, ast.cases); }
|
|
|
|
|
|
|
|
visitExpansionCase(ast: html.ExpansionCase, context: any): any {
|
|
|
|
html.visitAll(this, ast.expression);
|
|
|
|
}
|
|
|
|
|
2016-07-21 16:56:58 -04:00
|
|
|
visitComment(ast: html.Comment, context: any): any { return null; }
|
|
|
|
visitAttribute(ast: html.Attribute, context: any): any { return null; }
|
|
|
|
visitText(ast: html.Text, context: any): any { return null; }
|
2015-08-25 18:36:02 -04:00
|
|
|
}
|