Skip to content

Commit 8442790

Browse files
[sitecore-jss-angular] Prevent the scLink directive from adding an empty href attribute (#406)
1 parent 407d5b7 commit 8442790

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

packages/sitecore-jss-angular/src/components/link.directive.spec.ts

+33
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@ describe('<a *scLink />', () => {
8787
expect(rendered.nativeElement.innerHTML).toBe(field.text);
8888
});
8989

90+
it('should render value without href', () => {
91+
const field = {
92+
value: {
93+
href: '',
94+
},
95+
text: 'ipsum',
96+
};
97+
comp.editable = false;
98+
comp.field = field;
99+
fixture.detectChanges();
100+
101+
const rendered = de.query(By.css('a'));
102+
103+
expect(rendered.nativeElement.href).toBe('');
104+
expect(rendered.nativeElement.innerHTML).toBe(field.text);
105+
});
106+
90107
it('should render ee HTML', () => {
91108
const field = {
92109
editableFirstPart: eeLinkData,
@@ -219,4 +236,20 @@ describe('<a *scLink>children</a>', () => {
219236
expect(rendered.nativeElement.href).toContain(field.href);
220237
expect(rendered.nativeElement.innerHTML).toContain('<span>hello world</span>');
221238
});
239+
240+
it('should render children and value without href', () => {
241+
const field = {
242+
value: {
243+
href: '',
244+
},
245+
text: 'ipsum',
246+
};
247+
comp.field = field;
248+
fixture.detectChanges();
249+
250+
const rendered = de.query(By.css('a'));
251+
252+
expect(rendered.nativeElement.href).toBe('');
253+
expect(rendered.nativeElement.innerHTML).toContain('<span>hello world</span>')
254+
});
222255
});

packages/sitecore-jss-angular/src/components/link.directive.ts

+8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ export class LinkDirective implements OnChanges {
5050

5151
viewRef.rootNodes.forEach((node) => {
5252
Object.entries(props).forEach(([key, propValue]: [string, any]) => {
53+
if (key === 'href' && !propValue) {
54+
if (!node.href) {
55+
return;
56+
}
57+
58+
propValue = node.href;
59+
}
60+
5361
if (key === 'class' && node.className) {
5462
propValue += ` ${node.className}`;
5563
}

0 commit comments

Comments
 (0)