Skip to content

Commit

Permalink
fix(material/schematics): fix card tmpl migration (#26169)
Browse files Browse the repository at this point in the history
* fixes #26038

(cherry picked from commit c8961cd)
  • Loading branch information
wagnermaciel committed Dec 12, 2022
1 parent f12e3a7 commit 83825bd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
Expand Up @@ -190,6 +190,41 @@ describe('#visitElements', () => {
it('should add value to existing attribute that does not have a value', async () => {
runAddAttributeTest('<a add></a>', '<a add="val"></a>');
});

it('should handle all forms of indentation', async () => {
runAddAttributeTest(
'<a *ngFor="let item of items">',
'<a add="val" *ngFor="let item of items">',
);
runAddAttributeTest(
`
<a
*ngFor="let item of items">`,
`
<a
add="val"
*ngFor="let item of items">`,
);
runAddAttributeTest(
`
<a *ngFor="let item of items"
>`,
`
<a add="val" *ngFor="let item of items"
>`,
);
runAddAttributeTest(
`
<a
[attr]="
val">`,
`
<a
add="val"
[attr]="
val">`,
);
});
});

describe('remove attribute tests', () => {
Expand Down
Expand Up @@ -158,19 +158,24 @@ export function updateAttribute(
const prefix = html.slice(0, index);
const suffix = html.slice(index);
const attrText = newValue ? `${name}="${newValue}"` : `${name}`;
const indentation = parseIndentation(html, node);
return prefix + indentation + attrText + suffix;
}

if (node.startSourceSpan.start.line === node.startSourceSpan.end.line) {
return `${prefix} ${attrText}${suffix}`;
}
function parseIndentation(html: string, node: TmplAstElement): string {
let whitespace = '';
let startOffset = node.startSourceSpan.start.offset + node.name.length + 1;

const attr = node.attributes[0];
if (attr) {
const ctx = attr.sourceSpan.start.getContext(attr.sourceSpan.start.col + 1, 1)!;
const indentation = ctx.before;
return prefix + indentation + attrText + suffix;
}
// Starting after the start source span's tagname,
// read and store each char until we reach a non-whitespace char.

return prefix + attrText + suffix;
for (let i = startOffset; i < node.startSourceSpan.end.offset - 1; i++) {
if (!/\s/.test(html.charAt(i))) {
break;
}
whitespace += html.charAt(i);
}
return whitespace || ' ';
}

/**
Expand Down

0 comments on commit 83825bd

Please sign in to comment.