Skip to content

Core: Fixed race condition caused by deferring the script #2103

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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