Skip to content

Commit d814624

Browse files
crisbetotinayuangao
authored andcommitted
fix(icon): server-side error when registering icons (#8492)
Fixes an error that is thrown by the `IconRegistry` when registering icons on the server side. Fixes #6787.
1 parent d9380e2 commit d814624

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

src/lib/icon/icon-registry.ts

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,21 @@ import {tap} from 'rxjs/operators/tap';
1111
import {finalize} from 'rxjs/operators/finalize';
1212
import {map} from 'rxjs/operators/map';
1313
import {share} from 'rxjs/operators/share';
14-
import {Injectable, Optional, SecurityContext, SkipSelf} from '@angular/core';
14+
import {
15+
Injectable,
16+
Inject,
17+
InjectionToken,
18+
Optional,
19+
SecurityContext,
20+
SkipSelf,
21+
} from '@angular/core';
1522
import {HttpClient} from '@angular/common/http';
1623
import {DomSanitizer, SafeResourceUrl} from '@angular/platform-browser';
1724
import {Observable} from 'rxjs/Observable';
1825
import {forkJoin} from 'rxjs/observable/forkJoin';
1926
import {of as observableOf} from 'rxjs/observable/of';
2027
import {_throw as observableThrow} from 'rxjs/observable/throw';
28+
import {DOCUMENT} from '@angular/common';
2129

2230

2331
/**
@@ -97,7 +105,12 @@ export class MatIconRegistry {
97105
*/
98106
private _defaultFontSetClass = 'material-icons';
99107

100-
constructor(@Optional() private _httpClient: HttpClient, private _sanitizer: DomSanitizer) {}
108+
constructor(
109+
@Optional() private _httpClient: HttpClient,
110+
private _sanitizer: DomSanitizer,
111+
@Optional() @Inject(DOCUMENT) private _document?: any) {
112+
// TODO(crisbeto): make _document required next major release.
113+
}
101114

102115
/**
103116
* Registers an icon by URL in the default namespace.
@@ -405,13 +418,17 @@ export class MatIconRegistry {
405418
* Creates a DOM element from the given SVG string.
406419
*/
407420
private _svgElementFromString(str: string): SVGElement {
408-
const div = document.createElement('DIV');
409-
div.innerHTML = str;
410-
const svg = div.querySelector('svg') as SVGElement;
411-
if (!svg) {
412-
throw Error('<svg> tag not found');
421+
if (this._document || typeof document !== 'undefined') {
422+
const div = (this._document || document).createElement('DIV');
423+
div.innerHTML = str;
424+
const svg = div.querySelector('svg') as SVGElement;
425+
if (!svg) {
426+
throw Error('<svg> tag not found');
427+
}
428+
return svg;
413429
}
414-
return svg;
430+
431+
throw new Error('MatIconRegistry could not resolve document.');
415432
}
416433

417434
/**
@@ -483,8 +500,11 @@ export class MatIconRegistry {
483500

484501
/** @docs-private */
485502
export function ICON_REGISTRY_PROVIDER_FACTORY(
486-
parentRegistry: MatIconRegistry, httpClient: HttpClient, sanitizer: DomSanitizer) {
487-
return parentRegistry || new MatIconRegistry(httpClient, sanitizer);
503+
parentRegistry: MatIconRegistry,
504+
httpClient: HttpClient,
505+
sanitizer: DomSanitizer,
506+
document?: any) {
507+
return parentRegistry || new MatIconRegistry(httpClient, sanitizer, document);
488508
}
489509

490510
/** @docs-private */
@@ -494,7 +514,8 @@ export const ICON_REGISTRY_PROVIDER = {
494514
deps: [
495515
[new Optional(), new SkipSelf(), MatIconRegistry],
496516
[new Optional(), HttpClient],
497-
DomSanitizer
517+
DomSanitizer,
518+
[new Optional(), DOCUMENT as InjectionToken<any>]
498519
],
499520
useFactory: ICON_REGISTRY_PROVIDER_FACTORY
500521
};

0 commit comments

Comments
 (0)