@@ -11,13 +11,21 @@ import {tap} from 'rxjs/operators/tap';
11
11
import { finalize } from 'rxjs/operators/finalize' ;
12
12
import { map } from 'rxjs/operators/map' ;
13
13
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' ;
15
22
import { HttpClient } from '@angular/common/http' ;
16
23
import { DomSanitizer , SafeResourceUrl } from '@angular/platform-browser' ;
17
24
import { Observable } from 'rxjs/Observable' ;
18
25
import { forkJoin } from 'rxjs/observable/forkJoin' ;
19
26
import { of as observableOf } from 'rxjs/observable/of' ;
20
27
import { _throw as observableThrow } from 'rxjs/observable/throw' ;
28
+ import { DOCUMENT } from '@angular/common' ;
21
29
22
30
23
31
/**
@@ -97,7 +105,12 @@ export class MatIconRegistry {
97
105
*/
98
106
private _defaultFontSetClass = 'material-icons' ;
99
107
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
+ }
101
114
102
115
/**
103
116
* Registers an icon by URL in the default namespace.
@@ -405,13 +418,17 @@ export class MatIconRegistry {
405
418
* Creates a DOM element from the given SVG string.
406
419
*/
407
420
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 ;
413
429
}
414
- return svg ;
430
+
431
+ throw new Error ( 'MatIconRegistry could not resolve document.' ) ;
415
432
}
416
433
417
434
/**
@@ -483,8 +500,11 @@ export class MatIconRegistry {
483
500
484
501
/** @docs -private */
485
502
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 ) ;
488
508
}
489
509
490
510
/** @docs -private */
@@ -494,7 +514,8 @@ export const ICON_REGISTRY_PROVIDER = {
494
514
deps : [
495
515
[ new Optional ( ) , new SkipSelf ( ) , MatIconRegistry ] ,
496
516
[ new Optional ( ) , HttpClient ] ,
497
- DomSanitizer
517
+ DomSanitizer ,
518
+ [ new Optional ( ) , DOCUMENT as InjectionToken < any > ]
498
519
] ,
499
520
useFactory : ICON_REGISTRY_PROVIDER_FACTORY
500
521
} ;
0 commit comments