Skip to content

Commit 801c23c

Browse files
wagnermacielmmalerba
authored andcommitted
feat(material/schematics): add fn for adding attrs to templates (#24550)
* create and unit test addAttribute fn
1 parent 33c3277 commit 801c23c

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/material/schematics/ng-generate/mdc-migration/rules/tree-traversal.spec.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import {visitElements, parseTemplate, replaceStartTag, replaceEndTag} from './tree-traversal';
1+
import {
2+
addAttribute,
3+
visitElements,
4+
parseTemplate,
5+
replaceStartTag,
6+
replaceEndTag,
7+
} from './tree-traversal';
28

39
function runTagNameDuplicationTest(html: string, result: string): void {
410
visitElements(
@@ -13,6 +19,13 @@ function runTagNameDuplicationTest(html: string, result: string): void {
1319
expect(html).toBe(result);
1420
}
1521

22+
function runAddAttributeTest(html: string, result: string): void {
23+
visitElements(parseTemplate(html).nodes, undefined, node => {
24+
html = addAttribute(html, node, 'attr', 'val');
25+
});
26+
expect(html).toBe(result);
27+
}
28+
1629
describe('#visitElements', () => {
1730
describe('tag name replacements', () => {
1831
it('should handle basic cases', async () => {
@@ -76,4 +89,25 @@ describe('#visitElements', () => {
7689
);
7790
});
7891
});
92+
93+
describe('add attribute tests', () => {
94+
it('should handle single element', async () => {
95+
runAddAttributeTest('<a></a>', '<a attr="val"></a>');
96+
});
97+
98+
it('should handle multiple unnested', async () => {
99+
runAddAttributeTest('<a></a><b></b>', '<a attr="val"></a><b attr="val"></b>');
100+
});
101+
102+
it('should handle multiple nested', async () => {
103+
runAddAttributeTest('<a><b></b></a>', '<a attr="val"><b attr="val"></b></a>');
104+
});
105+
106+
it('should handle multiple nested and unnested', async () => {
107+
runAddAttributeTest(
108+
'<a><b></b><c></c></a>',
109+
'<a attr="val"><b attr="val"></b><c attr="val"></c></a>',
110+
);
111+
});
112+
});
79113
});

src/material/schematics/ng-generate/mdc-migration/rules/tree-traversal.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,27 @@ export function replaceEndTag(html: string, node: compiler.TmplAstElement, tag:
8080
return replaceAt(html, node.endSourceSpan.start.offset + 2, node.name, tag);
8181
}
8282

83+
/**
84+
* Appends an attribute to the given node of the template html.
85+
*
86+
* @param html The template html to be updated.
87+
* @param node The node to be updated.
88+
* @param name The name of the attribute.
89+
* @param value The value of the attribute.
90+
* @returns The updated template html.
91+
*/
92+
export function addAttribute(
93+
html: string,
94+
node: compiler.TmplAstElement,
95+
name: string,
96+
value: string,
97+
): string {
98+
const index = node.startSourceSpan.start.offset + node.name.length + 1;
99+
const prefix = html.slice(0, index);
100+
const suffix = html.slice(index);
101+
return prefix + ` ${name}="${value}"` + suffix;
102+
}
103+
83104
/**
84105
* Replaces a substring of a given string starting at some offset index.
85106
*

0 commit comments

Comments
 (0)