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

Prevent the Angular scLink directive from adding an href attribute, when href is equal to http:// or https:// #409

Merged
merged 1 commit into from
Jul 16, 2020
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
122 changes: 104 additions & 18 deletions packages/sitecore-jss-angular/src/components/link.directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,6 @@ describe('<a *scLink />', () => {
expect(rendered.nativeElement.innerHTML).toBe(field.text);
});

it('should render value without href', () => {
const field = {
value: {
href: '',
},
text: 'ipsum',
};
comp.editable = false;
comp.field = field;
fixture.detectChanges();

const rendered = de.query(By.css('a'));

expect(rendered.nativeElement.href).toBe('');
expect(rendered.nativeElement.innerHTML).toBe(field.text);
});

it('should render ee HTML', () => {
const field = {
editableFirstPart: eeLinkData,
Expand Down Expand Up @@ -181,6 +164,59 @@ describe('<a *scLink />', () => {
expect(rendered.nativeElement.target).toBe('_blank');
expect(rendered.nativeElement.title).toBe('footip');
});

describe('should render empty href when href attribute is invalid', () => {
it('undefined', () => {
const field = {
value: {
href: '',
},
text: 'ipsum',
};
comp.editable = false;
comp.field = field;
fixture.detectChanges();

const rendered = de.query(By.css('a'));

expect(rendered.nativeElement.href).toBe('');
expect(rendered.nativeElement.innerHTML).toBe(field.text);
});

it('http://', () => {
const field = {
value: {
href: 'http://',
},
text: 'ipsum',
};
comp.editable = false;
comp.field = field;
fixture.detectChanges();

const rendered = de.query(By.css('a'));

expect(rendered.nativeElement.href).toBe('');
expect(rendered.nativeElement.innerHTML).toBe(field.text);
});

it('https://', () => {
const field = {
value: {
href: 'https://',
},
text: 'ipsum',
};
comp.editable = false;
comp.field = field;
fixture.detectChanges();

const rendered = de.query(By.css('a'));

expect(rendered.nativeElement.href).toBe('');
expect(rendered.nativeElement.innerHTML).toBe(field.text);
});
});
});

// tslint:disable-next-line:max-classes-per-file
Expand Down Expand Up @@ -250,6 +286,56 @@ describe('<a *scLink>children</a>', () => {
const rendered = de.query(By.css('a'));

expect(rendered.nativeElement.href).toBe('');
expect(rendered.nativeElement.innerHTML).toContain('<span>hello world</span>')
expect(rendered.nativeElement.innerHTML).toContain('<span>hello world</span>');
});

describe('should render children and value with empty href when href attribute is invalid', () => {
it('undefined', () => {
const field = {
value: {
href: '',
},
text: 'ipsum',
};
comp.field = field;
fixture.detectChanges();

const rendered = de.query(By.css('a'));

expect(rendered.nativeElement.href).toBe('');
expect(rendered.nativeElement.innerHTML).toContain('<span>hello world</span>');
});

it('http://', () => {
const field = {
value: {
href: 'http://',
},
text: 'ipsum',
};
comp.field = field;
fixture.detectChanges();

const rendered = de.query(By.css('a'));

expect(rendered.nativeElement.href).toBe('');
expect(rendered.nativeElement.innerHTML).toContain('<span>hello world</span>');
});

it('https://', () => {
const field = {
value: {
href: 'https://',
},
text: 'ipsum',
};
comp.field = field;
fixture.detectChanges();

const rendered = de.query(By.css('a'));

expect(rendered.nativeElement.href).toBe('');
expect(rendered.nativeElement.innerHTML).toContain('<span>hello world</span>');
});
});
});
14 changes: 9 additions & 5 deletions packages/sitecore-jss-angular/src/components/link.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,16 @@ export class LinkDirective implements OnChanges {

viewRef.rootNodes.forEach((node) => {
Object.entries(props).forEach(([key, propValue]: [string, any]) => {
if (key === 'href' && !propValue) {
if (!node.href) {
return;
}
if (key === 'href') {
const isInvalidLink = !propValue || /^https?:\/\/$/.test(propValue);

if (isInvalidLink) {
if (!node.href) {
return;
}

propValue = node.href;
propValue = node.href;
}
}

if (key === 'class' && node.className) {
Expand Down