Skip to content

Commit 5020c79

Browse files
christopherthielenmergify[bot]
authored andcommitted
fix(uiSref): Render empty 'href' for states that have no urls
Fixes #721
1 parent 82225dd commit 5020c79

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

src/directives/uiSref.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { UIRouter, extend, Obj, TransitionOptions, TargetState, isNumber } from '@uirouter/core';
1+
import { UIRouter, extend, Obj, TransitionOptions, TargetState, isNumber, isNullOrUndefined } from '@uirouter/core';
22
import {
33
Directive,
44
Inject,
@@ -20,11 +20,13 @@ import { ReplaySubject, Subscription } from 'rxjs';
2020
@Directive({ selector: 'a[uiSref]' })
2121
export class AnchorUISref {
2222
constructor(public _el: ElementRef, public _renderer: Renderer2) {}
23+
2324
openInNewTab() {
2425
return this._el.nativeElement.target === '_blank';
2526
}
27+
2628
update(href: string) {
27-
if (href && href !== '') {
29+
if (!isNullOrUndefined(href)) {
2830
this._renderer.setProperty(this._el.nativeElement, 'href', href);
2931
} else {
3032
this._renderer.removeAttribute(this._el.nativeElement, 'href');
@@ -168,8 +170,12 @@ export class UISref implements OnChanges {
168170
}
169171

170172
if (this._anchorUISref) {
171-
const href = $state.href(this.state, this.params, this.getOptions());
172-
this._anchorUISref.update(href);
173+
if (!this.state) {
174+
this._anchorUISref.update(null);
175+
} else {
176+
const href = $state.href(this.state, this.params, this.getOptions()) || '';
177+
this._anchorUISref.update(href);
178+
}
173179
}
174180
}
175181

test/uiSref/uiSref.spec.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ describe('uiSref', () => {
5656
};
5757

5858
// Extract the logical portion of the URL after the hash
59-
const urlOfElement = (srefElement: DebugElement) => srefElement.nativeElement.href.replace(/^[^#]*#/, '');
59+
const urlOfElement = (srefElement: DebugElement) => {
60+
let href = srefElement.nativeElement.href;
61+
if (typeof location === 'object' && href.startsWith(location.href)) {
62+
href = href.substr(location.href.length);
63+
}
64+
return href.replace(/^[^#]*#/, '');
65+
};
6066

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

70-
// it('renders an empty href for a url-less state', () => {
71-
// const { fixture, srefElements, router } = setup();
72-
// router.stateRegistry.register({ name: 'urlless' });
73-
// fixture.componentInstance.linkA = 'urlless';
74-
// fixture.detectChanges();
75-
// expect(srefElements[0].nativeElement.hasAttribute('href')).toBeTruthy();
76-
// expect(urlOfElement(srefElements[0])).toBe('');
77-
// });
76+
it('renders an empty href for a url-less state', () => {
77+
const { fixture, srefElements, router } = setup();
78+
router.stateRegistry.register({ name: 'urlless' });
79+
fixture.componentInstance.linkA = 'urlless';
80+
fixture.detectChanges();
81+
expect(srefElements[0].nativeElement.hasAttribute('href')).toBeTruthy();
82+
expect(urlOfElement(srefElements[0])).toBe('');
83+
});
7884

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

0 commit comments

Comments
 (0)