Skip to content
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

HFP-3918 Fix small bugs with GoToSlide #303

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
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 @@ -1910,6 +1914,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 @@ -2101,7 +2109,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
4 changes: 2 additions & 2 deletions src/scripts/go-to-slide.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addClickAndKeyboardListeners } from './utils';
import { addClickAndKeyboardListeners, stripHTML, decodeHTML } from './utils';
import { jQuery as $, EventDispatcher } from './globals';

/**
Expand Down Expand Up @@ -70,7 +70,7 @@ export default class GoToSlide {
href: '#',
'class': classes,
tabindex: tabindex,
title: title
'aria-label': stripHTML(decodeHTML(title))
});

addClickAndKeyboardListeners(this.$element, (event) => {
Expand Down
4 changes: 4 additions & 0 deletions src/scripts/navigation-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,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
13 changes: 13 additions & 0 deletions src/scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,16 @@ const $STRIP_HTML_HELPER = $('<div>');
* @return {string}
*/
export const stripHTML = (str) => $STRIP_HTML_HELPER.html(str).text().trim();

/**
* Decode text with HTML entities to text.
*
* Beware that this does not strip HTML tags, it only decodes HTML entities.
* @param {string} html HTML with encoded entities.
* @returns {string} Decoded text.
*/
export const decodeHTML = (html) => {
const div = document.createElement('div');
div.innerHTML = html;
return div.textContent || div.innerText || '';
};
Loading