diff --git a/components/prism-core.js b/components/prism-core.js index 9188cb29bb..453dbe24f0 100644 --- a/components/prism-core.js +++ b/components/prism-core.js @@ -212,33 +212,37 @@ var _ = _self.Prism = { code: code }; + var insertHighlightedCode = function (highlightedCode) { + env.highlightedCode = highlightedCode; + + _.hooks.run('before-insert', env); + + env.element.innerHTML = env.highlightedCode; + + _.hooks.run('after-highlight', env); + _.hooks.run('complete', env); + callback && callback.call(env.element); + } + _.hooks.run('before-sanity-check', env); - if (!env.code || !env.grammar) { - if (env.code) { - _.hooks.run('before-highlight', env); - env.element.textContent = env.code; - _.hooks.run('after-highlight', env); - } + if (!env.code) { _.hooks.run('complete', env); return; } _.hooks.run('before-highlight', env); + if (!env.grammar) { + insertHighlightedCode(_.util.encode(env.code)); + return; + } + if (async && _self.Worker) { var worker = new Worker(_.filename); worker.onmessage = function(evt) { - env.highlightedCode = evt.data; - - _.hooks.run('before-insert', env); - - env.element.innerHTML = env.highlightedCode; - - _.hooks.run('after-highlight', env); - _.hooks.run('complete', env); - callback && callback.call(env.element); + insertHighlightedCode(evt.data); }; worker.postMessage(JSON.stringify({ @@ -248,17 +252,7 @@ var _ = _self.Prism = { })); } else { - env.highlightedCode = _.highlight(env.code, env.grammar, env.language); - - _.hooks.run('before-insert', env); - - env.element.innerHTML = env.highlightedCode; - - _.hooks.run('after-highlight', env); - - _.hooks.run('complete', env); - - callback && callback.call(element); + insertHighlightedCode(_.highlight(env.code, env.grammar, env.language)); } }, diff --git a/components/prism-core.min.js b/components/prism-core.min.js index 73c55c9682..bde09e05e4 100644 --- a/components/prism-core.min.js +++ b/components/prism-core.min.js @@ -1 +1 @@ -var _self="undefined"!=typeof window?window:"undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope?self:{},Prism=function(){var e=/\blang(?:uage)?-([\w-]+)\b/i,t=0,n=_self.Prism={manual:_self.Prism&&_self.Prism.manual,disableWorkerMessageHandler:_self.Prism&&_self.Prism.disableWorkerMessageHandler,util:{encode:function(e){return e instanceof a?new a(e.type,n.util.encode(e.content),e.alias):"Array"===n.util.type(e)?e.map(n.util.encode):e.replace(/&/g,"&").replace(/e.length)return;if(!(w instanceof s)){if(m&&b!=t.length-1){h.lastIndex=k;var _=h.exec(e);if(!_)break;for(var j=_.index+(d?_[1].length:0),P=_.index+_[0].length,A=b,x=k,O=t.length;O>A&&(P>x||!t[A].type&&!t[A-1].greedy);++A)x+=t[A].length,j>=x&&(++b,k=x);if(t[b]instanceof s)continue;I=A-b,w=e.slice(k,x),_.index-=k}else{h.lastIndex=0;var _=h.exec(w),I=1}if(_){d&&(p=_[1]?_[1].length:0);var j=_.index+p,_=_[0].slice(p),P=j+_.length,N=w.slice(0,j),S=w.slice(P),C=[b,I];N&&(++b,k+=N.length,C.push(N));var E=new s(u,f?n.tokenize(_,f):_,y,_,m);if(C.push(E),S&&C.push(S),Array.prototype.splice.apply(t,C),1!=I&&n.matchGrammar(e,t,a,b,k,!0,u),i)break}else if(i)break}}}}},tokenize:function(e,t){var a=[e],r=t.rest;if(r){for(var l in r)t[l]=r[l];delete t.rest}return n.matchGrammar(e,a,t,0,0,!1),a},hooks:{all:{},add:function(e,t){var a=n.hooks.all;a[e]=a[e]||[],a[e].push(t)},run:function(e,t){var a=n.hooks.all[e];if(a&&a.length)for(var r,l=0;r=a[l++];)r(t)}}},a=n.Token=function(e,t,n,a,r){this.type=e,this.content=t,this.alias=n,this.length=0|(a||"").length,this.greedy=!!r};if(a.stringify=function(e,t,r){if("string"==typeof e)return e;if("Array"===n.util.type(e))return e.map(function(n){return a.stringify(n,t,e)}).join("");var l={type:e.type,content:a.stringify(e.content,t,r),tag:"span",classes:["token",e.type],attributes:{},language:t,parent:r};if(e.alias){var i="Array"===n.util.type(e.alias)?e.alias:[e.alias];Array.prototype.push.apply(l.classes,i)}n.hooks.run("wrap",l);var o=Object.keys(l.attributes).map(function(e){return e+'="'+(l.attributes[e]||"").replace(/"/g,""")+'"'}).join(" ");return"<"+l.tag+' class="'+l.classes.join(" ")+'"'+(o?" "+o:"")+">"+l.content+""},!_self.document)return _self.addEventListener?(n.disableWorkerMessageHandler||_self.addEventListener("message",function(e){var t=JSON.parse(e.data),a=t.language,r=t.code,l=t.immediateClose;_self.postMessage(n.highlight(r,n.languages[a],a)),l&&_self.close()},!1),_self.Prism):_self.Prism;var r=document.currentScript||[].slice.call(document.getElementsByTagName("script")).pop();return r&&(n.filename=r.src,n.manual||r.hasAttribute("data-manual")||("loading"!==document.readyState?window.requestAnimationFrame?window.requestAnimationFrame(n.highlightAll):window.setTimeout(n.highlightAll,16):document.addEventListener("DOMContentLoaded",n.highlightAll))),_self.Prism}();"undefined"!=typeof module&&module.exports&&(module.exports=Prism),"undefined"!=typeof global&&(global.Prism=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=0,n=_self.Prism={manual:_self.Prism&&_self.Prism.manual,disableWorkerMessageHandler:_self.Prism&&_self.Prism.disableWorkerMessageHandler,util:{encode:function(e){return e instanceof a?new a(e.type,n.util.encode(e.content),e.alias):"Array"===n.util.type(e)?e.map(n.util.encode):e.replace(/&/g,"&").replace(/e.length)return;if(!(w instanceof s)){if(m&&b!=t.length-1){f.lastIndex=k;var _=f.exec(e);if(!_)break;for(var j=_.index+(d?_[1].length:0),P=_.index+_[0].length,A=b,O=k,x=t.length;x>A&&(P>O||!t[A].type&&!t[A-1].greedy);++A)O+=t[A].length,j>=O&&(++b,k=O);if(t[b]instanceof s)continue;I=A-b,w=e.slice(k,O),_.index-=k}else{f.lastIndex=0;var _=f.exec(w),I=1}if(_){d&&(p=_[1]?_[1].length:0);var j=_.index+p,_=_[0].slice(p),P=j+_.length,N=w.slice(0,j),S=w.slice(P),E=[b,I];N&&(++b,k+=N.length,E.push(N));var C=new s(u,h?n.tokenize(_,h):_,y,_,m);if(E.push(C),S&&E.push(S),Array.prototype.splice.apply(t,E),1!=I&&n.matchGrammar(e,t,a,b,k,!0,u),i)break}else if(i)break}}}}},tokenize:function(e,t){var a=[e],r=t.rest;if(r){for(var l in r)t[l]=r[l];delete t.rest}return n.matchGrammar(e,a,t,0,0,!1),a},hooks:{all:{},add:function(e,t){var a=n.hooks.all;a[e]=a[e]||[],a[e].push(t)},run:function(e,t){var a=n.hooks.all[e];if(a&&a.length)for(var r,l=0;r=a[l++];)r(t)}}},a=n.Token=function(e,t,n,a,r){this.type=e,this.content=t,this.alias=n,this.length=0|(a||"").length,this.greedy=!!r};if(a.stringify=function(e,t,r){if("string"==typeof e)return e;if("Array"===n.util.type(e))return e.map(function(n){return a.stringify(n,t,e)}).join("");var l={type:e.type,content:a.stringify(e.content,t,r),tag:"span",classes:["token",e.type],attributes:{},language:t,parent:r};if(e.alias){var i="Array"===n.util.type(e.alias)?e.alias:[e.alias];Array.prototype.push.apply(l.classes,i)}n.hooks.run("wrap",l);var o=Object.keys(l.attributes).map(function(e){return e+'="'+(l.attributes[e]||"").replace(/"/g,""")+'"'}).join(" ");return"<"+l.tag+' class="'+l.classes.join(" ")+'"'+(o?" "+o:"")+">"+l.content+""},!_self.document)return _self.addEventListener?(n.disableWorkerMessageHandler||_self.addEventListener("message",function(e){var t=JSON.parse(e.data),a=t.language,r=t.code,l=t.immediateClose;_self.postMessage(n.highlight(r,n.languages[a],a)),l&&_self.close()},!1),_self.Prism):_self.Prism;var r=document.currentScript||[].slice.call(document.getElementsByTagName("script")).pop();return r&&(n.filename=r.src,n.manual||r.hasAttribute("data-manual")||("loading"!==document.readyState?window.requestAnimationFrame?window.requestAnimationFrame(n.highlightAll):window.setTimeout(n.highlightAll,16):document.addEventListener("DOMContentLoaded",n.highlightAll))),_self.Prism}();"undefined"!=typeof module&&module.exports&&(module.exports=Prism),"undefined"!=typeof global&&(global.Prism=Prism); \ No newline at end of file diff --git a/prism.js b/prism.js index 5a8568734d..17d26a36cb 100644 --- a/prism.js +++ b/prism.js @@ -217,33 +217,37 @@ var _ = _self.Prism = { code: code }; + var insertHighlightedCode = function (highlightedCode) { + env.highlightedCode = highlightedCode; + + _.hooks.run('before-insert', env); + + env.element.innerHTML = env.highlightedCode; + + _.hooks.run('after-highlight', env); + _.hooks.run('complete', env); + callback && callback.call(env.element); + } + _.hooks.run('before-sanity-check', env); - if (!env.code || !env.grammar) { - if (env.code) { - _.hooks.run('before-highlight', env); - env.element.textContent = env.code; - _.hooks.run('after-highlight', env); - } + if (!env.code) { _.hooks.run('complete', env); return; } _.hooks.run('before-highlight', env); + if (!env.grammar) { + insertHighlightedCode(_.util.encode(env.code)); + return; + } + if (async && _self.Worker) { var worker = new Worker(_.filename); worker.onmessage = function(evt) { - env.highlightedCode = evt.data; - - _.hooks.run('before-insert', env); - - env.element.innerHTML = env.highlightedCode; - - _.hooks.run('after-highlight', env); - _.hooks.run('complete', env); - callback && callback.call(env.element); + insertHighlightedCode(evt.data); }; worker.postMessage(JSON.stringify({ @@ -253,17 +257,7 @@ var _ = _self.Prism = { })); } else { - env.highlightedCode = _.highlight(env.code, env.grammar, env.language); - - _.hooks.run('before-insert', env); - - env.element.innerHTML = env.highlightedCode; - - _.hooks.run('after-highlight', env); - - _.hooks.run('complete', env); - - callback && callback.call(element); + insertHighlightedCode(_.highlight(env.code, env.grammar, env.language)); } },