2015-09-14 18:59:09 -04:00
|
|
|
import {
|
2015-09-18 13:33:23 -04:00
|
|
|
CompileTypeMetadata,
|
|
|
|
CompileDirectiveMetadata,
|
|
|
|
CompileTemplateMetadata
|
2015-09-14 18:59:09 -04:00
|
|
|
} from './directive_metadata';
|
2015-09-11 16:35:46 -04:00
|
|
|
import {isPresent, isBlank} from 'angular2/src/core/facade/lang';
|
2015-08-25 18:36:02 -04:00
|
|
|
import {Promise, PromiseWrapper} from 'angular2/src/core/facade/async';
|
|
|
|
|
|
|
|
import {XHR} from 'angular2/src/core/render/xhr';
|
|
|
|
import {UrlResolver} from 'angular2/src/core/services/url_resolver';
|
2015-09-02 18:07:31 -04:00
|
|
|
import {resolveStyleUrls} from './style_url_resolver';
|
2015-09-14 18:59:09 -04:00
|
|
|
import {Injectable} from 'angular2/src/core/di';
|
2015-09-18 13:33:23 -04:00
|
|
|
import {ViewEncapsulation} from 'angular2/src/core/render/api';
|
2015-08-25 18:36:02 -04:00
|
|
|
|
|
|
|
import {
|
|
|
|
HtmlAstVisitor,
|
|
|
|
HtmlElementAst,
|
|
|
|
HtmlTextAst,
|
|
|
|
HtmlAttrAst,
|
|
|
|
HtmlAst,
|
|
|
|
htmlVisitAll
|
|
|
|
} from './html_ast';
|
|
|
|
import {HtmlParser} from './html_parser';
|
|
|
|
|
2015-09-18 13:33:23 -04:00
|
|
|
import {preparseElement, PreparsedElement, PreparsedElementType} from './template_preparser';
|
2015-08-25 18:36:02 -04:00
|
|
|
|
2015-09-14 18:59:09 -04:00
|
|
|
@Injectable()
|
|
|
|
export class TemplateNormalizer {
|
2015-08-25 18:36:02 -04:00
|
|
|
constructor(private _xhr: XHR, private _urlResolver: UrlResolver,
|
2015-09-02 18:07:31 -04:00
|
|
|
private _domParser: HtmlParser) {}
|
2015-08-25 18:36:02 -04:00
|
|
|
|
2015-09-18 13:33:23 -04:00
|
|
|
normalizeTemplate(directiveType: CompileTypeMetadata,
|
|
|
|
template: CompileTemplateMetadata): Promise<CompileTemplateMetadata> {
|
2015-09-14 18:59:09 -04:00
|
|
|
if (isPresent(template.template)) {
|
|
|
|
return PromiseWrapper.resolve(this.normalizeLoadedTemplate(
|
|
|
|
directiveType, template, template.template, directiveType.moduleId));
|
2015-08-25 18:36:02 -04:00
|
|
|
} else {
|
2015-09-14 18:59:09 -04:00
|
|
|
var sourceAbsUrl = this._urlResolver.resolve(directiveType.moduleId, template.templateUrl);
|
2015-08-25 18:36:02 -04:00
|
|
|
return this._xhr.get(sourceAbsUrl)
|
2015-09-14 18:59:09 -04:00
|
|
|
.then(templateContent => this.normalizeLoadedTemplate(directiveType, template,
|
|
|
|
templateContent, sourceAbsUrl));
|
2015-08-25 18:36:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-18 13:33:23 -04:00
|
|
|
normalizeLoadedTemplate(directiveType: CompileTypeMetadata, templateMeta: CompileTemplateMetadata,
|
|
|
|
template: string, templateAbsUrl: string): CompileTemplateMetadata {
|
2015-09-14 18:59:09 -04:00
|
|
|
var domNodes = this._domParser.parse(template, directiveType.name);
|
2015-08-25 18:36:02 -04:00
|
|
|
var visitor = new TemplatePreparseVisitor();
|
2015-09-18 13:33:23 -04:00
|
|
|
htmlVisitAll(visitor, domNodes);
|
2015-09-14 18:59:09 -04:00
|
|
|
var allStyles = templateMeta.styles.concat(visitor.styles);
|
|
|
|
|
|
|
|
var allStyleAbsUrls =
|
|
|
|
visitor.styleUrls.map(url => this._urlResolver.resolve(templateAbsUrl, url))
|
|
|
|
.concat(templateMeta.styleUrls.map(
|
|
|
|
url => this._urlResolver.resolve(directiveType.moduleId, url)));
|
|
|
|
|
2015-09-02 18:07:31 -04:00
|
|
|
var allResolvedStyles = allStyles.map(style => {
|
2015-09-14 18:59:09 -04:00
|
|
|
var styleWithImports = resolveStyleUrls(this._urlResolver, templateAbsUrl, style);
|
|
|
|
styleWithImports.styleUrls.forEach(styleUrl => allStyleAbsUrls.push(styleUrl));
|
2015-08-25 18:36:02 -04:00
|
|
|
return styleWithImports.style;
|
|
|
|
});
|
2015-09-18 13:33:23 -04:00
|
|
|
var encapsulation = templateMeta.encapsulation;
|
|
|
|
if (encapsulation === ViewEncapsulation.Emulated && allResolvedStyles.length === 0 &&
|
|
|
|
allStyleAbsUrls.length === 0) {
|
|
|
|
encapsulation = ViewEncapsulation.None;
|
|
|
|
}
|
|
|
|
return new CompileTemplateMetadata({
|
|
|
|
encapsulation: encapsulation,
|
|
|
|
template: template,
|
|
|
|
templateUrl: templateAbsUrl,
|
2015-08-25 18:36:02 -04:00
|
|
|
styles: allResolvedStyles,
|
2015-09-18 13:33:23 -04:00
|
|
|
styleUrls: allStyleAbsUrls,
|
2015-08-25 18:36:02 -04:00
|
|
|
ngContentSelectors: visitor.ngContentSelectors
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class TemplatePreparseVisitor implements HtmlAstVisitor {
|
|
|
|
ngContentSelectors: string[] = [];
|
|
|
|
styles: string[] = [];
|
|
|
|
styleUrls: string[] = [];
|
|
|
|
|
2015-09-18 13:33:23 -04:00
|
|
|
visitElement(ast: HtmlElementAst, context: any): any {
|
|
|
|
var preparsedElement = preparseElement(ast);
|
|
|
|
switch (preparsedElement.type) {
|
|
|
|
case PreparsedElementType.NG_CONTENT:
|
|
|
|
this.ngContentSelectors.push(preparsedElement.selectAttr);
|
|
|
|
break;
|
|
|
|
case PreparsedElementType.STYLE:
|
|
|
|
var textContent = '';
|
|
|
|
ast.children.forEach(child => {
|
|
|
|
if (child instanceof HtmlTextAst) {
|
|
|
|
textContent += (<HtmlTextAst>child).value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.styles.push(textContent);
|
|
|
|
break;
|
|
|
|
case PreparsedElementType.STYLESHEET:
|
|
|
|
this.styleUrls.push(preparsedElement.hrefAttr);
|
|
|
|
break;
|
2015-08-25 18:36:02 -04:00
|
|
|
}
|
2015-09-18 13:33:23 -04:00
|
|
|
if (preparsedElement.type !== PreparsedElementType.NON_BINDABLE) {
|
|
|
|
htmlVisitAll(this, ast.children);
|
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
|
|
|
}
|
2015-09-18 13:33:23 -04:00
|
|
|
visitAttr(ast: HtmlAttrAst, context: any): any { return null; }
|
|
|
|
visitText(ast: HtmlTextAst, context: any): any { return null; }
|
2015-08-25 18:36:02 -04:00
|
|
|
}
|