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

Core: IE11 workaround for currentScript #2104

Merged
merged 6 commits into from
Oct 25, 2019
Merged
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
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();
mAAdhaTTah marked this conversation as resolved.
Show resolved Hide resolved
} 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();
mAAdhaTTah marked this conversation as resolved.
Show resolved Hide resolved
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