Skip to content

Commit 9085d69

Browse files
Better cleanup of removed stylesheets
1 parent 328c121 commit 9085d69

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

highlight-helper.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,9 @@ function Highlighter(options = hhDefaultOptions) {
556556

557557
// Remove this Highlighter instance and its highlights
558558
this.removeHighlighter = () => {
559-
for (const stylesheet of Object.values(this.stylesheets)) if (stylesheet.parentElement) stylesheet.remove();
560559
this.loadHighlights([]);
561560
this.annotatableContainer.querySelectorAll('.hh-svg-background, .hh-selection-handle').forEach(el => el.remove())
561+
removeStylesheets(this.stylesheets);
562562
controller.abort();
563563

564564
this.annotatableContainer.highlighter = undefined;
@@ -1072,6 +1072,18 @@ function Highlighter(options = hhDefaultOptions) {
10721072
}
10731073
return stylesheet;
10741074
}
1075+
1076+
// Remove CSS stylesheets
1077+
function removeStylesheets(stylesheets) {
1078+
for (const stylesheet of Object.values(stylesheets)) {
1079+
if (supportsCssStylesheetApi) {
1080+
const adoptedStylesheetIndex = document.adoptedStyleSheets.indexOf(stylesheet);
1081+
document.adoptedStyleSheets.splice(adoptedStylesheetIndex, 1);
1082+
} else {
1083+
stylesheet.remove();
1084+
}
1085+
}
1086+
}
10751087

10761088
// Restore the previous selection range in case the browser clears the selection
10771089
const getRestoredSelectionOrCaret = (selection, pointerEvent = null) => {

0 commit comments

Comments
 (0)