Skip to content

fix(core): Avoid splicing arrays using a negative start index #10679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/core/ui/core/view/view-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,10 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
this._closeModalCallback = (...originalArgs) => {
const cleanupModalViews = () => {
const modalIndex = _rootModalViews.indexOf(this);
_rootModalViews.splice(modalIndex, 1);
if (modalIndex > -1) {
_rootModalViews.splice(modalIndex, 1);
}

this._modalParent = null;
this._modalContext = null;
this._closeModalCallback = null;
Expand Down
10 changes: 8 additions & 2 deletions packages/core/ui/layouts/layout-base-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export class LayoutBaseCommon extends CustomLayoutView implements LayoutBaseDefi
}

public insertChild(child: View, atIndex: number): void {
if (atIndex < 0) {
throw new Error('Cannot insert a child to a negative index.');
}

this._subViews.splice(atIndex, 0, child);
this._addView(child, atIndex);
this._registerLayoutChild(child);
Expand All @@ -60,8 +64,10 @@ export class LayoutBaseCommon extends CustomLayoutView implements LayoutBaseDefi

// TODO: consider caching the index on the child.
const index = this._subViews.indexOf(child);
this._subViews.splice(index, 1);
this._unregisterLayoutChild(child);
if (index > -1) {
this._subViews.splice(index, 1);
this._unregisterLayoutChild(child);
}
}

public removeChildren(): void {
Expand Down
18 changes: 10 additions & 8 deletions packages/core/ui/layouts/root-layout/root-layout-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ export class RootLayoutBase extends GridLayout {
},
(err) => {
rej(new Error(`Error playing enter animation: ${err}`));
}
},
);
});
})
}),
);

Promise.all(toOpen).then(
Expand All @@ -105,7 +105,7 @@ export class RootLayoutBase extends GridLayout {
},
(err) => {
reject(err);
}
},
);
});
}
Expand Down Expand Up @@ -140,7 +140,9 @@ export class RootLayoutBase extends GridLayout {
const exitAnimationDefinition = exitTo || poppedView?.options?.animation?.exitTo;

// Remove view from tracked popupviews
this.popupViews.splice(popupIndex, 1);
if (popupIndex > -1) {
this.popupViews.splice(popupIndex, 1);
}

toClose.push(
new Promise<void>((res, rej) => {
Expand All @@ -153,7 +155,7 @@ export class RootLayoutBase extends GridLayout {
} else {
res();
}
})
}),
);

if (this.shadeCover) {
Expand All @@ -177,7 +179,7 @@ export class RootLayoutBase extends GridLayout {
},
(err) => {
reject(err);
}
},
);
});
}
Expand Down Expand Up @@ -264,8 +266,8 @@ export class RootLayoutBase extends GridLayout {
}

// keep the popupViews array in sync with the stacking of the views
const currentView = this.popupViews[this.getPopupIndex(view)];
this.popupViews.splice(this.getPopupIndex(view), 1);
const currentView = this.popupViews[popupIndex];
this.popupViews.splice(popupIndex, 1);
this.popupViews.push(currentView);

const exitAnimation = this.getViewExitState(view);
Expand Down
Loading