Skip to content

Commit 2108c60

Browse files
Core: IE11 workaround for currentScript (#2104)
This adds a new function `Prism.util.currentScript` which is a functionally equalent to `document.currentScript` but includes support for IE11.
1 parent a3785ec commit 2108c60

File tree

5 files changed

+104
-21
lines changed

5 files changed

+104
-21
lines changed

components/prism-core.js

+43-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,48 @@ var _ = {
101101
default:
102102
return o;
103103
}
104+
},
105+
106+
/**
107+
* Returns the script element that is currently executing.
108+
*
109+
* This does __not__ work for line script element.
110+
*
111+
* @returns {HTMLScriptElement | null}
112+
*/
113+
currentScript: function () {
114+
if (typeof document === 'undefined') {
115+
return null;
116+
}
117+
if ('currentScript' in document) {
118+
return document.currentScript;
119+
}
120+
121+
// IE11 workaround
122+
// we'll get the src of the current script by parsing IE11's error stack trace
123+
// this will not work for inline scripts
124+
125+
try {
126+
throw new Error();
127+
} catch (err) {
128+
// Get file src url from stack. Specifically works with the format of stack traces in IE.
129+
// A stack will look like this:
130+
//
131+
// Error
132+
// at _.util.currentScript (http://localhost/components/prism-core.js:119:5)
133+
// at Global code (http://localhost/components/prism-core.js:606:1)
134+
135+
var src = (/at [^(\r\n]*\((.*):.+:.+\)$/i.exec(err.stack) || [])[1];
136+
if (src) {
137+
var scripts = document.getElementsByTagName('script');
138+
for (var i in scripts) {
139+
if (scripts[i].src == src) {
140+
return scripts[i];
141+
}
142+
}
143+
}
144+
return null;
145+
}
104146
}
105147
},
106148

@@ -527,7 +569,7 @@ if (!_self.document) {
527569
}
528570

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

532574
if (script) {
533575
_.filename = script.src;

components/prism-core.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/autoloader/prism-autoloader.js

+16-17
Original file line numberDiff line numberDiff line change
@@ -181,25 +181,24 @@
181181
var lang_data = {};
182182

183183
var ignored_language = 'none';
184-
185-
var scripts = document.getElementsByTagName('script');
186-
var script = scripts[scripts.length - 1];
187184
var languages_path = 'components/';
188185

189-
var autoloaderFile = /\bplugins\/autoloader\/prism-autoloader\.(?:min\.)js$/i;
190-
var prismFile = /[\w-]+\.(?:min\.)js$/i;
191-
192-
if (script.hasAttribute('data-autoloader-path')) {
193-
// data-autoloader-path is set, so just use it
194-
languages_path = script.getAttribute('data-autoloader-path').trim().replace(/\/?$/, '/');
195-
} else {
196-
var src = script.src;
197-
if (autoloaderFile.test(src)) {
198-
// the script is the original autoloader script in the usual Prism project structure
199-
languages_path = src.replace(autoloaderFile, 'components/');
200-
} else if (prismFile.test(src)) {
201-
// the script is part of a bundle like a custom prism.js from the download page
202-
languages_path = src.replace(prismFile, 'components/');
186+
var script = Prism.util.currentScript();
187+
if (script) {
188+
var autoloaderFile = /\bplugins\/autoloader\/prism-autoloader\.(?:min\.)js$/i;
189+
var prismFile = /[\w-]+\.(?:min\.)js$/i;
190+
if (script.hasAttribute('data-autoloader-path')) {
191+
// data-autoloader-path is set, so just use it
192+
languages_path = script.getAttribute('data-autoloader-path').trim().replace(/\/?$/, '/');
193+
} else {
194+
var src = script.src;
195+
if (autoloaderFile.test(src)) {
196+
// the script is the original autoloader script in the usual Prism project structure
197+
languages_path = src.replace(autoloaderFile, 'components/');
198+
} else if (prismFile.test(src)) {
199+
// the script is part of a bundle like a custom prism.js from the download page
200+
languages_path = src.replace(prismFile, 'components/');
201+
}
203202
}
204203
}
205204

0 commit comments

Comments
 (0)