Skip to content

Commit 19819d5

Browse files
authored
fix(parser) Fix memory leak when creating multiple instances (#4095)
* fix: The memory leaking issue when creating multiple HLJS instances
1 parent da79da6 commit 19819d5

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ Themes:
2929

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

32+
Improvements:
33+
34+
- Resolve the memory leak problem when creating multiple Highlight.js instances [Imken][]
35+
3236
CONTRIBUTORS
3337

3438
[Josh Goebel]: https://github.com/joshgoebel
@@ -39,6 +43,7 @@ CONTRIBUTORS
3943
[srawlins]: https://github.com/srawlins
4044
[Alvin Joy]: https://github.com/alvinsjoy
4145
[Aboobacker MK]: https://github.com/tachyons
46+
[Imken]: https://github.com/immccn123
4247

4348

4449
## Version 11.10.0

src/highlight.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -819,8 +819,17 @@ const HLJS = function(hljs) {
819819
* auto-highlights all pre>code elements on the page
820820
*/
821821
function highlightAll() {
822+
function boot() {
823+
// if a highlight was requested before DOM was loaded, do now
824+
highlightAll();
825+
}
826+
822827
// if we are called too early in the loading process
823828
if (document.readyState === "loading") {
829+
// make sure the event listener is only added once
830+
if (!wantsHighlight) {
831+
window.addEventListener('DOMContentLoaded', boot, false);
832+
}
824833
wantsHighlight = true;
825834
return;
826835
}
@@ -829,16 +838,6 @@ const HLJS = function(hljs) {
829838
blocks.forEach(highlightElement);
830839
}
831840

832-
function boot() {
833-
// if a highlight was requested before DOM was loaded, do now
834-
if (wantsHighlight) highlightAll();
835-
}
836-
837-
// make sure we are in the browser environment
838-
if (typeof window !== 'undefined' && window.addEventListener) {
839-
window.addEventListener('DOMContentLoaded', boot, false);
840-
}
841-
842841
/**
843842
* Register a language grammar module
844843
*

0 commit comments

Comments
 (0)