From c57dbdeec1aa43c313b405b8f2a717b7d768bbb3 Mon Sep 17 00:00:00 2001 From: Evan Luo Date: Sat, 21 Sep 2024 09:22:35 -0400 Subject: [PATCH] fix(utils): handle edge cases in scroll percentage calculation #432 - Add checks for NaN, negative, and infinite values - Ensure percentage is capped at 100% - Improve reliability of scroll progress indicator Co-Authored-By: xzadudu179 <129601567+xzadudu179@users.noreply.github.com> --- source/js/utils.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/source/js/utils.js b/source/js/utils.js index 772ddfbe..5c26b033 100755 --- a/source/js/utils.js +++ b/source/js/utils.js @@ -83,7 +83,19 @@ export default function initUtils() { }, calculatePercentage(scrollTop, scrollHeight, clientHeight) { - return Math.round((scrollTop / (scrollHeight - clientHeight)) * 100); + let percentageValue = Math.round( + (scrollTop / (scrollHeight - clientHeight)) * 100, + ); + if ( + isNaN(percentageValue) || + percentageValue < 0 || + !isFinite(percentageValue) + ) { + percentageValue = 0; + } else if (percentageValue > 100) { + percentageValue = 100; + } + return percentageValue; }, // register window scroll event