66 * found in the LICENSE file at https://angular.io/license
77 */
88
9- import { Directive , ElementRef , inject , InjectionToken , Injector , Input , NgZone , OnChanges , OnDestroy , OnInit , PLATFORM_ID , Renderer2 , SimpleChanges , ɵformatRuntimeError as formatRuntimeError , ɵRuntimeError as RuntimeError } from '@angular/core' ;
9+ import { booleanAttribute , Directive , ElementRef , inject , InjectionToken , Injector , Input , NgZone , numberAttribute , OnChanges , OnDestroy , OnInit , PLATFORM_ID , Renderer2 , SimpleChanges , ɵformatRuntimeError as formatRuntimeError , ɵRuntimeError as RuntimeError } from '@angular/core' ;
1010
1111import { RuntimeErrorCode } from '../../errors' ;
1212import { isPlatformServer } from '../../platform_id' ;
@@ -247,7 +247,7 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy {
247247 * Image name will be processed by the image loader and the final URL will be applied as the `src`
248248 * property of the image.
249249 */
250- @Input ( ) ngSrc ! : string ;
250+ @Input ( { required : true } ) ngSrc ! : string ;
251251
252252 /**
253253 * A comma separated list of width or density descriptors.
@@ -272,30 +272,14 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy {
272272 * For responsive images: the intrinsic width of the image in pixels.
273273 * For fixed size images: the desired rendered width of the image in pixels.
274274 */
275- @Input ( )
276- set width ( value : string | number | undefined ) {
277- ngDevMode && assertGreaterThanZero ( this , value , 'width' ) ;
278- this . _width = inputToInteger ( value ) ;
279- }
280- get width ( ) : number | undefined {
281- return this . _width ;
282- }
283- private _width ?: number ;
275+ @Input ( { transform : numberAttribute } ) width : number | undefined ;
284276
285277 /**
286278 * For responsive images: the intrinsic height of the image in pixels.
287279 * For fixed size images: the desired rendered height of the image in pixels.* The intrinsic
288280 * height of the image in pixels.
289281 */
290- @Input ( )
291- set height ( value : string | number | undefined ) {
292- ngDevMode && assertGreaterThanZero ( this , value , 'height' ) ;
293- this . _height = inputToInteger ( value ) ;
294- }
295- get height ( ) : number | undefined {
296- return this . _height ;
297- }
298- private _height ?: number ;
282+ @Input ( { transform : numberAttribute } ) height : number | undefined ;
299283
300284 /**
301285 * The desired loading behavior (lazy, eager, or auto).
@@ -308,14 +292,7 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy {
308292 /**
309293 * Indicates whether this image should have a high priority.
310294 */
311- @Input ( )
312- set priority ( value : string | boolean | undefined ) {
313- this . _priority = inputToBoolean ( value ) ;
314- }
315- get priority ( ) : boolean {
316- return this . _priority ;
317- }
318- private _priority = false ;
295+ @Input ( { transform : booleanAttribute } ) priority = false ;
319296
320297 /**
321298 * Data to pass through to custom loaders.
@@ -325,29 +302,15 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy {
325302 /**
326303 * Disables automatic srcset generation for this image.
327304 */
328- @Input ( )
329- set disableOptimizedSrcset ( value : string | boolean | undefined ) {
330- this . _disableOptimizedSrcset = inputToBoolean ( value ) ;
331- }
332- get disableOptimizedSrcset ( ) : boolean {
333- return this . _disableOptimizedSrcset ;
334- }
335- private _disableOptimizedSrcset = false ;
305+ @Input ( { transform : booleanAttribute } ) disableOptimizedSrcset = false ;
336306
337307 /**
338308 * Sets the image to "fill mode", which eliminates the height/width requirement and adds
339309 * styles such that the image fills its containing element.
340310 *
341311 * @developerPreview
342312 */
343- @Input ( )
344- set fill ( value : string | boolean | undefined ) {
345- this . _fill = inputToBoolean ( value ) ;
346- }
347- get fill ( ) : boolean {
348- return this . _fill ;
349- }
350- private _fill = false ;
313+ @Input ( { transform : booleanAttribute } ) fill = false ;
351314
352315 /**
353316 * Value of the `src` attribute if set on the host `<img>` element.
@@ -381,6 +344,12 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy {
381344 assertNonZeroRenderedHeight ( this , this . imgElement , this . renderer ) ;
382345 } else {
383346 assertNonEmptyWidthAndHeight ( this ) ;
347+ if ( this . height !== undefined ) {
348+ assertGreaterThanZero ( this , this . height , 'height' ) ;
349+ }
350+ if ( this . width !== undefined ) {
351+ assertGreaterThanZero ( this , this . width , 'width' ) ;
352+ }
384353 // Only check for distorted images when not in fill mode, where
385354 // images may be intentionally stretched, cropped or letterboxed.
386355 assertNoImageDistortion ( this , this . imgElement , this . renderer ) ;
@@ -548,7 +517,7 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy {
548517 }
549518
550519 private shouldGenerateAutomaticSrcset ( ) : boolean {
551- return ! this . _disableOptimizedSrcset && ! this . srcset && this . imageLoader !== noopImageLoader &&
520+ return ! this . disableOptimizedSrcset && ! this . srcset && this . imageLoader !== noopImageLoader &&
552521 ! ( this . width ! > FIXED_SRCSET_WIDTH_LIMIT || this . height ! > FIXED_SRCSET_HEIGHT_LIMIT ) ;
553522 }
554523
@@ -568,20 +537,6 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy {
568537
569538/***** Helpers *****/
570539
571- /**
572- * Convert input value to integer.
573- */
574- function inputToInteger ( value : string | number | undefined ) : number | undefined {
575- return typeof value === 'string' ? parseInt ( value , 10 ) : value ;
576- }
577-
578- /**
579- * Convert input value to boolean.
580- */
581- function inputToBoolean ( value : unknown ) : boolean {
582- return value != null && `${ value } ` !== 'false' ;
583- }
584-
585540/**
586541 * Sorts provided config breakpoints and uses defaults.
587542 */
@@ -778,9 +733,8 @@ function assertGreaterThanZero(dir: NgOptimizedImage, inputValue: unknown, input
778733 if ( ! validNumber && ! validString ) {
779734 throw new RuntimeError (
780735 RuntimeErrorCode . INVALID_INPUT ,
781- `${ imgDirectiveDetails ( dir . ngSrc ) } \`${ inputName } \` has an invalid value ` +
782- `(\`${ inputValue } \`). To fix this, provide \`${ inputName } \` ` +
783- `as a number greater than 0.` ) ;
736+ `${ imgDirectiveDetails ( dir . ngSrc ) } \`${ inputName } \` has an invalid value. ` +
737+ `To fix this, provide \`${ inputName } \` as a number greater than 0.` ) ;
784738 }
785739}
786740
0 commit comments