|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import * as compiler from '@angular/compiler'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Traverses the given tree of nodes and runs the given callbacks for each Element node encountered. |
| 13 | + * |
| 14 | + * Note that updates to the start tags of html element should be done in the postorder callback, |
| 15 | + * and updates to the end tags of html elements should be done in the preorder callback to avoid |
| 16 | + * issues with line collisions. |
| 17 | + * |
| 18 | + * @param nodes The nodes of the ast from a parsed template. |
| 19 | + * @param preorderCallback A function that gets run for each Element node in a preorder traversal. |
| 20 | + * @param postorderCallback A function that gets run for each Element node in a postorder traversal. |
| 21 | + */ |
| 22 | +export function visitElements( |
| 23 | + nodes: compiler.TmplAstNode[], |
| 24 | + preorderCallback: (node: compiler.TmplAstElement) => void = () => {}, |
| 25 | + postorderCallback: (node: compiler.TmplAstElement) => void = () => {}, |
| 26 | +): void { |
| 27 | + nodes.reverse(); |
| 28 | + for (let i = 0; i < nodes.length; i++) { |
| 29 | + const node = nodes[i]; |
| 30 | + if (node instanceof compiler.TmplAstElement) { |
| 31 | + preorderCallback(node); |
| 32 | + visitElements(node.children, preorderCallback, postorderCallback); |
| 33 | + postorderCallback(node); |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * A wrapper for the Angular compilers parseTemplate, which passes the correct options to ensure |
| 40 | + * the parsed template is accurate. |
| 41 | + * |
| 42 | + * For more details, see https://github.com/angular/angular/blob/4332897baa2226ef246ee054fdd5254e3c129109/packages/compiler-cli/src/ngtsc/annotations/component/src/resources.ts#L230. |
| 43 | + * |
| 44 | + * @param html text of the template to parse |
| 45 | + * @param filePath URL to use for source mapping of the parsed template |
| 46 | + * @returns the updated template html. |
| 47 | + */ |
| 48 | +export function parseTemplate(template: string, templateUrl: string = ''): compiler.ParsedTemplate { |
| 49 | + return compiler.parseTemplate(template, templateUrl, { |
| 50 | + preserveWhitespaces: true, |
| 51 | + preserveLineEndings: true, |
| 52 | + leadingTriviaChars: [], |
| 53 | + }); |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Replaces the start tag of the given Element node inside of the html document with a new tag name. |
| 58 | + * |
| 59 | + * @param html The full html document. |
| 60 | + * @param node The Element node to be updated. |
| 61 | + * @param tag A new tag name. |
| 62 | + * @returns an updated html document. |
| 63 | + */ |
| 64 | +export function replaceStartTag(html: string, node: compiler.TmplAstElement, tag: string): string { |
| 65 | + return replaceAt(html, node.startSourceSpan.start.offset + 1, node.name, tag); |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Replaces the end tag of the given Element node inside of the html document with a new tag name. |
| 70 | + * |
| 71 | + * @param html The full html document. |
| 72 | + * @param node The Element node to be updated. |
| 73 | + * @param tag A new tag name. |
| 74 | + * @returns an updated html document. |
| 75 | + */ |
| 76 | +export function replaceEndTag(html: string, node: compiler.TmplAstElement, tag: string): string { |
| 77 | + if (!node.endSourceSpan) { |
| 78 | + return html; |
| 79 | + } |
| 80 | + return replaceAt(html, node.endSourceSpan.start.offset + 2, node.name, tag); |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Replaces a substring of a given string starting at some offset index. |
| 85 | + * |
| 86 | + * @param str A string to be updated. |
| 87 | + * @param offset An offset index to start at. |
| 88 | + * @param oldSubstr The old substring to be replaced. |
| 89 | + * @param newSubstr A new substring. |
| 90 | + * @returns the updated string. |
| 91 | + */ |
| 92 | +function replaceAt(str: string, offset: number, oldSubstr: string, newSubstr: string): string { |
| 93 | + const index = offset; |
| 94 | + const prefix = str.slice(0, index); |
| 95 | + const suffix = str.slice(index + oldSubstr.length); |
| 96 | + return prefix + newSubstr + suffix; |
| 97 | +} |
0 commit comments