MCPcopy Create free account
hub / github.com/angular/angular / parseTemplate

Function parseTemplate

packages/compiler/src/render3/view/template.ts:137–271  ·  view source on GitHub ↗
(
  template: string,
  templateUrl: string,
  options: ParseTemplateOptions = {},
)

Source from the content-addressed store, hash-verified

135 * @param options options to modify how the template is parsed
136 */
137export function parseTemplate(
138 template: string,
139 templateUrl: string,
140 options: ParseTemplateOptions = {},
141): ParsedTemplate {
142 const {preserveWhitespaces, enableI18nLegacyMessageIdFormat} = options;
143 const bindingParser = makeBindingParser();
144 const htmlParser = new HtmlParser();
145 const parseResult = htmlParser.parse(template, templateUrl, {
146 leadingTriviaChars: LEADING_TRIVIA_CHARS,
147 ...options,
148 tokenizeExpansionForms: true,
149 tokenizeBlocks: options.enableBlockSyntax ?? true,
150 tokenizeLet: options.enableLetSyntax ?? true,
151 });
152
153 if (
154 !options.alwaysAttemptHtmlToR3AstConversion &&
155 parseResult.errors &&
156 parseResult.errors.length > 0
157 ) {
158 const parsedTemplate: ParsedTemplate = {
159 preserveWhitespaces,
160 errors: parseResult.errors,
161 nodes: [],
162 styleUrls: [],
163 styles: [],
164 ngContentSelectors: [],
165 };
166 if (options.collectCommentNodes) {
167 parsedTemplate.commentNodes = [];
168 }
169 return parsedTemplate;
170 }
171
172 let rootNodes: html.Node[] = parseResult.rootNodes;
173
174 // We need to use the same `retainEmptyTokens` value for both parses to avoid
175 // causing a mismatch when reusing source spans, even if the
176 // `preserveSignificantWhitespace` behavior is different between the two
177 // parses.
178 const retainEmptyTokens = !(options.preserveSignificantWhitespace ?? true);
179
180 // process i18n meta information (scan attributes, generate ids)
181 // before we run whitespace removal process, because existing i18n
182 // extraction process (ng extract-i18n) relies on a raw content to generate
183 // message ids
184 const i18nMetaVisitor = new I18nMetaVisitor(
185 /* keepI18nAttrs */ !preserveWhitespaces,
186 enableI18nLegacyMessageIdFormat,
187 options.preserveSignificantWhitespace,
188 retainEmptyTokens,
189 );
190 const i18nMetaResult = i18nMetaVisitor.visitAllWithErrors(rootNodes);
191
192 if (
193 !options.alwaysAttemptHtmlToR3AstConversion &&
194 i18nMetaResult.errors &&

Callers 15

binding_spec.tsFile · 0.90
tcbFunction · 0.90
parseJitTemplateFunction · 0.90
analyzeMethod · 0.90
visitNodeFunction · 0.90
parseFunction · 0.90
toR3ComponentMetaMethod · 0.90
getBoundTemplateFunction · 0.90
tcbFunction · 0.90
setupFunction · 0.90

Calls 6

parseMethod · 0.95
visitAllWithErrorsMethod · 0.95
htmlAstToRender3AstFunction · 0.90
makeBindingParserFunction · 0.85
visitAllMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected