Skip to content

Commit cde7bd3

Browse files
committed
fixup! fix(material/tabs): allow both foreground and background colors to be set (#26212)
1 parent 1d54f9a commit cde7bd3

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,41 @@ describe('#visitElements', () => {
190190
it('should add value to existing attribute that does not have a value', async () => {
191191
runAddAttributeTest('<a add></a>', '<a add="val"></a>');
192192
});
193+
194+
it('should handle all forms of indentation', async () => {
195+
runAddAttributeTest(
196+
'<a *ngFor="let item of items">',
197+
'<a add="val" *ngFor="let item of items">',
198+
);
199+
runAddAttributeTest(
200+
`
201+
<a
202+
*ngFor="let item of items">`,
203+
`
204+
<a
205+
add="val"
206+
*ngFor="let item of items">`,
207+
);
208+
runAddAttributeTest(
209+
`
210+
<a *ngFor="let item of items"
211+
>`,
212+
`
213+
<a add="val" *ngFor="let item of items"
214+
>`,
215+
);
216+
runAddAttributeTest(
217+
`
218+
<a
219+
[attr]="
220+
val">`,
221+
`
222+
<a
223+
add="val"
224+
[attr]="
225+
val">`,
226+
);
227+
});
193228
});
194229

195230
describe('remove attribute tests', () => {

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,24 @@ export function updateAttribute(
163163
return `${prefix} ${attrText}${suffix}`;
164164
}
165165

166-
const binding = node.attributes[0] ?? node.inputs[0] ?? node.outputs[0];
167-
if (binding) {
168-
const ctx = binding.sourceSpan.start.getContext(binding.sourceSpan.start.col + 1, 1)!;
169-
const indentation = ctx.before;
170-
return prefix + indentation + attrText + suffix;
171-
}
166+
const indentation = parseIndentation(html, node);
167+
return prefix + indentation + attrText + suffix;
168+
}
169+
170+
function parseIndentation(html: string, node: TmplAstElement): string {
171+
let whitespace = '';
172+
let startOffset = node.startSourceSpan.start.offset + node.name.length + 1;
172173

173-
return prefix + attrText + suffix;
174+
// Starting after the start source span's tagname,
175+
// read and store each char until we reach a non-whitespace char.
176+
177+
for (let i = startOffset; i < html.length - 1; i++) {
178+
if (!/\s/.test(html.charAt(i))) {
179+
break;
180+
}
181+
whitespace += html.charAt(i);
182+
}
183+
return whitespace;
174184
}
175185

176186
/**

0 commit comments

Comments
 (0)