Skip to content

Commit

Permalink
Add RFC3986-compliant URL format encoding
Browse files Browse the repository at this point in the history
Fixes #3315
  • Loading branch information
Martinomagnifico committed Feb 13, 2023
1 parent b1a9842 commit ea6b719
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion dist/reveal.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/reveal.esm.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/reveal.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/reveal.js.map

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions js/controllers/slidecontent.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { extend, queryAll, closest, getMimeTypeFromFile } from '../utils/util.js'
import { extend, queryAll, closest, getMimeTypeFromFile, encodeRFC3986URI } from '../utils/util.js'
import { isMobile } from '../utils/device.js'

import fitty from 'fitty';
Expand Down Expand Up @@ -108,7 +108,9 @@ export default class SlideContent {
// URL(s)
else {
backgroundContent.style.backgroundImage = backgroundImage.split( ',' ).map( background => {
return `url(${encodeURI(background.trim())})`;
// Decode URL(s) that are already encoded first
let decoded = decodeURI(background.trim());
return `url(${encodeRFC3986URI(decoded)})`;
}).join( ',' );
}
}
Expand Down
16 changes: 16 additions & 0 deletions js/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,20 @@ const fileExtensionToMimeMap = {
*/
export const getMimeTypeFromFile = ( filename='' ) => {
return fileExtensionToMimeMap[filename.split('.').pop()]
}

/**
* Encodes a string for RFC3986-compliant URL format.
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI#encoding_for_rfc3986
*
* @param {string} url
*/
export const encodeRFC3986URI = ( url='' ) => {
return encodeURI(url)
.replace(/%5B/g, "[")
.replace(/%5D/g, "]")
.replace(
/[!'()*]/g,
(c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`
);
}

0 comments on commit ea6b719

Please sign in to comment.