Skip to content

Commit

Permalink
fix: The memory leaking issue when creating multiple HLJS instances
Browse files Browse the repository at this point in the history
  • Loading branch information
immccn123 committed Aug 19, 2024
1 parent da79da6 commit 5c64e08
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -819,8 +819,18 @@ const HLJS = function(hljs) {
* auto-highlights all pre>code elements on the page
*/
function highlightAll() {
function boot() {
// if a highlight was requested before DOM was loaded, do now
highlightAll();
window.removeEventListener('DOMContentLoaded', boot, false);
}

// if we are called too early in the loading process
if (document.readyState === "loading") {
// make sure the event listener is only added once
if (!wantsHighlight) {
window.addEventListener('DOMContentLoaded', boot, false);
}
wantsHighlight = true;
return;
}
Expand All @@ -829,16 +839,6 @@ const HLJS = function(hljs) {
blocks.forEach(highlightElement);
}

function boot() {
// if a highlight was requested before DOM was loaded, do now
if (wantsHighlight) highlightAll();
}

// make sure we are in the browser environment
if (typeof window !== 'undefined' && window.addEventListener) {
window.addEventListener('DOMContentLoaded', boot, false);
}

/**
* Register a language grammar module
*
Expand Down

0 comments on commit 5c64e08

Please sign in to comment.