/
templateParser.ts
97 lines (88 loc) · 2.74 KB
/
templateParser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { __core_private__ as r, NO_ERRORS_SCHEMA } from '@angular/core';
import * as compiler from '@angular/compiler';
import { Config, DirectiveDeclaration } from '../config';
let refId = 0;
const dummyMetadataFactory = (declaration: DirectiveDeclaration) => {
if (refId > 1e10) {
refId = 0;
}
return {
inputs: declaration.inputs || [],
outputs: declaration.outputs || [],
hostListeners: declaration.hostListeners || {},
hostProperties: declaration.hostProperties || {},
hostAttributes: declaration.hostAttributes || {},
isSummary: true,
type: {
diDeps: [],
lifecycleHooks: [],
isHost: false,
reference: ++refId + '-ref'
},
isComponent: false,
selector: declaration.selector,
exportAs: declaration.exportAs,
providers: [],
viewProviders: [],
queries: [],
entryComponents: [],
changeDetection: 0,
template: {
isSummary: true,
animations: [],
ngContentSelectors: [],
encapsulation: 0
}
};
};
let defaultDirectives = [];
export const parseTemplate = (template: string, directives: DirectiveDeclaration[] = []) => {
defaultDirectives = directives.map(d => dummyMetadataFactory(d));
const TemplateParser = <any>compiler.TemplateParser;
const expressionParser = new compiler.Parser(new compiler.Lexer());
const elementSchemaRegistry = new compiler.DomElementSchemaRegistry();
const ngConsole = new r.Console();
const htmlParser =
new compiler.I18NHtmlParser(new compiler.HtmlParser());
const tmplParser =
new TemplateParser(expressionParser, elementSchemaRegistry, htmlParser, ngConsole, []);
const interpolation = Config.interpolation;
// Make sure it works with 2.2.x & 2.3.x
const summaryKind = ((compiler as any).CompileSummaryKind || {}).Template;
const templateMetadata: compiler.CompileTemplateMetadata = {
encapsulation: 0,
template: template,
templateUrl: '',
styles: [],
styleUrls: [],
ngContentSelectors: [],
animations: [],
externalStylesheets: [],
interpolation,
toSummary() {
return {
isSummary: true,
animations: this.animations.map(anim => anim.name),
ngContentSelectors: this.ngContentSelectors,
encapsulation: this.encapsulation,
summaryKind: summaryKind
} as any;
}
};
// Make sure it works with 2.2.x & 2.3.x
const type = {
diDeps: [],
lifecycleHooks: [],
reference: null,
// Used by Angular 2.2.x
isHost: false,
name: '',
prefix: '',
moduleUrl: '',
value: '',
identifier: null
};
return tmplParser.tryParse(
compiler.CompileDirectiveMetadata.create({ type, template: templateMetadata }),
template, defaultDirectives, [], [NO_ERRORS_SCHEMA], '').templateAst;
};