From 291a87cb61185b2f558ff7c83ff79ce3561d85f7 Mon Sep 17 00:00:00 2001 From: Jeremy Elbourn Date: Wed, 11 Oct 2017 16:48:40 -0400 Subject: [PATCH] fix(icon): use SafeResourceUrl in getSvgIconFromUrl (#7535) --- src/lib/icon/icon-registry.ts | 2 +- src/lib/icon/icon.spec.ts | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/lib/icon/icon-registry.ts b/src/lib/icon/icon-registry.ts index 6bd6db4864c..6bae77049de 100644 --- a/src/lib/icon/icon-registry.ts +++ b/src/lib/icon/icon-registry.ts @@ -201,7 +201,7 @@ export class MatIconRegistry { return observableOf(cloneSvg(cachedIcon)); } - return RxChain.from(this._loadSvgIconFromConfig(new SvgIconConfig(url))) + return RxChain.from(this._loadSvgIconFromConfig(new SvgIconConfig(safeUrl))) .call(doOperator, svg => this._cachedIconsByUrl.set(url!, svg)) .call(map, svg => cloneSvg(svg)) .result(); diff --git a/src/lib/icon/icon.spec.ts b/src/lib/icon/icon.spec.ts index 13ff507a24f..b84adc46734 100644 --- a/src/lib/icon/icon.spec.ts +++ b/src/lib/icon/icon.spec.ts @@ -1,4 +1,4 @@ -import {inject, async, TestBed} from '@angular/core/testing'; +import {inject, async, fakeAsync, tick, TestBed} from '@angular/core/testing'; import {SafeResourceUrl, DomSanitizer} from '@angular/platform-browser'; import {HttpModule, XHRBackend} from '@angular/http'; import {MockBackend} from '@angular/http/testing'; @@ -126,7 +126,7 @@ describe('MatIcon', () => { }); describe('Icons from URLs', () => { - it('should register icon URLs by name', () => { + it('should register icon URLs by name', fakeAsync(() => { iconRegistry.addSvgIcon('fluffy', trust('cat.svg')); iconRegistry.addSvgIcon('fido', trust('dog.svg')); @@ -153,7 +153,14 @@ describe('MatIcon', () => { svgElement = verifyAndGetSingleSvgChild(matIconElement); verifyPathChildElement(svgElement, 'woof'); expect(httpRequestUrls).toEqual(['dog.svg', 'cat.svg']); - }); + + // Assert that a registered icon can be looked-up by url. + iconRegistry.getSvgIconFromUrl(trust('cat.svg')).subscribe(element => { + verifyPathChildElement(element, 'meow'); + }); + + tick(); + })); it('should throw an error when using an untrusted icon url', () => { iconRegistry.addSvgIcon('fluffy', 'farm-set-1.svg');