Skip to content

Commit

Permalink
fix(parser) Fix memory leak when creating multiple instances (#4095)
Browse files Browse the repository at this point in the history
* fix: The memory leaking issue when creating multiple HLJS instances
  • Loading branch information
immccn123 authored Aug 22, 2024
1 parent da79da6 commit 19819d5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ Themes:

- Added `Rosé Pine` theme [William Wilkinson][]

Improvements:

- Resolve the memory leak problem when creating multiple Highlight.js instances [Imken][]

CONTRIBUTORS

[Josh Goebel]: https://github.com/joshgoebel
Expand All @@ -39,6 +43,7 @@ CONTRIBUTORS
[srawlins]: https://github.com/srawlins
[Alvin Joy]: https://github.com/alvinsjoy
[Aboobacker MK]: https://github.com/tachyons
[Imken]: https://github.com/immccn123


## Version 11.10.0
Expand Down
19 changes: 9 additions & 10 deletions src/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -819,8 +819,17 @@ 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();
}

// 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 +838,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 19819d5

Please sign in to comment.