|
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import {Directive, ElementRef, Inject, Injectable, Injector, Input, NgModule, NgZone, OnChanges, OnDestroy, OnInit, Renderer2, SimpleChanges, ɵformatRuntimeError as formatRuntimeError, ɵRuntimeError as RuntimeError} from '@angular/core'; |
| 9 | +import {Directive, ElementRef, Inject, Injector, Input, NgModule, NgZone, OnChanges, OnDestroy, OnInit, Renderer2, SimpleChanges, ɵ_sanitizeUrl as sanitizeUrl, ɵRuntimeError as RuntimeError} from '@angular/core'; |
10 | 10 |
|
11 | 11 | import {RuntimeErrorCode} from '../../errors'; |
12 | 12 |
|
@@ -161,9 +161,15 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy { |
161 | 161 | } |
162 | 162 | this.setHostAttribute('loading', this.getLoadingBehavior()); |
163 | 163 | this.setHostAttribute('fetchpriority', this.getFetchPriority()); |
| 164 | + |
| 165 | + // Use the `sanitizeUrl` function directly (vs using `DomSanitizer`), |
| 166 | + // to make the code more tree-shakable (avoid referencing the entire class). |
| 167 | + // The same function is used when regular `src` is used on an `<img>` element. |
| 168 | + const src = sanitizeUrl(this.getRewrittenSrc()); |
| 169 | + |
164 | 170 | // The `src` and `srcset` attributes should be set last since other attributes |
165 | 171 | // could affect the image's loading behavior. |
166 | | - this.setHostAttribute('src', this.getRewrittenSrc()); |
| 172 | + this.setHostAttribute('src', src); |
167 | 173 | if (this.rawSrcset) { |
168 | 174 | this.setHostAttribute('srcset', this.getRewrittenSrcset()); |
169 | 175 | } |
@@ -204,7 +210,8 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy { |
204 | 210 | const finalSrcs = this.rawSrcset.split(',').filter(src => src !== '').map(srcStr => { |
205 | 211 | srcStr = srcStr.trim(); |
206 | 212 | const width = widthSrcSet ? parseFloat(srcStr) : parseFloat(srcStr) * this.width!; |
207 | | - return `${this.imageLoader({src: this.rawSrc, width})} ${srcStr}`; |
| 213 | + const imgSrc = sanitizeUrl(this.imageLoader({src: this.rawSrc, width})); |
| 214 | + return `${imgSrc} ${srcStr}`; |
208 | 215 | }); |
209 | 216 | return finalSrcs.join(', '); |
210 | 217 | } |
|
0 commit comments