Skip to content

Commit

Permalink
Fixed prism-core and prism-file-highlight to prevent errors in IE8.
Browse files Browse the repository at this point in the history
Used local variable _self instead of overwriting self.
Closes #468.
  • Loading branch information
Golmote committed Jul 16, 2015
1 parent 86311f5 commit 5f133c8
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 99 deletions.
25 changes: 13 additions & 12 deletions components/prism-core.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
self = (typeof window !== 'undefined')
var _self = (typeof window !== 'undefined')
? window // if in browser
: (
(typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope)
Expand All @@ -17,7 +17,7 @@ var Prism = (function(){
// Private helper vars
var lang = /\blang(?:uage)?-(?!\*)(\w+)\b/i;

var _ = self.Prism = {
var _ = _self.Prism = {
util: {
encode: function (tokens) {
if (tokens instanceof Token) {
Expand Down Expand Up @@ -50,7 +50,8 @@ var _ = self.Prism = {
return clone;

case 'Array':
return o.map(function(v) { return _.util.clone(v); });
// Check for existence for IE8
return o.map && o.map(function(v) { return _.util.clone(v); });
}

return o;
Expand Down Expand Up @@ -192,7 +193,7 @@ var _ = self.Prism = {

_.hooks.run('before-highlight', env);

if (async && self.Worker) {
if (async && _self.Worker) {
var worker = new Worker(_.filename);

worker.onmessage = function(evt) {
Expand Down Expand Up @@ -386,22 +387,22 @@ Token.stringify = function(o, language, parent) {

};

if (!self.document) {
if (!self.addEventListener) {
if (!_self.document) {
if (!_self.addEventListener) {
// in Node.js
return self.Prism;
return _self.Prism;
}
// In worker
self.addEventListener('message', function(evt) {
_self.addEventListener('message', function(evt) {
var message = JSON.parse(evt.data),
lang = message.language,
code = message.code;

self.postMessage(JSON.stringify(_.util.encode(_.tokenize(code, _.languages[lang]))));
self.close();
_self.postMessage(JSON.stringify(_.util.encode(_.tokenize(code, _.languages[lang]))));
_self.close();
}, false);

return self.Prism;
return _self.Prism;
}

// Get current script and highlight
Expand All @@ -417,7 +418,7 @@ if (script) {
}
}

return self.Prism;
return _self.Prism;

})();

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.

76 changes: 39 additions & 37 deletions plugins/file-highlight/prism-file-highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,58 @@
'psm1': 'powershell'
};

Array.prototype.slice.call(document.querySelectorAll('pre[data-src]')).forEach(function(pre) {
var src = pre.getAttribute('data-src');

var language, parent = pre;
var lang = /\blang(?:uage)?-(?!\*)(\w+)\b/i;
while (parent && !lang.test(parent.className)) {
parent = parent.parentNode;
}
if(Array.prototype.forEach) { // Check to prevent error in IE8
Array.prototype.slice.call(document.querySelectorAll('pre[data-src]')).forEach(function (pre) {
var src = pre.getAttribute('data-src');

var language, parent = pre;
var lang = /\blang(?:uage)?-(?!\*)(\w+)\b/i;
while (parent && !lang.test(parent.className)) {
parent = parent.parentNode;
}

if (parent) {
language = (pre.className.match(lang) || [,''])[1];
}
if (parent) {
language = (pre.className.match(lang) || [, ''])[1];
}

if (!language) {
var extension = (src.match(/\.(\w+)$/) || [, ''])[1];
language = Extensions[extension] || extension;
}
if (!language) {
var extension = (src.match(/\.(\w+)$/) || [, ''])[1];
language = Extensions[extension] || extension;
}

var code = document.createElement('code');
code.className = 'language-' + language;
var code = document.createElement('code');
code.className = 'language-' + language;

pre.textContent = '';
pre.textContent = '';

code.textContent = 'Loading…';
code.textContent = 'Loading…';

pre.appendChild(code);
pre.appendChild(code);

var xhr = new XMLHttpRequest();
var xhr = new XMLHttpRequest();

xhr.open('GET', src, true);
xhr.open('GET', src, true);

xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {

if (xhr.status < 400 && xhr.responseText) {
code.textContent = xhr.responseText;
if (xhr.status < 400 && xhr.responseText) {
code.textContent = xhr.responseText;

Prism.highlightElement(code);
Prism.highlightElement(code);
}
else if (xhr.status >= 400) {
code.textContent = '✖ Error ' + xhr.status + ' while fetching file: ' + xhr.statusText;
}
else {
code.textContent = '✖ Error: File does not exist or is empty';
}
}
else if (xhr.status >= 400) {
code.textContent = '✖ Error ' + xhr.status + ' while fetching file: ' + xhr.statusText;
}
else {
code.textContent = '✖ Error: File does not exist or is empty';
}
}
};
};

xhr.send(null);
});
xhr.send(null);
});
}

};

Expand Down
2 changes: 1 addition & 1 deletion plugins/file-highlight/prism-file-highlight.min.js

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

Loading

0 comments on commit 5f133c8

Please sign in to comment.