Skip to content

Commit

Permalink
Core: IE11 workaround for currentScript (#2104)
Browse files Browse the repository at this point in the history
This adds a new function `Prism.util.currentScript` which is a functionally equalent to `document.currentScript` but includes support for IE11.
  • Loading branch information
RunDevelopment authored Oct 25, 2019
1 parent a3785ec commit 2108c60
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 21 deletions.
44 changes: 43 additions & 1 deletion components/prism-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,48 @@ var _ = {
default:
return o;
}
},

/**
* Returns the script element that is currently executing.
*
* This does __not__ work for line script element.
*
* @returns {HTMLScriptElement | null}
*/
currentScript: function () {
if (typeof document === 'undefined') {
return null;
}
if ('currentScript' in document) {
return document.currentScript;
}

// IE11 workaround
// we'll get the src of the current script by parsing IE11's error stack trace
// this will not work for inline scripts

try {
throw new Error();
} catch (err) {
// Get file src url from stack. Specifically works with the format of stack traces in IE.
// A stack will look like this:
//
// Error
// at _.util.currentScript (http://localhost/components/prism-core.js:119:5)
// at Global code (http://localhost/components/prism-core.js:606:1)

var src = (/at [^(\r\n]*\((.*):.+:.+\)$/i.exec(err.stack) || [])[1];
if (src) {
var scripts = document.getElementsByTagName('script');
for (var i in scripts) {
if (scripts[i].src == src) {
return scripts[i];
}
}
}
return null;
}
}
},

Expand Down Expand Up @@ -527,7 +569,7 @@ if (!_self.document) {
}

//Get current script and highlight
var script = document.currentScript || [].slice.call(document.getElementsByTagName('script')).pop();
var script = _.util.currentScript();

if (script) {
_.filename = script.src;
Expand Down
2 changes: 1 addition & 1 deletion components/prism-core.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 16 additions & 17 deletions plugins/autoloader/prism-autoloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,25 +181,24 @@
var lang_data = {};

var ignored_language = 'none';

var scripts = document.getElementsByTagName('script');
var script = scripts[scripts.length - 1];
var languages_path = 'components/';

var autoloaderFile = /\bplugins\/autoloader\/prism-autoloader\.(?:min\.)js$/i;
var prismFile = /[\w-]+\.(?:min\.)js$/i;

if (script.hasAttribute('data-autoloader-path')) {
// data-autoloader-path is set, so just use it
languages_path = script.getAttribute('data-autoloader-path').trim().replace(/\/?$/, '/');
} else {
var src = script.src;
if (autoloaderFile.test(src)) {
// the script is the original autoloader script in the usual Prism project structure
languages_path = src.replace(autoloaderFile, 'components/');
} else if (prismFile.test(src)) {
// the script is part of a bundle like a custom prism.js from the download page
languages_path = src.replace(prismFile, 'components/');
var script = Prism.util.currentScript();
if (script) {
var autoloaderFile = /\bplugins\/autoloader\/prism-autoloader\.(?:min\.)js$/i;
var prismFile = /[\w-]+\.(?:min\.)js$/i;
if (script.hasAttribute('data-autoloader-path')) {
// data-autoloader-path is set, so just use it
languages_path = script.getAttribute('data-autoloader-path').trim().replace(/\/?$/, '/');
} else {
var src = script.src;
if (autoloaderFile.test(src)) {
// the script is the original autoloader script in the usual Prism project structure
languages_path = src.replace(autoloaderFile, 'components/');
} else if (prismFile.test(src)) {
// the script is part of a bundle like a custom prism.js from the download page
languages_path = src.replace(prismFile, 'components/');
}
}
}

Expand Down
Loading

0 comments on commit 2108c60

Please sign in to comment.