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(linkTo): Remove trailing slashes from the linkTo path #59

Merged
merged 1 commit into from
Apr 19, 2016
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
14 changes: 13 additions & 1 deletion lib/link-to.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ export class LinkTo {
}

private _updateHref() {
this.linkHref = this._router.prepareExternalUrl(this._href, this._query);
let path = this._cleanUpHref(this._href);

this.linkHref = this._router.prepareExternalUrl(path, this._query);
}

/**
Expand All @@ -63,6 +65,16 @@ export class LinkTo {

return (buttonEvent > 1 || event.ctrlKey || event.metaKey || event.shiftKey);
}

private _cleanUpHref(href: string = ''): string {
// Check for trailing slashes in the path
while(href.length > 1 && href.substr(-1) === "/") {
// Remove trailing slashes
href = href.substring(0, href.length - 1);
}

return href;
}
}

export const LINK_TO_PROVIDERS = [
Expand Down
22 changes: 22 additions & 0 deletions spec/link-to.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,28 @@ describe('Link To', () => {
});
}));

it('should remove trailing slashes from the path', injectAsync([TestComponentBuilder], (tcb) => {
return compile(tcb, '<a linkTo="/page/////">Page</a>')
.then((fixture) => {
fixture.detectChanges();
let compiled = fixture.debugElement.nativeElement;
let link: Element = compiled.querySelector('a');

expect(link.getAttribute('href')).toEqual('/page');
});
}));

it('should not remove single slash from the path', injectAsync([TestComponentBuilder], (tcb) => {
return compile(tcb, '<a linkTo="/">Page</a>')
.then((fixture) => {
fixture.detectChanges();
let compiled = fixture.debugElement.nativeElement;
let link: Element = compiled.querySelector('a');

expect(link.getAttribute('href')).toEqual('/');
});
}));

describe('When Clicked', () => {
it('should go to the provided URL', injectAsync([TestComponentBuilder, Router], (tcb, router) => {
let linkHref = '/page';
Expand Down