Skip to content

Commit

Permalink
fixup! fix(material/tabs): allow both foreground and background color…
Browse files Browse the repository at this point in the history
…s to be set (angular#26212)
  • Loading branch information
wagnermaciel committed Dec 9, 2022
1 parent 1d54f9a commit cde7bd3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 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 @@ -163,14 +163,24 @@ export function updateAttribute(
return `${prefix} ${attrText}${suffix}`;
}

const binding = node.attributes[0] ?? node.inputs[0] ?? node.outputs[0];
if (binding) {
const ctx = binding.sourceSpan.start.getContext(binding.sourceSpan.start.col + 1, 1)!;
const indentation = ctx.before;
return prefix + indentation + attrText + suffix;
}
const indentation = parseIndentation(html, node);
return prefix + indentation + attrText + suffix;
}

function parseIndentation(html: string, node: TmplAstElement): string {
let whitespace = '';
let startOffset = node.startSourceSpan.start.offset + node.name.length + 1;

return prefix + attrText + suffix;
// Starting after the start source span's tagname,
// read and store each char until we reach a non-whitespace char.

for (let i = startOffset; i < html.length - 1; i++) {
if (!/\s/.test(html.charAt(i))) {
break;
}
whitespace += html.charAt(i);
}
return whitespace;
}

/**
Expand Down

0 comments on commit cde7bd3

Please sign in to comment.