@@ -73,9 +73,9 @@ class SvgIconConfig {
73
73
url : SafeResourceUrl | null ;
74
74
svgElement : SVGElement | null ;
75
75
76
- constructor ( url : SafeResourceUrl ) ;
77
- constructor ( svgElement : SVGElement ) ;
78
- constructor ( data : SafeResourceUrl | SVGElement ) {
76
+ constructor ( url : SafeResourceUrl , viewBox ?: string ) ;
77
+ constructor ( svgElement : SVGElement , viewBox ?: string ) ;
78
+ constructor ( data : SafeResourceUrl | SVGElement , public viewBox ?: string ) {
79
79
// Note that we can't use `instanceof SVGElement` here,
80
80
// because it'll break during server-side rendering.
81
81
if ( ! ! ( data as any ) . nodeName ) {
@@ -136,17 +136,17 @@ export class MatIconRegistry implements OnDestroy {
136
136
* @param iconName Name under which the icon should be registered.
137
137
* @param url
138
138
*/
139
- addSvgIcon ( iconName : string , url : SafeResourceUrl ) : this {
140
- return this . addSvgIconInNamespace ( '' , iconName , url ) ;
139
+ addSvgIcon ( iconName : string , url : SafeResourceUrl , viewBox ?: string ) : this {
140
+ return this . addSvgIconInNamespace ( '' , iconName , url , viewBox ) ;
141
141
}
142
142
143
143
/**
144
144
* Registers an icon using an HTML string in the default namespace.
145
145
* @param iconName Name under which the icon should be registered.
146
146
* @param literal SVG source of the icon.
147
147
*/
148
- addSvgIconLiteral ( iconName : string , literal : SafeHtml ) : this {
149
- return this . addSvgIconLiteralInNamespace ( '' , iconName , literal ) ;
148
+ addSvgIconLiteral ( iconName : string , literal : SafeHtml , viewBox ?: string ) : this {
149
+ return this . addSvgIconLiteralInNamespace ( '' , iconName , literal , viewBox ) ;
150
150
}
151
151
152
152
/**
@@ -155,8 +155,9 @@ export class MatIconRegistry implements OnDestroy {
155
155
* @param iconName Name under which the icon should be registered.
156
156
* @param url
157
157
*/
158
- addSvgIconInNamespace ( namespace : string , iconName : string , url : SafeResourceUrl ) : this {
159
- return this . _addSvgIconConfig ( namespace , iconName , new SvgIconConfig ( url ) ) ;
158
+ addSvgIconInNamespace ( namespace : string , iconName : string , url : SafeResourceUrl ,
159
+ viewBox ?: string ) : this {
160
+ return this . _addSvgIconConfig ( namespace , iconName , new SvgIconConfig ( url , viewBox ) ) ;
160
161
}
161
162
162
163
/**
@@ -165,56 +166,57 @@ export class MatIconRegistry implements OnDestroy {
165
166
* @param iconName Name under which the icon should be registered.
166
167
* @param literal SVG source of the icon.
167
168
*/
168
- addSvgIconLiteralInNamespace ( namespace : string , iconName : string , literal : SafeHtml ) : this {
169
+ addSvgIconLiteralInNamespace ( namespace : string , iconName : string , literal : SafeHtml ,
170
+ viewBox ?: string ) : this {
169
171
const sanitizedLiteral = this . _sanitizer . sanitize ( SecurityContext . HTML , literal ) ;
170
172
171
173
if ( ! sanitizedLiteral ) {
172
174
throw getMatIconFailedToSanitizeLiteralError ( literal ) ;
173
175
}
174
176
175
- const svgElement = this . _createSvgElementForSingleIcon ( sanitizedLiteral ) ;
176
- return this . _addSvgIconConfig ( namespace , iconName , new SvgIconConfig ( svgElement ) ) ;
177
+ const svgElement = this . _createSvgElementForSingleIcon ( sanitizedLiteral , viewBox ) ;
178
+ return this . _addSvgIconConfig ( namespace , iconName , new SvgIconConfig ( svgElement , viewBox ) ) ;
177
179
}
178
180
179
181
/**
180
182
* Registers an icon set by URL in the default namespace.
181
183
* @param url
182
184
*/
183
- addSvgIconSet ( url : SafeResourceUrl ) : this {
184
- return this . addSvgIconSetInNamespace ( '' , url ) ;
185
+ addSvgIconSet ( url : SafeResourceUrl , viewBox ?: string ) : this {
186
+ return this . addSvgIconSetInNamespace ( '' , url , viewBox ) ;
185
187
}
186
188
187
189
/**
188
190
* Registers an icon set using an HTML string in the default namespace.
189
191
* @param literal SVG source of the icon set.
190
192
*/
191
- addSvgIconSetLiteral ( literal : SafeHtml ) : this {
192
- return this . addSvgIconSetLiteralInNamespace ( '' , literal ) ;
193
+ addSvgIconSetLiteral ( literal : SafeHtml , viewBox ?: string ) : this {
194
+ return this . addSvgIconSetLiteralInNamespace ( '' , literal , viewBox ) ;
193
195
}
194
196
195
197
/**
196
198
* Registers an icon set by URL in the specified namespace.
197
199
* @param namespace Namespace in which to register the icon set.
198
200
* @param url
199
201
*/
200
- addSvgIconSetInNamespace ( namespace : string , url : SafeResourceUrl ) : this {
201
- return this . _addSvgIconSetConfig ( namespace , new SvgIconConfig ( url ) ) ;
202
+ addSvgIconSetInNamespace ( namespace : string , url : SafeResourceUrl , viewBox ?: string ) : this {
203
+ return this . _addSvgIconSetConfig ( namespace , new SvgIconConfig ( url , viewBox ) ) ;
202
204
}
203
205
204
206
/**
205
207
* Registers an icon set using an HTML string in the specified namespace.
206
208
* @param namespace Namespace in which to register the icon set.
207
209
* @param literal SVG source of the icon set.
208
210
*/
209
- addSvgIconSetLiteralInNamespace ( namespace : string , literal : SafeHtml ) : this {
211
+ addSvgIconSetLiteralInNamespace ( namespace : string , literal : SafeHtml , viewBox ?: string ) : this {
210
212
const sanitizedLiteral = this . _sanitizer . sanitize ( SecurityContext . HTML , literal ) ;
211
213
212
214
if ( ! sanitizedLiteral ) {
213
215
throw getMatIconFailedToSanitizeLiteralError ( literal ) ;
214
216
}
215
217
216
218
const svgElement = this . _svgElementFromString ( sanitizedLiteral ) ;
217
- return this . _addSvgIconSetConfig ( namespace , new SvgIconConfig ( svgElement ) ) ;
219
+ return this . _addSvgIconSetConfig ( namespace , new SvgIconConfig ( svgElement , viewBox ) ) ;
218
220
}
219
221
220
222
/**
@@ -395,7 +397,7 @@ export class MatIconRegistry implements OnDestroy {
395
397
for ( let i = iconSetConfigs . length - 1 ; i >= 0 ; i -- ) {
396
398
const config = iconSetConfigs [ i ] ;
397
399
if ( config . svgElement ) {
398
- const foundIcon = this . _extractSvgIconFromSet ( config . svgElement , iconName ) ;
400
+ const foundIcon = this . _extractSvgIconFromSet ( config . svgElement , iconName , config . viewBox ) ;
399
401
if ( foundIcon ) {
400
402
return foundIcon ;
401
403
}
@@ -410,7 +412,7 @@ export class MatIconRegistry implements OnDestroy {
410
412
*/
411
413
private _loadSvgIconFromConfig ( config : SvgIconConfig ) : Observable < SVGElement > {
412
414
return this . _fetchUrl ( config . url )
413
- . pipe ( map ( svgText => this . _createSvgElementForSingleIcon ( svgText ) ) ) ;
415
+ . pipe ( map ( svgText => this . _createSvgElementForSingleIcon ( svgText , config . viewBox ) ) ) ;
414
416
}
415
417
416
418
/**
@@ -437,9 +439,9 @@ export class MatIconRegistry implements OnDestroy {
437
439
/**
438
440
* Creates a DOM element from the given SVG string, and adds default attributes.
439
441
*/
440
- private _createSvgElementForSingleIcon ( responseText : string ) : SVGElement {
442
+ private _createSvgElementForSingleIcon ( responseText : string , viewBox ?: string ) : SVGElement {
441
443
const svg = this . _svgElementFromString ( responseText ) ;
442
- this . _setSvgAttributes ( svg ) ;
444
+ this . _setSvgAttributes ( svg , viewBox ) ;
443
445
return svg ;
444
446
}
445
447
@@ -448,7 +450,8 @@ export class MatIconRegistry implements OnDestroy {
448
450
* tag matches the specified name. If found, copies the nested element to a new SVG element and
449
451
* returns it. Returns null if no matching element is found.
450
452
*/
451
- private _extractSvgIconFromSet ( iconSet : SVGElement , iconName : string ) : SVGElement | null {
453
+ private _extractSvgIconFromSet ( iconSet : SVGElement , iconName : string ,
454
+ viewBox ?: string ) : SVGElement | null {
452
455
// Use the `id="iconName"` syntax in order to escape special
453
456
// characters in the ID (versus using the #iconName syntax).
454
457
const iconSource = iconSet . querySelector ( `[id="${ iconName } "]` ) ;
@@ -465,14 +468,14 @@ export class MatIconRegistry implements OnDestroy {
465
468
// If the icon node is itself an <svg> node, clone and return it directly. If not, set it as
466
469
// the content of a new <svg> node.
467
470
if ( iconElement . nodeName . toLowerCase ( ) === 'svg' ) {
468
- return this . _setSvgAttributes ( iconElement as SVGElement ) ;
471
+ return this . _setSvgAttributes ( iconElement as SVGElement , viewBox ) ;
469
472
}
470
473
471
474
// If the node is a <symbol>, it won't be rendered so we have to convert it into <svg>. Note
472
475
// that the same could be achieved by referring to it via <use href="#id">, however the <use>
473
476
// tag is problematic on Firefox, because it needs to include the current page path.
474
477
if ( iconElement . nodeName . toLowerCase ( ) === 'symbol' ) {
475
- return this . _setSvgAttributes ( this . _toSvgElement ( iconElement ) ) ;
478
+ return this . _setSvgAttributes ( this . _toSvgElement ( iconElement ) , viewBox ) ;
476
479
}
477
480
478
481
// createElement('SVG') doesn't work as expected; the DOM ends up with
@@ -484,7 +487,7 @@ export class MatIconRegistry implements OnDestroy {
484
487
// Clone the node so we don't remove it from the parent icon set element.
485
488
svg . appendChild ( iconElement ) ;
486
489
487
- return this . _setSvgAttributes ( svg ) ;
490
+ return this . _setSvgAttributes ( svg , viewBox ) ;
488
491
}
489
492
490
493
/**
@@ -520,12 +523,17 @@ export class MatIconRegistry implements OnDestroy {
520
523
/**
521
524
* Sets the default attributes for an SVG element to be used as an icon.
522
525
*/
523
- private _setSvgAttributes ( svg : SVGElement ) : SVGElement {
526
+ private _setSvgAttributes ( svg : SVGElement , viewBox ?: string ) : SVGElement {
524
527
svg . setAttribute ( 'fit' , '' ) ;
525
528
svg . setAttribute ( 'height' , '100%' ) ;
526
529
svg . setAttribute ( 'width' , '100%' ) ;
527
530
svg . setAttribute ( 'preserveAspectRatio' , 'xMidYMid meet' ) ;
528
531
svg . setAttribute ( 'focusable' , 'false' ) ; // Disable IE11 default behavior to make SVGs focusable.
532
+
533
+ if ( viewBox ) {
534
+ svg . setAttribute ( 'viewBox' , viewBox ) ;
535
+ }
536
+
529
537
return svg ;
530
538
}
531
539
0 commit comments