2015-09-18 10:33:23 -07:00
|
|
|
import {HtmlElementAst} from './html_ast';
|
2015-11-06 17:34:07 -08:00
|
|
|
import {isBlank, isPresent} from 'angular2/src/facade/lang';
|
2015-09-18 10:33:23 -07:00
|
|
|
|
|
|
|
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';
|
|
|
|
const SCRIPT_ELEMENT = 'script';
|
|
|
|
const NG_NON_BINDABLE_ATTR = 'ng-non-bindable';
|
|
|
|
|
|
|
|
export function preparseElement(ast: HtmlElementAst): PreparsedElement {
|
|
|
|
var selectAttr = null;
|
|
|
|
var hrefAttr = null;
|
|
|
|
var relAttr = null;
|
|
|
|
var nonBindable = false;
|
|
|
|
ast.attrs.forEach(attr => {
|
2015-11-10 15:56:25 -08:00
|
|
|
let attrName = attr.name.toLowerCase();
|
|
|
|
if (attrName == NG_CONTENT_SELECT_ATTR) {
|
2015-09-18 10:33:23 -07:00
|
|
|
selectAttr = attr.value;
|
2015-11-10 15:56:25 -08:00
|
|
|
} else if (attrName == LINK_STYLE_HREF_ATTR) {
|
2015-09-18 10:33:23 -07:00
|
|
|
hrefAttr = attr.value;
|
2015-11-10 15:56:25 -08:00
|
|
|
} else if (attrName == LINK_STYLE_REL_ATTR) {
|
2015-09-18 10:33:23 -07:00
|
|
|
relAttr = attr.value;
|
2015-11-10 15:56:25 -08:00
|
|
|
} else if (attrName == NG_NON_BINDABLE_ATTR) {
|
2015-09-18 10:33:23 -07:00
|
|
|
nonBindable = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
selectAttr = normalizeNgContentSelect(selectAttr);
|
2015-11-10 15:56:25 -08:00
|
|
|
var nodeName = ast.name.toLowerCase();
|
2015-09-18 10:33:23 -07:00
|
|
|
var type = PreparsedElementType.OTHER;
|
2015-09-18 10:33:23 -07:00
|
|
|
if (nodeName == NG_CONTENT_ELEMENT) {
|
2015-09-18 10:33:23 -07:00
|
|
|
type = PreparsedElementType.NG_CONTENT;
|
|
|
|
} else if (nodeName == STYLE_ELEMENT) {
|
|
|
|
type = PreparsedElementType.STYLE;
|
|
|
|
} else if (nodeName == SCRIPT_ELEMENT) {
|
|
|
|
type = PreparsedElementType.SCRIPT;
|
|
|
|
} else if (nodeName == LINK_ELEMENT && relAttr == LINK_STYLE_REL_VALUE) {
|
|
|
|
type = PreparsedElementType.STYLESHEET;
|
|
|
|
}
|
2015-09-18 10:33:23 -07:00
|
|
|
return new PreparsedElement(type, selectAttr, hrefAttr, nonBindable);
|
2015-09-18 10:33:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export enum PreparsedElementType {
|
|
|
|
NG_CONTENT,
|
|
|
|
STYLE,
|
|
|
|
STYLESHEET,
|
|
|
|
SCRIPT,
|
|
|
|
OTHER
|
|
|
|
}
|
|
|
|
|
|
|
|
export class PreparsedElement {
|
2015-09-18 10:33:23 -07:00
|
|
|
constructor(public type: PreparsedElementType, public selectAttr: string, public hrefAttr: string,
|
|
|
|
public nonBindable: boolean) {}
|
2015-09-18 10:33:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function normalizeNgContentSelect(selectAttr: string): string {
|
|
|
|
if (isBlank(selectAttr) || selectAttr.length === 0) {
|
|
|
|
return '*';
|
|
|
|
}
|
|
|
|
return selectAttr;
|
|
|
|
}
|