Skip to content

Commit 9bd147c

Browse files
authored
fix(core): avoid splicing arrays using a negative start index (#10679)
1 parent 20fc1cc commit 9bd147c

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

packages/core/ui/core/view/view-common.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,10 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
447447
this._closeModalCallback = (...originalArgs) => {
448448
const cleanupModalViews = () => {
449449
const modalIndex = _rootModalViews.indexOf(this);
450-
_rootModalViews.splice(modalIndex, 1);
450+
if (modalIndex > -1) {
451+
_rootModalViews.splice(modalIndex, 1);
452+
}
453+
451454
this._modalParent = null;
452455
this._modalContext = null;
453456
this._closeModalCallback = null;

packages/core/ui/layouts/layout-base-common.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ export class LayoutBaseCommon extends CustomLayoutView implements LayoutBaseDefi
5050
}
5151

5252
public insertChild(child: View, atIndex: number): void {
53+
if (atIndex < 0) {
54+
throw new Error('Cannot insert a child to a negative index.');
55+
}
56+
5357
this._subViews.splice(atIndex, 0, child);
5458
this._addView(child, atIndex);
5559
this._registerLayoutChild(child);
@@ -60,8 +64,10 @@ export class LayoutBaseCommon extends CustomLayoutView implements LayoutBaseDefi
6064

6165
// TODO: consider caching the index on the child.
6266
const index = this._subViews.indexOf(child);
63-
this._subViews.splice(index, 1);
64-
this._unregisterLayoutChild(child);
67+
if (index > -1) {
68+
this._subViews.splice(index, 1);
69+
this._unregisterLayoutChild(child);
70+
}
6571
}
6672

6773
public removeChildren(): void {

packages/core/ui/layouts/root-layout/root-layout-common.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ export class RootLayoutBase extends GridLayout {
9393
},
9494
(err) => {
9595
rej(new Error(`Error playing enter animation: ${err}`));
96-
}
96+
},
9797
);
9898
});
99-
})
99+
}),
100100
);
101101

102102
Promise.all(toOpen).then(
@@ -105,7 +105,7 @@ export class RootLayoutBase extends GridLayout {
105105
},
106106
(err) => {
107107
reject(err);
108-
}
108+
},
109109
);
110110
});
111111
}
@@ -140,7 +140,9 @@ export class RootLayoutBase extends GridLayout {
140140
const exitAnimationDefinition = exitTo || poppedView?.options?.animation?.exitTo;
141141

142142
// Remove view from tracked popupviews
143-
this.popupViews.splice(popupIndex, 1);
143+
if (popupIndex > -1) {
144+
this.popupViews.splice(popupIndex, 1);
145+
}
144146

145147
toClose.push(
146148
new Promise<void>((res, rej) => {
@@ -153,7 +155,7 @@ export class RootLayoutBase extends GridLayout {
153155
} else {
154156
res();
155157
}
156-
})
158+
}),
157159
);
158160

159161
if (this.shadeCover) {
@@ -177,7 +179,7 @@ export class RootLayoutBase extends GridLayout {
177179
},
178180
(err) => {
179181
reject(err);
180-
}
182+
},
181183
);
182184
});
183185
}
@@ -264,8 +266,8 @@ export class RootLayoutBase extends GridLayout {
264266
}
265267

266268
// keep the popupViews array in sync with the stacking of the views
267-
const currentView = this.popupViews[this.getPopupIndex(view)];
268-
this.popupViews.splice(this.getPopupIndex(view), 1);
269+
const currentView = this.popupViews[popupIndex];
270+
this.popupViews.splice(popupIndex, 1);
269271
this.popupViews.push(currentView);
270272

271273
const exitAnimation = this.getViewExitState(view);

0 commit comments

Comments
 (0)