Skip to content

Commit

Permalink
fix(icon): server-side error when registering icons
Browse files Browse the repository at this point in the history
Fixes an error that is thrown by the `IconRegistry` when registering icons on the server side.

Fixes #6787.
  • Loading branch information
crisbeto committed Nov 16, 2017
1 parent 541a95e commit ec0df1a
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/lib/icon/icon-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ import {tap} from 'rxjs/operators/tap';
import {finalize} from 'rxjs/operators/finalize';
import {map} from 'rxjs/operators/map';
import {share} from 'rxjs/operators/share';
import {Injectable, Optional, SecurityContext, SkipSelf} from '@angular/core';
import {
Injectable,
Inject,
InjectionToken,
Optional,
SecurityContext,
SkipSelf,
} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {DomSanitizer, SafeResourceUrl} from '@angular/platform-browser';
import {Observable} from 'rxjs/Observable';
import {forkJoin} from 'rxjs/observable/forkJoin';
import {of as observableOf} from 'rxjs/observable/of';
import {_throw as observableThrow} from 'rxjs/observable/throw';
import {DOCUMENT} from '@angular/common';


/**
Expand Down Expand Up @@ -97,7 +105,10 @@ export class MatIconRegistry {
*/
private _defaultFontSetClass = 'material-icons';

constructor(@Optional() private _httpClient: HttpClient, private _sanitizer: DomSanitizer) {}
constructor(
@Optional() private _httpClient: HttpClient,
@Inject(DOCUMENT) private _document: any,
private _sanitizer: DomSanitizer) {}

/**
* Registers an icon by URL in the default namespace.
Expand Down Expand Up @@ -405,7 +416,7 @@ export class MatIconRegistry {
* Creates a DOM element from the given SVG string.
*/
private _svgElementFromString(str: string): SVGElement {
const div = document.createElement('DIV');
const div = this._document.createElement('DIV');
div.innerHTML = str;
const svg = div.querySelector('svg') as SVGElement;
if (!svg) {
Expand Down Expand Up @@ -482,8 +493,11 @@ export class MatIconRegistry {

/** @docs-private */
export function ICON_REGISTRY_PROVIDER_FACTORY(
parentRegistry: MatIconRegistry, httpClient: HttpClient, sanitizer: DomSanitizer) {
return parentRegistry || new MatIconRegistry(httpClient, sanitizer);
parentRegistry: MatIconRegistry,
httpClient: HttpClient,
document: any,
sanitizer: DomSanitizer) {
return parentRegistry || new MatIconRegistry(httpClient, document, sanitizer);
}

/** @docs-private */
Expand All @@ -493,6 +507,7 @@ export const ICON_REGISTRY_PROVIDER = {
deps: [
[new Optional(), new SkipSelf(), MatIconRegistry],
[new Optional(), HttpClient],
DOCUMENT as InjectionToken<any>,
DomSanitizer
],
useFactory: ICON_REGISTRY_PROVIDER_FACTORY
Expand Down

0 comments on commit ec0df1a

Please sign in to comment.