Skip to content

Commit 92b23f4

Browse files
alxhubatscott
authored andcommitted
fix(compiler-cli): properly index <svg> elements (#44678)
In the `Element` node of a parsed `<svg>` element, the `name` is recorded as `:svg:svg`. When the Angular Indexer ran over this element, it would attempt to find this name in the template text and fail, as the namespace portion of the name was added automatically at parse time and is of course missing from the original template. This commit changes the indexer to detect these namespaced elements and only search for the tag name. PR Close #44678
1 parent b8ed03b commit 92b23f4

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

packages/compiler-cli/src/ngtsc/indexer/src/template.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,15 @@ class TemplateVisitor extends TmplAstRecursiveVisitor {
232232
name = node.tagName;
233233
kind = IdentifierKind.Template;
234234
} else {
235-
name = node.name;
235+
// Namespaced elements have a particular format for `node.name` that needs to be handled.
236+
// For example, an `<svg>` element has a `node.name` of `':svg:svg'`.
237+
238+
// TODO(alxhub): properly handle namespaced elements
239+
if (node.name.startsWith(':')) {
240+
name = node.name.split(':').pop()!;
241+
} else {
242+
name = node.name;
243+
}
236244
kind = IdentifierKind.Element;
237245
}
238246
const sourceSpan = node.startSourceSpan;

packages/compiler-cli/src/ngtsc/indexer/test/template_spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ function bind(template: string) {
2121

2222
runInEachFileSystem(() => {
2323
describe('getTemplateIdentifiers', () => {
24+
it('should handle svg elements', () => {
25+
const template = '<svg></svg>';
26+
const refs = getTemplateIdentifiers(bind(template));
27+
28+
const [ref] = Array.from(refs);
29+
expect(ref).toEqual({
30+
kind: IdentifierKind.Element,
31+
name: 'svg',
32+
span: new AbsoluteSourceSpan(1, 4),
33+
usedDirectives: new Set(),
34+
attributes: new Set(),
35+
});
36+
});
37+
2438
it('should handle comments in interpolations', () => {
2539
const template = '{{foo // comment}}';
2640
const refs = getTemplateIdentifiers(bind(template));

0 commit comments

Comments
 (0)