Skip to content

Commit

Permalink
fix(uiSref): Render empty 'href' for states that have no urls
Browse files Browse the repository at this point in the history
Fixes #721
  • Loading branch information
christopherthielen authored and mergify[bot] committed Jul 6, 2020
1 parent 82225dd commit 5020c79
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
14 changes: 10 additions & 4 deletions src/directives/uiSref.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UIRouter, extend, Obj, TransitionOptions, TargetState, isNumber } from '@uirouter/core';
import { UIRouter, extend, Obj, TransitionOptions, TargetState, isNumber, isNullOrUndefined } from '@uirouter/core';
import {
Directive,
Inject,
Expand All @@ -20,11 +20,13 @@ import { ReplaySubject, Subscription } from 'rxjs';
@Directive({ selector: 'a[uiSref]' })
export class AnchorUISref {
constructor(public _el: ElementRef, public _renderer: Renderer2) {}

openInNewTab() {
return this._el.nativeElement.target === '_blank';
}

update(href: string) {
if (href && href !== '') {
if (!isNullOrUndefined(href)) {
this._renderer.setProperty(this._el.nativeElement, 'href', href);
} else {
this._renderer.removeAttribute(this._el.nativeElement, 'href');
Expand Down Expand Up @@ -168,8 +170,12 @@ export class UISref implements OnChanges {
}

if (this._anchorUISref) {
const href = $state.href(this.state, this.params, this.getOptions());
this._anchorUISref.update(href);
if (!this.state) {
this._anchorUISref.update(null);
} else {
const href = $state.href(this.state, this.params, this.getOptions()) || '';
this._anchorUISref.update(href);
}
}
}

Expand Down
24 changes: 15 additions & 9 deletions test/uiSref/uiSref.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ describe('uiSref', () => {
};

// Extract the logical portion of the URL after the hash
const urlOfElement = (srefElement: DebugElement) => srefElement.nativeElement.href.replace(/^[^#]*#/, '');
const urlOfElement = (srefElement: DebugElement) => {
let href = srefElement.nativeElement.href;
if (typeof location === 'object' && href.startsWith(location.href)) {
href = href.substr(location.href.length);
}
return href.replace(/^[^#]*#/, '');
};

it('renders an href for a state with an url', () => {
const { fixture, srefElements, router } = setup();
Expand All @@ -67,14 +73,14 @@ describe('uiSref', () => {
expect(urlOfElement(srefElements[0])).toBe('/myurl');
});

// it('renders an empty href for a url-less state', () => {
// const { fixture, srefElements, router } = setup();
// router.stateRegistry.register({ name: 'urlless' });
// fixture.componentInstance.linkA = 'urlless';
// fixture.detectChanges();
// expect(srefElements[0].nativeElement.hasAttribute('href')).toBeTruthy();
// expect(urlOfElement(srefElements[0])).toBe('');
// });
it('renders an empty href for a url-less state', () => {
const { fixture, srefElements, router } = setup();
router.stateRegistry.register({ name: 'urlless' });
fixture.componentInstance.linkA = 'urlless';
fixture.detectChanges();
expect(srefElements[0].nativeElement.hasAttribute('href')).toBeTruthy();
expect(urlOfElement(srefElements[0])).toBe('');
});

it('renders no href when the sref state is empty', () => {
const { fixture, srefElements } = setup();
Expand Down

0 comments on commit 5020c79

Please sign in to comment.