Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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