From 5f133c800fdd178f6899c1ffa8a38ffbdc7a746f Mon Sep 17 00:00:00 2001 From: Golmote Date: Thu, 16 Jul 2015 21:09:40 +0200 Subject: [PATCH] Fixed prism-core and prism-file-highlight to prevent errors in IE8. Used local variable _self instead of overwriting self. Closes #468. --- components/prism-core.js | 25 ++--- components/prism-core.min.js | 2 +- .../file-highlight/prism-file-highlight.js | 76 +++++++------- .../prism-file-highlight.min.js | 2 +- prism.js | 99 ++++++++++--------- 5 files changed, 105 insertions(+), 99 deletions(-) diff --git a/components/prism-core.js b/components/prism-core.js index b9a0ac61a8..542464e084 100644 --- a/components/prism-core.js +++ b/components/prism-core.js @@ -1,4 +1,4 @@ -self = (typeof window !== 'undefined') +var _self = (typeof window !== 'undefined') ? window // if in browser : ( (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) @@ -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) { @@ -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; @@ -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) { @@ -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 @@ -417,7 +418,7 @@ if (script) { } } -return self.Prism; +return _self.Prism; })(); diff --git a/components/prism-core.min.js b/components/prism-core.min.js index df4ccd4c43..5f781e29fe 100644 --- a/components/prism-core.min.js +++ b/components/prism-core.min.js @@ -1 +1 @@ -self="undefined"!=typeof window?window:"undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope?self:{};var Prism=function(){var e=/\blang(?:uage)?-(?!\*)(\w+)\b/i,t=self.Prism={util:{encode:function(e){return e instanceof n?new n(e.type,t.util.encode(e.content),e.alias):"Array"===t.util.type(e)?e.map(t.util.encode):e.replace(/&/g,"&").replace(/e.length)break e;if(!(d instanceof a)){u.lastIndex=0;var m=u.exec(d);if(m){c&&(f=m[1].length);var y=m.index-1+f,m=m[0].slice(f),v=m.length,k=y+v,b=d.slice(0,y+1),w=d.slice(k+1),N=[p,1];b&&N.push(b);var O=new a(l,g?t.tokenize(m,g):m,h);N.push(O),w&&N.push(w),Array.prototype.splice.apply(r,N)}}}}}return r},hooks:{all:{},add:function(e,n){var a=t.hooks.all;a[e]=a[e]||[],a[e].push(n)},run:function(e,n){var a=t.hooks.all[e];if(a&&a.length)for(var r,i=0;r=a[i++];)r(n)}}},n=t.Token=function(e,t,n){this.type=e,this.content=t,this.alias=n};if(n.stringify=function(e,a,r){if("string"==typeof e)return e;if("Array"===t.util.type(e))return e.map(function(t){return n.stringify(t,a,e)}).join("");var i={type:e.type,content:n.stringify(e.content,a,r),tag:"span",classes:["token",e.type],attributes:{},language:a,parent:r};if("comment"==i.type&&(i.attributes.spellcheck="true"),e.alias){var l="Array"===t.util.type(e.alias)?e.alias:[e.alias];Array.prototype.push.apply(i.classes,l)}t.hooks.run("wrap",i);var s="";for(var o in i.attributes)s+=o+'="'+(i.attributes[o]||"")+'"';return"<"+i.tag+' class="'+i.classes.join(" ")+'" '+s+">"+i.content+""},!self.document)return self.addEventListener?(self.addEventListener("message",function(e){var n=JSON.parse(e.data),a=n.language,r=n.code;self.postMessage(JSON.stringify(t.util.encode(t.tokenize(r,t.languages[a])))),self.close()},!1),self.Prism):self.Prism;var a=document.getElementsByTagName("script");return a=a[a.length-1],a&&(t.filename=a.src,document.addEventListener&&!a.hasAttribute("data-manual")&&document.addEventListener("DOMContentLoaded",t.highlightAll)),self.Prism}();"undefined"!=typeof module&&module.exports&&(module.exports=Prism); \ No newline at end of file +var _self="undefined"!=typeof window?window:"undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope?self:{},Prism=function(){var e=/\blang(?:uage)?-(?!\*)(\w+)\b/i,t=_self.Prism={util:{encode:function(e){return e instanceof n?new n(e.type,t.util.encode(e.content),e.alias):"Array"===t.util.type(e)?e.map(t.util.encode):e.replace(/&/g,"&").replace(/e.length)break e;if(!(d instanceof a)){u.lastIndex=0;var m=u.exec(d);if(m){c&&(f=m[1].length);var y=m.index-1+f,m=m[0].slice(f),v=m.length,k=y+v,b=d.slice(0,y+1),w=d.slice(k+1),N=[p,1];b&&N.push(b);var O=new a(l,g?t.tokenize(m,g):m,h);N.push(O),w&&N.push(w),Array.prototype.splice.apply(r,N)}}}}}return r},hooks:{all:{},add:function(e,n){var a=t.hooks.all;a[e]=a[e]||[],a[e].push(n)},run:function(e,n){var a=t.hooks.all[e];if(a&&a.length)for(var r,i=0;r=a[i++];)r(n)}}},n=t.Token=function(e,t,n){this.type=e,this.content=t,this.alias=n};if(n.stringify=function(e,a,r){if("string"==typeof e)return e;if("Array"===t.util.type(e))return e.map(function(t){return n.stringify(t,a,e)}).join("");var i={type:e.type,content:n.stringify(e.content,a,r),tag:"span",classes:["token",e.type],attributes:{},language:a,parent:r};if("comment"==i.type&&(i.attributes.spellcheck="true"),e.alias){var l="Array"===t.util.type(e.alias)?e.alias:[e.alias];Array.prototype.push.apply(i.classes,l)}t.hooks.run("wrap",i);var s="";for(var o in i.attributes)s+=o+'="'+(i.attributes[o]||"")+'"';return"<"+i.tag+' class="'+i.classes.join(" ")+'" '+s+">"+i.content+""},!_self.document)return _self.addEventListener?(_self.addEventListener("message",function(e){var n=JSON.parse(e.data),a=n.language,r=n.code;_self.postMessage(JSON.stringify(t.util.encode(t.tokenize(r,t.languages[a])))),_self.close()},!1),_self.Prism):_self.Prism;var a=document.getElementsByTagName("script");return a=a[a.length-1],a&&(t.filename=a.src,document.addEventListener&&!a.hasAttribute("data-manual")&&document.addEventListener("DOMContentLoaded",t.highlightAll)),_self.Prism}();"undefined"!=typeof module&&module.exports&&(module.exports=Prism); \ No newline at end of file diff --git a/plugins/file-highlight/prism-file-highlight.js b/plugins/file-highlight/prism-file-highlight.js index 5fea85abf0..1a49254f27 100644 --- a/plugins/file-highlight/prism-file-highlight.js +++ b/plugins/file-highlight/prism-file-highlight.js @@ -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); + }); + } }; diff --git a/plugins/file-highlight/prism-file-highlight.min.js b/plugins/file-highlight/prism-file-highlight.min.js index 42889f90b6..2d75007f66 100644 --- a/plugins/file-highlight/prism-file-highlight.min.js +++ b/plugins/file-highlight/prism-file-highlight.min.js @@ -1 +1 @@ -!function(){self.Prism&&self.document&&document.querySelector&&(self.Prism.fileHighlight=function(){var e={js:"javascript",html:"markup",svg:"markup",xml:"markup",py:"python",rb:"ruby",ps1:"powershell",psm1:"powershell"};Array.prototype.slice.call(document.querySelectorAll("pre[data-src]")).forEach(function(t){for(var r,s=t.getAttribute("data-src"),a=t,n=/\blang(?:uage)?-(?!\*)(\w+)\b/i;a&&!n.test(a.className);)a=a.parentNode;if(a&&(r=(t.className.match(n)||[,""])[1]),!r){var l=(s.match(/\.(\w+)$/)||[,""])[1];r=e[l]||l}var o=document.createElement("code");o.className="language-"+r,t.textContent="",o.textContent="Loading…",t.appendChild(o);var i=new XMLHttpRequest;i.open("GET",s,!0),i.onreadystatechange=function(){4==i.readyState&&(i.status<400&&i.responseText?(o.textContent=i.responseText,Prism.highlightElement(o)):o.textContent=i.status>=400?"✖ Error "+i.status+" while fetching file: "+i.statusText:"✖ Error: File does not exist or is empty")},i.send(null)})},self.Prism.fileHighlight())}(); \ No newline at end of file +!function(){self.Prism&&self.document&&document.querySelector&&(self.Prism.fileHighlight=function(){var e={js:"javascript",html:"markup",svg:"markup",xml:"markup",py:"python",rb:"ruby",ps1:"powershell",psm1:"powershell"};Array.prototype.forEach&&Array.prototype.slice.call(document.querySelectorAll("pre[data-src]")).forEach(function(t){for(var r,a=t.getAttribute("data-src"),s=t,n=/\blang(?:uage)?-(?!\*)(\w+)\b/i;s&&!n.test(s.className);)s=s.parentNode;if(s&&(r=(t.className.match(n)||[,""])[1]),!r){var o=(a.match(/\.(\w+)$/)||[,""])[1];r=e[o]||o}var l=document.createElement("code");l.className="language-"+r,t.textContent="",l.textContent="Loading…",t.appendChild(l);var i=new XMLHttpRequest;i.open("GET",a,!0),i.onreadystatechange=function(){4==i.readyState&&(i.status<400&&i.responseText?(l.textContent=i.responseText,Prism.highlightElement(l)):l.textContent=i.status>=400?"✖ Error "+i.status+" while fetching file: "+i.statusText:"✖ Error: File does not exist or is empty")},i.send(null)})},self.Prism.fileHighlight())}(); \ No newline at end of file diff --git a/prism.js b/prism.js index 91fa896622..8282b57193 100644 --- a/prism.js +++ b/prism.js @@ -3,7 +3,7 @@ Begin prism-core.js ********************************************** */ -self = (typeof window !== 'undefined') +var _self = (typeof window !== 'undefined') ? window // if in browser : ( (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) @@ -22,7 +22,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) { @@ -55,7 +55,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; @@ -197,7 +198,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) { @@ -391,22 +392,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 @@ -422,7 +423,7 @@ if (script) { } } -return self.Prism; +return _self.Prism; })(); @@ -643,56 +644,58 @@ if (Prism.languages.markup) { 'psm1': 'powershell' }; - Array.prototype.slice.call(document.querySelectorAll('pre[data-src]')).forEach(function(pre) { - var src = pre.getAttribute('data-src'); + 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; - } + 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); - } - 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'; + 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'; + } } - } - }; + }; - xhr.send(null); - }); + xhr.send(null); + }); + } };