Skip to content

Commit

Permalink
fix(uiSref): avoid empty links (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristHian Gz authored and christopherthielen committed Apr 22, 2017
1 parent ccbc56f commit 5f6870d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/directives/uiSref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import { Subscription } from "rxjs/Subscription";
export class AnchorUISref {
constructor(public _el: ElementRef, public _renderer: Renderer) { }
update(href: string) {
this._renderer.setElementProperty(this._el.nativeElement, 'href', href);
if (href && href != '') {
this._renderer.setElementProperty(this._el.nativeElement, 'href', href);
} else {
this._el.nativeElement.removeAttribute('href');
}
}
}

Expand Down
38 changes: 38 additions & 0 deletions test/uiSref.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Component, NO_ERRORS_SCHEMA } from "@angular/core";
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';

import { UIRouterModule } from '../src/uiRouterNgModule';
import { UISref } from '../src/directives/uiSref';

describe('uiSref', () => {
describe('empty links', () => {
@Component({
template: `
<a [uiSref]="null"></a>
<a [uiSref]="''"></a>
`
})
class TestComponent { }

let des: DebugElement[];
let comp: TestComponent;
let fixture: ComponentFixture<TestComponent>;

beforeEach(() => {
fixture = TestBed.configureTestingModule({
declarations: [ TestComponent ],
imports: [ UIRouterModule ]
}).createComponent(TestComponent);
fixture.detectChanges();
des = fixture.debugElement.queryAll(By.directive(UISref));
});

it('should not bind "null" string to `href`', () => {
expect(des[0].nativeElement.hasAttribute('href')).toBeFalsy();
expect(des[1].nativeElement.hasAttribute('href')).toBeFalsy();
});
});
});

0 comments on commit 5f6870d

Please sign in to comment.