Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(material/schematics): fix card tmpl migration #26169

Merged
merged 1 commit into from Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 || ' ';
devversion marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down