(
template: string,
templateUrl: string,
options: ParseTemplateOptions = {},
)
| 135 | * @param options options to modify how the template is parsed |
| 136 | */ |
| 137 | export 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 && |
no test coverage detected