Skip to content

Commit

Permalink
Core: Fixed race condition caused by deferring the script (#2103)
Browse files Browse the repository at this point in the history
This fixes a race condition that can occur when deferring Prism components.

For more details see #2102.
  • Loading branch information
RunDevelopment authored Oct 25, 2019
1 parent 3640b3f commit a3785ec
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
16 changes: 11 additions & 5 deletions components/prism-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ var script = document.currentScript || [].slice.call(document.getElementsByTagNa

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

if (script.hasAttribute('data-manual')) {
_.manual = true;
}
Expand All @@ -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 _;
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.

16 changes: 11 additions & 5 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ var script = document.currentScript || [].slice.call(document.getElementsByTagNa

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

if (script.hasAttribute('data-manual')) {
_.manual = true;
}
Expand All @@ -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 _;
Expand Down

0 comments on commit a3785ec

Please sign in to comment.