Skip to content

Commit 4def2a7

Browse files
arturovtthePunderWoman
authored andcommitted
refactor(common): replace forEach with index loop for Domino compatibility (angular#62290)
In this commit, the use of `images.forEach` was replaced with a traditional `for (let i = 0; ...)` loop to ensure compatibility with Domino. Domino may return a custom internal data structure (e.g., a lazily evaluated query result) that does not fully support standard iteration protocols like `forEach` or `for...of`. Naturally, this would never happen in any browser. It occurs only in unit tests when using `renderApplication()`, because Domino's `querySelectorAll` returns the following: ```js var nodes = select(selector, context); return nodes.item ? nodes : new NodeList(nodes); ``` In certain unit tests, it returns an object like: `{ root: document, filter: function(e) { ... }, lastModTime: 1, done: true, cache: [] }`, which means that the object does not have a `forEach` method. As a result, this causes an `afterAll` error: `forEach is not a function`. PR Close angular#62290
1 parent c87f367 commit 4def2a7

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

packages/core/src/image_performance_warning.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,17 @@ export class ImagePerformanceWarning implements OnDestroy {
102102
const images = getDocument().querySelectorAll('img');
103103
let lcpElementFound,
104104
lcpElementLoadedCorrectly = false;
105-
images.forEach((image) => {
105+
// Important: do not refactor this to use `images.forEach` or
106+
// `for (const ... of ...)`, because images might be a custom internal
107+
// data structure — such as a lazily evaluated query result in Domino.
108+
// (This naturally would never be a case in any browser).
109+
for (let index = 0; index < images.length; index++) {
110+
const image = images[index];
111+
112+
if (!image) {
113+
continue;
114+
}
115+
106116
if (!this.options?.disableImageSizeWarning) {
107117
// Image elements using the NgOptimizedImage directive are excluded,
108118
// as that directive has its own version of this check.
@@ -122,7 +132,7 @@ export class ImagePerformanceWarning implements OnDestroy {
122132
}
123133
}
124134
}
125-
});
135+
}
126136
if (
127137
lcpElementFound &&
128138
!lcpElementLoadedCorrectly &&

0 commit comments

Comments
 (0)