Skip to content

Commit

Permalink
HFP-3918 Fix crash when going to non-existent slide
Browse files Browse the repository at this point in the history
  • Loading branch information
otacke committed Apr 16, 2024
1 parent 82bf251 commit 86326c4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/scripts/cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,10 @@ CoursePresentation.prototype.attachElements = function ($slide, index) {
return; // Already attached
}

if (index < 0 || index > this.slides.length - 1) {
return; // Slide does not exist
}

var slide = this.slides[index];
var instances = this.elementInstances[index];
if (slide.elements !== undefined) {
Expand Down Expand Up @@ -1848,6 +1852,10 @@ CoursePresentation.prototype.attachAllElements = function () {
* @returns {Boolean} Always true.
*/
CoursePresentation.prototype.processJumpToSlide = function (slideNumber, noScroll, handleFocus) {
if (slideNumber < 0 || slideNumber > this.slides.length - 1) {
return;
}

var that = this;
if (this.editor === undefined && this.contentId) { // Content ID avoids crash when previewing in editor before saving
var progressedEvent = this.createXAPIEventTemplate('progressed');
Expand Down Expand Up @@ -2039,7 +2047,11 @@ CoursePresentation.prototype.setOverflowTabIndex = function () {
CoursePresentation.prototype.setSlideNumberAnnouncer = function (slideNumber, handleFocus = false) {
let slideTitle = '';

if (!this.navigationLine) {
if (
!this.navigationLine ||
slideNumber < 0 ||
slideNumber >= this.slides.length - 1
) {
return slideTitle;
}

Expand Down
6 changes: 5 additions & 1 deletion src/scripts/navigation-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,10 @@ const NavigationLine = (function ($) {
NavigationLine.prototype.updateProgressBar = function (slideNumber, prevSlideNumber, solutionMode) {
var that = this;

if (slideNumber < 0 || slideNumber > this.cp.progressbarParts.length - 1) {
return; // Slide number is out of bounds
}

// Updates progress bar progress (blue line)
var i;
for (i = 0; i < that.cp.progressbarParts.length; i += 1) {
Expand Down Expand Up @@ -489,7 +493,7 @@ const NavigationLine = (function ($) {
const $part = this.cp.progressbarParts[index];
const $partTitle = $part.find('.h5p-progressbar-part-title');
const answeredLabel = this.answeredLabels[state].replace('@slideName', this.createSlideTitle(index));

$partTitle.html(`${answeredLabel}`);
};

Expand Down

0 comments on commit 86326c4

Please sign in to comment.