@angular/common 21.0.0-next.9 → 21.0.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/fesm2022/_common_module-chunk.mjs +3050 -4380
- package/fesm2022/_common_module-chunk.mjs.map +1 -1
- package/fesm2022/_location-chunk.mjs +392 -588
- package/fesm2022/_location-chunk.mjs.map +1 -1
- package/fesm2022/_module-chunk.mjs +2042 -3001
- package/fesm2022/_module-chunk.mjs.map +1 -1
- package/fesm2022/_platform_navigation-chunk.mjs +30 -16
- package/fesm2022/_platform_navigation-chunk.mjs.map +1 -1
- package/fesm2022/_xhr-chunk.mjs +10 -16
- package/fesm2022/_xhr-chunk.mjs.map +1 -1
- package/fesm2022/common.mjs +1135 -1837
- package/fesm2022/common.mjs.map +1 -1
- package/fesm2022/http-testing.mjs +259 -310
- package/fesm2022/http-testing.mjs.map +1 -1
- package/fesm2022/http.mjs +302 -370
- package/fesm2022/http.mjs.map +1 -1
- package/fesm2022/testing.mjs +596 -552
- package/fesm2022/testing.mjs.map +1 -1
- package/fesm2022/upgrade.mjs +601 -838
- package/fesm2022/upgrade.mjs.map +1 -1
- package/package.json +2 -2
- package/types/_common_module-chunk.d.ts +1 -1
- package/types/_module-chunk.d.ts +33 -1
- package/types/_platform_location-chunk.d.ts +1 -1
- package/types/_xhr-chunk.d.ts +1 -1
- package/types/common.d.ts +34 -4
- package/types/http-testing.d.ts +1 -1
- package/types/http.d.ts +13 -2
- package/types/testing.d.ts +88 -62
- package/types/upgrade.d.ts +1 -1
package/fesm2022/common.mjs
CHANGED
|
@@ -1,2048 +1,1346 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @license Angular v21.0.0-
|
|
2
|
+
* @license Angular v21.0.0-rc.0
|
|
3
3
|
* (c) 2010-2025 Google LLC. https://angular.dev/
|
|
4
4
|
* License: MIT
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
export { AsyncPipe, CommonModule, CurrencyPipe, DATE_PIPE_DEFAULT_OPTIONS, DATE_PIPE_DEFAULT_TIMEZONE, DatePipe, DecimalPipe, FormStyle, FormatWidth, HashLocationStrategy, I18nPluralPipe, I18nSelectPipe, JsonPipe, KeyValuePipe, LowerCasePipe, NgClass, NgComponentOutlet, NgForOf as NgFor, NgForOf, NgForOfContext, NgIf, NgIfContext, NgLocaleLocalization, NgLocalization, NgPlural, NgPluralCase, NgStyle, NgSwitch, NgSwitchCase, NgSwitchDefault, NgTemplateOutlet, NumberFormatStyle, NumberSymbol, PercentPipe, Plural, SlicePipe, TitleCasePipe, TranslationWidth, UpperCasePipe, WeekDay, formatCurrency, formatDate, formatNumber, formatPercent, getCurrencySymbol, getLocaleCurrencyCode, getLocaleCurrencyName, getLocaleCurrencySymbol, getLocaleDateFormat, getLocaleDateTimeFormat, getLocaleDayNames, getLocaleDayPeriods, getLocaleDirection, getLocaleEraNames, getLocaleExtraDayPeriodRules, getLocaleExtraDayPeriods, getLocaleFirstDayOfWeek, getLocaleId, getLocaleMonthNames, getLocaleNumberFormat, getLocaleNumberSymbol, getLocalePluralCase, getLocaleTimeFormat, getLocaleWeekEndRange, getNumberOfCurrencyDigits } from './_common_module-chunk.mjs';
|
|
8
8
|
import * as i0 from '@angular/core';
|
|
9
|
-
import { ɵregisterLocaleData as _registerLocaleData, Version, ɵɵdefineInjectable as __defineInjectable,
|
|
9
|
+
import { inject, DestroyRef, Injectable, ɵregisterLocaleData as _registerLocaleData, Version, ɵɵdefineInjectable as __defineInjectable, DOCUMENT, ɵformatRuntimeError as _formatRuntimeError, InjectionToken, ɵRuntimeError as _RuntimeError, ɵIMAGE_CONFIG as _IMAGE_CONFIG, Renderer2, ElementRef, Injector, ɵperformanceMarkFeature as _performanceMarkFeature, NgZone, ApplicationRef, numberAttribute, booleanAttribute, Directive, Input, ɵIMAGE_CONFIG_DEFAULTS as _IMAGE_CONFIG_DEFAULTS, ɵunwrapSafeValue as _unwrapSafeValue, ChangeDetectorRef } from '@angular/core';
|
|
10
10
|
export { DOCUMENT, ɵIMAGE_CONFIG as IMAGE_CONFIG } from '@angular/core';
|
|
11
|
-
|
|
11
|
+
import { PlatformNavigation } from './_platform_navigation-chunk.mjs';
|
|
12
12
|
export { XhrFactory, parseCookieValue as ɵparseCookieValue } from './_xhr-chunk.mjs';
|
|
13
|
-
|
|
13
|
+
import { Location, LocationStrategy, normalizeQueryParams } from './_location-chunk.mjs';
|
|
14
|
+
export { APP_BASE_HREF, BrowserPlatformLocation, LOCATION_INITIALIZED, PathLocationStrategy, PlatformLocation, DomAdapter as ɵDomAdapter, getDOM as ɵgetDOM, setRootDomAdapter as ɵsetRootDomAdapter } from './_location-chunk.mjs';
|
|
14
15
|
import 'rxjs';
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
17
|
+
class NavigationAdapterForLocation extends Location {
|
|
18
|
+
navigation = inject(PlatformNavigation);
|
|
19
|
+
destroyRef = inject(DestroyRef);
|
|
20
|
+
constructor() {
|
|
21
|
+
super(inject(LocationStrategy));
|
|
22
|
+
this.registerNavigationListeners();
|
|
23
|
+
}
|
|
24
|
+
registerNavigationListeners() {
|
|
25
|
+
const currentEntryChangeListener = () => {
|
|
26
|
+
this._notifyUrlChangeListeners(this.path(true), this.getState());
|
|
27
|
+
};
|
|
28
|
+
this.navigation.addEventListener('currententrychange', currentEntryChangeListener);
|
|
29
|
+
this.destroyRef.onDestroy(() => {
|
|
30
|
+
this.navigation.removeEventListener('currententrychange', currentEntryChangeListener);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
getState() {
|
|
34
|
+
return this.navigation.currentEntry?.getState();
|
|
35
|
+
}
|
|
36
|
+
replaceState(path, query = '', state = null) {
|
|
37
|
+
const url = this.prepareExternalUrl(path + normalizeQueryParams(query));
|
|
38
|
+
this.navigation.navigate(url, {
|
|
39
|
+
state,
|
|
40
|
+
history: 'replace'
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
go(path, query = '', state = null) {
|
|
44
|
+
const url = this.prepareExternalUrl(path + normalizeQueryParams(query));
|
|
45
|
+
this.navigation.navigate(url, {
|
|
46
|
+
state,
|
|
47
|
+
history: 'push'
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
back() {
|
|
51
|
+
this.navigation.back();
|
|
52
|
+
}
|
|
53
|
+
forward() {
|
|
54
|
+
this.navigation.forward();
|
|
55
|
+
}
|
|
56
|
+
onUrlChange(fn) {
|
|
57
|
+
this._urlChangeListeners.push(fn);
|
|
58
|
+
return () => {
|
|
59
|
+
const fnIndex = this._urlChangeListeners.indexOf(fn);
|
|
60
|
+
this._urlChangeListeners.splice(fnIndex, 1);
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
static ɵfac = i0.ɵɵngDeclareFactory({
|
|
64
|
+
minVersion: "12.0.0",
|
|
65
|
+
version: "21.0.0-rc.0",
|
|
66
|
+
ngImport: i0,
|
|
67
|
+
type: NavigationAdapterForLocation,
|
|
68
|
+
deps: [],
|
|
69
|
+
target: i0.ɵɵFactoryTarget.Injectable
|
|
70
|
+
});
|
|
71
|
+
static ɵprov = i0.ɵɵngDeclareInjectable({
|
|
72
|
+
minVersion: "12.0.0",
|
|
73
|
+
version: "21.0.0-rc.0",
|
|
74
|
+
ngImport: i0,
|
|
75
|
+
type: NavigationAdapterForLocation
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
i0.ɵɵngDeclareClassMetadata({
|
|
79
|
+
minVersion: "12.0.0",
|
|
80
|
+
version: "21.0.0-rc.0",
|
|
81
|
+
ngImport: i0,
|
|
82
|
+
type: NavigationAdapterForLocation,
|
|
83
|
+
decorators: [{
|
|
84
|
+
type: Injectable
|
|
85
|
+
}],
|
|
86
|
+
ctorParameters: () => []
|
|
87
|
+
});
|
|
88
|
+
|
|
25
89
|
function registerLocaleData(data, localeId, extraData) {
|
|
26
|
-
|
|
90
|
+
return _registerLocaleData(data, localeId, extraData);
|
|
27
91
|
}
|
|
28
92
|
|
|
29
93
|
const PLATFORM_BROWSER_ID = 'browser';
|
|
30
94
|
const PLATFORM_SERVER_ID = 'server';
|
|
31
|
-
/**
|
|
32
|
-
* Returns whether a platform id represents a browser platform.
|
|
33
|
-
* @publicApi
|
|
34
|
-
*/
|
|
35
95
|
function isPlatformBrowser(platformId) {
|
|
36
|
-
|
|
96
|
+
return platformId === PLATFORM_BROWSER_ID;
|
|
37
97
|
}
|
|
38
|
-
/**
|
|
39
|
-
* Returns whether a platform id represents a server platform.
|
|
40
|
-
* @publicApi
|
|
41
|
-
*/
|
|
42
98
|
function isPlatformServer(platformId) {
|
|
43
|
-
|
|
99
|
+
return platformId === PLATFORM_SERVER_ID;
|
|
44
100
|
}
|
|
45
101
|
|
|
46
|
-
|
|
47
|
-
* @module
|
|
48
|
-
* @description
|
|
49
|
-
* Entry point for all public APIs of the common package.
|
|
50
|
-
*/
|
|
51
|
-
/**
|
|
52
|
-
* @publicApi
|
|
53
|
-
*/
|
|
54
|
-
const VERSION = new Version('21.0.0-next.9');
|
|
102
|
+
const VERSION = new Version('21.0.0-rc.0');
|
|
55
103
|
|
|
56
|
-
/**
|
|
57
|
-
* Defines a scroll position manager. Implemented by `BrowserViewportScroller`.
|
|
58
|
-
*
|
|
59
|
-
* @publicApi
|
|
60
|
-
*/
|
|
61
104
|
class ViewportScroller {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
? new NullViewportScroller()
|
|
70
|
-
: new BrowserViewportScroller(inject(DOCUMENT), window),
|
|
71
|
-
});
|
|
105
|
+
static ɵprov =
|
|
106
|
+
/* @__PURE__ */
|
|
107
|
+
__defineInjectable({
|
|
108
|
+
token: ViewportScroller,
|
|
109
|
+
providedIn: 'root',
|
|
110
|
+
factory: () => typeof ngServerMode !== 'undefined' && ngServerMode ? new NullViewportScroller() : new BrowserViewportScroller(inject(DOCUMENT), window)
|
|
111
|
+
});
|
|
72
112
|
}
|
|
73
|
-
/**
|
|
74
|
-
* Manages the scroll position for a browser window.
|
|
75
|
-
*/
|
|
76
113
|
class BrowserViewportScroller {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
this.scrollToElement(elSelected, options);
|
|
127
|
-
// After scrolling to the element, the spec dictates that we follow the focus steps for the
|
|
128
|
-
// target. Rather than following the robust steps, simply attempt focus.
|
|
129
|
-
//
|
|
130
|
-
// @see https://html.spec.whatwg.org/#get-the-focusable-area
|
|
131
|
-
// @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLOrForeignElement/focus
|
|
132
|
-
// @see https://html.spec.whatwg.org/#focusable-area
|
|
133
|
-
elSelected.focus();
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Disables automatic scroll restoration provided by the browser.
|
|
138
|
-
*/
|
|
139
|
-
setHistoryScrollRestoration(scrollRestoration) {
|
|
140
|
-
try {
|
|
141
|
-
this.window.history.scrollRestoration = scrollRestoration;
|
|
142
|
-
}
|
|
143
|
-
catch {
|
|
144
|
-
console.warn(_formatRuntimeError(2400 /* RuntimeErrorCode.SCROLL_RESTORATION_UNSUPPORTED */, ngDevMode &&
|
|
145
|
-
'Failed to set `window.history.scrollRestoration`. ' +
|
|
146
|
-
'This may occur when:\n' +
|
|
147
|
-
'• The script is running inside a sandboxed iframe\n' +
|
|
148
|
-
'• The window is partially navigated or inactive\n' +
|
|
149
|
-
'• The script is executed in an untrusted or special context (e.g., test runners, browser extensions, or content previews)\n' +
|
|
150
|
-
'Scroll position may not be preserved across navigation.'));
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Scrolls to an element using the native offset and the specified offset set on this scroller.
|
|
155
|
-
*
|
|
156
|
-
* The offset can be used when we know that there is a floating header and scrolling naively to an
|
|
157
|
-
* element (ex: `scrollIntoView`) leaves the element hidden behind the floating header.
|
|
158
|
-
*/
|
|
159
|
-
scrollToElement(el, options) {
|
|
160
|
-
const rect = el.getBoundingClientRect();
|
|
161
|
-
const left = rect.left + this.window.pageXOffset;
|
|
162
|
-
const top = rect.top + this.window.pageYOffset;
|
|
163
|
-
const offset = this.offset();
|
|
164
|
-
this.window.scrollTo({
|
|
165
|
-
...options,
|
|
166
|
-
left: left - offset[0],
|
|
167
|
-
top: top - offset[1],
|
|
168
|
-
});
|
|
169
|
-
}
|
|
114
|
+
document;
|
|
115
|
+
window;
|
|
116
|
+
offset = () => [0, 0];
|
|
117
|
+
constructor(document, window) {
|
|
118
|
+
this.document = document;
|
|
119
|
+
this.window = window;
|
|
120
|
+
}
|
|
121
|
+
setOffset(offset) {
|
|
122
|
+
if (Array.isArray(offset)) {
|
|
123
|
+
this.offset = () => offset;
|
|
124
|
+
} else {
|
|
125
|
+
this.offset = offset;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
getScrollPosition() {
|
|
129
|
+
return [this.window.scrollX, this.window.scrollY];
|
|
130
|
+
}
|
|
131
|
+
scrollToPosition(position, options) {
|
|
132
|
+
this.window.scrollTo({
|
|
133
|
+
...options,
|
|
134
|
+
left: position[0],
|
|
135
|
+
top: position[1]
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
scrollToAnchor(target, options) {
|
|
139
|
+
const elSelected = findAnchorFromDocument(this.document, target);
|
|
140
|
+
if (elSelected) {
|
|
141
|
+
this.scrollToElement(elSelected, options);
|
|
142
|
+
elSelected.focus();
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
setHistoryScrollRestoration(scrollRestoration) {
|
|
146
|
+
try {
|
|
147
|
+
this.window.history.scrollRestoration = scrollRestoration;
|
|
148
|
+
} catch {
|
|
149
|
+
console.warn(_formatRuntimeError(2400, ngDevMode && 'Failed to set `window.history.scrollRestoration`. ' + 'This may occur when:\n' + '• The script is running inside a sandboxed iframe\n' + '• The window is partially navigated or inactive\n' + '• The script is executed in an untrusted or special context (e.g., test runners, browser extensions, or content previews)\n' + 'Scroll position may not be preserved across navigation.'));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
scrollToElement(el, options) {
|
|
153
|
+
const rect = el.getBoundingClientRect();
|
|
154
|
+
const left = rect.left + this.window.pageXOffset;
|
|
155
|
+
const top = rect.top + this.window.pageYOffset;
|
|
156
|
+
const offset = this.offset();
|
|
157
|
+
this.window.scrollTo({
|
|
158
|
+
...options,
|
|
159
|
+
left: left - offset[0],
|
|
160
|
+
top: top - offset[1]
|
|
161
|
+
});
|
|
162
|
+
}
|
|
170
163
|
}
|
|
171
164
|
function findAnchorFromDocument(document, target) {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
if (shadowRoot) {
|
|
186
|
-
// Note that `ShadowRoot` doesn't support `getElementsByName`
|
|
187
|
-
// so we have to fall back to `querySelector`.
|
|
188
|
-
const result = shadowRoot.getElementById(target) || shadowRoot.querySelector(`[name="${target}"]`);
|
|
189
|
-
if (result) {
|
|
190
|
-
return result;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
currentNode = treeWalker.nextNode();
|
|
165
|
+
const documentResult = document.getElementById(target) || document.getElementsByName(target)[0];
|
|
166
|
+
if (documentResult) {
|
|
167
|
+
return documentResult;
|
|
168
|
+
}
|
|
169
|
+
if (typeof document.createTreeWalker === 'function' && document.body && typeof document.body.attachShadow === 'function') {
|
|
170
|
+
const treeWalker = document.createTreeWalker(document.body, NodeFilter.SHOW_ELEMENT);
|
|
171
|
+
let currentNode = treeWalker.currentNode;
|
|
172
|
+
while (currentNode) {
|
|
173
|
+
const shadowRoot = currentNode.shadowRoot;
|
|
174
|
+
if (shadowRoot) {
|
|
175
|
+
const result = shadowRoot.getElementById(target) || shadowRoot.querySelector(`[name="${target}"]`);
|
|
176
|
+
if (result) {
|
|
177
|
+
return result;
|
|
194
178
|
}
|
|
179
|
+
}
|
|
180
|
+
currentNode = treeWalker.nextNode();
|
|
195
181
|
}
|
|
196
|
-
|
|
182
|
+
}
|
|
183
|
+
return null;
|
|
197
184
|
}
|
|
198
|
-
/**
|
|
199
|
-
* Provides an empty implementation of the viewport scroller.
|
|
200
|
-
*/
|
|
201
185
|
class NullViewportScroller {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
getScrollPosition() {
|
|
210
|
-
return [0, 0];
|
|
211
|
-
}
|
|
212
|
-
/**
|
|
213
|
-
* Empty implementation
|
|
214
|
-
*/
|
|
215
|
-
scrollToPosition(position) { }
|
|
216
|
-
/**
|
|
217
|
-
* Empty implementation
|
|
218
|
-
*/
|
|
219
|
-
scrollToAnchor(anchor) { }
|
|
220
|
-
/**
|
|
221
|
-
* Empty implementation
|
|
222
|
-
*/
|
|
223
|
-
setHistoryScrollRestoration(scrollRestoration) { }
|
|
186
|
+
setOffset(offset) {}
|
|
187
|
+
getScrollPosition() {
|
|
188
|
+
return [0, 0];
|
|
189
|
+
}
|
|
190
|
+
scrollToPosition(position) {}
|
|
191
|
+
scrollToAnchor(anchor) {}
|
|
192
|
+
setHistoryScrollRestoration(scrollRestoration) {}
|
|
224
193
|
}
|
|
225
194
|
|
|
226
|
-
/**
|
|
227
|
-
* Value (out of 100) of the requested quality for placeholder images.
|
|
228
|
-
*/
|
|
229
195
|
const PLACEHOLDER_QUALITY = '20';
|
|
230
196
|
|
|
231
|
-
// Converts a string that represents a URL into a URL class instance.
|
|
232
197
|
function getUrl(src, win) {
|
|
233
|
-
|
|
234
|
-
return isAbsoluteUrl(src) ? new URL(src) : new URL(src, win.location.href);
|
|
198
|
+
return isAbsoluteUrl(src) ? new URL(src) : new URL(src, win.location.href);
|
|
235
199
|
}
|
|
236
|
-
// Checks whether a URL is absolute (i.e. starts with `http://` or `https://`).
|
|
237
200
|
function isAbsoluteUrl(src) {
|
|
238
|
-
|
|
201
|
+
return /^https?:\/\//.test(src);
|
|
239
202
|
}
|
|
240
|
-
// Given a URL, extract the hostname part.
|
|
241
|
-
// If a URL is a relative one - the URL is returned as is.
|
|
242
203
|
function extractHostname(url) {
|
|
243
|
-
|
|
204
|
+
return isAbsoluteUrl(url) ? new URL(url).hostname : url;
|
|
244
205
|
}
|
|
245
206
|
function isValidPath(path) {
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
return false;
|
|
257
|
-
}
|
|
207
|
+
const isString = typeof path === 'string';
|
|
208
|
+
if (!isString || path.trim() === '') {
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
try {
|
|
212
|
+
const url = new URL(path);
|
|
213
|
+
return true;
|
|
214
|
+
} catch {
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
258
217
|
}
|
|
259
218
|
function normalizePath(path) {
|
|
260
|
-
|
|
219
|
+
return path.endsWith('/') ? path.slice(0, -1) : path;
|
|
261
220
|
}
|
|
262
221
|
function normalizeSrc(src) {
|
|
263
|
-
|
|
222
|
+
return src.startsWith('/') ? src.slice(1) : src;
|
|
264
223
|
}
|
|
265
224
|
|
|
266
|
-
|
|
267
|
-
* Noop image loader that does no transformation to the original src and just returns it as is.
|
|
268
|
-
* This loader is used as a default one if more specific logic is not provided in an app config.
|
|
269
|
-
*
|
|
270
|
-
* @see {@link ImageLoader}
|
|
271
|
-
* @see {@link NgOptimizedImage}
|
|
272
|
-
*/
|
|
273
|
-
const noopImageLoader = (config) => config.src;
|
|
274
|
-
/**
|
|
275
|
-
* Injection token that configures the image loader function.
|
|
276
|
-
*
|
|
277
|
-
* @see {@link ImageLoader}
|
|
278
|
-
* @see {@link NgOptimizedImage}
|
|
279
|
-
* @publicApi
|
|
280
|
-
*/
|
|
225
|
+
const noopImageLoader = config => config.src;
|
|
281
226
|
const IMAGE_LOADER = new InjectionToken(typeof ngDevMode !== undefined && ngDevMode ? 'ImageLoader' : '', {
|
|
282
|
-
|
|
283
|
-
|
|
227
|
+
providedIn: 'root',
|
|
228
|
+
factory: () => noopImageLoader
|
|
284
229
|
});
|
|
285
|
-
/**
|
|
286
|
-
* Internal helper function that makes it easier to introduce custom image loaders for the
|
|
287
|
-
* `NgOptimizedImage` directive. It is enough to specify a URL builder function to obtain full DI
|
|
288
|
-
* configuration for a given loader: a DI token corresponding to the actual loader function, plus DI
|
|
289
|
-
* tokens managing preconnect check functionality.
|
|
290
|
-
* @param buildUrlFn a function returning a full URL based on loader's configuration
|
|
291
|
-
* @param exampleUrls example of full URLs for a given loader (used in error messages)
|
|
292
|
-
* @returns a set of DI providers corresponding to the configured image loader
|
|
293
|
-
*/
|
|
294
230
|
function createImageLoader(buildUrlFn, exampleUrls) {
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
// build a final URL, thus the error is thrown to indicate that.
|
|
309
|
-
throwUnexpectedAbsoluteUrlError(path, config.src);
|
|
310
|
-
}
|
|
311
|
-
return buildUrlFn(path, { ...config, src: normalizeSrc(config.src) });
|
|
312
|
-
};
|
|
313
|
-
const providers = [{ provide: IMAGE_LOADER, useValue: loaderFn }];
|
|
314
|
-
return providers;
|
|
231
|
+
return function provideImageLoader(path) {
|
|
232
|
+
if (!isValidPath(path)) {
|
|
233
|
+
throwInvalidPathError(path, exampleUrls || []);
|
|
234
|
+
}
|
|
235
|
+
path = normalizePath(path);
|
|
236
|
+
const loaderFn = config => {
|
|
237
|
+
if (isAbsoluteUrl(config.src)) {
|
|
238
|
+
throwUnexpectedAbsoluteUrlError(path, config.src);
|
|
239
|
+
}
|
|
240
|
+
return buildUrlFn(path, {
|
|
241
|
+
...config,
|
|
242
|
+
src: normalizeSrc(config.src)
|
|
243
|
+
});
|
|
315
244
|
};
|
|
245
|
+
const providers = [{
|
|
246
|
+
provide: IMAGE_LOADER,
|
|
247
|
+
useValue: loaderFn
|
|
248
|
+
}];
|
|
249
|
+
return providers;
|
|
250
|
+
};
|
|
316
251
|
}
|
|
317
252
|
function throwInvalidPathError(path, exampleUrls) {
|
|
318
|
-
|
|
319
|
-
`Image loader has detected an invalid path (\`${path}\`). ` +
|
|
320
|
-
`To fix this, supply a path using one of the following formats: ${exampleUrls.join(' or ')}`);
|
|
253
|
+
throw new _RuntimeError(2959, ngDevMode && `Image loader has detected an invalid path (\`${path}\`). ` + `To fix this, supply a path using one of the following formats: ${exampleUrls.join(' or ')}`);
|
|
321
254
|
}
|
|
322
255
|
function throwUnexpectedAbsoluteUrlError(path, url) {
|
|
323
|
-
|
|
324
|
-
`Image loader has detected a \`<img>\` tag with an invalid \`ngSrc\` attribute: ${url}. ` +
|
|
325
|
-
`This image loader expects \`ngSrc\` to be a relative URL - ` +
|
|
326
|
-
`however the provided value is an absolute URL. ` +
|
|
327
|
-
`To fix this, provide \`ngSrc\` as a path relative to the base URL ` +
|
|
328
|
-
`configured for this loader (\`${path}\`).`);
|
|
256
|
+
throw new _RuntimeError(2959, ngDevMode && `Image loader has detected a \`<img>\` tag with an invalid \`ngSrc\` attribute: ${url}. ` + `This image loader expects \`ngSrc\` to be a relative URL - ` + `however the provided value is an absolute URL. ` + `To fix this, provide \`ngSrc\` as a path relative to the base URL ` + `configured for this loader (\`${path}\`).`);
|
|
329
257
|
}
|
|
330
258
|
|
|
331
|
-
/**
|
|
332
|
-
* Function that generates an ImageLoader for [Cloudflare Image
|
|
333
|
-
* Resizing](https://developers.cloudflare.com/images/image-resizing/) and turns it into an Angular
|
|
334
|
-
* provider. Note: Cloudflare has multiple image products - this provider is specifically for
|
|
335
|
-
* Cloudflare Image Resizing; it will not work with Cloudflare Images or Cloudflare Polish.
|
|
336
|
-
*
|
|
337
|
-
* @param path Your domain name, e.g. https://mysite.com
|
|
338
|
-
* @returns Provider that provides an ImageLoader function
|
|
339
|
-
*
|
|
340
|
-
* @publicApi
|
|
341
|
-
*/
|
|
342
259
|
const provideCloudflareLoader = createImageLoader(createCloudflareUrl, ngDevMode ? ['https://<ZONE>/cdn-cgi/image/<OPTIONS>/<SOURCE-IMAGE>'] : undefined);
|
|
343
260
|
function createCloudflareUrl(path, config) {
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
// Cloudflare image URLs format:
|
|
353
|
-
// https://developers.cloudflare.com/images/image-resizing/url-format/
|
|
354
|
-
return `${path}/cdn-cgi/image/${params}/${config.src}`;
|
|
261
|
+
let params = `format=auto`;
|
|
262
|
+
if (config.width) {
|
|
263
|
+
params += `,width=${config.width}`;
|
|
264
|
+
}
|
|
265
|
+
if (config.isPlaceholder) {
|
|
266
|
+
params += `,quality=${PLACEHOLDER_QUALITY}`;
|
|
267
|
+
}
|
|
268
|
+
return `${path}/cdn-cgi/image/${params}/${config.src}`;
|
|
355
269
|
}
|
|
356
270
|
|
|
357
|
-
/**
|
|
358
|
-
* Name and URL tester for Cloudinary.
|
|
359
|
-
*/
|
|
360
271
|
const cloudinaryLoaderInfo = {
|
|
361
|
-
|
|
362
|
-
|
|
272
|
+
name: 'Cloudinary',
|
|
273
|
+
testUrl: isCloudinaryUrl
|
|
363
274
|
};
|
|
364
275
|
const CLOUDINARY_LOADER_REGEX = /https?\:\/\/[^\/]+\.cloudinary\.com\/.+/;
|
|
365
|
-
/**
|
|
366
|
-
* Tests whether a URL is from Cloudinary CDN.
|
|
367
|
-
*/
|
|
368
276
|
function isCloudinaryUrl(url) {
|
|
369
|
-
|
|
277
|
+
return CLOUDINARY_LOADER_REGEX.test(url);
|
|
370
278
|
}
|
|
371
|
-
|
|
372
|
-
* Function that generates an ImageLoader for Cloudinary and turns it into an Angular provider.
|
|
373
|
-
*
|
|
374
|
-
* @param path Base URL of your Cloudinary images
|
|
375
|
-
* This URL should match one of the following formats:
|
|
376
|
-
* https://res.cloudinary.com/mysite
|
|
377
|
-
* https://mysite.cloudinary.com
|
|
378
|
-
* https://subdomain.mysite.com
|
|
379
|
-
* @returns Set of providers to configure the Cloudinary loader.
|
|
380
|
-
*
|
|
381
|
-
* @publicApi
|
|
382
|
-
*/
|
|
383
|
-
const provideCloudinaryLoader = createImageLoader(createCloudinaryUrl, ngDevMode
|
|
384
|
-
? [
|
|
385
|
-
'https://res.cloudinary.com/mysite',
|
|
386
|
-
'https://mysite.cloudinary.com',
|
|
387
|
-
'https://subdomain.mysite.com',
|
|
388
|
-
]
|
|
389
|
-
: undefined);
|
|
279
|
+
const provideCloudinaryLoader = createImageLoader(createCloudinaryUrl, ngDevMode ? ['https://res.cloudinary.com/mysite', 'https://mysite.cloudinary.com', 'https://subdomain.mysite.com'] : undefined);
|
|
390
280
|
function createCloudinaryUrl(path, config) {
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
params += `,w_${config.width}`;
|
|
401
|
-
}
|
|
402
|
-
if (config.loaderParams?.['rounded']) {
|
|
403
|
-
params += `,r_max`;
|
|
404
|
-
}
|
|
405
|
-
return `${path}/image/upload/${params}/${config.src}`;
|
|
281
|
+
const quality = config.isPlaceholder ? 'q_auto:low' : 'q_auto';
|
|
282
|
+
let params = `f_auto,${quality}`;
|
|
283
|
+
if (config.width) {
|
|
284
|
+
params += `,w_${config.width}`;
|
|
285
|
+
}
|
|
286
|
+
if (config.loaderParams?.['rounded']) {
|
|
287
|
+
params += `,r_max`;
|
|
288
|
+
}
|
|
289
|
+
return `${path}/image/upload/${params}/${config.src}`;
|
|
406
290
|
}
|
|
407
291
|
|
|
408
|
-
/**
|
|
409
|
-
* Name and URL tester for ImageKit.
|
|
410
|
-
*/
|
|
411
292
|
const imageKitLoaderInfo = {
|
|
412
|
-
|
|
413
|
-
|
|
293
|
+
name: 'ImageKit',
|
|
294
|
+
testUrl: isImageKitUrl
|
|
414
295
|
};
|
|
415
296
|
const IMAGE_KIT_LOADER_REGEX = /https?\:\/\/[^\/]+\.imagekit\.io\/.+/;
|
|
416
|
-
/**
|
|
417
|
-
* Tests whether a URL is from ImageKit CDN.
|
|
418
|
-
*/
|
|
419
297
|
function isImageKitUrl(url) {
|
|
420
|
-
|
|
298
|
+
return IMAGE_KIT_LOADER_REGEX.test(url);
|
|
421
299
|
}
|
|
422
|
-
/**
|
|
423
|
-
* Function that generates an ImageLoader for ImageKit and turns it into an Angular provider.
|
|
424
|
-
*
|
|
425
|
-
* @param path Base URL of your ImageKit images
|
|
426
|
-
* This URL should match one of the following formats:
|
|
427
|
-
* https://ik.imagekit.io/myaccount
|
|
428
|
-
* https://subdomain.mysite.com
|
|
429
|
-
* @returns Set of providers to configure the ImageKit loader.
|
|
430
|
-
*
|
|
431
|
-
* @publicApi
|
|
432
|
-
*/
|
|
433
300
|
const provideImageKitLoader = createImageLoader(createImagekitUrl, ngDevMode ? ['https://ik.imagekit.io/mysite', 'https://subdomain.mysite.com'] : undefined);
|
|
434
301
|
function createImagekitUrl(path, config) {
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
302
|
+
const {
|
|
303
|
+
src,
|
|
304
|
+
width
|
|
305
|
+
} = config;
|
|
306
|
+
const params = [];
|
|
307
|
+
if (width) {
|
|
308
|
+
params.push(`w-${width}`);
|
|
309
|
+
}
|
|
310
|
+
if (config.isPlaceholder) {
|
|
311
|
+
params.push(`q-${PLACEHOLDER_QUALITY}`);
|
|
312
|
+
}
|
|
313
|
+
const urlSegments = params.length ? [path, `tr:${params.join(',')}`, src] : [path, src];
|
|
314
|
+
const url = new URL(urlSegments.join('/'));
|
|
315
|
+
return url.href;
|
|
449
316
|
}
|
|
450
317
|
|
|
451
|
-
/**
|
|
452
|
-
* Name and URL tester for Imgix.
|
|
453
|
-
*/
|
|
454
318
|
const imgixLoaderInfo = {
|
|
455
|
-
|
|
456
|
-
|
|
319
|
+
name: 'Imgix',
|
|
320
|
+
testUrl: isImgixUrl
|
|
457
321
|
};
|
|
458
322
|
const IMGIX_LOADER_REGEX = /https?\:\/\/[^\/]+\.imgix\.net\/.+/;
|
|
459
|
-
/**
|
|
460
|
-
* Tests whether a URL is from Imgix CDN.
|
|
461
|
-
*/
|
|
462
323
|
function isImgixUrl(url) {
|
|
463
|
-
|
|
324
|
+
return IMGIX_LOADER_REGEX.test(url);
|
|
464
325
|
}
|
|
465
|
-
/**
|
|
466
|
-
* Function that generates an ImageLoader for Imgix and turns it into an Angular provider.
|
|
467
|
-
*
|
|
468
|
-
* @param path path to the desired Imgix origin,
|
|
469
|
-
* e.g. https://somepath.imgix.net or https://images.mysite.com
|
|
470
|
-
* @returns Set of providers to configure the Imgix loader.
|
|
471
|
-
*
|
|
472
|
-
* @publicApi
|
|
473
|
-
*/
|
|
474
326
|
const provideImgixLoader = createImageLoader(createImgixUrl, ngDevMode ? ['https://somepath.imgix.net/'] : undefined);
|
|
475
327
|
function createImgixUrl(path, config) {
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
}
|
|
486
|
-
return url.href;
|
|
328
|
+
const url = new URL(`${path}/${config.src}`);
|
|
329
|
+
url.searchParams.set('auto', 'format');
|
|
330
|
+
if (config.width) {
|
|
331
|
+
url.searchParams.set('w', config.width.toString());
|
|
332
|
+
}
|
|
333
|
+
if (config.isPlaceholder) {
|
|
334
|
+
url.searchParams.set('q', PLACEHOLDER_QUALITY);
|
|
335
|
+
}
|
|
336
|
+
return url.href;
|
|
487
337
|
}
|
|
488
338
|
|
|
489
|
-
/**
|
|
490
|
-
* Name and URL tester for Netlify.
|
|
491
|
-
*/
|
|
492
339
|
const netlifyLoaderInfo = {
|
|
493
|
-
|
|
494
|
-
|
|
340
|
+
name: 'Netlify',
|
|
341
|
+
testUrl: isNetlifyUrl
|
|
495
342
|
};
|
|
496
343
|
const NETLIFY_LOADER_REGEX = /https?\:\/\/[^\/]+\.netlify\.app\/.+/;
|
|
497
|
-
/**
|
|
498
|
-
* Tests whether a URL is from a Netlify site. This won't catch sites with a custom domain,
|
|
499
|
-
* but it's a good start for sites in development. This is only used to warn users who haven't
|
|
500
|
-
* configured an image loader.
|
|
501
|
-
*/
|
|
502
344
|
function isNetlifyUrl(url) {
|
|
503
|
-
|
|
345
|
+
return NETLIFY_LOADER_REGEX.test(url);
|
|
504
346
|
}
|
|
505
|
-
/**
|
|
506
|
-
* Function that generates an ImageLoader for Netlify and turns it into an Angular provider.
|
|
507
|
-
*
|
|
508
|
-
* @param path optional URL of the desired Netlify site. Defaults to the current site.
|
|
509
|
-
* @returns Set of providers to configure the Netlify loader.
|
|
510
|
-
*
|
|
511
|
-
* @publicApi
|
|
512
|
-
*/
|
|
513
347
|
function provideNetlifyLoader(path) {
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
['fit', 'fit'],
|
|
532
|
-
['quality', 'q'],
|
|
533
|
-
['q', 'q'],
|
|
534
|
-
['position', 'position'],
|
|
535
|
-
]);
|
|
348
|
+
if (path && !isValidPath(path)) {
|
|
349
|
+
throw new _RuntimeError(2959, ngDevMode && `Image loader has detected an invalid path (\`${path}\`). ` + `To fix this, supply either the full URL to the Netlify site, or leave it empty to use the current site.`);
|
|
350
|
+
}
|
|
351
|
+
if (path) {
|
|
352
|
+
const url = new URL(path);
|
|
353
|
+
path = url.origin;
|
|
354
|
+
}
|
|
355
|
+
const loaderFn = config => {
|
|
356
|
+
return createNetlifyUrl(config, path);
|
|
357
|
+
};
|
|
358
|
+
const providers = [{
|
|
359
|
+
provide: IMAGE_LOADER,
|
|
360
|
+
useValue: loaderFn
|
|
361
|
+
}];
|
|
362
|
+
return providers;
|
|
363
|
+
}
|
|
364
|
+
const validParams = new Map([['height', 'h'], ['fit', 'fit'], ['quality', 'q'], ['q', 'q'], ['position', 'position']]);
|
|
536
365
|
function createNetlifyUrl(config, path) {
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
// The "a" hostname is used for relative URLs, so we can remove it from the final URL.
|
|
564
|
-
return url.hostname === 'a' ? url.href.replace(url.origin, '') : url.href;
|
|
366
|
+
const url = new URL(path ?? 'https://a/');
|
|
367
|
+
url.pathname = '/.netlify/images';
|
|
368
|
+
if (!isAbsoluteUrl(config.src) && !config.src.startsWith('/')) {
|
|
369
|
+
config.src = '/' + config.src;
|
|
370
|
+
}
|
|
371
|
+
url.searchParams.set('url', config.src);
|
|
372
|
+
if (config.width) {
|
|
373
|
+
url.searchParams.set('w', config.width.toString());
|
|
374
|
+
}
|
|
375
|
+
const configQuality = config.loaderParams?.['quality'] ?? config.loaderParams?.['q'];
|
|
376
|
+
if (config.isPlaceholder && !configQuality) {
|
|
377
|
+
url.searchParams.set('q', PLACEHOLDER_QUALITY);
|
|
378
|
+
}
|
|
379
|
+
for (const [param, value] of Object.entries(config.loaderParams ?? {})) {
|
|
380
|
+
if (validParams.has(param)) {
|
|
381
|
+
url.searchParams.set(validParams.get(param), value.toString());
|
|
382
|
+
} else {
|
|
383
|
+
if (ngDevMode) {
|
|
384
|
+
console.warn(_formatRuntimeError(2959, `The Netlify image loader has detected an \`<img>\` tag with the unsupported attribute "\`${param}\`".`));
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
return url.hostname === 'a' ? url.href.replace(url.origin, '') : url.href;
|
|
565
389
|
}
|
|
566
390
|
|
|
567
|
-
// Assembles directive details string, useful for error messages.
|
|
568
391
|
function imgDirectiveDetails(ngSrc, includeNgSrc = true) {
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
: '';
|
|
572
|
-
return `The NgOptimizedImage directive ${ngSrcInfo}has detected that`;
|
|
392
|
+
const ngSrcInfo = includeNgSrc ? `(activated on an <img> element with the \`ngSrc="${ngSrc}"\`) ` : '';
|
|
393
|
+
return `The NgOptimizedImage directive ${ngSrcInfo}has detected that`;
|
|
573
394
|
}
|
|
574
395
|
|
|
575
|
-
/**
|
|
576
|
-
* Asserts that the application is in development mode. Throws an error if the application is in
|
|
577
|
-
* production mode. This assert can be used to make sure that there is no dev-mode code invoked in
|
|
578
|
-
* the prod mode accidentally.
|
|
579
|
-
*/
|
|
580
396
|
function assertDevMode(checkName) {
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
}
|
|
397
|
+
if (!ngDevMode) {
|
|
398
|
+
throw new _RuntimeError(2958, `Unexpected invocation of the ${checkName} in the prod mode. ` + `Please make sure that the prod mode is enabled for production builds.`);
|
|
399
|
+
}
|
|
585
400
|
}
|
|
586
401
|
|
|
587
|
-
/**
|
|
588
|
-
* Observer that detects whether an image with `NgOptimizedImage`
|
|
589
|
-
* is treated as a Largest Contentful Paint (LCP) element. If so,
|
|
590
|
-
* asserts that the image has the `priority` attribute.
|
|
591
|
-
*
|
|
592
|
-
* Note: this is a dev-mode only class and it does not appear in prod bundles,
|
|
593
|
-
* thus there is no `ngDevMode` use in the code.
|
|
594
|
-
*
|
|
595
|
-
* Based on https://web.dev/lcp/#measure-lcp-in-javascript.
|
|
596
|
-
*/
|
|
597
402
|
class LCPImageObserver {
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
403
|
+
images = new Map();
|
|
404
|
+
window = inject(DOCUMENT).defaultView;
|
|
405
|
+
observer = null;
|
|
406
|
+
constructor() {
|
|
407
|
+
assertDevMode('LCP checker');
|
|
408
|
+
if ((typeof ngServerMode === 'undefined' || !ngServerMode) && typeof PerformanceObserver !== 'undefined') {
|
|
409
|
+
this.observer = this.initPerformanceObserver();
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
initPerformanceObserver() {
|
|
413
|
+
const observer = new PerformanceObserver(entryList => {
|
|
414
|
+
const entries = entryList.getEntries();
|
|
415
|
+
if (entries.length === 0) return;
|
|
416
|
+
const lcpElement = entries[entries.length - 1];
|
|
417
|
+
const imgSrc = lcpElement.element?.src ?? '';
|
|
418
|
+
if (imgSrc.startsWith('data:') || imgSrc.startsWith('blob:')) return;
|
|
419
|
+
const img = this.images.get(imgSrc);
|
|
420
|
+
if (!img) return;
|
|
421
|
+
if (!img.priority && !img.alreadyWarnedPriority) {
|
|
422
|
+
img.alreadyWarnedPriority = true;
|
|
423
|
+
logMissingPriorityError(imgSrc);
|
|
424
|
+
}
|
|
425
|
+
if (img.modified && !img.alreadyWarnedModified) {
|
|
426
|
+
img.alreadyWarnedModified = true;
|
|
427
|
+
logModifiedWarning(imgSrc);
|
|
428
|
+
}
|
|
429
|
+
});
|
|
430
|
+
observer.observe({
|
|
431
|
+
type: 'largest-contentful-paint',
|
|
432
|
+
buffered: true
|
|
433
|
+
});
|
|
434
|
+
return observer;
|
|
435
|
+
}
|
|
436
|
+
registerImage(rewrittenSrc, originalNgSrc, isPriority) {
|
|
437
|
+
if (!this.observer) return;
|
|
438
|
+
const newObservedImageState = {
|
|
439
|
+
priority: isPriority,
|
|
440
|
+
modified: false,
|
|
441
|
+
alreadyWarnedModified: false,
|
|
442
|
+
alreadyWarnedPriority: false
|
|
443
|
+
};
|
|
444
|
+
this.images.set(getUrl(rewrittenSrc, this.window).href, newObservedImageState);
|
|
445
|
+
}
|
|
446
|
+
unregisterImage(rewrittenSrc) {
|
|
447
|
+
if (!this.observer) return;
|
|
448
|
+
this.images.delete(getUrl(rewrittenSrc, this.window).href);
|
|
449
|
+
}
|
|
450
|
+
updateImage(originalSrc, newSrc) {
|
|
451
|
+
if (!this.observer) return;
|
|
452
|
+
const originalUrl = getUrl(originalSrc, this.window).href;
|
|
453
|
+
const img = this.images.get(originalUrl);
|
|
454
|
+
if (img) {
|
|
455
|
+
img.modified = true;
|
|
456
|
+
this.images.set(getUrl(newSrc, this.window).href, img);
|
|
457
|
+
this.images.delete(originalUrl);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
ngOnDestroy() {
|
|
461
|
+
if (!this.observer) return;
|
|
462
|
+
this.observer.disconnect();
|
|
463
|
+
this.images.clear();
|
|
464
|
+
}
|
|
465
|
+
static ɵfac = i0.ɵɵngDeclareFactory({
|
|
466
|
+
minVersion: "12.0.0",
|
|
467
|
+
version: "21.0.0-rc.0",
|
|
468
|
+
ngImport: i0,
|
|
469
|
+
type: LCPImageObserver,
|
|
470
|
+
deps: [],
|
|
471
|
+
target: i0.ɵɵFactoryTarget.Injectable
|
|
472
|
+
});
|
|
473
|
+
static ɵprov = i0.ɵɵngDeclareInjectable({
|
|
474
|
+
minVersion: "12.0.0",
|
|
475
|
+
version: "21.0.0-rc.0",
|
|
476
|
+
ngImport: i0,
|
|
477
|
+
type: LCPImageObserver,
|
|
478
|
+
providedIn: 'root'
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
i0.ɵɵngDeclareClassMetadata({
|
|
482
|
+
minVersion: "12.0.0",
|
|
483
|
+
version: "21.0.0-rc.0",
|
|
484
|
+
ngImport: i0,
|
|
485
|
+
type: LCPImageObserver,
|
|
486
|
+
decorators: [{
|
|
487
|
+
type: Injectable,
|
|
488
|
+
args: [{
|
|
489
|
+
providedIn: 'root'
|
|
490
|
+
}]
|
|
491
|
+
}],
|
|
492
|
+
ctorParameters: () => []
|
|
493
|
+
});
|
|
684
494
|
function logMissingPriorityError(ngSrc) {
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
`element but was not marked "priority". This image should be marked ` +
|
|
688
|
-
`"priority" in order to prioritize its loading. ` +
|
|
689
|
-
`To fix this, add the "priority" attribute.`));
|
|
495
|
+
const directiveDetails = imgDirectiveDetails(ngSrc);
|
|
496
|
+
console.error(_formatRuntimeError(2955, `${directiveDetails} this image is the Largest Contentful Paint (LCP) ` + `element but was not marked "priority". This image should be marked ` + `"priority" in order to prioritize its loading. ` + `To fix this, add the "priority" attribute.`));
|
|
690
497
|
}
|
|
691
498
|
function logModifiedWarning(ngSrc) {
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
`element and has had its "ngSrc" attribute modified. This can cause ` +
|
|
695
|
-
`slower loading performance. It is recommended not to modify the "ngSrc" ` +
|
|
696
|
-
`property on any image which could be the LCP element.`));
|
|
499
|
+
const directiveDetails = imgDirectiveDetails(ngSrc);
|
|
500
|
+
console.warn(_formatRuntimeError(2964, `${directiveDetails} this image is the Largest Contentful Paint (LCP) ` + `element and has had its "ngSrc" attribute modified. This can cause ` + `slower loading performance. It is recommended not to modify the "ngSrc" ` + `property on any image which could be the LCP element.`));
|
|
697
501
|
}
|
|
698
502
|
|
|
699
|
-
|
|
700
|
-
const INTERNAL_PRECONNECT_CHECK_BLOCKLIST = new Set(['localhost', '127.0.0.1', '0.0.0.0']);
|
|
701
|
-
/**
|
|
702
|
-
* Injection token to configure which origins should be excluded
|
|
703
|
-
* from the preconnect checks. It can either be a single string or an array of strings
|
|
704
|
-
* to represent a group of origins, for example:
|
|
705
|
-
*
|
|
706
|
-
* ```ts
|
|
707
|
-
* {provide: PRECONNECT_CHECK_BLOCKLIST, useValue: 'https://your-domain.com'}
|
|
708
|
-
* ```
|
|
709
|
-
*
|
|
710
|
-
* or:
|
|
711
|
-
*
|
|
712
|
-
* ```ts
|
|
713
|
-
* {provide: PRECONNECT_CHECK_BLOCKLIST,
|
|
714
|
-
* useValue: ['https://your-domain-1.com', 'https://your-domain-2.com']}
|
|
715
|
-
* ```
|
|
716
|
-
*
|
|
717
|
-
* @publicApi
|
|
718
|
-
*/
|
|
503
|
+
const INTERNAL_PRECONNECT_CHECK_BLOCKLIST = new Set(['localhost', '127.0.0.1', '0.0.0.0', '[::1]']);
|
|
719
504
|
const PRECONNECT_CHECK_BLOCKLIST = new InjectionToken(typeof ngDevMode !== undefined && ngDevMode ? 'PRECONNECT_CHECK_BLOCKLIST' : '');
|
|
720
|
-
/**
|
|
721
|
-
* Contains the logic to detect whether an image, marked with the "priority" attribute
|
|
722
|
-
* has a corresponding `<link rel="preconnect">` tag in the `document.head`.
|
|
723
|
-
*
|
|
724
|
-
* Note: this is a dev-mode only class, which should not appear in prod bundles,
|
|
725
|
-
* thus there is no `ngDevMode` use in the code.
|
|
726
|
-
*/
|
|
727
505
|
class PreconnectLinkChecker {
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
}
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
args: [{ providedIn: 'root' }]
|
|
804
|
-
}], ctorParameters: () => [] });
|
|
805
|
-
/**
|
|
806
|
-
* Invokes a callback for each element in the array. Also invokes a callback
|
|
807
|
-
* recursively for each nested array.
|
|
808
|
-
*/
|
|
506
|
+
document = inject(DOCUMENT);
|
|
507
|
+
preconnectLinks = null;
|
|
508
|
+
alreadySeen = new Set();
|
|
509
|
+
window = this.document.defaultView;
|
|
510
|
+
blocklist = new Set(INTERNAL_PRECONNECT_CHECK_BLOCKLIST);
|
|
511
|
+
constructor() {
|
|
512
|
+
assertDevMode('preconnect link checker');
|
|
513
|
+
const blocklist = inject(PRECONNECT_CHECK_BLOCKLIST, {
|
|
514
|
+
optional: true
|
|
515
|
+
});
|
|
516
|
+
if (blocklist) {
|
|
517
|
+
this.populateBlocklist(blocklist);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
populateBlocklist(origins) {
|
|
521
|
+
if (Array.isArray(origins)) {
|
|
522
|
+
deepForEach(origins, origin => {
|
|
523
|
+
this.blocklist.add(extractHostname(origin));
|
|
524
|
+
});
|
|
525
|
+
} else {
|
|
526
|
+
this.blocklist.add(extractHostname(origins));
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
assertPreconnect(rewrittenSrc, originalNgSrc) {
|
|
530
|
+
if (typeof ngServerMode !== 'undefined' && ngServerMode) return;
|
|
531
|
+
const imgUrl = getUrl(rewrittenSrc, this.window);
|
|
532
|
+
if (this.blocklist.has(imgUrl.hostname) || this.alreadySeen.has(imgUrl.origin)) return;
|
|
533
|
+
this.alreadySeen.add(imgUrl.origin);
|
|
534
|
+
this.preconnectLinks ??= this.queryPreconnectLinks();
|
|
535
|
+
if (!this.preconnectLinks.has(imgUrl.origin)) {
|
|
536
|
+
console.warn(_formatRuntimeError(2956, `${imgDirectiveDetails(originalNgSrc)} there is no preconnect tag present for this ` + `image. Preconnecting to the origin(s) that serve priority images ensures that these ` + `images are delivered as soon as possible. To fix this, please add the following ` + `element into the <head> of the document:\n` + ` <link rel="preconnect" href="${imgUrl.origin}">`));
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
queryPreconnectLinks() {
|
|
540
|
+
const preconnectUrls = new Set();
|
|
541
|
+
const links = this.document.querySelectorAll('link[rel=preconnect]');
|
|
542
|
+
for (const link of links) {
|
|
543
|
+
const url = getUrl(link.href, this.window);
|
|
544
|
+
preconnectUrls.add(url.origin);
|
|
545
|
+
}
|
|
546
|
+
return preconnectUrls;
|
|
547
|
+
}
|
|
548
|
+
ngOnDestroy() {
|
|
549
|
+
this.preconnectLinks?.clear();
|
|
550
|
+
this.alreadySeen.clear();
|
|
551
|
+
}
|
|
552
|
+
static ɵfac = i0.ɵɵngDeclareFactory({
|
|
553
|
+
minVersion: "12.0.0",
|
|
554
|
+
version: "21.0.0-rc.0",
|
|
555
|
+
ngImport: i0,
|
|
556
|
+
type: PreconnectLinkChecker,
|
|
557
|
+
deps: [],
|
|
558
|
+
target: i0.ɵɵFactoryTarget.Injectable
|
|
559
|
+
});
|
|
560
|
+
static ɵprov = i0.ɵɵngDeclareInjectable({
|
|
561
|
+
minVersion: "12.0.0",
|
|
562
|
+
version: "21.0.0-rc.0",
|
|
563
|
+
ngImport: i0,
|
|
564
|
+
type: PreconnectLinkChecker,
|
|
565
|
+
providedIn: 'root'
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
i0.ɵɵngDeclareClassMetadata({
|
|
569
|
+
minVersion: "12.0.0",
|
|
570
|
+
version: "21.0.0-rc.0",
|
|
571
|
+
ngImport: i0,
|
|
572
|
+
type: PreconnectLinkChecker,
|
|
573
|
+
decorators: [{
|
|
574
|
+
type: Injectable,
|
|
575
|
+
args: [{
|
|
576
|
+
providedIn: 'root'
|
|
577
|
+
}]
|
|
578
|
+
}],
|
|
579
|
+
ctorParameters: () => []
|
|
580
|
+
});
|
|
809
581
|
function deepForEach(input, fn) {
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
582
|
+
for (let value of input) {
|
|
583
|
+
Array.isArray(value) ? deepForEach(value, fn) : fn(value);
|
|
584
|
+
}
|
|
813
585
|
}
|
|
814
586
|
|
|
815
|
-
/**
|
|
816
|
-
* In SSR scenarios, a preload `<link>` element is generated for priority images.
|
|
817
|
-
* Having a large number of preload tags may negatively affect the performance,
|
|
818
|
-
* so we warn developers (by throwing an error) if the number of preloaded images
|
|
819
|
-
* is above a certain threshold. This const specifies this threshold.
|
|
820
|
-
*/
|
|
821
587
|
const DEFAULT_PRELOADED_IMAGES_LIMIT = 5;
|
|
822
|
-
/**
|
|
823
|
-
* Helps to keep track of priority images that already have a corresponding
|
|
824
|
-
* preload tag (to avoid generating multiple preload tags with the same URL).
|
|
825
|
-
*
|
|
826
|
-
* This Set tracks the original src passed into the `ngSrc` input not the src after it has been
|
|
827
|
-
* run through the specified `IMAGE_LOADER`.
|
|
828
|
-
*/
|
|
829
588
|
const PRELOADED_IMAGES = new InjectionToken(typeof ngDevMode === 'undefined' || ngDevMode ? 'NG_OPTIMIZED_PRELOADED_IMAGES' : '', {
|
|
830
|
-
|
|
831
|
-
|
|
589
|
+
providedIn: 'root',
|
|
590
|
+
factory: () => new Set()
|
|
832
591
|
});
|
|
833
592
|
|
|
834
|
-
/**
|
|
835
|
-
* @description Contains the logic needed to track and add preload link tags to the `<head>` tag. It
|
|
836
|
-
* will also track what images have already had preload link tags added so as to not duplicate link
|
|
837
|
-
* tags.
|
|
838
|
-
*
|
|
839
|
-
* In dev mode this service will validate that the number of preloaded images does not exceed the
|
|
840
|
-
* configured default preloaded images limit: {@link DEFAULT_PRELOADED_IMAGES_LIMIT}.
|
|
841
|
-
*/
|
|
842
593
|
class PreloadLinkCreator {
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
594
|
+
preloadedImages = inject(PRELOADED_IMAGES);
|
|
595
|
+
document = inject(DOCUMENT);
|
|
596
|
+
errorShown = false;
|
|
597
|
+
createPreloadLinkTag(renderer, src, srcset, sizes) {
|
|
598
|
+
if (ngDevMode && !this.errorShown && this.preloadedImages.size >= DEFAULT_PRELOADED_IMAGES_LIMIT) {
|
|
599
|
+
this.errorShown = true;
|
|
600
|
+
console.warn(_formatRuntimeError(2961, `The \`NgOptimizedImage\` directive has detected that more than ` + `${DEFAULT_PRELOADED_IMAGES_LIMIT} images were marked as priority. ` + `This might negatively affect an overall performance of the page. ` + `To fix this, remove the "priority" attribute from images with less priority.`));
|
|
601
|
+
}
|
|
602
|
+
if (this.preloadedImages.has(src)) {
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
this.preloadedImages.add(src);
|
|
606
|
+
const preload = renderer.createElement('link');
|
|
607
|
+
renderer.setAttribute(preload, 'as', 'image');
|
|
608
|
+
renderer.setAttribute(preload, 'href', src);
|
|
609
|
+
renderer.setAttribute(preload, 'rel', 'preload');
|
|
610
|
+
renderer.setAttribute(preload, 'fetchpriority', 'high');
|
|
611
|
+
if (sizes) {
|
|
612
|
+
renderer.setAttribute(preload, 'imageSizes', sizes);
|
|
613
|
+
}
|
|
614
|
+
if (srcset) {
|
|
615
|
+
renderer.setAttribute(preload, 'imageSrcset', srcset);
|
|
616
|
+
}
|
|
617
|
+
renderer.appendChild(this.document.head, preload);
|
|
618
|
+
}
|
|
619
|
+
static ɵfac = i0.ɵɵngDeclareFactory({
|
|
620
|
+
minVersion: "12.0.0",
|
|
621
|
+
version: "21.0.0-rc.0",
|
|
622
|
+
ngImport: i0,
|
|
623
|
+
type: PreloadLinkCreator,
|
|
624
|
+
deps: [],
|
|
625
|
+
target: i0.ɵɵFactoryTarget.Injectable
|
|
626
|
+
});
|
|
627
|
+
static ɵprov = i0.ɵɵngDeclareInjectable({
|
|
628
|
+
minVersion: "12.0.0",
|
|
629
|
+
version: "21.0.0-rc.0",
|
|
630
|
+
ngImport: i0,
|
|
631
|
+
type: PreloadLinkCreator,
|
|
632
|
+
providedIn: 'root'
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
i0.ɵɵngDeclareClassMetadata({
|
|
636
|
+
minVersion: "12.0.0",
|
|
637
|
+
version: "21.0.0-rc.0",
|
|
638
|
+
ngImport: i0,
|
|
639
|
+
type: PreloadLinkCreator,
|
|
640
|
+
decorators: [{
|
|
641
|
+
type: Injectable,
|
|
642
|
+
args: [{
|
|
643
|
+
providedIn: 'root'
|
|
644
|
+
}]
|
|
645
|
+
}]
|
|
646
|
+
});
|
|
896
647
|
|
|
897
|
-
/**
|
|
898
|
-
* When a Base64-encoded image is passed as an input to the `NgOptimizedImage` directive,
|
|
899
|
-
* an error is thrown. The image content (as a string) might be very long, thus making
|
|
900
|
-
* it hard to read an error message if the entire string is included. This const defines
|
|
901
|
-
* the number of characters that should be included into the error message. The rest
|
|
902
|
-
* of the content is truncated.
|
|
903
|
-
*/
|
|
904
648
|
const BASE64_IMG_MAX_LENGTH_IN_ERROR = 50;
|
|
905
|
-
/**
|
|
906
|
-
* RegExpr to determine whether a src in a srcset is using width descriptors.
|
|
907
|
-
* Should match something like: "100w, 200w".
|
|
908
|
-
*/
|
|
909
649
|
const VALID_WIDTH_DESCRIPTOR_SRCSET = /^((\s*\d+w\s*(,|$)){1,})$/;
|
|
910
|
-
/**
|
|
911
|
-
* RegExpr to determine whether a src in a srcset is using density descriptors.
|
|
912
|
-
* Should match something like: "1x, 2x, 50x". Also supports decimals like "1.5x, 1.50x".
|
|
913
|
-
*/
|
|
914
650
|
const VALID_DENSITY_DESCRIPTOR_SRCSET = /^((\s*\d+(\.\d+)?x\s*(,|$)){1,})$/;
|
|
915
|
-
/**
|
|
916
|
-
* Srcset values with a density descriptor higher than this value will actively
|
|
917
|
-
* throw an error. Such densities are not permitted as they cause image sizes
|
|
918
|
-
* to be unreasonably large and slow down LCP.
|
|
919
|
-
*/
|
|
920
651
|
const ABSOLUTE_SRCSET_DENSITY_CAP = 3;
|
|
921
|
-
/**
|
|
922
|
-
* Used only in error message text to communicate best practices, as we will
|
|
923
|
-
* only throw based on the slightly more conservative ABSOLUTE_SRCSET_DENSITY_CAP.
|
|
924
|
-
*/
|
|
925
652
|
const RECOMMENDED_SRCSET_DENSITY_CAP = 2;
|
|
926
|
-
/**
|
|
927
|
-
* Used in generating automatic density-based srcsets
|
|
928
|
-
*/
|
|
929
653
|
const DENSITY_SRCSET_MULTIPLIERS = [1, 2];
|
|
930
|
-
/**
|
|
931
|
-
* Used to determine which breakpoints to use on full-width images
|
|
932
|
-
*/
|
|
933
654
|
const VIEWPORT_BREAKPOINT_CUTOFF = 640;
|
|
934
|
-
/**
|
|
935
|
-
* Used to determine whether two aspect ratios are similar in value.
|
|
936
|
-
*/
|
|
937
655
|
const ASPECT_RATIO_TOLERANCE = 0.1;
|
|
938
|
-
/**
|
|
939
|
-
* Used to determine whether the image has been requested at an overly
|
|
940
|
-
* large size compared to the actual rendered image size (after taking
|
|
941
|
-
* into account a typical device pixel ratio). In pixels.
|
|
942
|
-
*/
|
|
943
656
|
const OVERSIZED_IMAGE_TOLERANCE = 1000;
|
|
944
|
-
/**
|
|
945
|
-
* Used to limit automatic srcset generation of very large sources for
|
|
946
|
-
* fixed-size images. In pixels.
|
|
947
|
-
*/
|
|
948
657
|
const FIXED_SRCSET_WIDTH_LIMIT = 1920;
|
|
949
658
|
const FIXED_SRCSET_HEIGHT_LIMIT = 1080;
|
|
950
|
-
/**
|
|
951
|
-
* Placeholder dimension (height or width) limit in pixels. Angular produces a warning
|
|
952
|
-
* when this limit is crossed.
|
|
953
|
-
*/
|
|
954
659
|
const PLACEHOLDER_DIMENSION_LIMIT = 1000;
|
|
955
|
-
/**
|
|
956
|
-
* Used to warn or error when the user provides an overly large dataURL for the placeholder
|
|
957
|
-
* attribute.
|
|
958
|
-
* Character count of Base64 images is 1 character per byte, and base64 encoding is approximately
|
|
959
|
-
* 33% larger than base images, so 4000 characters is around 3KB on disk and 10000 characters is
|
|
960
|
-
* around 7.7KB. Experimentally, 4000 characters is about 20x20px in PNG or medium-quality JPEG
|
|
961
|
-
* format, and 10,000 is around 50x50px, but there's quite a bit of variation depending on how the
|
|
962
|
-
* image is saved.
|
|
963
|
-
*/
|
|
964
660
|
const DATA_URL_WARN_LIMIT = 4000;
|
|
965
661
|
const DATA_URL_ERROR_LIMIT = 10000;
|
|
966
|
-
|
|
967
|
-
const BUILT_IN_LOADERS = [
|
|
968
|
-
imgixLoaderInfo,
|
|
969
|
-
imageKitLoaderInfo,
|
|
970
|
-
cloudinaryLoaderInfo,
|
|
971
|
-
netlifyLoaderInfo,
|
|
972
|
-
];
|
|
973
|
-
/**
|
|
974
|
-
* Threshold for the PRIORITY_TRUE_COUNT
|
|
975
|
-
*/
|
|
662
|
+
const BUILT_IN_LOADERS = [imgixLoaderInfo, imageKitLoaderInfo, cloudinaryLoaderInfo, netlifyLoaderInfo];
|
|
976
663
|
const PRIORITY_COUNT_THRESHOLD = 10;
|
|
977
|
-
/**
|
|
978
|
-
* This count is used to log a devMode warning
|
|
979
|
-
* when the count of directive instances with priority=true
|
|
980
|
-
* exceeds the threshold PRIORITY_COUNT_THRESHOLD
|
|
981
|
-
*/
|
|
982
664
|
let IMGS_WITH_PRIORITY_ATTR_COUNT = 0;
|
|
983
|
-
/**
|
|
984
|
-
* Directive that improves image loading performance by enforcing best practices.
|
|
985
|
-
*
|
|
986
|
-
* `NgOptimizedImage` ensures that the loading of the Largest Contentful Paint (LCP) image is
|
|
987
|
-
* prioritized by:
|
|
988
|
-
* - Automatically setting the `fetchpriority` attribute on the `<img>` tag
|
|
989
|
-
* - Lazy loading non-priority images by default
|
|
990
|
-
* - Automatically generating a preconnect link tag in the document head
|
|
991
|
-
*
|
|
992
|
-
* In addition, the directive:
|
|
993
|
-
* - Generates appropriate asset URLs if a corresponding `ImageLoader` function is provided
|
|
994
|
-
* - Automatically generates a srcset
|
|
995
|
-
* - Requires that `width` and `height` are set
|
|
996
|
-
* - Warns if `width` or `height` have been set incorrectly
|
|
997
|
-
* - Warns if the image will be visually distorted when rendered
|
|
998
|
-
*
|
|
999
|
-
* @usageNotes
|
|
1000
|
-
* The `NgOptimizedImage` directive is marked as [standalone](guide/components/importing) and can
|
|
1001
|
-
* be imported directly.
|
|
1002
|
-
*
|
|
1003
|
-
* Follow the steps below to enable and use the directive:
|
|
1004
|
-
* 1. Import it into the necessary NgModule or a standalone Component.
|
|
1005
|
-
* 2. Optionally provide an `ImageLoader` if you use an image hosting service.
|
|
1006
|
-
* 3. Update the necessary `<img>` tags in templates and replace `src` attributes with `ngSrc`.
|
|
1007
|
-
* Using a `ngSrc` allows the directive to control when the `src` gets set, which triggers an image
|
|
1008
|
-
* download.
|
|
1009
|
-
*
|
|
1010
|
-
* Step 1: import the `NgOptimizedImage` directive.
|
|
1011
|
-
*
|
|
1012
|
-
* ```ts
|
|
1013
|
-
* import { NgOptimizedImage } from '@angular/common';
|
|
1014
|
-
*
|
|
1015
|
-
* // Include it into the necessary NgModule
|
|
1016
|
-
* @NgModule({
|
|
1017
|
-
* imports: [NgOptimizedImage],
|
|
1018
|
-
* })
|
|
1019
|
-
* class AppModule {}
|
|
1020
|
-
*
|
|
1021
|
-
* // ... or a standalone Component
|
|
1022
|
-
* @Component({
|
|
1023
|
-
* imports: [NgOptimizedImage],
|
|
1024
|
-
* })
|
|
1025
|
-
* class MyStandaloneComponent {}
|
|
1026
|
-
* ```
|
|
1027
|
-
*
|
|
1028
|
-
* Step 2: configure a loader.
|
|
1029
|
-
*
|
|
1030
|
-
* To use the **default loader**: no additional code changes are necessary. The URL returned by the
|
|
1031
|
-
* generic loader will always match the value of "src". In other words, this loader applies no
|
|
1032
|
-
* transformations to the resource URL and the value of the `ngSrc` attribute will be used as is.
|
|
1033
|
-
*
|
|
1034
|
-
* To use an existing loader for a **third-party image service**: add the provider factory for your
|
|
1035
|
-
* chosen service to the `providers` array. In the example below, the Imgix loader is used:
|
|
1036
|
-
*
|
|
1037
|
-
* ```ts
|
|
1038
|
-
* import {provideImgixLoader} from '@angular/common';
|
|
1039
|
-
*
|
|
1040
|
-
* // Call the function and add the result to the `providers` array:
|
|
1041
|
-
* providers: [
|
|
1042
|
-
* provideImgixLoader("https://my.base.url/"),
|
|
1043
|
-
* ],
|
|
1044
|
-
* ```
|
|
1045
|
-
*
|
|
1046
|
-
* The `NgOptimizedImage` directive provides the following functions:
|
|
1047
|
-
* - `provideCloudflareLoader`
|
|
1048
|
-
* - `provideCloudinaryLoader`
|
|
1049
|
-
* - `provideImageKitLoader`
|
|
1050
|
-
* - `provideImgixLoader`
|
|
1051
|
-
*
|
|
1052
|
-
* If you use a different image provider, you can create a custom loader function as described
|
|
1053
|
-
* below.
|
|
1054
|
-
*
|
|
1055
|
-
* To use a **custom loader**: provide your loader function as a value for the `IMAGE_LOADER` DI
|
|
1056
|
-
* token.
|
|
1057
|
-
*
|
|
1058
|
-
* ```ts
|
|
1059
|
-
* import {IMAGE_LOADER, ImageLoaderConfig} from '@angular/common';
|
|
1060
|
-
*
|
|
1061
|
-
* // Configure the loader using the `IMAGE_LOADER` token.
|
|
1062
|
-
* providers: [
|
|
1063
|
-
* {
|
|
1064
|
-
* provide: IMAGE_LOADER,
|
|
1065
|
-
* useValue: (config: ImageLoaderConfig) => {
|
|
1066
|
-
* return `https://example.com/${config.src}-${config.width}.jpg`;
|
|
1067
|
-
* }
|
|
1068
|
-
* },
|
|
1069
|
-
* ],
|
|
1070
|
-
* ```
|
|
1071
|
-
*
|
|
1072
|
-
* Step 3: update `<img>` tags in templates to use `ngSrc` instead of `src`.
|
|
1073
|
-
*
|
|
1074
|
-
* ```html
|
|
1075
|
-
* <img ngSrc="logo.png" width="200" height="100">
|
|
1076
|
-
* ```
|
|
1077
|
-
*
|
|
1078
|
-
* @publicApi
|
|
1079
|
-
* @see [Image Optimization Guide](guide/image-optimization)
|
|
1080
|
-
*/
|
|
1081
665
|
class NgOptimizedImage {
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
* <img src="path/hello.jpg" srcset="path/hello.jpg?w=100 100w, path/hello.jpg?w=200 200w" />
|
|
1112
|
-
* ```
|
|
1113
|
-
*/
|
|
1114
|
-
ngSrcset;
|
|
1115
|
-
/**
|
|
1116
|
-
* The base `sizes` attribute passed through to the `<img>` element.
|
|
1117
|
-
* Providing sizes causes the image to create an automatic responsive srcset.
|
|
1118
|
-
*/
|
|
1119
|
-
sizes;
|
|
1120
|
-
/**
|
|
1121
|
-
* For responsive images: the intrinsic width of the image in pixels.
|
|
1122
|
-
* For fixed size images: the desired rendered width of the image in pixels.
|
|
1123
|
-
*/
|
|
1124
|
-
width;
|
|
1125
|
-
/**
|
|
1126
|
-
* For responsive images: the intrinsic height of the image in pixels.
|
|
1127
|
-
* For fixed size images: the desired rendered height of the image in pixels.
|
|
1128
|
-
*/
|
|
1129
|
-
height;
|
|
1130
|
-
/**
|
|
1131
|
-
* The desired decoding behavior for the image. Defaults to `auto`
|
|
1132
|
-
* if not explicitly set, matching native browser behavior.
|
|
1133
|
-
*
|
|
1134
|
-
* Use `async` to decode the image off the main thread (non-blocking),
|
|
1135
|
-
* `sync` for immediate decoding (blocking), or `auto` to let the
|
|
1136
|
-
* browser decide the optimal strategy.
|
|
1137
|
-
*
|
|
1138
|
-
* [Spec](https://html.spec.whatwg.org/multipage/images.html#image-decoding-hint)
|
|
1139
|
-
*/
|
|
1140
|
-
decoding;
|
|
1141
|
-
/**
|
|
1142
|
-
* The desired loading behavior (lazy, eager, or auto). Defaults to `lazy`,
|
|
1143
|
-
* which is recommended for most images.
|
|
1144
|
-
*
|
|
1145
|
-
* Warning: Setting images as loading="eager" or loading="auto" marks them
|
|
1146
|
-
* as non-priority images and can hurt loading performance. For images which
|
|
1147
|
-
* may be the LCP element, use the `priority` attribute instead of `loading`.
|
|
1148
|
-
*/
|
|
1149
|
-
loading;
|
|
1150
|
-
/**
|
|
1151
|
-
* Indicates whether this image should have a high priority.
|
|
1152
|
-
*/
|
|
1153
|
-
priority = false;
|
|
1154
|
-
/**
|
|
1155
|
-
* Data to pass through to custom loaders.
|
|
1156
|
-
*/
|
|
1157
|
-
loaderParams;
|
|
1158
|
-
/**
|
|
1159
|
-
* Disables automatic srcset generation for this image.
|
|
1160
|
-
*/
|
|
1161
|
-
disableOptimizedSrcset = false;
|
|
1162
|
-
/**
|
|
1163
|
-
* Sets the image to "fill mode", which eliminates the height/width requirement and adds
|
|
1164
|
-
* styles such that the image fills its containing element.
|
|
1165
|
-
*/
|
|
1166
|
-
fill = false;
|
|
1167
|
-
/**
|
|
1168
|
-
* A URL or data URL for an image to be used as a placeholder while this image loads.
|
|
1169
|
-
*/
|
|
1170
|
-
placeholder;
|
|
1171
|
-
/**
|
|
1172
|
-
* Configuration object for placeholder settings. Options:
|
|
1173
|
-
* * blur: Setting this to false disables the automatic CSS blur.
|
|
1174
|
-
*/
|
|
1175
|
-
placeholderConfig;
|
|
1176
|
-
/**
|
|
1177
|
-
* Value of the `src` attribute if set on the host `<img>` element.
|
|
1178
|
-
* This input is exclusively read to assert that `src` is not set in conflict
|
|
1179
|
-
* with `ngSrc` and that images don't start to load until a lazy loading strategy is set.
|
|
1180
|
-
* @internal
|
|
1181
|
-
*/
|
|
1182
|
-
src;
|
|
1183
|
-
/**
|
|
1184
|
-
* Value of the `srcset` attribute if set on the host `<img>` element.
|
|
1185
|
-
* This input is exclusively read to assert that `srcset` is not set in conflict
|
|
1186
|
-
* with `ngSrcset` and that images don't start to load until a lazy loading strategy is set.
|
|
1187
|
-
* @internal
|
|
1188
|
-
*/
|
|
1189
|
-
srcset;
|
|
1190
|
-
constructor() {
|
|
1191
|
-
if (ngDevMode) {
|
|
1192
|
-
this.lcpObserver = this.injector.get(LCPImageObserver);
|
|
1193
|
-
// Using `DestroyRef` to avoid having an empty `ngOnDestroy` method since this
|
|
1194
|
-
// is only run in development mode.
|
|
1195
|
-
const destroyRef = inject(DestroyRef);
|
|
1196
|
-
destroyRef.onDestroy(() => {
|
|
1197
|
-
if (!this.priority && this._renderedSrc !== null) {
|
|
1198
|
-
this.lcpObserver.unregisterImage(this._renderedSrc);
|
|
1199
|
-
}
|
|
1200
|
-
});
|
|
1201
|
-
}
|
|
1202
|
-
}
|
|
1203
|
-
/** @docs-private */
|
|
1204
|
-
ngOnInit() {
|
|
1205
|
-
_performanceMarkFeature('NgOptimizedImage');
|
|
1206
|
-
if (ngDevMode) {
|
|
1207
|
-
const ngZone = this.injector.get(NgZone);
|
|
1208
|
-
assertNonEmptyInput(this, 'ngSrc', this.ngSrc);
|
|
1209
|
-
assertValidNgSrcset(this, this.ngSrcset);
|
|
1210
|
-
assertNoConflictingSrc(this);
|
|
1211
|
-
if (this.ngSrcset) {
|
|
1212
|
-
assertNoConflictingSrcset(this);
|
|
1213
|
-
}
|
|
1214
|
-
assertNotBase64Image(this);
|
|
1215
|
-
assertNotBlobUrl(this);
|
|
1216
|
-
if (this.fill) {
|
|
1217
|
-
assertEmptyWidthAndHeight(this);
|
|
1218
|
-
// This leaves the Angular zone to avoid triggering unnecessary change detection cycles when
|
|
1219
|
-
// `load` tasks are invoked on images.
|
|
1220
|
-
ngZone.runOutsideAngular(() => assertNonZeroRenderedHeight(this, this.imgElement, this.renderer));
|
|
1221
|
-
}
|
|
1222
|
-
else {
|
|
1223
|
-
assertNonEmptyWidthAndHeight(this);
|
|
1224
|
-
if (this.height !== undefined) {
|
|
1225
|
-
assertGreaterThanZero(this, this.height, 'height');
|
|
1226
|
-
}
|
|
1227
|
-
if (this.width !== undefined) {
|
|
1228
|
-
assertGreaterThanZero(this, this.width, 'width');
|
|
1229
|
-
}
|
|
1230
|
-
// Only check for distorted images when not in fill mode, where
|
|
1231
|
-
// images may be intentionally stretched, cropped or letterboxed.
|
|
1232
|
-
ngZone.runOutsideAngular(() => assertNoImageDistortion(this, this.imgElement, this.renderer));
|
|
1233
|
-
}
|
|
1234
|
-
assertValidLoadingInput(this);
|
|
1235
|
-
assertValidDecodingInput(this);
|
|
1236
|
-
if (!this.ngSrcset) {
|
|
1237
|
-
assertNoComplexSizes(this);
|
|
1238
|
-
}
|
|
1239
|
-
assertValidPlaceholder(this, this.imageLoader);
|
|
1240
|
-
assertNotMissingBuiltInLoader(this.ngSrc, this.imageLoader);
|
|
1241
|
-
assertNoNgSrcsetWithoutLoader(this, this.imageLoader);
|
|
1242
|
-
assertNoLoaderParamsWithoutLoader(this, this.imageLoader);
|
|
1243
|
-
ngZone.runOutsideAngular(() => {
|
|
1244
|
-
this.lcpObserver.registerImage(this.getRewrittenSrc(), this.ngSrc, this.priority);
|
|
1245
|
-
});
|
|
1246
|
-
if (this.priority) {
|
|
1247
|
-
const checker = this.injector.get(PreconnectLinkChecker);
|
|
1248
|
-
checker.assertPreconnect(this.getRewrittenSrc(), this.ngSrc);
|
|
1249
|
-
if (typeof ngServerMode !== 'undefined' && !ngServerMode) {
|
|
1250
|
-
const applicationRef = this.injector.get(ApplicationRef);
|
|
1251
|
-
assetPriorityCountBelowThreshold(applicationRef);
|
|
1252
|
-
}
|
|
1253
|
-
}
|
|
666
|
+
imageLoader = inject(IMAGE_LOADER);
|
|
667
|
+
config = processConfig(inject(_IMAGE_CONFIG));
|
|
668
|
+
renderer = inject(Renderer2);
|
|
669
|
+
imgElement = inject(ElementRef).nativeElement;
|
|
670
|
+
injector = inject(Injector);
|
|
671
|
+
lcpObserver;
|
|
672
|
+
_renderedSrc = null;
|
|
673
|
+
ngSrc;
|
|
674
|
+
ngSrcset;
|
|
675
|
+
sizes;
|
|
676
|
+
width;
|
|
677
|
+
height;
|
|
678
|
+
decoding;
|
|
679
|
+
loading;
|
|
680
|
+
priority = false;
|
|
681
|
+
loaderParams;
|
|
682
|
+
disableOptimizedSrcset = false;
|
|
683
|
+
fill = false;
|
|
684
|
+
placeholder;
|
|
685
|
+
placeholderConfig;
|
|
686
|
+
src;
|
|
687
|
+
srcset;
|
|
688
|
+
constructor() {
|
|
689
|
+
if (ngDevMode) {
|
|
690
|
+
this.lcpObserver = this.injector.get(LCPImageObserver);
|
|
691
|
+
const destroyRef = inject(DestroyRef);
|
|
692
|
+
destroyRef.onDestroy(() => {
|
|
693
|
+
if (!this.priority && this._renderedSrc !== null) {
|
|
694
|
+
this.lcpObserver.unregisterImage(this._renderedSrc);
|
|
1254
695
|
}
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
696
|
+
});
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
ngOnInit() {
|
|
700
|
+
_performanceMarkFeature('NgOptimizedImage');
|
|
701
|
+
if (ngDevMode) {
|
|
702
|
+
const ngZone = this.injector.get(NgZone);
|
|
703
|
+
assertNonEmptyInput(this, 'ngSrc', this.ngSrc);
|
|
704
|
+
assertValidNgSrcset(this, this.ngSrcset);
|
|
705
|
+
assertNoConflictingSrc(this);
|
|
706
|
+
if (this.ngSrcset) {
|
|
707
|
+
assertNoConflictingSrcset(this);
|
|
708
|
+
}
|
|
709
|
+
assertNotBase64Image(this);
|
|
710
|
+
assertNotBlobUrl(this);
|
|
711
|
+
if (this.fill) {
|
|
712
|
+
assertEmptyWidthAndHeight(this);
|
|
713
|
+
ngZone.runOutsideAngular(() => assertNonZeroRenderedHeight(this, this.imgElement, this.renderer));
|
|
714
|
+
} else {
|
|
715
|
+
assertNonEmptyWidthAndHeight(this);
|
|
716
|
+
if (this.height !== undefined) {
|
|
717
|
+
assertGreaterThanZero(this, this.height, 'height');
|
|
1265
718
|
}
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
this.setHostAttribute('height', this.height.toString());
|
|
719
|
+
if (this.width !== undefined) {
|
|
720
|
+
assertGreaterThanZero(this, this.width, 'width');
|
|
1269
721
|
}
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
this
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
722
|
+
ngZone.runOutsideAngular(() => assertNoImageDistortion(this, this.imgElement, this.renderer));
|
|
723
|
+
}
|
|
724
|
+
assertValidLoadingInput(this);
|
|
725
|
+
assertValidDecodingInput(this);
|
|
726
|
+
if (!this.ngSrcset) {
|
|
727
|
+
assertNoComplexSizes(this);
|
|
728
|
+
}
|
|
729
|
+
assertValidPlaceholder(this, this.imageLoader);
|
|
730
|
+
assertNotMissingBuiltInLoader(this.ngSrc, this.imageLoader);
|
|
731
|
+
assertNoNgSrcsetWithoutLoader(this, this.imageLoader);
|
|
732
|
+
assertNoLoaderParamsWithoutLoader(this, this.imageLoader);
|
|
733
|
+
ngZone.runOutsideAngular(() => {
|
|
734
|
+
this.lcpObserver.registerImage(this.getRewrittenSrc(), this.ngSrc, this.priority);
|
|
735
|
+
});
|
|
736
|
+
if (this.priority) {
|
|
737
|
+
const checker = this.injector.get(PreconnectLinkChecker);
|
|
738
|
+
checker.assertPreconnect(this.getRewrittenSrc(), this.ngSrc);
|
|
739
|
+
if (typeof ngServerMode !== 'undefined' && !ngServerMode) {
|
|
740
|
+
const applicationRef = this.injector.get(ApplicationRef);
|
|
741
|
+
assetPriorityCountBelowThreshold(applicationRef);
|
|
1286
742
|
}
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
if (this.placeholder) {
|
|
746
|
+
this.removePlaceholderOnLoad(this.imgElement);
|
|
747
|
+
}
|
|
748
|
+
this.setHostAttributes();
|
|
749
|
+
}
|
|
750
|
+
setHostAttributes() {
|
|
751
|
+
if (this.fill) {
|
|
752
|
+
this.sizes ||= '100vw';
|
|
753
|
+
} else {
|
|
754
|
+
this.setHostAttribute('width', this.width.toString());
|
|
755
|
+
this.setHostAttribute('height', this.height.toString());
|
|
756
|
+
}
|
|
757
|
+
this.setHostAttribute('loading', this.getLoadingBehavior());
|
|
758
|
+
this.setHostAttribute('fetchpriority', this.getFetchPriority());
|
|
759
|
+
this.setHostAttribute('decoding', this.getDecoding());
|
|
760
|
+
this.setHostAttribute('ng-img', 'true');
|
|
761
|
+
const rewrittenSrcset = this.updateSrcAndSrcset();
|
|
762
|
+
if (this.sizes) {
|
|
763
|
+
if (this.getLoadingBehavior() === 'lazy') {
|
|
764
|
+
this.setHostAttribute('sizes', 'auto, ' + this.sizes);
|
|
765
|
+
} else {
|
|
766
|
+
this.setHostAttribute('sizes', this.sizes);
|
|
767
|
+
}
|
|
768
|
+
} else {
|
|
769
|
+
if (this.ngSrcset && VALID_WIDTH_DESCRIPTOR_SRCSET.test(this.ngSrcset) && this.getLoadingBehavior() === 'lazy') {
|
|
770
|
+
this.setHostAttribute('sizes', 'auto, 100vw');
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
if (typeof ngServerMode !== 'undefined' && ngServerMode && this.priority) {
|
|
774
|
+
const preloadLinkCreator = this.injector.get(PreloadLinkCreator);
|
|
775
|
+
preloadLinkCreator.createPreloadLinkTag(this.renderer, this.getRewrittenSrc(), rewrittenSrcset, this.sizes);
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
ngOnChanges(changes) {
|
|
779
|
+
if (ngDevMode) {
|
|
780
|
+
assertNoPostInitInputChange(this, changes, ['ngSrcset', 'width', 'height', 'priority', 'fill', 'loading', 'sizes', 'loaderParams', 'disableOptimizedSrcset']);
|
|
781
|
+
}
|
|
782
|
+
if (changes['ngSrc'] && !changes['ngSrc'].isFirstChange()) {
|
|
783
|
+
const oldSrc = this._renderedSrc;
|
|
784
|
+
this.updateSrcAndSrcset(true);
|
|
785
|
+
if (ngDevMode) {
|
|
786
|
+
const newSrc = this._renderedSrc;
|
|
787
|
+
if (oldSrc && newSrc && oldSrc !== newSrc) {
|
|
788
|
+
const ngZone = this.injector.get(NgZone);
|
|
789
|
+
ngZone.runOutsideAngular(() => {
|
|
790
|
+
this.lcpObserver.updateImage(oldSrc, newSrc);
|
|
791
|
+
});
|
|
1293
792
|
}
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
}
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
}
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
}
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
}
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
}
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
// Reset cached value, so that the followup `getRewrittenSrc()` call
|
|
1408
|
-
// will recalculate it and update the cache.
|
|
1409
|
-
this._renderedSrc = null;
|
|
1410
|
-
}
|
|
1411
|
-
const rewrittenSrc = this.getRewrittenSrc();
|
|
1412
|
-
this.setHostAttribute('src', rewrittenSrc);
|
|
1413
|
-
let rewrittenSrcset = undefined;
|
|
1414
|
-
if (this.ngSrcset) {
|
|
1415
|
-
rewrittenSrcset = this.getRewrittenSrcset();
|
|
1416
|
-
}
|
|
1417
|
-
else if (this.shouldGenerateAutomaticSrcset()) {
|
|
1418
|
-
rewrittenSrcset = this.getAutomaticSrcset();
|
|
1419
|
-
}
|
|
1420
|
-
if (rewrittenSrcset) {
|
|
1421
|
-
this.setHostAttribute('srcset', rewrittenSrcset);
|
|
1422
|
-
}
|
|
1423
|
-
return rewrittenSrcset;
|
|
1424
|
-
}
|
|
1425
|
-
getFixedSrcset() {
|
|
1426
|
-
const finalSrcs = DENSITY_SRCSET_MULTIPLIERS.map((multiplier) => `${this.callImageLoader({
|
|
1427
|
-
src: this.ngSrc,
|
|
1428
|
-
width: this.width * multiplier,
|
|
1429
|
-
})} ${multiplier}x`);
|
|
1430
|
-
return finalSrcs.join(', ');
|
|
1431
|
-
}
|
|
1432
|
-
shouldGenerateAutomaticSrcset() {
|
|
1433
|
-
let oversizedImage = false;
|
|
1434
|
-
if (!this.sizes) {
|
|
1435
|
-
oversizedImage =
|
|
1436
|
-
this.width > FIXED_SRCSET_WIDTH_LIMIT || this.height > FIXED_SRCSET_HEIGHT_LIMIT;
|
|
1437
|
-
}
|
|
1438
|
-
return (!this.disableOptimizedSrcset &&
|
|
1439
|
-
!this.srcset &&
|
|
1440
|
-
this.imageLoader !== noopImageLoader &&
|
|
1441
|
-
!oversizedImage);
|
|
1442
|
-
}
|
|
1443
|
-
/**
|
|
1444
|
-
* Returns an image url formatted for use with the CSS background-image property. Expects one of:
|
|
1445
|
-
* * A base64 encoded image, which is wrapped and passed through.
|
|
1446
|
-
* * A boolean. If true, calls the image loader to generate a small placeholder url.
|
|
1447
|
-
*/
|
|
1448
|
-
generatePlaceholder(placeholderInput) {
|
|
1449
|
-
const { placeholderResolution } = this.config;
|
|
1450
|
-
if (placeholderInput === true) {
|
|
1451
|
-
return `url(${this.callImageLoader({
|
|
1452
|
-
src: this.ngSrc,
|
|
1453
|
-
width: placeholderResolution,
|
|
1454
|
-
isPlaceholder: true,
|
|
1455
|
-
})})`;
|
|
1456
|
-
}
|
|
1457
|
-
else if (typeof placeholderInput === 'string') {
|
|
1458
|
-
return `url(${placeholderInput})`;
|
|
1459
|
-
}
|
|
1460
|
-
return null;
|
|
1461
|
-
}
|
|
1462
|
-
/**
|
|
1463
|
-
* Determines if blur should be applied, based on an optional boolean
|
|
1464
|
-
* property `blur` within the optional configuration object `placeholderConfig`.
|
|
1465
|
-
*/
|
|
1466
|
-
shouldBlurPlaceholder(placeholderConfig) {
|
|
1467
|
-
if (!placeholderConfig || !placeholderConfig.hasOwnProperty('blur')) {
|
|
1468
|
-
return true;
|
|
1469
|
-
}
|
|
1470
|
-
return Boolean(placeholderConfig.blur);
|
|
1471
|
-
}
|
|
1472
|
-
removePlaceholderOnLoad(img) {
|
|
1473
|
-
const callback = () => {
|
|
1474
|
-
const changeDetectorRef = this.injector.get(ChangeDetectorRef);
|
|
1475
|
-
removeLoadListenerFn();
|
|
1476
|
-
removeErrorListenerFn();
|
|
1477
|
-
this.placeholder = false;
|
|
1478
|
-
changeDetectorRef.markForCheck();
|
|
1479
|
-
};
|
|
1480
|
-
const removeLoadListenerFn = this.renderer.listen(img, 'load', callback);
|
|
1481
|
-
const removeErrorListenerFn = this.renderer.listen(img, 'error', callback);
|
|
1482
|
-
callOnLoadIfImageIsLoaded(img, callback);
|
|
1483
|
-
}
|
|
1484
|
-
setHostAttribute(name, value) {
|
|
1485
|
-
this.renderer.setAttribute(this.imgElement, name, value);
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
if (ngDevMode && changes['placeholder']?.currentValue && typeof ngServerMode !== 'undefined' && !ngServerMode) {
|
|
796
|
+
assertPlaceholderDimensions(this, this.imgElement);
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
callImageLoader(configWithoutCustomParams) {
|
|
800
|
+
let augmentedConfig = configWithoutCustomParams;
|
|
801
|
+
if (this.loaderParams) {
|
|
802
|
+
augmentedConfig.loaderParams = this.loaderParams;
|
|
803
|
+
}
|
|
804
|
+
return this.imageLoader(augmentedConfig);
|
|
805
|
+
}
|
|
806
|
+
getLoadingBehavior() {
|
|
807
|
+
if (!this.priority && this.loading !== undefined) {
|
|
808
|
+
return this.loading;
|
|
809
|
+
}
|
|
810
|
+
return this.priority ? 'eager' : 'lazy';
|
|
811
|
+
}
|
|
812
|
+
getFetchPriority() {
|
|
813
|
+
return this.priority ? 'high' : 'auto';
|
|
814
|
+
}
|
|
815
|
+
getDecoding() {
|
|
816
|
+
if (this.priority) {
|
|
817
|
+
return 'sync';
|
|
818
|
+
}
|
|
819
|
+
return this.decoding ?? 'auto';
|
|
820
|
+
}
|
|
821
|
+
getRewrittenSrc() {
|
|
822
|
+
if (!this._renderedSrc) {
|
|
823
|
+
const imgConfig = {
|
|
824
|
+
src: this.ngSrc
|
|
825
|
+
};
|
|
826
|
+
this._renderedSrc = this.callImageLoader(imgConfig);
|
|
827
|
+
}
|
|
828
|
+
return this._renderedSrc;
|
|
829
|
+
}
|
|
830
|
+
getRewrittenSrcset() {
|
|
831
|
+
const widthSrcSet = VALID_WIDTH_DESCRIPTOR_SRCSET.test(this.ngSrcset);
|
|
832
|
+
const finalSrcs = this.ngSrcset.split(',').filter(src => src !== '').map(srcStr => {
|
|
833
|
+
srcStr = srcStr.trim();
|
|
834
|
+
const width = widthSrcSet ? parseFloat(srcStr) : parseFloat(srcStr) * this.width;
|
|
835
|
+
return `${this.callImageLoader({
|
|
836
|
+
src: this.ngSrc,
|
|
837
|
+
width
|
|
838
|
+
})} ${srcStr}`;
|
|
839
|
+
});
|
|
840
|
+
return finalSrcs.join(', ');
|
|
841
|
+
}
|
|
842
|
+
getAutomaticSrcset() {
|
|
843
|
+
if (this.sizes) {
|
|
844
|
+
return this.getResponsiveSrcset();
|
|
845
|
+
} else {
|
|
846
|
+
return this.getFixedSrcset();
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
getResponsiveSrcset() {
|
|
850
|
+
const {
|
|
851
|
+
breakpoints
|
|
852
|
+
} = this.config;
|
|
853
|
+
let filteredBreakpoints = breakpoints;
|
|
854
|
+
if (this.sizes?.trim() === '100vw') {
|
|
855
|
+
filteredBreakpoints = breakpoints.filter(bp => bp >= VIEWPORT_BREAKPOINT_CUTOFF);
|
|
856
|
+
}
|
|
857
|
+
const finalSrcs = filteredBreakpoints.map(bp => `${this.callImageLoader({
|
|
858
|
+
src: this.ngSrc,
|
|
859
|
+
width: bp
|
|
860
|
+
})} ${bp}w`);
|
|
861
|
+
return finalSrcs.join(', ');
|
|
862
|
+
}
|
|
863
|
+
updateSrcAndSrcset(forceSrcRecalc = false) {
|
|
864
|
+
if (forceSrcRecalc) {
|
|
865
|
+
this._renderedSrc = null;
|
|
866
|
+
}
|
|
867
|
+
const rewrittenSrc = this.getRewrittenSrc();
|
|
868
|
+
this.setHostAttribute('src', rewrittenSrc);
|
|
869
|
+
let rewrittenSrcset = undefined;
|
|
870
|
+
if (this.ngSrcset) {
|
|
871
|
+
rewrittenSrcset = this.getRewrittenSrcset();
|
|
872
|
+
} else if (this.shouldGenerateAutomaticSrcset()) {
|
|
873
|
+
rewrittenSrcset = this.getAutomaticSrcset();
|
|
874
|
+
}
|
|
875
|
+
if (rewrittenSrcset) {
|
|
876
|
+
this.setHostAttribute('srcset', rewrittenSrcset);
|
|
877
|
+
}
|
|
878
|
+
return rewrittenSrcset;
|
|
879
|
+
}
|
|
880
|
+
getFixedSrcset() {
|
|
881
|
+
const finalSrcs = DENSITY_SRCSET_MULTIPLIERS.map(multiplier => `${this.callImageLoader({
|
|
882
|
+
src: this.ngSrc,
|
|
883
|
+
width: this.width * multiplier
|
|
884
|
+
})} ${multiplier}x`);
|
|
885
|
+
return finalSrcs.join(', ');
|
|
886
|
+
}
|
|
887
|
+
shouldGenerateAutomaticSrcset() {
|
|
888
|
+
let oversizedImage = false;
|
|
889
|
+
if (!this.sizes) {
|
|
890
|
+
oversizedImage = this.width > FIXED_SRCSET_WIDTH_LIMIT || this.height > FIXED_SRCSET_HEIGHT_LIMIT;
|
|
891
|
+
}
|
|
892
|
+
return !this.disableOptimizedSrcset && !this.srcset && this.imageLoader !== noopImageLoader && !oversizedImage;
|
|
893
|
+
}
|
|
894
|
+
generatePlaceholder(placeholderInput) {
|
|
895
|
+
const {
|
|
896
|
+
placeholderResolution
|
|
897
|
+
} = this.config;
|
|
898
|
+
if (placeholderInput === true) {
|
|
899
|
+
return `url(${this.callImageLoader({
|
|
900
|
+
src: this.ngSrc,
|
|
901
|
+
width: placeholderResolution,
|
|
902
|
+
isPlaceholder: true
|
|
903
|
+
})})`;
|
|
904
|
+
} else if (typeof placeholderInput === 'string') {
|
|
905
|
+
return `url(${placeholderInput})`;
|
|
1486
906
|
}
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
907
|
+
return null;
|
|
908
|
+
}
|
|
909
|
+
shouldBlurPlaceholder(placeholderConfig) {
|
|
910
|
+
if (!placeholderConfig || !placeholderConfig.hasOwnProperty('blur')) {
|
|
911
|
+
return true;
|
|
912
|
+
}
|
|
913
|
+
return Boolean(placeholderConfig.blur);
|
|
914
|
+
}
|
|
915
|
+
removePlaceholderOnLoad(img) {
|
|
916
|
+
const callback = () => {
|
|
917
|
+
const changeDetectorRef = this.injector.get(ChangeDetectorRef);
|
|
918
|
+
removeLoadListenerFn();
|
|
919
|
+
removeErrorListenerFn();
|
|
920
|
+
this.placeholder = false;
|
|
921
|
+
changeDetectorRef.markForCheck();
|
|
922
|
+
};
|
|
923
|
+
const removeLoadListenerFn = this.renderer.listen(img, 'load', callback);
|
|
924
|
+
const removeErrorListenerFn = this.renderer.listen(img, 'error', callback);
|
|
925
|
+
callOnLoadIfImageIsLoaded(img, callback);
|
|
926
|
+
}
|
|
927
|
+
setHostAttribute(name, value) {
|
|
928
|
+
this.renderer.setAttribute(this.imgElement, name, value);
|
|
929
|
+
}
|
|
930
|
+
static ɵfac = i0.ɵɵngDeclareFactory({
|
|
931
|
+
minVersion: "12.0.0",
|
|
932
|
+
version: "21.0.0-rc.0",
|
|
933
|
+
ngImport: i0,
|
|
934
|
+
type: NgOptimizedImage,
|
|
935
|
+
deps: [],
|
|
936
|
+
target: i0.ɵɵFactoryTarget.Directive
|
|
937
|
+
});
|
|
938
|
+
static ɵdir = i0.ɵɵngDeclareDirective({
|
|
939
|
+
minVersion: "16.1.0",
|
|
940
|
+
version: "21.0.0-rc.0",
|
|
941
|
+
type: NgOptimizedImage,
|
|
942
|
+
isStandalone: true,
|
|
943
|
+
selector: "img[ngSrc]",
|
|
944
|
+
inputs: {
|
|
945
|
+
ngSrc: ["ngSrc", "ngSrc", unwrapSafeUrl],
|
|
946
|
+
ngSrcset: "ngSrcset",
|
|
947
|
+
sizes: "sizes",
|
|
948
|
+
width: ["width", "width", numberAttribute],
|
|
949
|
+
height: ["height", "height", numberAttribute],
|
|
950
|
+
decoding: "decoding",
|
|
951
|
+
loading: "loading",
|
|
952
|
+
priority: ["priority", "priority", booleanAttribute],
|
|
953
|
+
loaderParams: "loaderParams",
|
|
954
|
+
disableOptimizedSrcset: ["disableOptimizedSrcset", "disableOptimizedSrcset", booleanAttribute],
|
|
955
|
+
fill: ["fill", "fill", booleanAttribute],
|
|
956
|
+
placeholder: ["placeholder", "placeholder", booleanOrUrlAttribute],
|
|
957
|
+
placeholderConfig: "placeholderConfig",
|
|
958
|
+
src: "src",
|
|
959
|
+
srcset: "srcset"
|
|
960
|
+
},
|
|
961
|
+
host: {
|
|
962
|
+
properties: {
|
|
963
|
+
"style.position": "fill ? \"absolute\" : null",
|
|
964
|
+
"style.width": "fill ? \"100%\" : null",
|
|
965
|
+
"style.height": "fill ? \"100%\" : null",
|
|
966
|
+
"style.inset": "fill ? \"0\" : null",
|
|
967
|
+
"style.background-size": "placeholder ? \"cover\" : null",
|
|
968
|
+
"style.background-position": "placeholder ? \"50% 50%\" : null",
|
|
969
|
+
"style.background-repeat": "placeholder ? \"no-repeat\" : null",
|
|
970
|
+
"style.background-image": "placeholder ? generatePlaceholder(placeholder) : null",
|
|
971
|
+
"style.filter": "placeholder && shouldBlurPlaceholder(placeholderConfig) ? \"blur(15px)\" : null"
|
|
972
|
+
}
|
|
973
|
+
},
|
|
974
|
+
usesOnChanges: true,
|
|
975
|
+
ngImport: i0
|
|
976
|
+
});
|
|
977
|
+
}
|
|
978
|
+
i0.ɵɵngDeclareClassMetadata({
|
|
979
|
+
minVersion: "12.0.0",
|
|
980
|
+
version: "21.0.0-rc.0",
|
|
981
|
+
ngImport: i0,
|
|
982
|
+
type: NgOptimizedImage,
|
|
983
|
+
decorators: [{
|
|
984
|
+
type: Directive,
|
|
985
|
+
args: [{
|
|
986
|
+
selector: 'img[ngSrc]',
|
|
987
|
+
host: {
|
|
988
|
+
'[style.position]': 'fill ? "absolute" : null',
|
|
989
|
+
'[style.width]': 'fill ? "100%" : null',
|
|
990
|
+
'[style.height]': 'fill ? "100%" : null',
|
|
991
|
+
'[style.inset]': 'fill ? "0" : null',
|
|
992
|
+
'[style.background-size]': 'placeholder ? "cover" : null',
|
|
993
|
+
'[style.background-position]': 'placeholder ? "50% 50%" : null',
|
|
994
|
+
'[style.background-repeat]': 'placeholder ? "no-repeat" : null',
|
|
995
|
+
'[style.background-image]': 'placeholder ? generatePlaceholder(placeholder) : null',
|
|
996
|
+
'[style.filter]': 'placeholder && shouldBlurPlaceholder(placeholderConfig) ? "blur(15px)" : null'
|
|
997
|
+
}
|
|
998
|
+
}]
|
|
999
|
+
}],
|
|
1000
|
+
ctorParameters: () => [],
|
|
1001
|
+
propDecorators: {
|
|
1002
|
+
ngSrc: [{
|
|
1003
|
+
type: Input,
|
|
1004
|
+
args: [{
|
|
1005
|
+
required: true,
|
|
1006
|
+
transform: unwrapSafeUrl
|
|
1007
|
+
}]
|
|
1008
|
+
}],
|
|
1009
|
+
ngSrcset: [{
|
|
1010
|
+
type: Input
|
|
1011
|
+
}],
|
|
1012
|
+
sizes: [{
|
|
1013
|
+
type: Input
|
|
1014
|
+
}],
|
|
1015
|
+
width: [{
|
|
1016
|
+
type: Input,
|
|
1017
|
+
args: [{
|
|
1018
|
+
transform: numberAttribute
|
|
1019
|
+
}]
|
|
1020
|
+
}],
|
|
1021
|
+
height: [{
|
|
1022
|
+
type: Input,
|
|
1023
|
+
args: [{
|
|
1024
|
+
transform: numberAttribute
|
|
1025
|
+
}]
|
|
1026
|
+
}],
|
|
1027
|
+
decoding: [{
|
|
1028
|
+
type: Input
|
|
1029
|
+
}],
|
|
1030
|
+
loading: [{
|
|
1031
|
+
type: Input
|
|
1032
|
+
}],
|
|
1033
|
+
priority: [{
|
|
1034
|
+
type: Input,
|
|
1035
|
+
args: [{
|
|
1036
|
+
transform: booleanAttribute
|
|
1037
|
+
}]
|
|
1038
|
+
}],
|
|
1039
|
+
loaderParams: [{
|
|
1040
|
+
type: Input
|
|
1041
|
+
}],
|
|
1042
|
+
disableOptimizedSrcset: [{
|
|
1043
|
+
type: Input,
|
|
1044
|
+
args: [{
|
|
1045
|
+
transform: booleanAttribute
|
|
1046
|
+
}]
|
|
1047
|
+
}],
|
|
1048
|
+
fill: [{
|
|
1049
|
+
type: Input,
|
|
1050
|
+
args: [{
|
|
1051
|
+
transform: booleanAttribute
|
|
1052
|
+
}]
|
|
1053
|
+
}],
|
|
1054
|
+
placeholder: [{
|
|
1055
|
+
type: Input,
|
|
1056
|
+
args: [{
|
|
1057
|
+
transform: booleanOrUrlAttribute
|
|
1058
|
+
}]
|
|
1059
|
+
}],
|
|
1060
|
+
placeholderConfig: [{
|
|
1061
|
+
type: Input
|
|
1062
|
+
}],
|
|
1063
|
+
src: [{
|
|
1064
|
+
type: Input
|
|
1065
|
+
}],
|
|
1066
|
+
srcset: [{
|
|
1067
|
+
type: Input
|
|
1068
|
+
}]
|
|
1069
|
+
}
|
|
1070
|
+
});
|
|
1548
1071
|
function processConfig(config) {
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1072
|
+
let sortedBreakpoints = {};
|
|
1073
|
+
if (config.breakpoints) {
|
|
1074
|
+
sortedBreakpoints.breakpoints = config.breakpoints.sort((a, b) => a - b);
|
|
1075
|
+
}
|
|
1076
|
+
return Object.assign({}, _IMAGE_CONFIG_DEFAULTS, config, sortedBreakpoints);
|
|
1554
1077
|
}
|
|
1555
|
-
/***** Assert functions *****/
|
|
1556
|
-
/**
|
|
1557
|
-
* Verifies that there is no `src` set on a host element.
|
|
1558
|
-
*/
|
|
1559
1078
|
function assertNoConflictingSrc(dir) {
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
`The NgOptimizedImage directive sets \`src\` itself based on the value of \`ngSrc\`. ` +
|
|
1564
|
-
`To fix this, please remove the \`src\` attribute.`);
|
|
1565
|
-
}
|
|
1079
|
+
if (dir.src) {
|
|
1080
|
+
throw new _RuntimeError(2950, `${imgDirectiveDetails(dir.ngSrc)} both \`src\` and \`ngSrc\` have been set. ` + `Supplying both of these attributes breaks lazy loading. ` + `The NgOptimizedImage directive sets \`src\` itself based on the value of \`ngSrc\`. ` + `To fix this, please remove the \`src\` attribute.`);
|
|
1081
|
+
}
|
|
1566
1082
|
}
|
|
1567
|
-
/**
|
|
1568
|
-
* Verifies that there is no `srcset` set on a host element.
|
|
1569
|
-
*/
|
|
1570
1083
|
function assertNoConflictingSrcset(dir) {
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
`The NgOptimizedImage directive sets \`srcset\` itself based on the value of ` +
|
|
1575
|
-
`\`ngSrcset\`. To fix this, please remove the \`srcset\` attribute.`);
|
|
1576
|
-
}
|
|
1084
|
+
if (dir.srcset) {
|
|
1085
|
+
throw new _RuntimeError(2951, `${imgDirectiveDetails(dir.ngSrc)} both \`srcset\` and \`ngSrcset\` have been set. ` + `Supplying both of these attributes breaks lazy loading. ` + `The NgOptimizedImage directive sets \`srcset\` itself based on the value of ` + `\`ngSrcset\`. To fix this, please remove the \`srcset\` attribute.`);
|
|
1086
|
+
}
|
|
1577
1087
|
}
|
|
1578
|
-
/**
|
|
1579
|
-
* Verifies that the `ngSrc` is not a Base64-encoded image.
|
|
1580
|
-
*/
|
|
1581
1088
|
function assertNotBase64Image(dir) {
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
}
|
|
1587
|
-
throw new _RuntimeError(2952 /* RuntimeErrorCode.INVALID_INPUT */, `${imgDirectiveDetails(dir.ngSrc, false)} \`ngSrc\` is a Base64-encoded string ` +
|
|
1588
|
-
`(${ngSrc}). NgOptimizedImage does not support Base64-encoded strings. ` +
|
|
1589
|
-
`To fix this, disable the NgOptimizedImage directive for this element ` +
|
|
1590
|
-
`by removing \`ngSrc\` and using a standard \`src\` attribute instead.`);
|
|
1089
|
+
let ngSrc = dir.ngSrc.trim();
|
|
1090
|
+
if (ngSrc.startsWith('data:')) {
|
|
1091
|
+
if (ngSrc.length > BASE64_IMG_MAX_LENGTH_IN_ERROR) {
|
|
1092
|
+
ngSrc = ngSrc.substring(0, BASE64_IMG_MAX_LENGTH_IN_ERROR) + '...';
|
|
1591
1093
|
}
|
|
1094
|
+
throw new _RuntimeError(2952, `${imgDirectiveDetails(dir.ngSrc, false)} \`ngSrc\` is a Base64-encoded string ` + `(${ngSrc}). NgOptimizedImage does not support Base64-encoded strings. ` + `To fix this, disable the NgOptimizedImage directive for this element ` + `by removing \`ngSrc\` and using a standard \`src\` attribute instead.`);
|
|
1095
|
+
}
|
|
1592
1096
|
}
|
|
1593
|
-
/**
|
|
1594
|
-
* Verifies that the 'sizes' only includes responsive values.
|
|
1595
|
-
*/
|
|
1596
1097
|
function assertNoComplexSizes(dir) {
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
`values, such as \`sizes="50vw"\` or \`sizes="(min-width: 768px) 50vw, 100vw"\`. ` +
|
|
1602
|
-
`To fix this, modify the \`sizes\` attribute, or provide your own \`ngSrcset\` value directly.`);
|
|
1603
|
-
}
|
|
1098
|
+
let sizes = dir.sizes;
|
|
1099
|
+
if (sizes?.match(/((\)|,)\s|^)\d+px/)) {
|
|
1100
|
+
throw new _RuntimeError(2952, `${imgDirectiveDetails(dir.ngSrc, false)} \`sizes\` was set to a string including ` + `pixel values. For automatic \`srcset\` generation, \`sizes\` must only include responsive ` + `values, such as \`sizes="50vw"\` or \`sizes="(min-width: 768px) 50vw, 100vw"\`. ` + `To fix this, modify the \`sizes\` attribute, or provide your own \`ngSrcset\` value directly.`);
|
|
1101
|
+
}
|
|
1604
1102
|
}
|
|
1605
1103
|
function assertValidPlaceholder(dir, imageLoader) {
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1104
|
+
assertNoPlaceholderConfigWithoutPlaceholder(dir);
|
|
1105
|
+
assertNoRelativePlaceholderWithoutLoader(dir, imageLoader);
|
|
1106
|
+
assertNoOversizedDataUrl(dir);
|
|
1609
1107
|
}
|
|
1610
|
-
/**
|
|
1611
|
-
* Verifies that placeholderConfig isn't being used without placeholder
|
|
1612
|
-
*/
|
|
1613
1108
|
function assertNoPlaceholderConfigWithoutPlaceholder(dir) {
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
}
|
|
1109
|
+
if (dir.placeholderConfig && !dir.placeholder) {
|
|
1110
|
+
throw new _RuntimeError(2952, `${imgDirectiveDetails(dir.ngSrc, false)} \`placeholderConfig\` options were provided for an ` + `image that does not use the \`placeholder\` attribute, and will have no effect.`);
|
|
1111
|
+
}
|
|
1618
1112
|
}
|
|
1619
|
-
/**
|
|
1620
|
-
* Warns if a relative URL placeholder is specified, but no loader is present to provide the small
|
|
1621
|
-
* image.
|
|
1622
|
-
*/
|
|
1623
1113
|
function assertNoRelativePlaceholderWithoutLoader(dir, imageLoader) {
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
`which would result in the same image being used for the primary image and its placeholder. ` +
|
|
1628
|
-
`To fix this, provide a loader or remove the \`placeholder\` attribute from the image.`);
|
|
1629
|
-
}
|
|
1114
|
+
if (dir.placeholder === true && imageLoader === noopImageLoader) {
|
|
1115
|
+
throw new _RuntimeError(2963, `${imgDirectiveDetails(dir.ngSrc)} the \`placeholder\` attribute is set to true but ` + `no image loader is configured (i.e. the default one is being used), ` + `which would result in the same image being used for the primary image and its placeholder. ` + `To fix this, provide a loader or remove the \`placeholder\` attribute from the image.`);
|
|
1116
|
+
}
|
|
1630
1117
|
}
|
|
1631
|
-
/**
|
|
1632
|
-
* Warns or throws an error if an oversized dataURL placeholder is provided.
|
|
1633
|
-
*/
|
|
1634
1118
|
function assertNoOversizedDataUrl(dir) {
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
`directly increase the bundle size of Angular and hurt page load performance. To fix this, generate ` +
|
|
1642
|
-
`a smaller data URL placeholder.`);
|
|
1643
|
-
}
|
|
1644
|
-
if (dir.placeholder.length > DATA_URL_WARN_LIMIT) {
|
|
1645
|
-
console.warn(_formatRuntimeError(2965 /* RuntimeErrorCode.OVERSIZED_PLACEHOLDER */, `${imgDirectiveDetails(dir.ngSrc)} the \`placeholder\` attribute is set to a data URL which is longer ` +
|
|
1646
|
-
`than ${DATA_URL_WARN_LIMIT} characters. This is discouraged, as large inline placeholders ` +
|
|
1647
|
-
`directly increase the bundle size of Angular and hurt page load performance. For better loading performance, ` +
|
|
1648
|
-
`generate a smaller data URL placeholder.`));
|
|
1649
|
-
}
|
|
1119
|
+
if (dir.placeholder && typeof dir.placeholder === 'string' && dir.placeholder.startsWith('data:')) {
|
|
1120
|
+
if (dir.placeholder.length > DATA_URL_ERROR_LIMIT) {
|
|
1121
|
+
throw new _RuntimeError(2965, `${imgDirectiveDetails(dir.ngSrc)} the \`placeholder\` attribute is set to a data URL which is longer ` + `than ${DATA_URL_ERROR_LIMIT} characters. This is strongly discouraged, as large inline placeholders ` + `directly increase the bundle size of Angular and hurt page load performance. To fix this, generate ` + `a smaller data URL placeholder.`);
|
|
1122
|
+
}
|
|
1123
|
+
if (dir.placeholder.length > DATA_URL_WARN_LIMIT) {
|
|
1124
|
+
console.warn(_formatRuntimeError(2965, `${imgDirectiveDetails(dir.ngSrc)} the \`placeholder\` attribute is set to a data URL which is longer ` + `than ${DATA_URL_WARN_LIMIT} characters. This is discouraged, as large inline placeholders ` + `directly increase the bundle size of Angular and hurt page load performance. For better loading performance, ` + `generate a smaller data URL placeholder.`));
|
|
1650
1125
|
}
|
|
1126
|
+
}
|
|
1651
1127
|
}
|
|
1652
|
-
/**
|
|
1653
|
-
* Verifies that the `ngSrc` is not a Blob URL.
|
|
1654
|
-
*/
|
|
1655
1128
|
function assertNotBlobUrl(dir) {
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
`To fix this, disable the NgOptimizedImage directive for this element ` +
|
|
1661
|
-
`by removing \`ngSrc\` and using a regular \`src\` attribute instead.`);
|
|
1662
|
-
}
|
|
1129
|
+
const ngSrc = dir.ngSrc.trim();
|
|
1130
|
+
if (ngSrc.startsWith('blob:')) {
|
|
1131
|
+
throw new _RuntimeError(2952, `${imgDirectiveDetails(dir.ngSrc)} \`ngSrc\` was set to a blob URL (${ngSrc}). ` + `Blob URLs are not supported by the NgOptimizedImage directive. ` + `To fix this, disable the NgOptimizedImage directive for this element ` + `by removing \`ngSrc\` and using a regular \`src\` attribute instead.`);
|
|
1132
|
+
}
|
|
1663
1133
|
}
|
|
1664
|
-
/**
|
|
1665
|
-
* Verifies that the input is set to a non-empty string.
|
|
1666
|
-
*/
|
|
1667
1134
|
function assertNonEmptyInput(dir, name, value) {
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
}
|
|
1135
|
+
const isString = typeof value === 'string';
|
|
1136
|
+
const isEmptyString = isString && value.trim() === '';
|
|
1137
|
+
if (!isString || isEmptyString) {
|
|
1138
|
+
throw new _RuntimeError(2952, `${imgDirectiveDetails(dir.ngSrc)} \`${name}\` has an invalid value ` + `(\`${value}\`). To fix this, change the value to a non-empty string.`);
|
|
1139
|
+
}
|
|
1674
1140
|
}
|
|
1675
|
-
/**
|
|
1676
|
-
* Verifies that the `ngSrcset` is in a valid format, e.g. "100w, 200w" or "1x, 2x".
|
|
1677
|
-
*/
|
|
1678
1141
|
function assertValidNgSrcset(dir, value) {
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
`To fix this, supply \`ngSrcset\` using a comma-separated list of one or more width ` +
|
|
1692
|
-
`descriptors (e.g. "100w, 200w") or density descriptors (e.g. "1x, 2x").`);
|
|
1693
|
-
}
|
|
1142
|
+
if (value == null) return;
|
|
1143
|
+
assertNonEmptyInput(dir, 'ngSrcset', value);
|
|
1144
|
+
const stringVal = value;
|
|
1145
|
+
const isValidWidthDescriptor = VALID_WIDTH_DESCRIPTOR_SRCSET.test(stringVal);
|
|
1146
|
+
const isValidDensityDescriptor = VALID_DENSITY_DESCRIPTOR_SRCSET.test(stringVal);
|
|
1147
|
+
if (isValidDensityDescriptor) {
|
|
1148
|
+
assertUnderDensityCap(dir, stringVal);
|
|
1149
|
+
}
|
|
1150
|
+
const isValidSrcset = isValidWidthDescriptor || isValidDensityDescriptor;
|
|
1151
|
+
if (!isValidSrcset) {
|
|
1152
|
+
throw new _RuntimeError(2952, `${imgDirectiveDetails(dir.ngSrc)} \`ngSrcset\` has an invalid value (\`${value}\`). ` + `To fix this, supply \`ngSrcset\` using a comma-separated list of one or more width ` + `descriptors (e.g. "100w, 200w") or density descriptors (e.g. "1x, 2x").`);
|
|
1153
|
+
}
|
|
1694
1154
|
}
|
|
1695
1155
|
function assertUnderDensityCap(dir, value) {
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
throw new _RuntimeError(2952 /* RuntimeErrorCode.INVALID_INPUT */, `${imgDirectiveDetails(dir.ngSrc)} the \`ngSrcset\` contains an unsupported image density:` +
|
|
1701
|
-
`\`${value}\`. NgOptimizedImage generally recommends a max image density of ` +
|
|
1702
|
-
`${RECOMMENDED_SRCSET_DENSITY_CAP}x but supports image densities up to ` +
|
|
1703
|
-
`${ABSOLUTE_SRCSET_DENSITY_CAP}x. The human eye cannot distinguish between image densities ` +
|
|
1704
|
-
`greater than ${RECOMMENDED_SRCSET_DENSITY_CAP}x - which makes them unnecessary for ` +
|
|
1705
|
-
`most use cases. Images that will be pinch-zoomed are typically the primary use case for ` +
|
|
1706
|
-
`${ABSOLUTE_SRCSET_DENSITY_CAP}x images. Please remove the high density descriptor and try again.`);
|
|
1707
|
-
}
|
|
1156
|
+
const underDensityCap = value.split(',').every(num => num === '' || parseFloat(num) <= ABSOLUTE_SRCSET_DENSITY_CAP);
|
|
1157
|
+
if (!underDensityCap) {
|
|
1158
|
+
throw new _RuntimeError(2952, `${imgDirectiveDetails(dir.ngSrc)} the \`ngSrcset\` contains an unsupported image density:` + `\`${value}\`. NgOptimizedImage generally recommends a max image density of ` + `${RECOMMENDED_SRCSET_DENSITY_CAP}x but supports image densities up to ` + `${ABSOLUTE_SRCSET_DENSITY_CAP}x. The human eye cannot distinguish between image densities ` + `greater than ${RECOMMENDED_SRCSET_DENSITY_CAP}x - which makes them unnecessary for ` + `most use cases. Images that will be pinch-zoomed are typically the primary use case for ` + `${ABSOLUTE_SRCSET_DENSITY_CAP}x images. Please remove the high density descriptor and try again.`);
|
|
1159
|
+
}
|
|
1708
1160
|
}
|
|
1709
|
-
/**
|
|
1710
|
-
* Creates a `RuntimeError` instance to represent a situation when an input is set after
|
|
1711
|
-
* the directive has initialized.
|
|
1712
|
-
*/
|
|
1713
1161
|
function postInitInputChangeError(dir, inputName) {
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
reason =
|
|
1722
|
-
`Changing the \`${inputName}\` would have no effect on the underlying ` +
|
|
1723
|
-
`image element, because the resource loading has already occurred.`;
|
|
1724
|
-
}
|
|
1725
|
-
return new _RuntimeError(2953 /* RuntimeErrorCode.UNEXPECTED_INPUT_CHANGE */, `${imgDirectiveDetails(dir.ngSrc)} \`${inputName}\` was updated after initialization. ` +
|
|
1726
|
-
`The NgOptimizedImage directive will not react to this input change. ${reason} ` +
|
|
1727
|
-
`To fix this, either switch \`${inputName}\` to a static value ` +
|
|
1728
|
-
`or wrap the image element in an @if that is gated on the necessary value.`);
|
|
1162
|
+
let reason;
|
|
1163
|
+
if (inputName === 'width' || inputName === 'height') {
|
|
1164
|
+
reason = `Changing \`${inputName}\` may result in different attribute value ` + `applied to the underlying image element and cause layout shifts on a page.`;
|
|
1165
|
+
} else {
|
|
1166
|
+
reason = `Changing the \`${inputName}\` would have no effect on the underlying ` + `image element, because the resource loading has already occurred.`;
|
|
1167
|
+
}
|
|
1168
|
+
return new _RuntimeError(2953, `${imgDirectiveDetails(dir.ngSrc)} \`${inputName}\` was updated after initialization. ` + `The NgOptimizedImage directive will not react to this input change. ${reason} ` + `To fix this, either switch \`${inputName}\` to a static value ` + `or wrap the image element in an @if that is gated on the necessary value.`);
|
|
1729
1169
|
}
|
|
1730
|
-
/**
|
|
1731
|
-
* Verify that none of the listed inputs has changed.
|
|
1732
|
-
*/
|
|
1733
1170
|
function assertNoPostInitInputChange(dir, changes, inputs) {
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
}
|
|
1746
|
-
});
|
|
1171
|
+
inputs.forEach(input => {
|
|
1172
|
+
const isUpdated = changes.hasOwnProperty(input);
|
|
1173
|
+
if (isUpdated && !changes[input].isFirstChange()) {
|
|
1174
|
+
if (input === 'ngSrc') {
|
|
1175
|
+
dir = {
|
|
1176
|
+
ngSrc: changes[input].previousValue
|
|
1177
|
+
};
|
|
1178
|
+
}
|
|
1179
|
+
throw postInitInputChangeError(dir, input);
|
|
1180
|
+
}
|
|
1181
|
+
});
|
|
1747
1182
|
}
|
|
1748
|
-
/**
|
|
1749
|
-
* Verifies that a specified input is a number greater than 0.
|
|
1750
|
-
*/
|
|
1751
1183
|
function assertGreaterThanZero(dir, inputValue, inputName) {
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
}
|
|
1184
|
+
const validNumber = typeof inputValue === 'number' && inputValue > 0;
|
|
1185
|
+
const validString = typeof inputValue === 'string' && /^\d+$/.test(inputValue.trim()) && parseInt(inputValue) > 0;
|
|
1186
|
+
if (!validNumber && !validString) {
|
|
1187
|
+
throw new _RuntimeError(2952, `${imgDirectiveDetails(dir.ngSrc)} \`${inputName}\` has an invalid value. ` + `To fix this, provide \`${inputName}\` as a number greater than 0.`);
|
|
1188
|
+
}
|
|
1758
1189
|
}
|
|
1759
|
-
/**
|
|
1760
|
-
* Verifies that the rendered image is not visually distorted. Effectively this is checking:
|
|
1761
|
-
* - Whether the "width" and "height" attributes reflect the actual dimensions of the image.
|
|
1762
|
-
* - Whether image styling is "correct" (see below for a longer explanation).
|
|
1763
|
-
*/
|
|
1764
1190
|
function assertNoImageDistortion(dir, img, renderer) {
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
`${round(renderedAspectRatio)}). \nThis issue can occur if "width" and "height" ` +
|
|
1811
|
-
`attributes are added to an image without updating the corresponding ` +
|
|
1812
|
-
`image styling. To fix this, adjust image styling. In most cases, ` +
|
|
1813
|
-
`adding "height: auto" or "width: auto" to the image styling will fix ` +
|
|
1814
|
-
`this issue.`));
|
|
1815
|
-
}
|
|
1816
|
-
else if (!dir.ngSrcset && nonZeroRenderedDimensions) {
|
|
1817
|
-
// If `ngSrcset` hasn't been set, sanity check the intrinsic size.
|
|
1818
|
-
const recommendedWidth = RECOMMENDED_SRCSET_DENSITY_CAP * renderedWidth;
|
|
1819
|
-
const recommendedHeight = RECOMMENDED_SRCSET_DENSITY_CAP * renderedHeight;
|
|
1820
|
-
const oversizedWidth = intrinsicWidth - recommendedWidth >= OVERSIZED_IMAGE_TOLERANCE;
|
|
1821
|
-
const oversizedHeight = intrinsicHeight - recommendedHeight >= OVERSIZED_IMAGE_TOLERANCE;
|
|
1822
|
-
if (oversizedWidth || oversizedHeight) {
|
|
1823
|
-
console.warn(_formatRuntimeError(2960 /* RuntimeErrorCode.OVERSIZED_IMAGE */, `${imgDirectiveDetails(dir.ngSrc)} the intrinsic image is significantly ` +
|
|
1824
|
-
`larger than necessary. ` +
|
|
1825
|
-
`\nRendered image size: ${renderedWidth}w x ${renderedHeight}h. ` +
|
|
1826
|
-
`\nIntrinsic image size: ${intrinsicWidth}w x ${intrinsicHeight}h. ` +
|
|
1827
|
-
`\nRecommended intrinsic image size: ${recommendedWidth}w x ${recommendedHeight}h. ` +
|
|
1828
|
-
`\nNote: Recommended intrinsic image size is calculated assuming a maximum DPR of ` +
|
|
1829
|
-
`${RECOMMENDED_SRCSET_DENSITY_CAP}. To improve loading time, resize the image ` +
|
|
1830
|
-
`or consider using the "ngSrcset" and "sizes" attributes.`));
|
|
1831
|
-
}
|
|
1832
|
-
}
|
|
1833
|
-
};
|
|
1834
|
-
const removeLoadListenerFn = renderer.listen(img, 'load', callback);
|
|
1835
|
-
// We only listen to the `error` event to remove the `load` event listener because it will not be
|
|
1836
|
-
// fired if the image fails to load. This is done to prevent memory leaks in development mode
|
|
1837
|
-
// because image elements aren't garbage-collected properly. It happens because zone.js stores the
|
|
1838
|
-
// event listener directly on the element and closures capture `dir`.
|
|
1839
|
-
const removeErrorListenerFn = renderer.listen(img, 'error', () => {
|
|
1840
|
-
removeLoadListenerFn();
|
|
1841
|
-
removeErrorListenerFn();
|
|
1842
|
-
});
|
|
1843
|
-
callOnLoadIfImageIsLoaded(img, callback);
|
|
1191
|
+
const callback = () => {
|
|
1192
|
+
removeLoadListenerFn();
|
|
1193
|
+
removeErrorListenerFn();
|
|
1194
|
+
const computedStyle = window.getComputedStyle(img);
|
|
1195
|
+
let renderedWidth = parseFloat(computedStyle.getPropertyValue('width'));
|
|
1196
|
+
let renderedHeight = parseFloat(computedStyle.getPropertyValue('height'));
|
|
1197
|
+
const boxSizing = computedStyle.getPropertyValue('box-sizing');
|
|
1198
|
+
if (boxSizing === 'border-box') {
|
|
1199
|
+
const paddingTop = computedStyle.getPropertyValue('padding-top');
|
|
1200
|
+
const paddingRight = computedStyle.getPropertyValue('padding-right');
|
|
1201
|
+
const paddingBottom = computedStyle.getPropertyValue('padding-bottom');
|
|
1202
|
+
const paddingLeft = computedStyle.getPropertyValue('padding-left');
|
|
1203
|
+
renderedWidth -= parseFloat(paddingRight) + parseFloat(paddingLeft);
|
|
1204
|
+
renderedHeight -= parseFloat(paddingTop) + parseFloat(paddingBottom);
|
|
1205
|
+
}
|
|
1206
|
+
const renderedAspectRatio = renderedWidth / renderedHeight;
|
|
1207
|
+
const nonZeroRenderedDimensions = renderedWidth !== 0 && renderedHeight !== 0;
|
|
1208
|
+
const intrinsicWidth = img.naturalWidth;
|
|
1209
|
+
const intrinsicHeight = img.naturalHeight;
|
|
1210
|
+
const intrinsicAspectRatio = intrinsicWidth / intrinsicHeight;
|
|
1211
|
+
const suppliedWidth = dir.width;
|
|
1212
|
+
const suppliedHeight = dir.height;
|
|
1213
|
+
const suppliedAspectRatio = suppliedWidth / suppliedHeight;
|
|
1214
|
+
const inaccurateDimensions = Math.abs(suppliedAspectRatio - intrinsicAspectRatio) > ASPECT_RATIO_TOLERANCE;
|
|
1215
|
+
const stylingDistortion = nonZeroRenderedDimensions && Math.abs(intrinsicAspectRatio - renderedAspectRatio) > ASPECT_RATIO_TOLERANCE;
|
|
1216
|
+
if (inaccurateDimensions) {
|
|
1217
|
+
console.warn(_formatRuntimeError(2952, `${imgDirectiveDetails(dir.ngSrc)} the aspect ratio of the image does not match ` + `the aspect ratio indicated by the width and height attributes. ` + `\nIntrinsic image size: ${intrinsicWidth}w x ${intrinsicHeight}h ` + `(aspect-ratio: ${round(intrinsicAspectRatio)}). \nSupplied width and height attributes: ` + `${suppliedWidth}w x ${suppliedHeight}h (aspect-ratio: ${round(suppliedAspectRatio)}). ` + `\nTo fix this, update the width and height attributes.`));
|
|
1218
|
+
} else if (stylingDistortion) {
|
|
1219
|
+
console.warn(_formatRuntimeError(2952, `${imgDirectiveDetails(dir.ngSrc)} the aspect ratio of the rendered image ` + `does not match the image's intrinsic aspect ratio. ` + `\nIntrinsic image size: ${intrinsicWidth}w x ${intrinsicHeight}h ` + `(aspect-ratio: ${round(intrinsicAspectRatio)}). \nRendered image size: ` + `${renderedWidth}w x ${renderedHeight}h (aspect-ratio: ` + `${round(renderedAspectRatio)}). \nThis issue can occur if "width" and "height" ` + `attributes are added to an image without updating the corresponding ` + `image styling. To fix this, adjust image styling. In most cases, ` + `adding "height: auto" or "width: auto" to the image styling will fix ` + `this issue.`));
|
|
1220
|
+
} else if (!dir.ngSrcset && nonZeroRenderedDimensions) {
|
|
1221
|
+
const recommendedWidth = RECOMMENDED_SRCSET_DENSITY_CAP * renderedWidth;
|
|
1222
|
+
const recommendedHeight = RECOMMENDED_SRCSET_DENSITY_CAP * renderedHeight;
|
|
1223
|
+
const oversizedWidth = intrinsicWidth - recommendedWidth >= OVERSIZED_IMAGE_TOLERANCE;
|
|
1224
|
+
const oversizedHeight = intrinsicHeight - recommendedHeight >= OVERSIZED_IMAGE_TOLERANCE;
|
|
1225
|
+
if (oversizedWidth || oversizedHeight) {
|
|
1226
|
+
console.warn(_formatRuntimeError(2960, `${imgDirectiveDetails(dir.ngSrc)} the intrinsic image is significantly ` + `larger than necessary. ` + `\nRendered image size: ${renderedWidth}w x ${renderedHeight}h. ` + `\nIntrinsic image size: ${intrinsicWidth}w x ${intrinsicHeight}h. ` + `\nRecommended intrinsic image size: ${recommendedWidth}w x ${recommendedHeight}h. ` + `\nNote: Recommended intrinsic image size is calculated assuming a maximum DPR of ` + `${RECOMMENDED_SRCSET_DENSITY_CAP}. To improve loading time, resize the image ` + `or consider using the "ngSrcset" and "sizes" attributes.`));
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
};
|
|
1230
|
+
const removeLoadListenerFn = renderer.listen(img, 'load', callback);
|
|
1231
|
+
const removeErrorListenerFn = renderer.listen(img, 'error', () => {
|
|
1232
|
+
removeLoadListenerFn();
|
|
1233
|
+
removeErrorListenerFn();
|
|
1234
|
+
});
|
|
1235
|
+
callOnLoadIfImageIsLoaded(img, callback);
|
|
1844
1236
|
}
|
|
1845
|
-
/**
|
|
1846
|
-
* Verifies that a specified input is set.
|
|
1847
|
-
*/
|
|
1848
1237
|
function assertNonEmptyWidthAndHeight(dir) {
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
throw new _RuntimeError(2954 /* RuntimeErrorCode.REQUIRED_INPUT_MISSING */, `${imgDirectiveDetails(dir.ngSrc)} these required attributes ` +
|
|
1856
|
-
`are missing: ${missingAttributes.map((attr) => `"${attr}"`).join(', ')}. ` +
|
|
1857
|
-
`Including "width" and "height" attributes will prevent image-related layout shifts. ` +
|
|
1858
|
-
`To fix this, include "width" and "height" attributes on the image tag or turn on ` +
|
|
1859
|
-
`"fill" mode with the \`fill\` attribute.`);
|
|
1860
|
-
}
|
|
1238
|
+
let missingAttributes = [];
|
|
1239
|
+
if (dir.width === undefined) missingAttributes.push('width');
|
|
1240
|
+
if (dir.height === undefined) missingAttributes.push('height');
|
|
1241
|
+
if (missingAttributes.length > 0) {
|
|
1242
|
+
throw new _RuntimeError(2954, `${imgDirectiveDetails(dir.ngSrc)} these required attributes ` + `are missing: ${missingAttributes.map(attr => `"${attr}"`).join(', ')}. ` + `Including "width" and "height" attributes will prevent image-related layout shifts. ` + `To fix this, include "width" and "height" attributes on the image tag or turn on ` + `"fill" mode with the \`fill\` attribute.`);
|
|
1243
|
+
}
|
|
1861
1244
|
}
|
|
1862
|
-
/**
|
|
1863
|
-
* Verifies that width and height are not set. Used in fill mode, where those attributes don't make
|
|
1864
|
-
* sense.
|
|
1865
|
-
*/
|
|
1866
1245
|
function assertEmptyWidthAndHeight(dir) {
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
`element, the size attributes have no effect and should be removed.`);
|
|
1871
|
-
}
|
|
1246
|
+
if (dir.width || dir.height) {
|
|
1247
|
+
throw new _RuntimeError(2952, `${imgDirectiveDetails(dir.ngSrc)} the attributes \`height\` and/or \`width\` are present ` + `along with the \`fill\` attribute. Because \`fill\` mode causes an image to fill its containing ` + `element, the size attributes have no effect and should be removed.`);
|
|
1248
|
+
}
|
|
1872
1249
|
}
|
|
1873
|
-
/**
|
|
1874
|
-
* Verifies that the rendered image has a nonzero height. If the image is in fill mode, provides
|
|
1875
|
-
* guidance that this can be caused by the containing element's CSS position property.
|
|
1876
|
-
*/
|
|
1877
1250
|
function assertNonZeroRenderedHeight(dir, img, renderer) {
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
const removeErrorListenerFn = renderer.listen(img, 'error', () => {
|
|
1893
|
-
removeLoadListenerFn();
|
|
1894
|
-
removeErrorListenerFn();
|
|
1895
|
-
});
|
|
1896
|
-
callOnLoadIfImageIsLoaded(img, callback);
|
|
1251
|
+
const callback = () => {
|
|
1252
|
+
removeLoadListenerFn();
|
|
1253
|
+
removeErrorListenerFn();
|
|
1254
|
+
const renderedHeight = img.clientHeight;
|
|
1255
|
+
if (dir.fill && renderedHeight === 0) {
|
|
1256
|
+
console.warn(_formatRuntimeError(2952, `${imgDirectiveDetails(dir.ngSrc)} the height of the fill-mode image is zero. ` + `This is likely because the containing element does not have the CSS 'position' ` + `property set to one of the following: "relative", "fixed", or "absolute". ` + `To fix this problem, make sure the container element has the CSS 'position' ` + `property defined and the height of the element is not zero.`));
|
|
1257
|
+
}
|
|
1258
|
+
};
|
|
1259
|
+
const removeLoadListenerFn = renderer.listen(img, 'load', callback);
|
|
1260
|
+
const removeErrorListenerFn = renderer.listen(img, 'error', () => {
|
|
1261
|
+
removeLoadListenerFn();
|
|
1262
|
+
removeErrorListenerFn();
|
|
1263
|
+
});
|
|
1264
|
+
callOnLoadIfImageIsLoaded(img, callback);
|
|
1897
1265
|
}
|
|
1898
|
-
/**
|
|
1899
|
-
* Verifies that the `loading` attribute is set to a valid input &
|
|
1900
|
-
* is not used on priority images.
|
|
1901
|
-
*/
|
|
1902
1266
|
function assertValidLoadingInput(dir) {
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
const validInputs = ['auto', 'eager', 'lazy'];
|
|
1911
|
-
if (typeof dir.loading === 'string' && !validInputs.includes(dir.loading)) {
|
|
1912
|
-
throw new _RuntimeError(2952 /* RuntimeErrorCode.INVALID_INPUT */, `${imgDirectiveDetails(dir.ngSrc)} the \`loading\` attribute ` +
|
|
1913
|
-
`has an invalid value (\`${dir.loading}\`). ` +
|
|
1914
|
-
`To fix this, provide a valid value ("lazy", "eager", or "auto").`);
|
|
1915
|
-
}
|
|
1267
|
+
if (dir.loading && dir.priority) {
|
|
1268
|
+
throw new _RuntimeError(2952, `${imgDirectiveDetails(dir.ngSrc)} the \`loading\` attribute ` + `was used on an image that was marked "priority". ` + `Setting \`loading\` on priority images is not allowed ` + `because these images will always be eagerly loaded. ` + `To fix this, remove the “loading” attribute from the priority image.`);
|
|
1269
|
+
}
|
|
1270
|
+
const validInputs = ['auto', 'eager', 'lazy'];
|
|
1271
|
+
if (typeof dir.loading === 'string' && !validInputs.includes(dir.loading)) {
|
|
1272
|
+
throw new _RuntimeError(2952, `${imgDirectiveDetails(dir.ngSrc)} the \`loading\` attribute ` + `has an invalid value (\`${dir.loading}\`). ` + `To fix this, provide a valid value ("lazy", "eager", or "auto").`);
|
|
1273
|
+
}
|
|
1916
1274
|
}
|
|
1917
|
-
/**
|
|
1918
|
-
* Verifies that the `decoding` attribute is set to a valid input.
|
|
1919
|
-
*/
|
|
1920
1275
|
function assertValidDecodingInput(dir) {
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
`To fix this, provide a valid value ("sync", "async", or "auto").`);
|
|
1926
|
-
}
|
|
1276
|
+
const validInputs = ['sync', 'async', 'auto'];
|
|
1277
|
+
if (typeof dir.decoding === 'string' && !validInputs.includes(dir.decoding)) {
|
|
1278
|
+
throw new _RuntimeError(2952, `${imgDirectiveDetails(dir.ngSrc)} the \`decoding\` attribute ` + `has an invalid value (\`${dir.decoding}\`). ` + `To fix this, provide a valid value ("sync", "async", or "auto").`);
|
|
1279
|
+
}
|
|
1927
1280
|
}
|
|
1928
|
-
/**
|
|
1929
|
-
* Warns if NOT using a loader (falling back to the generic loader) and
|
|
1930
|
-
* the image appears to be hosted on one of the image CDNs for which
|
|
1931
|
-
* we do have a built-in image loader. Suggests switching to the
|
|
1932
|
-
* built-in loader.
|
|
1933
|
-
*
|
|
1934
|
-
* @param ngSrc Value of the ngSrc attribute
|
|
1935
|
-
* @param imageLoader ImageLoader provided
|
|
1936
|
-
*/
|
|
1937
1281
|
function assertNotMissingBuiltInLoader(ngSrc, imageLoader) {
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
`${builtInLoaderName} CDN, but your app is not using Angular's ` +
|
|
1949
|
-
`built-in loader for that CDN. We recommend switching to use ` +
|
|
1950
|
-
`the built-in by calling \`provide${builtInLoaderName}Loader()\` ` +
|
|
1951
|
-
`in your \`providers\` and passing it your instance's base URL. ` +
|
|
1952
|
-
`If you don't want to use the built-in loader, define a custom ` +
|
|
1953
|
-
`loader function using IMAGE_LOADER to silence this warning.`));
|
|
1954
|
-
}
|
|
1282
|
+
if (imageLoader === noopImageLoader) {
|
|
1283
|
+
let builtInLoaderName = '';
|
|
1284
|
+
for (const loader of BUILT_IN_LOADERS) {
|
|
1285
|
+
if (loader.testUrl(ngSrc)) {
|
|
1286
|
+
builtInLoaderName = loader.name;
|
|
1287
|
+
break;
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
if (builtInLoaderName) {
|
|
1291
|
+
console.warn(_formatRuntimeError(2962, `NgOptimizedImage: It looks like your images may be hosted on the ` + `${builtInLoaderName} CDN, but your app is not using Angular's ` + `built-in loader for that CDN. We recommend switching to use ` + `the built-in by calling \`provide${builtInLoaderName}Loader()\` ` + `in your \`providers\` and passing it your instance's base URL. ` + `If you don't want to use the built-in loader, define a custom ` + `loader function using IMAGE_LOADER to silence this warning.`));
|
|
1955
1292
|
}
|
|
1293
|
+
}
|
|
1956
1294
|
}
|
|
1957
|
-
/**
|
|
1958
|
-
* Warns if ngSrcset is present and no loader is configured (i.e. the default one is being used).
|
|
1959
|
-
*/
|
|
1960
1295
|
function assertNoNgSrcsetWithoutLoader(dir, imageLoader) {
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
`which would result in the same image being used for all configured sizes. ` +
|
|
1965
|
-
`To fix this, provide a loader or remove the \`ngSrcset\` attribute from the image.`));
|
|
1966
|
-
}
|
|
1296
|
+
if (dir.ngSrcset && imageLoader === noopImageLoader) {
|
|
1297
|
+
console.warn(_formatRuntimeError(2963, `${imgDirectiveDetails(dir.ngSrc)} the \`ngSrcset\` attribute is present but ` + `no image loader is configured (i.e. the default one is being used), ` + `which would result in the same image being used for all configured sizes. ` + `To fix this, provide a loader or remove the \`ngSrcset\` attribute from the image.`));
|
|
1298
|
+
}
|
|
1967
1299
|
}
|
|
1968
|
-
/**
|
|
1969
|
-
* Warns if loaderParams is present and no loader is configured (i.e. the default one is being
|
|
1970
|
-
* used).
|
|
1971
|
-
*/
|
|
1972
1300
|
function assertNoLoaderParamsWithoutLoader(dir, imageLoader) {
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
`which means that the loaderParams data will not be consumed and will not affect the URL. ` +
|
|
1977
|
-
`To fix this, provide a custom loader or remove the \`loaderParams\` attribute from the image.`));
|
|
1978
|
-
}
|
|
1301
|
+
if (dir.loaderParams && imageLoader === noopImageLoader) {
|
|
1302
|
+
console.warn(_formatRuntimeError(2963, `${imgDirectiveDetails(dir.ngSrc)} the \`loaderParams\` attribute is present but ` + `no image loader is configured (i.e. the default one is being used), ` + `which means that the loaderParams data will not be consumed and will not affect the URL. ` + `To fix this, provide a custom loader or remove the \`loaderParams\` attribute from the image.`));
|
|
1303
|
+
}
|
|
1979
1304
|
}
|
|
1980
|
-
/**
|
|
1981
|
-
* Warns if the priority attribute is used too often on page load
|
|
1982
|
-
*/
|
|
1983
1305
|
async function assetPriorityCountBelowThreshold(appRef) {
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
`Marking too many images as "high" priority can hurt your application's LCP (https://web.dev/lcp). ` +
|
|
1990
|
-
`"Priority" should only be set on the image expected to be the page's LCP element.`));
|
|
1991
|
-
}
|
|
1992
|
-
}
|
|
1993
|
-
else {
|
|
1994
|
-
IMGS_WITH_PRIORITY_ATTR_COUNT++;
|
|
1306
|
+
if (IMGS_WITH_PRIORITY_ATTR_COUNT === 0) {
|
|
1307
|
+
IMGS_WITH_PRIORITY_ATTR_COUNT++;
|
|
1308
|
+
await appRef.whenStable();
|
|
1309
|
+
if (IMGS_WITH_PRIORITY_ATTR_COUNT > PRIORITY_COUNT_THRESHOLD) {
|
|
1310
|
+
console.warn(_formatRuntimeError(2966, `NgOptimizedImage: The "priority" attribute is set to true more than ${PRIORITY_COUNT_THRESHOLD} times (${IMGS_WITH_PRIORITY_ATTR_COUNT} times). ` + `Marking too many images as "high" priority can hurt your application's LCP (https://web.dev/lcp). ` + `"Priority" should only be set on the image expected to be the page's LCP element.`));
|
|
1995
1311
|
}
|
|
1312
|
+
} else {
|
|
1313
|
+
IMGS_WITH_PRIORITY_ATTR_COUNT++;
|
|
1314
|
+
}
|
|
1996
1315
|
}
|
|
1997
|
-
/**
|
|
1998
|
-
* Warns if placeholder's dimension are over a threshold.
|
|
1999
|
-
*
|
|
2000
|
-
* This assert function is meant to only run on the browser.
|
|
2001
|
-
*/
|
|
2002
1316
|
function assertPlaceholderDimensions(dir, imgElement) {
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
`To fix this, use a smaller image as a placeholder.`));
|
|
2010
|
-
}
|
|
1317
|
+
const computedStyle = window.getComputedStyle(imgElement);
|
|
1318
|
+
let renderedWidth = parseFloat(computedStyle.getPropertyValue('width'));
|
|
1319
|
+
let renderedHeight = parseFloat(computedStyle.getPropertyValue('height'));
|
|
1320
|
+
if (renderedWidth > PLACEHOLDER_DIMENSION_LIMIT || renderedHeight > PLACEHOLDER_DIMENSION_LIMIT) {
|
|
1321
|
+
console.warn(_formatRuntimeError(2967, `${imgDirectiveDetails(dir.ngSrc)} it uses a placeholder image, but at least one ` + `of the dimensions attribute (height or width) exceeds the limit of ${PLACEHOLDER_DIMENSION_LIMIT}px. ` + `To fix this, use a smaller image as a placeholder.`));
|
|
1322
|
+
}
|
|
2011
1323
|
}
|
|
2012
1324
|
function callOnLoadIfImageIsLoaded(img, callback) {
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
// In that case, the `load` event will not fire at all, meaning that all setup
|
|
2017
|
-
// callbacks listening for the `load` event will not be invoked.
|
|
2018
|
-
// In Safari, there is a known behavior where the `complete` property of an
|
|
2019
|
-
// `HTMLImageElement` may sometimes return `true` even when the image is not fully loaded.
|
|
2020
|
-
// Checking both `img.complete` and `img.naturalWidth` is the most reliable way to
|
|
2021
|
-
// determine if an image has been fully loaded, especially in browsers where the
|
|
2022
|
-
// `complete` property may return `true` prematurely.
|
|
2023
|
-
if (img.complete && img.naturalWidth) {
|
|
2024
|
-
callback();
|
|
2025
|
-
}
|
|
1325
|
+
if (img.complete && img.naturalWidth) {
|
|
1326
|
+
callback();
|
|
1327
|
+
}
|
|
2026
1328
|
}
|
|
2027
1329
|
function round(input) {
|
|
2028
|
-
|
|
1330
|
+
return Number.isInteger(input) ? input : input.toFixed(2);
|
|
2029
1331
|
}
|
|
2030
|
-
// Transform function to handle SafeValue input for ngSrc. This doesn't do any sanitization,
|
|
2031
|
-
// as that is not needed for img.src and img.srcset. This transform is purely for compatibility.
|
|
2032
1332
|
function unwrapSafeUrl(value) {
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
1333
|
+
if (typeof value === 'string') {
|
|
1334
|
+
return value;
|
|
1335
|
+
}
|
|
1336
|
+
return _unwrapSafeValue(value);
|
|
2037
1337
|
}
|
|
2038
|
-
// Transform function to handle inputs which may be booleans, strings, or string representations
|
|
2039
|
-
// of boolean values. Used for the placeholder attribute.
|
|
2040
1338
|
function booleanOrUrlAttribute(value) {
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
1339
|
+
if (typeof value === 'string' && value !== 'true' && value !== 'false' && value !== '') {
|
|
1340
|
+
return value;
|
|
1341
|
+
}
|
|
1342
|
+
return booleanAttribute(value);
|
|
2045
1343
|
}
|
|
2046
1344
|
|
|
2047
|
-
export { IMAGE_LOADER, NgOptimizedImage, PRECONNECT_CHECK_BLOCKLIST, VERSION, ViewportScroller, isPlatformBrowser, isPlatformServer, provideCloudflareLoader, provideCloudinaryLoader, provideImageKitLoader, provideImgixLoader, provideNetlifyLoader, registerLocaleData, NullViewportScroller as ɵNullViewportScroller, PLATFORM_BROWSER_ID as ɵPLATFORM_BROWSER_ID, PLATFORM_SERVER_ID as ɵPLATFORM_SERVER_ID };
|
|
1345
|
+
export { IMAGE_LOADER, Location, LocationStrategy, NgOptimizedImage, PRECONNECT_CHECK_BLOCKLIST, PlatformNavigation, VERSION, ViewportScroller, isPlatformBrowser, isPlatformServer, provideCloudflareLoader, provideCloudinaryLoader, provideImageKitLoader, provideImgixLoader, provideNetlifyLoader, registerLocaleData, NavigationAdapterForLocation as ɵNavigationAdapterForLocation, NullViewportScroller as ɵNullViewportScroller, PLATFORM_BROWSER_ID as ɵPLATFORM_BROWSER_ID, PLATFORM_SERVER_ID as ɵPLATFORM_SERVER_ID, normalizeQueryParams as ɵnormalizeQueryParams };
|
|
2048
1346
|
//# sourceMappingURL=common.mjs.map
|