Skip to content

Commit

Permalink
Merge pull request #4619 from Tyriar/tyriar/4611_2
Browse files Browse the repository at this point in the history
Avoid using global document, add copyright
  • Loading branch information
Tyriar committed Jul 29, 2023
2 parents 17b8247 + a9662e8 commit ebdafe5
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/browser/renderer/dom/StyleSheet.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
/**
* Copyright (c) 2023 The xterm.js authors. All rights reserved.
* @license MIT
*/

export interface IStyleSheet {
dispose: () => void;
setCss: (value: string) => void;
}

const createCssStyleSheet = (): IStyleSheet => {
const createCssStyleSheet = (doc: Document): IStyleSheet => {
const sheet = new CSSStyleSheet();
document.adoptedStyleSheets.push(sheet);
doc.adoptedStyleSheets.push(sheet);
return {
dispose() {
const index = document.adoptedStyleSheets.indexOf(sheet);
document.adoptedStyleSheets.splice(index, 1);
const index = doc.adoptedStyleSheets.indexOf(sheet);
doc.adoptedStyleSheets.splice(index, 1);
},
setCss(css) {
sheet.replaceSync(css);
Expand All @@ -18,7 +23,8 @@ const createCssStyleSheet = (): IStyleSheet => {
};

const createStyleElement = (parent: HTMLElement): IStyleSheet => {
const element = document.createElement('style');
const doc = parent.ownerDocument;
const element = doc.createElement('style');
parent.append(element);
return {
dispose() {
Expand All @@ -32,7 +38,7 @@ const createStyleElement = (parent: HTMLElement): IStyleSheet => {

export const createStyle = (parent: HTMLElement): IStyleSheet => {
try {
return createCssStyleSheet();
return createCssStyleSheet(parent.ownerDocument);
} catch {
return createStyleElement(parent);
}
Expand Down

0 comments on commit ebdafe5

Please sign in to comment.