2015-09-14 15:59:09 -07:00
|
|
|
import {
|
|
|
|
TypeMetadata,
|
|
|
|
TemplateMetadata,
|
|
|
|
NormalizedDirectiveMetadata,
|
|
|
|
NormalizedTemplateMetadata
|
|
|
|
} from './directive_metadata';
|
2015-09-11 13:35:46 -07:00
|
|
|
import {isPresent, isBlank} from 'angular2/src/core/facade/lang';
|
2015-08-25 15:36:02 -07: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 15:07:31 -07:00
|
|
|
import {resolveStyleUrls} from './style_url_resolver';
|
2015-09-14 15:59:09 -07:00
|
|
|
import {Injectable} from 'angular2/src/core/di';
|
2015-08-25 15:36:02 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
HtmlAstVisitor,
|
|
|
|
HtmlElementAst,
|
|
|
|
HtmlTextAst,
|
|
|
|
HtmlAttrAst,
|
|
|
|
HtmlAst,
|
|
|
|
htmlVisitAll
|
|
|
|
} from './html_ast';
|
|
|
|
import {HtmlParser} from './html_parser';
|
|
|
|
|
|
|
|
const NG_CONTENT_SELECT_ATTR = 'select';
|
|
|
|
const NG_CONTENT_ELEMENT = 'ng-content';
|
|
|
|
const LINK_ELEMENT = 'link';
|
|
|
|
const LINK_STYLE_REL_ATTR = 'rel';
|
|
|
|
const LINK_STYLE_HREF_ATTR = 'href';
|
|
|
|
const LINK_STYLE_REL_VALUE = 'stylesheet';
|
|
|
|
const STYLE_ELEMENT = 'style';
|
|
|
|
|
2015-09-14 15:59:09 -07:00
|
|
|
@Injectable()
|
|
|
|
export class TemplateNormalizer {
|
2015-08-25 15:36:02 -07:00
|
|
|
constructor(private _xhr: XHR, private _urlResolver: UrlResolver,
|
2015-09-02 15:07:31 -07:00
|
|
|
private _domParser: HtmlParser) {}
|
2015-08-25 15:36:02 -07:00
|
|
|
|
2015-09-14 15:59:09 -07:00
|
|
|
normalizeTemplate(directiveType: TypeMetadata,
|
|
|
|
template: TemplateMetadata): Promise<NormalizedTemplateMetadata> {
|
|
|
|
if (isPresent(template.template)) {
|
|
|
|
return PromiseWrapper.resolve(this.normalizeLoadedTemplate(
|
|
|
|
directiveType, template, template.template, directiveType.moduleId));
|
2015-08-25 15:36:02 -07:00
|
|
|
} else {
|
2015-09-14 15:59:09 -07:00
|
|
|
var sourceAbsUrl = this._urlResolver.resolve(directiveType.moduleId, template.templateUrl);
|
2015-08-25 15:36:02 -07:00
|
|
|
return this._xhr.get(sourceAbsUrl)
|
2015-09-14 15:59:09 -07:00
|
|
|
.then(templateContent => this.normalizeLoadedTemplate(directiveType, template,
|
|
|
|
templateContent, sourceAbsUrl));
|
2015-08-25 15:36:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:59:09 -07:00
|
|
|
normalizeLoadedTemplate(directiveType: TypeMetadata, templateMeta: TemplateMetadata,
|
|
|
|
template: string, templateAbsUrl: string): NormalizedTemplateMetadata {
|
|
|
|
var domNodes = this._domParser.parse(template, directiveType.name);
|
2015-08-25 15:36:02 -07:00
|
|
|
var visitor = new TemplatePreparseVisitor();
|
|
|
|
var remainingNodes = htmlVisitAll(visitor, domNodes);
|
2015-09-14 15:59:09 -07: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 15:07:31 -07:00
|
|
|
var allResolvedStyles = allStyles.map(style => {
|
2015-09-14 15:59:09 -07:00
|
|
|
var styleWithImports = resolveStyleUrls(this._urlResolver, templateAbsUrl, style);
|
|
|
|
styleWithImports.styleUrls.forEach(styleUrl => allStyleAbsUrls.push(styleUrl));
|
2015-08-25 15:36:02 -07:00
|
|
|
return styleWithImports.style;
|
|
|
|
});
|
2015-09-14 15:59:09 -07:00
|
|
|
return new NormalizedTemplateMetadata({
|
|
|
|
encapsulation: templateMeta.encapsulation,
|
2015-09-11 13:35:46 -07:00
|
|
|
template: this._domParser.unparse(remainingNodes),
|
2015-08-25 15:36:02 -07:00
|
|
|
styles: allResolvedStyles,
|
|
|
|
styleAbsUrls: allStyleAbsUrls,
|
|
|
|
ngContentSelectors: visitor.ngContentSelectors
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class TemplatePreparseVisitor implements HtmlAstVisitor {
|
|
|
|
ngContentSelectors: string[] = [];
|
|
|
|
styles: string[] = [];
|
|
|
|
styleUrls: string[] = [];
|
|
|
|
|
2015-09-11 13:35:46 -07:00
|
|
|
visitElement(ast: HtmlElementAst, context: any): HtmlElementAst {
|
2015-08-25 15:36:02 -07:00
|
|
|
var selectAttr = null;
|
|
|
|
var hrefAttr = null;
|
|
|
|
var relAttr = null;
|
|
|
|
ast.attrs.forEach(attr => {
|
|
|
|
if (attr.name == NG_CONTENT_SELECT_ATTR) {
|
|
|
|
selectAttr = attr.value;
|
|
|
|
} else if (attr.name == LINK_STYLE_HREF_ATTR) {
|
|
|
|
hrefAttr = attr.value;
|
|
|
|
} else if (attr.name == LINK_STYLE_REL_ATTR) {
|
|
|
|
relAttr = attr.value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var nodeName = ast.name;
|
|
|
|
var keepElement = true;
|
|
|
|
if (nodeName == NG_CONTENT_ELEMENT) {
|
2015-09-11 13:35:46 -07:00
|
|
|
this.ngContentSelectors.push(normalizeNgContentSelect(selectAttr));
|
2015-08-25 15:36:02 -07:00
|
|
|
} else if (nodeName == STYLE_ELEMENT) {
|
|
|
|
keepElement = false;
|
|
|
|
var textContent = '';
|
|
|
|
ast.children.forEach(child => {
|
|
|
|
if (child instanceof HtmlTextAst) {
|
|
|
|
textContent += (<HtmlTextAst>child).value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.styles.push(textContent);
|
|
|
|
} else if (nodeName == LINK_ELEMENT && relAttr == LINK_STYLE_REL_VALUE) {
|
|
|
|
keepElement = false;
|
|
|
|
this.styleUrls.push(hrefAttr);
|
|
|
|
}
|
|
|
|
if (keepElement) {
|
|
|
|
return new HtmlElementAst(ast.name, ast.attrs, htmlVisitAll(this, ast.children),
|
|
|
|
ast.sourceInfo);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2015-09-11 13:35:46 -07:00
|
|
|
visitAttr(ast: HtmlAttrAst, context: any): HtmlAttrAst { return ast; }
|
|
|
|
visitText(ast: HtmlTextAst, context: any): HtmlTextAst { return ast; }
|
2015-08-25 15:36:02 -07:00
|
|
|
}
|
2015-09-11 13:35:46 -07:00
|
|
|
|
|
|
|
function normalizeNgContentSelect(selectAttr: string): string {
|
|
|
|
if (isBlank(selectAttr) || selectAttr.length === 0) {
|
|
|
|
return '*';
|
|
|
|
}
|
|
|
|
return selectAttr;
|
|
|
|
}
|