diff --git a/components/prism-core.js b/components/prism-core.js index f1d001f432..a2eb44c547 100644 --- a/components/prism-core.js +++ b/components/prism-core.js @@ -531,7 +531,7 @@ var script = document.currentScript || [].slice.call(document.getElementsByTagNa if (script) { _.filename = script.src; - + if (script.hasAttribute('data-manual')) { _.manual = true; } @@ -544,16 +544,22 @@ if (!_.manual) { } } - if(document.readyState !== 'loading') { + // If the document state is "loading", then we'll use DOMContentLoaded. + // If the document state is "interactive" and the prism.js script is deferred, then we'll also use the + // DOMContentLoaded event because there might be some plugins or languages which have also been deferred and they + // might take longer one animation frame to execute which can create a race condition where only some plugins have + // been loaded when Prism.highlightAll() is executed, depending on how fast resources are loaded. + // See https://github.com/PrismJS/prism/issues/2102 + var readyState = document.readyState; + if (readyState === 'loading' || readyState === 'interactive' && script.defer) { + document.addEventListener('DOMContentLoaded', highlightAutomaticallyCallback); + } else { if (window.requestAnimationFrame) { window.requestAnimationFrame(highlightAutomaticallyCallback); } else { window.setTimeout(highlightAutomaticallyCallback, 16); } } - else { - document.addEventListener('DOMContentLoaded', highlightAutomaticallyCallback); - } } return _; diff --git a/components/prism-core.min.js b/components/prism-core.min.js index d7e53f2afc..930a7236ee 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(u){var c=/\blang(?:uage)?-([\w-]+)\b/i,a=0;var _={manual:u.Prism&&u.Prism.manual,disableWorkerMessageHandler:u.Prism&&u.Prism.disableWorkerMessageHandler,util:{encode:function(e){return e instanceof L?new L(e.type,_.util.encode(e.content),e.alias):Array.isArray(e)?e.map(_.util.encode):e.replace(/&/g,"&").replace(/e.length)return;if(!(k instanceof L)){if(h&&y!=a.length-1){if(c.lastIndex=v,!(x=c.exec(e)))break;for(var b=x.index+(f&&x[1]?x[1].length:0),w=x.index+x[0].length,A=y,P=v,O=a.length;A"+n.content+""},!u.document)return u.addEventListener&&(_.disableWorkerMessageHandler||u.addEventListener("message",function(e){var a=JSON.parse(e.data),n=a.language,r=a.code,t=a.immediateClose;u.postMessage(_.highlight(r,_.languages[n],n)),t&&u.close()},!1)),_;var e=document.currentScript||[].slice.call(document.getElementsByTagName("script")).pop();if(e&&(_.filename=e.src,e.hasAttribute("data-manual")&&(_.manual=!0)),!_.manual){function n(){_.manual||_.highlightAll()}"loading"!==document.readyState?window.requestAnimationFrame?window.requestAnimationFrame(n):window.setTimeout(n,16):document.addEventListener("DOMContentLoaded",n)}return _}(_self);"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(c){var u=/\blang(?:uage)?-([\w-]+)\b/i,a=0;var _={manual:c.Prism&&c.Prism.manual,disableWorkerMessageHandler:c.Prism&&c.Prism.disableWorkerMessageHandler,util:{encode:function(e){return e instanceof L?new L(e.type,_.util.encode(e.content),e.alias):Array.isArray(e)?e.map(_.util.encode):e.replace(/&/g,"&").replace(/e.length)return;if(!(k instanceof L)){if(h&&y!=a.length-1){if(u.lastIndex=v,!(x=u.exec(e)))break;for(var b=x.index+(f&&x[1]?x[1].length:0),w=x.index+x[0].length,A=y,P=v,O=a.length;A"+n.content+""},!c.document)return c.addEventListener&&(_.disableWorkerMessageHandler||c.addEventListener("message",function(e){var a=JSON.parse(e.data),n=a.language,r=a.code,t=a.immediateClose;c.postMessage(_.highlight(r,_.languages[n],n)),t&&c.close()},!1)),_;var e=document.currentScript||[].slice.call(document.getElementsByTagName("script")).pop();if(e&&(_.filename=e.src,e.hasAttribute("data-manual")&&(_.manual=!0)),!_.manual){function n(){_.manual||_.highlightAll()}var r=document.readyState;"loading"===r||"interactive"===r&&e.defer?document.addEventListener("DOMContentLoaded",n):window.requestAnimationFrame?window.requestAnimationFrame(n):window.setTimeout(n,16)}return _}(_self);"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 d8839fa6c8..8e7e244022 100644 --- a/prism.js +++ b/prism.js @@ -536,7 +536,7 @@ var script = document.currentScript || [].slice.call(document.getElementsByTagNa if (script) { _.filename = script.src; - + if (script.hasAttribute('data-manual')) { _.manual = true; } @@ -549,16 +549,22 @@ if (!_.manual) { } } - if(document.readyState !== 'loading') { + // If the document state is "loading", then we'll use DOMContentLoaded. + // If the document state is "interactive" and the prism.js script is deferred, then we'll also use the + // DOMContentLoaded event because there might be some plugins or languages which have also been deferred and they + // might take longer one animation frame to execute which can create a race condition where only some plugins have + // been loaded when Prism.highlightAll() is executed, depending on how fast resources are loaded. + // See https://github.com/PrismJS/prism/issues/2102 + var readyState = document.readyState; + if (readyState === 'loading' || readyState === 'interactive' && script.defer) { + document.addEventListener('DOMContentLoaded', highlightAutomaticallyCallback); + } else { if (window.requestAnimationFrame) { window.requestAnimationFrame(highlightAutomaticallyCallback); } else { window.setTimeout(highlightAutomaticallyCallback, 16); } } - else { - document.addEventListener('DOMContentLoaded', highlightAutomaticallyCallback); - } } return _;