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